Skip to content

Commit 6a48f72

Browse files
committed
feat!: transfer package from monorepo
0 parents  commit 6a48f72

File tree

16 files changed

+6111
-0
lines changed

16 files changed

+6111
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
9+
[**.{json,js,ts,y{a,}ml}]
10+
indent_style = space
11+
indent_size = 2
12+
13+
[*.md]
14+
indent_style = space
15+
indent_size = 4

.eslintrc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es2021": true
5+
},
6+
"extends": [
7+
"eslint:recommended",
8+
"plugin:@typescript-eslint/recommended"
9+
],
10+
"overrides": [],
11+
"parser": "@typescript-eslint/parser",
12+
"parserOptions": {
13+
"ecmaVersion": "latest",
14+
"sourceType": "module"
15+
},
16+
"plugins": [
17+
"@typescript-eslint"
18+
],
19+
"rules": {
20+
"indent": [
21+
"error",
22+
2
23+
],
24+
"linebreak-style": [
25+
"error",
26+
"unix"
27+
],
28+
"quotes": [
29+
"error",
30+
"double"
31+
],
32+
"semi": [
33+
"error",
34+
"never"
35+
]
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: "\U0001F41E Bug report"
2+
description: Create a report to help us improve sidebase
3+
labels: ["bug"]
4+
body:
5+
- type: markdown
6+
attributes:
7+
value: |
8+
Please help us finding and resolving problems in sidebase.
9+
10+
- type: textarea
11+
id: bug-env
12+
attributes:
13+
label: Environment
14+
description: You can use `npx nuxi info` to fill this section
15+
placeholder: Environment
16+
- type: textarea
17+
id: reproduction
18+
attributes:
19+
label: Reproduction
20+
description: Provide a link to a repo that can reproduce the problem you ran into if you can.
21+
placeholder: Reproduction
22+
- type: textarea
23+
id: bug-description
24+
attributes:
25+
label: Describe the bug
26+
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!
27+
placeholder: Bug description
28+
validations:
29+
required: true
30+
- type: textarea
31+
id: additonal
32+
attributes:
33+
label: Additional context
34+
description: If applicable, add any other context about the problem here
35+
- type: textarea
36+
id: logs
37+
attributes:
38+
label: Logs
39+
description: |
40+
Optional if provided reproduction. Please try not to insert an image but copy paste the log text.
41+
render: shell

.github/ISSUE_TEMPLATE/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
contact_links:
2+
- name: 💬 Discussions
3+
url: https://github.com/sidestream-tech/sidebase/discussions
4+
about: Use discussions if you have an idea for improvement and asking questions
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: "🚀 Feature request"
2+
description: Suggest a feature that will improve sidebase
3+
labels: ["enhancement"]
4+
body:
5+
- type: markdown
6+
attributes:
7+
value: |
8+
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! ❤️
9+
- type: textarea
10+
id: feature-description
11+
attributes:
12+
label: Describe the feature
13+
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.
14+
placeholder: Feature description
15+
validations:
16+
required: true
17+
- type: textarea
18+
id: additional-info
19+
attributes:
20+
label: Additional information
21+
description: Additional information that helps us decide how to proceed.
22+
placeholder: Additional information

.github/workflows/ci.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v3
16+
- name: Use Node.js 16.14.2
17+
uses: actions/setup-node@v3
18+
with:
19+
node-version: 16.14.2
20+
21+
- run: npm ci
22+
23+
# Initialize all sub-dependencies
24+
- run: npm run lint
25+
- run: npm run test

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules/
2+
coverage/
3+
dist/
4+
5+
*.log
6+
docs/
7+
.DS_Store

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vitest.config.ts
2+
tsconfig.json
3+
.eslintrc
4+
5+
coverage/
6+
tests/
7+
src/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 SIDESTREAM GmbH
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# nuxt-parse
2+
3+
[![npm version][npm-version-src]][npm-version-href]
4+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
5+
[![GitHub stars](https://badgen.net/github/stars/sidebase/nuxt-parse)](https://GitHub.com/sidebase/nuxt-parse/)
6+
[![License][license-src]][license-href]
7+
[![Follow us on Twitter](https://badgen.net/badge/icon/twitter?icon=twitter&label)](https://twitter.com/sidebase_io)
8+
[![Join our Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/9MUHR8WT9B)
9+
10+
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, ...
11+
12+
Full tsdoc-documentation is here: https://nuxt-sidebase-parse.sidebase.io
13+
14+
[Moved here from original mono-repo](https://github.com/sidebase/sidebase-libs/tree/main/packages/nuxt-sidebase-parse)
15+
16+
## Features
17+
18+
- ✔️ Validate Data using [`zod`](https://github.com/colinhacks/zod)
19+
- ✔️ Deserialize and Serialize user, backend, api data
20+
- ✔️ Helpers focused on Nuxt 3 usage and developer experience
21+
22+
## Usage
23+
24+
25+
```bash
26+
npm i @sidebase/nuxt-parse
27+
```
28+
29+
Then, e.g., in your code:
30+
31+
- Make an arbitrary parser, e.g., to deserialize data from an API:
32+
- Example with valid data:
33+
```ts
34+
import { z, makeParser } from "@sidebase/nuxt-parse"
35+
36+
// Define the expected response schema
37+
const responseSchema = z.object({
38+
uuid: z.string().uuid(),
39+
})
40+
41+
// Perform the request, use `makeParse` to pass a transformer for the data
42+
const { data, error } = useFetch('https://httpbin.org/uuid', {
43+
transform: makeParser(responseSchema),
44+
})
45+
46+
console.log(`data is ${data.value}`)
47+
// -> `data is {"uuid":"f8df921c-d7f3-43c1-ac9b-3cf5d4da2f7b"}`
48+
49+
console.log(`error is ${error.value}`)
50+
// -> `error is false`
51+
```
52+
- Example with invalid data:
53+
```ts
54+
import { z, makeParser } from "@sidebase/nuxt-parse"
55+
56+
// Define the expected response schema
57+
const responseSchema = z.object({
58+
uuid: z.string().uuid(),
59+
})
60+
61+
// Perform the request, use `makeParse` to pass a transformer for the data
62+
const { data, error } = useFetch('https://httpbin.org/ip', {
63+
transform: makeParser(responseSchema),
64+
})
65+
66+
console.log(`data is ${data.value}`)
67+
// -> `data is null`
68+
69+
console.log(`error is ${error.value}`)
70+
// -> `error is true`
71+
```
72+
- Handle user data in an endpoint:
73+
```ts
74+
import { defineEventHandler } from 'h3'
75+
import type { CompatibilityEvent } from 'h3'
76+
import { z, parseParamsAs, parseBodyAs } from "@sidebase/nuxt-parse"
77+
78+
// Define the schema of the parameters you expect the user to provide you with
79+
const paramsSchema = z.object({
80+
id: z.string().uuid(),
81+
})
82+
83+
// Define the schema of the body you expect the user to provide you with
84+
const bodySchema = z.object({
85+
name: z.string(),
86+
age: z.number()
87+
})
88+
89+
// Get a nice type to use throughout your code and components
90+
type RequestBody = z.infer<typeof bodySchema>
91+
92+
export default defineEventHandler(async (event: CompatibilityEvent) => {
93+
// Validate and then get the parameters
94+
// This automatically throws a nice HTTP 422 error with more information if the data is invalid
95+
const params = parseParamsAs(event, paramsSchema)
96+
97+
let body: RequestBody;
98+
try {
99+
body = parseBodyAs(event, paramsSchema)
100+
} catch(error) {
101+
// Fallback, this avoids automatic raising + returning of the HTTP 422 error
102+
body = {
103+
name: 'Bernd',
104+
age: 88
105+
}
106+
}
107+
108+
// Return the full entity
109+
return {
110+
id: params.id,
111+
...body
112+
}
113+
})
114+
```
115+
- Parse any data:
116+
```ts
117+
import { z, parseDataAs } from "@sidebase/nuxt-parse"
118+
119+
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.number() )}))
120+
// -> throws! `"1"` is not a number, but a string!
121+
122+
const parsedData = await parseDataAs({ test: 1 }, z.object({ test: z.number() )}))
123+
console.log(parsedData)
124+
// -> output: `1`
125+
126+
127+
const parsedData = await parseDataAs({ test: "1" }, z.object({ test: z.string().transform(v => parseInt(v)) )}))
128+
console.log(parsedData)
129+
// -> output: `1` (we used `.transform` to ensure that we get a number)
130+
131+
132+
```
133+
- Also works with async data, e.g., when fetching from another API or DB:
134+
```ts
135+
import { z, parseDataAs } from "@sidebase/nuxt-parse"
136+
137+
const fakeDatabaseQuery = async () => { id: 1 }
138+
const parsedData = await parseDataAs(fakeDatabaseQuery, z.object({ id: z.number() )}))
139+
140+
console.log(parsedData)
141+
// -> output: `1`
142+
```
143+
144+
## Documentation
145+
146+
Full tsdoc-documentation is here: https://nuxt-sidebase-parse.sidebase.io
147+
148+
This module exports:
149+
- `parseBodyAs`: Parse body of `h3` event
150+
- `parseParamsAs`: Parse params of `h3` event
151+
- `parseQueryAs`: Parse query of `h3` event
152+
- `parseCookieAs`: Parse cookies of `h3` event
153+
- `parseHeaderAs`: Parse header of `h3` event
154+
- `parseDataAs`: Parse sync or async data
155+
- `makeParser`: Make your own parser (see example above)
156+
- `z`: [`zod`](https://github.com/colinhacks/zod), the library used for parsing
157+
158+
159+
<!-- Badges -->
160+
[npm-version-src]: https://img.shields.io/npm/v/@sidebase/nuxt-parse/latest.svg
161+
[npm-version-href]: https://npmjs.com/package/@sidebase/nuxt-parse
162+
163+
[npm-downloads-src]: https://img.shields.io/npm/dt/@sidebase/nuxt-parse.svg
164+
[npm-downloads-href]: https://npmjs.com/package/@sidebase/nuxt-parse
165+
166+
[license-src]: https://img.shields.io/npm/l/@sidebase/nuxt-parse.svg
167+
[license-href]: https://npmjs.com/package/@sidebase/nuxt-parse

0 commit comments

Comments
 (0)