Skip to content

Commit

Permalink
refactor: extract parsers from examples
Browse files Browse the repository at this point in the history
  • Loading branch information
refactorthis committed Sep 5, 2024
1 parent ca9add2 commit e2c1eb0
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 78 deletions.
22 changes: 22 additions & 0 deletions examples/parsers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "parsers",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@tsconfig/node20": "^20.1.4",
"typescript": "^5.5.4",
"typescript-eslint": "^7.18.0"
},
"dependencies": {
"yup": "^1.4.0",
"zod": "^3.23.8"
}
}
3 changes: 3 additions & 0 deletions examples/parsers/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Parsers

Shows example usage of various parsers / validation libraries
59 changes: 59 additions & 0 deletions examples/parsers/src/yup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { api, res } from '@refactorthis/funcy'
import { object, string, number, date, array } from 'yup'

export const GetTodoPath = object({
id: string().required(),
})

export const ListQuery = object({
skip: number(),
take: number(),
})

export const CreateTodoRequest = object({
id: string().required(),
title: string().required(),
description: string().required(),
due: date().required(),
})

export const TodoResponse = object({
id: string(),
title: string(),
})

export const ListTodoResponse = object({
items: array(TodoResponse),
skip: number(),
take: number(),
})

export const create = api({
parser: {
request: CreateTodoRequest,
},
handler: async ({ request }) => res.created(request),
})

export const get = api({
parser: {
path: GetTodoPath,
response: TodoResponse,
},
handler: async ({ path }) => res.ok(path),
})

export const list = api({
parser: {
query: ListQuery,
response: ListTodoResponse,
},
handler: async ({ query }) => res.ok(query),
})

export const remove = api({
parser: {
path: GetTodoPath,
},
handler: async ({ path }) => res.ok(path),
})
59 changes: 59 additions & 0 deletions examples/parsers/src/zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { api, res } from '@refactorthis/funcy'
import z from 'zod'

export const GetTodoPath = z.object({
id: z.string(),
})

export const ListQuery = z.object({
skip: z.number().optional(),
take: z.number().optional(),
})

export const CreateTodoRequest = z.object({
id: z.string(),
title: z.string(),
description: z.string(),
due: z.coerce.date(),
})

export const TodoResponse = z.object({
id: z.string(),
title: z.string(),
})

export const ListTodoResponse = z.object({
items: z.array(TodoResponse),
skip: z.number(),
take: z.number(),
})

export const create = api({
parser: {
request: CreateTodoRequest,
},
handler: async ({ request }) => res.created(request),
})

export const get = api({
parser: {
path: GetTodoPath,
response: TodoResponse,
},
handler: async ({ path }) => res.ok(path),
})

export const list = api({
parser: {
query: ListQuery,
response: ListTodoResponse,
},
handler: async ({ query }) => res.ok(query),
})

export const remove = api({
parser: {
path: GetTodoPath,
},
handler: async ({ path }) => res.ok(path),
})
15 changes: 15 additions & 0 deletions examples/parsers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "@tsconfig/node20/tsconfig.json",
"include": ["./src/*.ts"],
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
"baseUrl": ".",
"strictNullChecks": true,
"strictFunctionTypes": false,
"allowSyntheticDefaultImports": true,
"paths": {
"@refactorthis/funcy": ["../../package"]
}
}
}
3 changes: 1 addition & 2 deletions examples/sst-v2/packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"vitest": "^1.5.0"
},
"dependencies": {
"zod": "^3.22.4",
"yup": "^1.4.0"
"zod": "^3.22.4"
}
}
28 changes: 0 additions & 28 deletions examples/sst-v2/packages/functions/src/api-yup/models.ts

This file was deleted.

46 changes: 0 additions & 46 deletions examples/sst-v2/packages/functions/src/api-zod/todo.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@
},
"homepage": "https://github.com/refactorthis/funcy#readme",
"workspaces": [
"examples/*"
"examples/**/*"
]
}
54 changes: 54 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
packages:
- 'examples/*'
- 'examples/**/*'

0 comments on commit e2c1eb0

Please sign in to comment.