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 51115fc
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 88 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.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
},
"scripts": {
"preinstall": "npx -y only-allow pnpm",
"build": "tsup package/index.ts --format cjs,esm --dts",
"build": "tsup ./package/index.ts --format cjs,esm --dts",
"test": "vitest run --coverage",
"test:watch": "vitest --ui",
"typecheck": "pnpm run -r typecheck",
"lint": "eslint --cache ./package && prettier --check .",
"format": "prettier --write --cache .",
"ci": "pnpm run typecheck && pnpm run lint && pnpm run test && pnpm run build",
"ci": "pnpm run lint && pnpm run test && pnpm run build",
"prepare": "husky",
"commitlint": "commitlint --edit"
},
Expand Down Expand Up @@ -91,6 +90,6 @@
},
"homepage": "https://github.com/refactorthis/funcy#readme",
"workspaces": [
"examples/*"
"examples/**/*"
]
}
2 changes: 1 addition & 1 deletion package/src/integrations/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import pipeline from './middleware/api.pipeline'
import { FuncyApiOptions } from './types'
import merge from 'lodash.merge'
import { baseOptions } from 'package/src/core/defaults'
import { baseOptions } from '@core/defaults'

const defaults: Omit<FuncyApiOptions, 'handler'> = {
...baseOptions,
Expand Down
4 changes: 2 additions & 2 deletions package/src/integrations/api/middleware/api.pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ApiResultV2, FuncyApiOptions } from '../types'
import validator from './validator.mware'
import { createPipeline } from 'package/src/core/middleware/pipeline'
import { createPipeline } from '@core/middleware/pipeline'
import { MiddyfiedHandler } from '@middy/core'
import { Context } from 'package/src/core/types'
import { Context } from '@core/types'

// middy middleware
import httpContentNegotiationMiddleware from '@middy/http-content-negotiation'
Expand Down
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/**/*'
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"strictFunctionTypes": false,
"outDir": "./dist",
"paths": {
"@core": ["package/src/core"],
"@core/*": ["package/src/core/*"],
"@api": ["package/src/integrations/api"],
"@api/*": ["package/src/integrations/api/*"]
"@core": ["./package/src/core"],
"@core/*": ["./package/src/core/*"],
"@api": ["./package/src/integrations/api"],
"@api/*": ["./package/src/integrations/api/*"]
}
},
"include": ["./**/*.ts"],
Expand Down

0 comments on commit 51115fc

Please sign in to comment.