-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for yup schema library
* modifications to allow either yup or zod to be used * renamed sst example to sst2 as sst3 is released.
- Loading branch information
1 parent
f38f034
commit 6b02dbe
Showing
32 changed files
with
242 additions
and
30 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "sst-api", | ||
"name": "sst-v2", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
|
2 changes: 1 addition & 1 deletion
2
examples/sst-api/packages/core/package.json → examples/sst-v2/packages/core/package.json
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "@sst-api/core", | ||
"name": "@sst-v2/core", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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,28 @@ | ||
import { object, string, number, date, array } from 'yup' | ||
|
||
export const GetTodoPath = object({ | ||
id: string().required(), | ||
}) | ||
|
||
export const ListQuery = object({ | ||
skip: number().optional(), | ||
take: number().optional(), | ||
}) | ||
|
||
export const CreateTodoRequest = object({ | ||
id: string(), | ||
title: string(), | ||
description: string(), | ||
due: date(), | ||
}) | ||
|
||
export const TodoResponse = object({ | ||
id: string(), | ||
title: string(), | ||
}) | ||
|
||
export const ListTodoResponse = object({ | ||
items: array(TodoResponse), | ||
skip: number(), | ||
take: number(), | ||
}) |
2 changes: 1 addition & 1 deletion
2
...es/sst-api/packages/functions/src/todo.ts → ...v2/packages/functions/src/api-yup/todo.ts
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
File renamed without changes.
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,46 @@ | ||
import { Todo } from '@sst-v2/core/todo' | ||
import { api, res } from '@refactorthis/funcy' | ||
import { CreateTodoRequest, GetTodoPath, ListQuery, ListTodoResponse, TodoResponse } from './models' | ||
|
||
export const create = api({ | ||
parser: { | ||
request: CreateTodoRequest, | ||
}, | ||
handler: async ({ request }) => { | ||
await Todo.create(request) | ||
return res.created() | ||
}, | ||
}) | ||
|
||
export const get = api({ | ||
parser: { | ||
path: GetTodoPath, | ||
response: TodoResponse, | ||
}, | ||
handler: async ({ path }) => { | ||
const response = await Todo.get(path.id) | ||
return res.ok(response) | ||
}, | ||
}) | ||
|
||
export const list = api({ | ||
parser: { | ||
query: ListQuery, | ||
response: ListTodoResponse, | ||
}, | ||
handler: async ({ query }) => { | ||
const { skip = 0, take = 20 } = query | ||
const items = await Todo.list(skip, take) | ||
return res.ok({ items, skip, take }) | ||
}, | ||
}) | ||
|
||
export const remove = api({ | ||
parser: { | ||
path: GetTodoPath, | ||
}, | ||
handler: async ({ path }) => { | ||
await Todo.remove(path.id) | ||
return res.ok() | ||
}, | ||
}) |
2 changes: 1 addition & 1 deletion
2
...ages/functions/src/events/todo-created.ts → ...ages/functions/src/events/todo-created.ts
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
File renamed without changes.
File renamed without changes.
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
35 changes: 35 additions & 0 deletions
35
examples/sst-api/pnpm-lock.yaml → examples/sst-v2/pnpm-lock.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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
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,47 @@ | ||
import { it, expect } from 'vitest' | ||
import { createApi, res } from 'package/src/integrations/api' | ||
import * as events from '../../mocks/api-proxy-events' | ||
import { ctx } from '../../mocks/lambda-context' | ||
import { object, number, string } from 'yup' | ||
import { APIGatewayProxyEventV2 } from 'aws-lambda' | ||
|
||
const api = createApi() | ||
|
||
const fn = api({ | ||
parser: { | ||
request: object({ | ||
id: number(), | ||
name: string().required(), | ||
}), | ||
}, | ||
handler: () => res.ok(), | ||
}) | ||
|
||
it("should fail if doesn't pass validation", async () => { | ||
const event: APIGatewayProxyEventV2 = { | ||
...events.payloadV2, | ||
body: JSON.stringify({ | ||
id: 1, | ||
}), | ||
} | ||
|
||
const response = await fn(event, ctx()) | ||
expect(response.statusCode).toBe(400) | ||
expect(JSON.parse(response.body!)).toMatchObject({ | ||
message: 'Invalid Request', | ||
details: ['name is a required field'], | ||
}) | ||
}) | ||
|
||
it('should succeed if passes validation', async () => { | ||
const event: APIGatewayProxyEventV2 = { | ||
...events.payloadV2, | ||
body: JSON.stringify({ | ||
id: 1, | ||
name: 'Test', | ||
}), | ||
} | ||
|
||
const response = await fn(event, ctx()) | ||
expect(response.statusCode).toBe(200) | ||
}) |
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
Oops, something went wrong.