|
1 | | -import { Elysia, t } from '../src' |
2 | | -import { req } from '../test/utils' |
| 1 | +import { Elysia, ElysiaStatus, t, type UnwrapSchema } from '../src' |
3 | 2 |
|
4 | | -const challengeModel = t.Object({ |
5 | | - nonce: t.String(), |
6 | | - issued: t.Number(), |
7 | | - bits: t.Number() |
8 | | -}) |
| 3 | +const Models = { |
| 4 | + 'user.update': t.Object({ |
| 5 | + id: t.String(), |
| 6 | + name: t.Optional(t.String()) |
| 7 | + }) |
| 8 | +} |
9 | 9 |
|
10 | | -const app = new Elysia({ |
11 | | - cookie: { |
12 | | - secrets: ['a', null], |
13 | | - sign: 'challenge' |
14 | | - } |
15 | | -}) |
16 | | - .get( |
17 | | - '/set', |
18 | | - ({ cookie: { challenge } }) => { |
19 | | - challenge.value = { |
20 | | - nonce: 'hello', |
21 | | - bits: 19, |
22 | | - issued: Date.now() |
23 | | - } |
| 10 | +type Models = { |
| 11 | + [k in keyof typeof Models]: UnwrapSchema<(typeof Models)[k]> |
| 12 | +} |
| 13 | + |
| 14 | +const app = new Elysia() |
| 15 | + .macro('isAuth', { |
| 16 | + // headers: t.Object({ |
| 17 | + // authorization: t.TemplateLiteral('Authorization ${string}') |
| 18 | + // }), |
| 19 | + async resolve({ headers, status }) { |
| 20 | + // Mock authentication logic |
| 21 | + if (Math.random() > 0.5) return status(401, 'Not signed in') |
| 22 | + if (Math.random() > 0.5) return status(401, 'Deactivated account') |
24 | 23 |
|
25 | | - return challenge.value |
26 | | - }, |
27 | | - { |
28 | | - cookie: t.Cookie({ |
29 | | - challenge: t.Optional(challengeModel) |
30 | | - }) |
31 | | - } |
32 | | - ) |
33 | | - .get( |
34 | | - '/get', |
35 | | - ({ cookie: { challenge } }) => { |
36 | 24 | return { |
37 | | - type: typeof challenge, |
38 | | - value: challenge.value |
| 25 | + user: 'saltyaom' |
39 | 26 | } |
40 | | - }, |
41 | | - { |
42 | | - cookie: t.Cookie({ |
43 | | - challenge: challengeModel |
44 | | - }) |
45 | | - } |
46 | | - ) |
47 | | - |
48 | | -const first = await app.handle( |
49 | | - req('/set', { |
50 | | - headers: { |
51 | | - cookie: `challenge=${JSON.stringify({ |
52 | | - nonce: 'hello', |
53 | | - bits: 19, |
54 | | - issued: 1770750432990 |
55 | | - })}` |
56 | 27 | } |
57 | 28 | }) |
58 | | -) |
| 29 | + .model(Models) |
| 30 | + .macro('isAdmin', { |
| 31 | + isAuth: true, |
| 32 | + async resolve({ headers, status }) { |
| 33 | + // Mock admin check logic |
| 34 | + if (Math.random() > 0.5) return status(403, 'Not allowed') |
| 35 | + |
59 | 36 |
|
60 | | -console.log(first.status) |
61 | | -console.log(await first.json()) |
62 | 37 |
|
63 | | -const cookie = first.headers.get('set-cookie') |
64 | | -const challenge = cookie!.match(/challenge=([^;]*)/)![1] |
| 38 | + headers.authorization |
65 | 39 |
|
66 | | -console.log(challenge) |
| 40 | + return { |
| 41 | + admin: { |
| 42 | + async updateUser({ id, ...rest }: Models['user.update']) { |
| 43 | + if (Math.random() > 0.5) return status(404, 'No user') |
67 | 44 |
|
68 | | -const second = await app |
69 | | - .handle( |
70 | | - req('/get', { |
71 | | - headers: { |
72 | | - cookie: `challenge=${challenge}` |
| 45 | + return { id } |
| 46 | + } |
| 47 | + } |
73 | 48 | } |
74 | | - }) |
| 49 | + } |
| 50 | + }) |
| 51 | + .post( |
| 52 | + '/', |
| 53 | + async ({ user, admin, body, headers }) => { |
| 54 | + return 'ok' |
| 55 | + // const updated = await admin.updateUser(body) |
| 56 | + |
| 57 | + // if (updated instanceof ElysiaStatus) return updated |
| 58 | + |
| 59 | + // return `User ${user} updated user ${updated.id}` as const |
| 60 | + }, |
| 61 | + { |
| 62 | + isAuth: true, |
| 63 | + // body: 'user.update' |
| 64 | + } |
75 | 65 | ) |
76 | 66 |
|
77 | | -console.log(second.status) |
| 67 | +type Result = (typeof app)['~Routes']['post']['response'] |
0 commit comments