|
1 | | -import { Elysia, t } from 'elysia' |
2 | | -import { openapi, withHeaders } from '../src/index' |
3 | | - |
4 | | -const schema = t.Object({ |
5 | | - test: t.Literal('hello') |
6 | | -}) |
7 | | - |
8 | | -const schema2 = t.Object({ |
9 | | - test: t.Literal('world') |
10 | | -}) |
11 | | - |
12 | | -const user = t.Object({ |
13 | | - name: t.String({ |
14 | | - example: 'saltyaom' |
15 | | - }) |
16 | | -}) |
17 | | - |
18 | | -export const app = new Elysia() |
19 | | - .use( |
20 | | - openapi({ |
21 | | - provider: 'scalar', |
22 | | - documentation: { |
23 | | - info: { |
24 | | - title: 'Elysia Scalar', |
25 | | - version: '1.3.1a' |
26 | | - }, |
27 | | - tags: [ |
28 | | - { |
29 | | - name: 'Test', |
30 | | - description: 'Hello' |
31 | | - } |
32 | | - ], |
33 | | - components: { |
34 | | - securitySchemes: { |
35 | | - bearer: { |
36 | | - type: 'http', |
37 | | - scheme: 'bearer' |
38 | | - }, |
39 | | - cookie: { |
40 | | - type: 'apiKey', |
41 | | - in: 'cookie', |
42 | | - name: 'session_id' |
43 | | - } |
44 | | - } |
45 | | - } |
46 | | - } |
47 | | - }) |
48 | | - ) |
49 | | - .model({ schema, schema2, user }) |
50 | | - .get( |
51 | | - '/', |
52 | | - { test: 'hello' as const }, |
53 | | - { |
54 | | - response: { |
55 | | - 200: t.Object({ |
56 | | - test: t.Literal('hello') |
57 | | - }), |
58 | | - 204: withHeaders( |
59 | | - t.Void({ |
60 | | - title: 'Thing', |
61 | | - description: 'Void response' |
62 | | - }), |
63 | | - { |
64 | | - 'X-Custom-Header': t.Literal('Elysia') |
65 | | - } |
66 | | - ) |
67 | | - } |
68 | | - } |
69 | | - ) |
70 | | - .post( |
71 | | - '/json', |
72 | | - ({ body }) => ({ |
73 | | - test: 'world' |
74 | | - }), |
75 | | - { |
76 | | - parse: ['json', 'formdata'], |
77 | | - body: 'user', |
78 | | - response: { |
79 | | - 200: 'schema', |
80 | | - 400: 'schema2' |
81 | | - } |
82 | | - } |
83 | | - ) |
84 | | - .get('/id/:id/name/:name', () => {}) |
85 | | - .listen(3000) |
| 1 | +import { Elysia, t } from 'elysia' |
| 2 | +import z from 'zod' |
| 3 | + |
| 4 | +import { openapi, withHeaders } from '../src/index' |
| 5 | + |
| 6 | +const schema = t.Object({ |
| 7 | + test: t.Literal('hello') |
| 8 | +}) |
| 9 | + |
| 10 | +const schema2 = t.Object({ |
| 11 | + test: t.Literal('world') |
| 12 | +}) |
| 13 | + |
| 14 | +const user = t.Object({ |
| 15 | + name: t.String({ |
| 16 | + example: 'saltyaom' |
| 17 | + }) |
| 18 | +}) |
| 19 | + |
| 20 | +export const app = new Elysia() |
| 21 | + .use( |
| 22 | + openapi({ |
| 23 | + provider: 'scalar', |
| 24 | + documentation: { |
| 25 | + info: { |
| 26 | + title: 'Elysia Scalar', |
| 27 | + version: '1.3.1a' |
| 28 | + }, |
| 29 | + tags: [ |
| 30 | + { |
| 31 | + name: 'Test', |
| 32 | + description: 'Hello' |
| 33 | + } |
| 34 | + ], |
| 35 | + components: { |
| 36 | + securitySchemes: { |
| 37 | + bearer: { |
| 38 | + type: 'http', |
| 39 | + scheme: 'bearer' |
| 40 | + }, |
| 41 | + cookie: { |
| 42 | + type: 'apiKey', |
| 43 | + in: 'cookie', |
| 44 | + name: 'session_id' |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + }) |
| 50 | + ) |
| 51 | + .model({ schema, schema2, user }) |
| 52 | + .get( |
| 53 | + '/', |
| 54 | + { test: 'hello' as const }, |
| 55 | + { |
| 56 | + response: { |
| 57 | + 200: t.Object({ |
| 58 | + test: t.Literal('hello') |
| 59 | + }), |
| 60 | + 204: withHeaders( |
| 61 | + t.Void({ |
| 62 | + title: 'Thing', |
| 63 | + description: 'Void response' |
| 64 | + }), |
| 65 | + { |
| 66 | + 'X-Custom-Header': t.Literal('Elysia') |
| 67 | + } |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + ) |
| 72 | + .post( |
| 73 | + '/json', |
| 74 | + ({ body }) => ({ |
| 75 | + test: 'world' |
| 76 | + }), |
| 77 | + { |
| 78 | + parse: ['json', 'formdata'], |
| 79 | + body: 'schema', |
| 80 | + response: { |
| 81 | + 200: 'schema', |
| 82 | + 400: z.object({ |
| 83 | + a: z.string(), |
| 84 | + b: z.literal('a') |
| 85 | + }) |
| 86 | + } |
| 87 | + } |
| 88 | + ) |
| 89 | + .post( |
| 90 | + '/json/:id', |
| 91 | + ({ body, params: { id }, query: { name, email, birthday } }) => ({ |
| 92 | + ...body, |
| 93 | + id, |
| 94 | + name, |
| 95 | + email, |
| 96 | + birthday |
| 97 | + }), |
| 98 | + { |
| 99 | + params: t.Object({ |
| 100 | + id: t.String() |
| 101 | + }), |
| 102 | + query: t.Object({ |
| 103 | + name: t.String(), |
| 104 | + email: t.String({ |
| 105 | + description: 'sample email description', |
| 106 | + format: 'email' |
| 107 | + }), |
| 108 | + birthday: t.String({ |
| 109 | + description: 'sample birthday description', |
| 110 | + pattern: '\\d{4}-\\d{2}-\\d{2}', |
| 111 | + minLength: 10, |
| 112 | + maxLength: 10 |
| 113 | + }) |
| 114 | + }), |
| 115 | + body: t.Object({ |
| 116 | + username: t.String(), |
| 117 | + password: t.String() |
| 118 | + }), |
| 119 | + response: t.Object( |
| 120 | + { |
| 121 | + username: t.String(), |
| 122 | + password: t.String(), |
| 123 | + id: t.String(), |
| 124 | + name: t.String(), |
| 125 | + email: t.String({ |
| 126 | + description: 'sample email description', |
| 127 | + format: 'email' |
| 128 | + }), |
| 129 | + birthday: t.String({ |
| 130 | + description: 'sample birthday description', |
| 131 | + pattern: '\\d{4}-\\d{2}-\\d{2}', |
| 132 | + minLength: 10, |
| 133 | + maxLength: 10 |
| 134 | + }) |
| 135 | + }, |
| 136 | + { description: 'sample description 3' } |
| 137 | + ) |
| 138 | + } |
| 139 | + ) |
| 140 | + .get('/id/:id/name/:name', () => {}) |
| 141 | + .listen(3000) |
0 commit comments