|
| 1 | +import { |
| 2 | + type ctx, |
| 3 | + type MiddlewareDataArray, |
| 4 | + getDataFromMiddleware, |
| 5 | + SetResponse, |
| 6 | + ValidationType, |
| 7 | +} from '@/endpoints.ts'; |
| 8 | +import { IUser } from '@/db/models/Users.ts'; |
| 9 | +import { z } from 'npm:zod'; |
| 10 | + |
| 11 | +/** |
| 12 | + * STEP 1: (OPTIONAL) Do this setting on |
| 13 | + * If you using VSCode, enable 'editor.foldingImportsByDefault' to fold imports |
| 14 | + * and get better DX. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * STEP 2: (REQUIRED) Endpoint path implementation |
| 19 | + * Don't forget to add filepath to endpoints.ts's loadEndpoints function's return array. |
| 20 | + * Because Deno Deploy don't allow dynamic imports cause of security reasons. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * STEP 3: (REQUIRED) Readability |
| 25 | + * Don't want create a new file? Create it! Readability is most important thing. |
| 26 | + * Split to multiple files if you need. Single Responsibility Principle is base rule. |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * POST /api/postEvent |
| 31 | + * |
| 32 | + * What do: Post the event |
| 33 | + */ |
| 34 | +export function POST(ctx: ctx, _middlewareDatas: MiddlewareDataArray): void { |
| 35 | + // .user means user so developer level which not base(library) level |
| 36 | + const authData = getDataFromMiddleware( |
| 37 | + _middlewareDatas, |
| 38 | + 'auth:validToken' |
| 39 | + ).user; |
| 40 | + const user = authData as IUser; |
| 41 | + |
| 42 | + const requestData = getDataFromMiddleware( |
| 43 | + _middlewareDatas, |
| 44 | + 'dataValidation' |
| 45 | + ).user; |
| 46 | + const query: QueryType = requestData.query; |
| 47 | + const body: BodyType = requestData.body; |
| 48 | + |
| 49 | + console.log('logging', query.eventId); |
| 50 | + console.log('Another logging', body.name); |
| 51 | + |
| 52 | + return SetResponse(ctx, { |
| 53 | + status: 200, |
| 54 | + body: { |
| 55 | + username: user.username, |
| 56 | + }, |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * GET /api/postEvent |
| 62 | + * |
| 63 | + * What do: Get the event |
| 64 | + */ |
| 65 | +export function GET(ctx: ctx, _middlewareDatas: MiddlewareDataArray): void { |
| 66 | + return SetResponse(ctx, { |
| 67 | + status: 200, |
| 68 | + body: {}, |
| 69 | + }); |
| 70 | + // ... |
| 71 | +} |
| 72 | + |
| 73 | +// Configurations |
| 74 | +export const pattern = new URLPattern({ pathname: '/api/postEvent' }); |
| 75 | +export const validation: ValidationType = { |
| 76 | + // Please use for 'GET', 'DELETE', 'OPTIONS' or 'HEAD' |
| 77 | + // /api/postEvent?test=anystring |
| 78 | + query: { |
| 79 | + eventId: z.string().min(4), |
| 80 | + }, |
| 81 | + |
| 82 | + // Please use for 'POST', 'PUT' or 'PATCH' |
| 83 | + // /api/postEvent |
| 84 | + // { |
| 85 | + // name: 'anystring', |
| 86 | + // description: 'anystring', |
| 87 | + // } |
| 88 | + body: z.object({ |
| 89 | + name: z.string().min(5), |
| 90 | + description: z.string().min(1), |
| 91 | + }), |
| 92 | +}; |
| 93 | +export const middlewares = ['auth:validToken']; |
| 94 | + |
| 95 | +const querySchema = z.object(validation.query as z.ZodRawShape); |
| 96 | +type QueryType = z.infer<typeof querySchema>; |
| 97 | +type BodyType = z.infer<typeof validation.body>; |
0 commit comments