-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: transfer package from monorepo
- Loading branch information
0 parents
commit 6a48f72
Showing
16 changed files
with
6,111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[**.{json,js,ts,y{a,}ml}] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[*.md] | ||
indent_style = space | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"overrides": [], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"double" | ||
], | ||
"semi": [ | ||
"error", | ||
"never" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: "\U0001F41E Bug report" | ||
description: Create a report to help us improve sidebase | ||
labels: ["bug"] | ||
body: | ||
- type: markdown | ||
attributes: | ||
value: | | ||
Please help us finding and resolving problems in sidebase. | ||
- type: textarea | ||
id: bug-env | ||
attributes: | ||
label: Environment | ||
description: You can use `npx nuxi info` to fill this section | ||
placeholder: Environment | ||
- type: textarea | ||
id: reproduction | ||
attributes: | ||
label: Reproduction | ||
description: Provide a link to a repo that can reproduce the problem you ran into if you can. | ||
placeholder: Reproduction | ||
- type: textarea | ||
id: bug-description | ||
attributes: | ||
label: Describe the bug | ||
description: A clear and concise description of what the bug is. If you intend to submit a PR for this issue, tell us in the description. Thanks! | ||
placeholder: Bug description | ||
validations: | ||
required: true | ||
- type: textarea | ||
id: additonal | ||
attributes: | ||
label: Additional context | ||
description: If applicable, add any other context about the problem here | ||
- type: textarea | ||
id: logs | ||
attributes: | ||
label: Logs | ||
description: | | ||
Optional if provided reproduction. Please try not to insert an image but copy paste the log text. | ||
render: shell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
contact_links: | ||
- name: 💬 Discussions | ||
url: https://github.com/sidestream-tech/sidebase/discussions | ||
about: Use discussions if you have an idea for improvement and asking questions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: "🚀 Feature request" | ||
description: Suggest a feature that will improve sidebase | ||
labels: ["enhancement"] | ||
body: | ||
- type: markdown | ||
attributes: | ||
value: | | ||
Thank you for taking the time to fill out this feature request of this young, just growing project. It really means a lot to us! ❤️ | ||
- type: textarea | ||
id: feature-description | ||
attributes: | ||
label: Describe the feature | ||
description: A clear and concise description of what you think would be a helpful addition to sidebase, including the possible use cases and alternatives you have considered. If you have a working prototype or module that implements it, please include a link. | ||
placeholder: Feature description | ||
validations: | ||
required: true | ||
- type: textarea | ||
id: additional-info | ||
attributes: | ||
label: Additional information | ||
description: Additional information that helps us decide how to proceed. | ||
placeholder: Additional information |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Node.js CI | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Use Node.js 16.14.2 | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16.14.2 | ||
|
||
- run: npm ci | ||
|
||
# Initialize all sub-dependencies | ||
- run: npm run lint | ||
- run: npm run test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
node_modules/ | ||
coverage/ | ||
dist/ | ||
|
||
*.log | ||
docs/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
vitest.config.ts | ||
tsconfig.json | ||
.eslintrc | ||
|
||
coverage/ | ||
tests/ | ||
src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 SIDESTREAM GmbH | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# nuxt-parse | ||
|
||
[![npm version][npm-version-src]][npm-version-href] | ||
[![npm downloads][npm-downloads-src]][npm-downloads-href] | ||
[![GitHub stars](https://badgen.net/github/stars/sidebase/nuxt-parse)](https://GitHub.com/sidebase/nuxt-parse/) | ||
[![License][license-src]][license-href] | ||
[![Follow us on Twitter](https://badgen.net/badge/icon/twitter?icon=twitter&label)](https://twitter.com/sidebase_io) | ||
[![Join our Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/9MUHR8WT9B) | ||
|
||
A nuxt focused package to make data validation and parsing easy. This package follows the design philosophy of the article [parse, don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/). It uses [`zod`](https://github.com/colinhacks/zod) for parsing data from the user, APIs, your own functions, ... | ||
|
||
Full tsdoc-documentation is here: https://nuxt-sidebase-parse.sidebase.io | ||
|
||
[Moved here from original mono-repo](https://github.com/sidebase/sidebase-libs/tree/main/packages/nuxt-sidebase-parse) | ||
|
||
## Features | ||
|
||
- ✔️ Validate Data using [`zod`](https://github.com/colinhacks/zod) | ||
- ✔️ Deserialize and Serialize user, backend, api data | ||
- ✔️ Helpers focused on Nuxt 3 usage and developer experience | ||
|
||
## Usage | ||
|
||
|
||
```bash | ||
npm i @sidebase/nuxt-parse | ||
``` | ||
|
||
Then, e.g., in your code: | ||
|
||
- Make an arbitrary parser, e.g., to deserialize data from an API: | ||
- Example with valid data: | ||
```ts | ||
import { z, makeParser } from "@sidebase/nuxt-parse" | ||
|
||
// Define the expected response schema | ||
const responseSchema = z.object({ | ||
uuid: z.string().uuid(), | ||
}) | ||
|
||
// Perform the request, use `makeParse` to pass a transformer for the data | ||
const { data, error } = useFetch('https://httpbin.org/uuid', { | ||
transform: makeParser(responseSchema), | ||
}) | ||
|
||
console.log(`data is ${data.value}`) | ||
// -> `data is {"uuid":"f8df921c-d7f3-43c1-ac9b-3cf5d4da2f7b"}` | ||
|
||
console.log(`error is ${error.value}`) | ||
// -> `error is false` | ||
``` | ||
- Example with invalid data: | ||
```ts | ||
import { z, makeParser } from "@sidebase/nuxt-parse" | ||
|
||
// Define the expected response schema | ||
const responseSchema = z.object({ | ||
uuid: z.string().uuid(), | ||
}) | ||
|
||
// Perform the request, use `makeParse` to pass a transformer for the data | ||
const { data, error } = useFetch('https://httpbin.org/ip', { | ||
transform: makeParser(responseSchema), | ||
}) | ||
|
||
console.log(`data is ${data.value}`) | ||
// -> `data is null` | ||
|
||
console.log(`error is ${error.value}`) | ||
// -> `error is true` | ||
``` | ||
- Handle user data in an endpoint: | ||
```ts | ||
import { defineEventHandler } from 'h3' | ||
import type { CompatibilityEvent } from 'h3' | ||
import { z, parseParamsAs, parseBodyAs } from "@sidebase/nuxt-parse" | ||
// Define the schema of the parameters you expect the user to provide you with | ||
const paramsSchema = z.object({ | ||
id: z.string().uuid(), | ||
}) | ||
// Define the schema of the body you expect the user to provide you with | ||
const bodySchema = z.object({ | ||
name: z.string(), | ||
age: z.number() | ||
}) | ||
// Get a nice type to use throughout your code and components | ||
type RequestBody = z.infer<typeof bodySchema> | ||
export default defineEventHandler(async (event: CompatibilityEvent) => { | ||
// Validate and then get the parameters | ||
// This automatically throws a nice HTTP 422 error with more information if the data is invalid | ||
const params = parseParamsAs(event, paramsSchema) | ||
let body: RequestBody; | ||
try { | ||
body = parseBodyAs(event, paramsSchema) | ||
} catch(error) { | ||
// Fallback, this avoids automatic raising + returning of the HTTP 422 error | ||
body = { | ||
name: 'Bernd', | ||
age: 88 | ||
} | ||
} | ||
// Return the full entity | ||
return { | ||
id: params.id, | ||
...body | ||
} | ||
}) | ||
``` | ||
- Parse any data: | ||
```ts | ||
import { z, parseDataAs } from "@sidebase/nuxt-parse" | ||
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.number() )})) | ||
// -> throws! `"1"` is not a number, but a string! | ||
const parsedData = await parseDataAs({ test: 1 }, z.object({ test: z.number() )})) | ||
console.log(parsedData) | ||
// -> output: `1` | ||
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.string().transform(v => parseInt(v)) )})) | ||
console.log(parsedData) | ||
// -> output: `1` (we used `.transform` to ensure that we get a number) | ||
``` | ||
- Also works with async data, e.g., when fetching from another API or DB: | ||
```ts | ||
import { z, parseDataAs } from "@sidebase/nuxt-parse" | ||
const fakeDatabaseQuery = async () => { id: 1 } | ||
const parsedData = await parseDataAs(fakeDatabaseQuery, z.object({ id: z.number() )})) | ||
console.log(parsedData) | ||
// -> output: `1` | ||
``` | ||
|
||
## Documentation | ||
|
||
Full tsdoc-documentation is here: https://nuxt-sidebase-parse.sidebase.io | ||
|
||
This module exports: | ||
- `parseBodyAs`: Parse body of `h3` event | ||
- `parseParamsAs`: Parse params of `h3` event | ||
- `parseQueryAs`: Parse query of `h3` event | ||
- `parseCookieAs`: Parse cookies of `h3` event | ||
- `parseHeaderAs`: Parse header of `h3` event | ||
- `parseDataAs`: Parse sync or async data | ||
- `makeParser`: Make your own parser (see example above) | ||
- `z`: [`zod`](https://github.com/colinhacks/zod), the library used for parsing | ||
|
||
|
||
<!-- Badges --> | ||
[npm-version-src]: https://img.shields.io/npm/v/@sidebase/nuxt-parse/latest.svg | ||
[npm-version-href]: https://npmjs.com/package/@sidebase/nuxt-parse | ||
|
||
[npm-downloads-src]: https://img.shields.io/npm/dt/@sidebase/nuxt-parse.svg | ||
[npm-downloads-href]: https://npmjs.com/package/@sidebase/nuxt-parse | ||
|
||
[license-src]: https://img.shields.io/npm/l/@sidebase/nuxt-parse.svg | ||
[license-href]: https://npmjs.com/package/@sidebase/nuxt-parse |
Oops, something went wrong.