spiceflow 1.1.8 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -92
- package/dist/benchmark.benchmark.js.map +1 -1
- package/dist/client/errors.d.ts.map +1 -1
- package/dist/client/errors.js.map +1 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +8 -12
- package/dist/client/index.js.map +1 -1
- package/dist/client/types.d.ts.map +1 -1
- package/dist/client/utils.js.map +1 -1
- package/dist/client/ws.d.ts.map +1 -1
- package/dist/client/ws.js +1 -3
- package/dist/client/ws.js.map +1 -1
- package/dist/client.test.js.map +1 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/cors.d.ts.map +1 -1
- package/dist/cors.js.map +1 -1
- package/dist/cors.test.js.map +1 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js.map +1 -1
- package/dist/middleware.test.js.map +1 -1
- package/dist/openapi.d.ts.map +1 -1
- package/dist/openapi.js +1 -1
- package/dist/openapi.js.map +1 -1
- package/dist/openapi.test.js.map +1 -1
- package/dist/spiceflow.d.ts.map +1 -1
- package/dist/spiceflow.js +4 -2
- package/dist/spiceflow.js.map +1 -1
- package/dist/spiceflow.test.js.map +1 -1
- package/dist/stream.test.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/types.test.js +2 -6
- package/dist/types.test.js.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js.map +1 -1
- package/dist/zod.test.js.map +1 -1
- package/package.json +1 -1
- package/src/benchmark.benchmark.ts +8 -8
- package/src/client/errors.ts +17 -17
- package/src/client/index.ts +437 -469
- package/src/client/types.ts +168 -191
- package/src/client/utils.ts +5 -5
- package/src/client/ws.ts +87 -89
- package/src/client.test.ts +176 -183
- package/src/context.ts +82 -82
- package/src/cors.test.ts +38 -38
- package/src/cors.ts +87 -92
- package/src/error.ts +13 -13
- package/src/middleware.test.ts +201 -201
- package/src/openapi.test.ts +97 -97
- package/src/openapi.ts +365 -365
- package/src/spiceflow.test.ts +461 -467
- package/src/spiceflow.ts +1117 -1161
- package/src/stream.test.ts +310 -310
- package/src/types.test.ts +46 -50
- package/src/types.ts +698 -701
- package/src/utils.ts +79 -79
- package/src/zod.test.ts +64 -64
package/src/client.test.ts
CHANGED
|
@@ -4,223 +4,216 @@ import { Spiceflow, t } from './spiceflow.js'
|
|
|
4
4
|
import { describe, expect, it } from 'vitest'
|
|
5
5
|
|
|
6
6
|
const app = new Spiceflow()
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
.get('/stream-return', function* stream() {
|
|
90
|
-
return 'a'
|
|
91
|
-
})
|
|
92
|
-
.get('/stream-return-async', function* stream() {
|
|
93
|
-
return 'a'
|
|
94
|
-
})
|
|
95
|
-
.get('/id/:id?', ({ params: { id = 'unknown' } }) => id)
|
|
7
|
+
.get('/', () => 'a')
|
|
8
|
+
.post('/', () => 'a')
|
|
9
|
+
.get('/number', () => 1)
|
|
10
|
+
.get('/true', () => true)
|
|
11
|
+
.get('/false', () => false)
|
|
12
|
+
.post('/array', async ({ request }) => await request.json(), {
|
|
13
|
+
body: t.Array(t.String()),
|
|
14
|
+
})
|
|
15
|
+
.post('/mirror', async ({ request }) => await request.json())
|
|
16
|
+
.post('/body', async ({ request }) => await request.text(), {
|
|
17
|
+
body: t.String(),
|
|
18
|
+
})
|
|
19
|
+
.delete('/empty', async ({ request }) => {
|
|
20
|
+
const body = await request.text()
|
|
21
|
+
return { body: body || null }
|
|
22
|
+
})
|
|
23
|
+
.post('/deep/nested/mirror', async ({ request }) => await request.json(), {
|
|
24
|
+
body: t.Object({
|
|
25
|
+
username: t.String(),
|
|
26
|
+
password: t.String(),
|
|
27
|
+
}),
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
.use(
|
|
31
|
+
new Spiceflow({ basePath: '/nested' }).get('/data', ({ params }) => 'hi'),
|
|
32
|
+
)
|
|
33
|
+
// .get('/error', ({ error }) => error("I'm a teapot", 'Kirifuji Nagisa'), {
|
|
34
|
+
// response: {
|
|
35
|
+
// 200: t.Void(),
|
|
36
|
+
// 418: t.Literal('Kirifuji Nagisa'),
|
|
37
|
+
// 420: t.Literal('Snoop Dogg')
|
|
38
|
+
// }
|
|
39
|
+
// })
|
|
40
|
+
.get(
|
|
41
|
+
'/validationError',
|
|
42
|
+
// @ts-expect-error
|
|
43
|
+
() => {
|
|
44
|
+
return 'this errors because validation is wrong'
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
response: {
|
|
48
|
+
200: t.Object({
|
|
49
|
+
x: t.String(),
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
// TODO ajv does not accept dates for some reason
|
|
56
|
+
// .post('/date', ({ body: { date } }) => date, {
|
|
57
|
+
// body: t.Object({
|
|
58
|
+
// date: t.Date()
|
|
59
|
+
// })
|
|
60
|
+
// })
|
|
61
|
+
.get('/dateObject', () => ({ date: new Date() }))
|
|
62
|
+
.get('/redirect', ({ redirect }) => redirect('http://localhost:8083/true'))
|
|
63
|
+
.post('/redirect', ({ redirect }) => redirect('http://localhost:8083/true'), {
|
|
64
|
+
body: t.Object({
|
|
65
|
+
username: t.String(),
|
|
66
|
+
}),
|
|
67
|
+
})
|
|
68
|
+
// .get('/formdata', () => ({
|
|
69
|
+
// image: Bun.file('./test/kyuukurarin.mp4')
|
|
70
|
+
// }))
|
|
71
|
+
|
|
72
|
+
.get('/stream', function* stream() {
|
|
73
|
+
yield 'a'
|
|
74
|
+
yield 'b'
|
|
75
|
+
yield 'c'
|
|
76
|
+
})
|
|
77
|
+
.get('/stream-async', async function* stream() {
|
|
78
|
+
yield 'a'
|
|
79
|
+
yield 'b'
|
|
80
|
+
yield 'c'
|
|
81
|
+
})
|
|
82
|
+
.get('/stream-return', function* stream() {
|
|
83
|
+
return 'a'
|
|
84
|
+
})
|
|
85
|
+
.get('/stream-return-async', function* stream() {
|
|
86
|
+
return 'a'
|
|
87
|
+
})
|
|
88
|
+
.get('/id/:id?', ({ params: { id = 'unknown' } }) => id)
|
|
96
89
|
|
|
97
90
|
const client = createSpiceflowClient(app)
|
|
98
91
|
|
|
99
92
|
describe('client', () => {
|
|
100
|
-
|
|
101
|
-
|
|
93
|
+
it('get index', async () => {
|
|
94
|
+
const { data, error } = await client.index.get({})
|
|
102
95
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
96
|
+
expect(data).toBe('a')
|
|
97
|
+
expect(error).toBeNull()
|
|
98
|
+
})
|
|
106
99
|
|
|
107
|
-
|
|
108
|
-
|
|
100
|
+
it('post index', async () => {
|
|
101
|
+
const { data, error } = await client.index.post()
|
|
109
102
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
103
|
+
expect(data).toBe('a')
|
|
104
|
+
expect(error).toBeNull()
|
|
105
|
+
})
|
|
113
106
|
|
|
114
|
-
|
|
115
|
-
|
|
107
|
+
it('parse number', async () => {
|
|
108
|
+
const { data } = await client.number.get()
|
|
116
109
|
|
|
117
|
-
|
|
118
|
-
|
|
110
|
+
expect(data).toEqual(1)
|
|
111
|
+
})
|
|
119
112
|
|
|
120
|
-
|
|
121
|
-
|
|
113
|
+
it('parse true', async () => {
|
|
114
|
+
const { data } = await client.true.get()
|
|
122
115
|
|
|
123
|
-
|
|
124
|
-
|
|
116
|
+
expect(data).toEqual(true)
|
|
117
|
+
})
|
|
125
118
|
|
|
126
|
-
|
|
127
|
-
|
|
119
|
+
it('parse false', async () => {
|
|
120
|
+
const { data } = await client.false.get()
|
|
128
121
|
|
|
129
|
-
|
|
130
|
-
|
|
122
|
+
expect(data).toEqual(false)
|
|
123
|
+
})
|
|
131
124
|
|
|
132
|
-
|
|
133
|
-
|
|
125
|
+
it.todo('parse object with date', async () => {
|
|
126
|
+
const { data } = await client.dateObject.get()
|
|
134
127
|
|
|
135
|
-
|
|
136
|
-
|
|
128
|
+
expect(data?.date).toBeInstanceOf(Date)
|
|
129
|
+
})
|
|
137
130
|
|
|
138
|
-
|
|
139
|
-
|
|
131
|
+
it('post array', async () => {
|
|
132
|
+
const { data } = await client.array.post(['a', 'b'])
|
|
140
133
|
|
|
141
|
-
|
|
142
|
-
|
|
134
|
+
expect(data).toEqual(['a', 'b'])
|
|
135
|
+
})
|
|
143
136
|
|
|
144
|
-
|
|
145
|
-
|
|
137
|
+
it('post body', async () => {
|
|
138
|
+
const { data } = await client.body.post('a')
|
|
146
139
|
|
|
147
|
-
|
|
148
|
-
|
|
140
|
+
expect(data).toEqual('a')
|
|
141
|
+
})
|
|
149
142
|
|
|
150
|
-
|
|
151
|
-
|
|
143
|
+
it('post mirror', async () => {
|
|
144
|
+
const body = { username: 'A', password: 'B' }
|
|
152
145
|
|
|
153
|
-
|
|
146
|
+
const { data } = await client.mirror.post(body)
|
|
154
147
|
|
|
155
|
-
|
|
156
|
-
|
|
148
|
+
expect(data).toEqual(body)
|
|
149
|
+
})
|
|
157
150
|
|
|
158
|
-
|
|
159
|
-
|
|
151
|
+
it('delete empty', async () => {
|
|
152
|
+
const { data } = await client.empty.delete()
|
|
160
153
|
|
|
161
|
-
|
|
162
|
-
|
|
154
|
+
expect(data).toEqual({ body: null })
|
|
155
|
+
})
|
|
163
156
|
|
|
164
|
-
|
|
165
|
-
|
|
157
|
+
it('post deep nested mirror', async () => {
|
|
158
|
+
const body = { username: 'A', password: 'B' }
|
|
166
159
|
|
|
167
|
-
|
|
160
|
+
const { data } = await client.deep.nested.mirror.post(body)
|
|
168
161
|
|
|
169
|
-
|
|
170
|
-
|
|
162
|
+
expect(data).toEqual(body)
|
|
163
|
+
})
|
|
171
164
|
|
|
172
|
-
|
|
173
|
-
|
|
165
|
+
it('get nested data', async () => {
|
|
166
|
+
const { data } = await client.nested.data.get()
|
|
174
167
|
|
|
175
|
-
|
|
176
|
-
|
|
168
|
+
expect(data).toEqual('hi')
|
|
169
|
+
})
|
|
177
170
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
171
|
+
it('stream ', async () => {
|
|
172
|
+
const { data } = await client.stream.get()
|
|
173
|
+
let all = ''
|
|
174
|
+
for await (const chunk of data!) {
|
|
175
|
+
// console.log(chunk)
|
|
176
|
+
all += chunk + '-'
|
|
177
|
+
}
|
|
178
|
+
expect(all).toEqual('a-b-c-')
|
|
179
|
+
})
|
|
180
|
+
it('stream async', async () => {
|
|
181
|
+
const { data } = await client['stream-async'].get()
|
|
182
|
+
let all = ''
|
|
183
|
+
for await (const chunk of data!) {
|
|
184
|
+
// console.log(chunk)
|
|
185
|
+
all += chunk + '-'
|
|
186
|
+
}
|
|
187
|
+
expect(all).toEqual('a-b-c-')
|
|
188
|
+
})
|
|
196
189
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
190
|
+
it('stream return', async () => {
|
|
191
|
+
const { data } = await client['stream-return'].get()
|
|
192
|
+
expect(data).toEqual('a')
|
|
193
|
+
})
|
|
194
|
+
it('stream return async', async () => {
|
|
195
|
+
const { data } = await client['stream-return-async'].get({})
|
|
196
|
+
// console.log(data)
|
|
197
|
+
expect(data).toEqual('a')
|
|
198
|
+
})
|
|
206
199
|
|
|
207
|
-
|
|
208
|
-
|
|
200
|
+
// it('handle error', async () => {
|
|
201
|
+
// const { data, error } = await client.error.get()
|
|
209
202
|
|
|
210
|
-
|
|
203
|
+
// let value
|
|
211
204
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
205
|
+
// if (error)
|
|
206
|
+
// switch (error.status) {
|
|
207
|
+
// case 418:
|
|
208
|
+
// value = error.value
|
|
209
|
+
// break
|
|
217
210
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
211
|
+
// case 420:
|
|
212
|
+
// value = error.value
|
|
213
|
+
// break
|
|
214
|
+
// }
|
|
222
215
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
216
|
+
// expect(data).toBeNull()
|
|
217
|
+
// expect(value).toEqual('Kirifuji Nagisa')
|
|
218
|
+
// })
|
|
226
219
|
})
|
package/src/context.ts
CHANGED
|
@@ -1,110 +1,110 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
StatusMap,
|
|
3
|
+
InvertedStatusMap,
|
|
4
|
+
redirect as Redirect,
|
|
5
5
|
} from './utils.js'
|
|
6
6
|
|
|
7
7
|
import type {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
RouteSchema,
|
|
9
|
+
Prettify,
|
|
10
|
+
ResolvePath,
|
|
11
|
+
SingletonBase,
|
|
12
|
+
HTTPHeaders,
|
|
13
13
|
} from './types.js'
|
|
14
14
|
|
|
15
15
|
import { SpiceflowRequest } from './spiceflow.js'
|
|
16
16
|
|
|
17
17
|
export type ErrorContext<
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
in out Route extends RouteSchema = {},
|
|
19
|
+
in out Singleton extends SingletonBase = {
|
|
20
|
+
state: {}
|
|
21
|
+
derive: {}
|
|
22
|
+
resolve: {}
|
|
23
|
+
},
|
|
24
|
+
Path extends string = '',
|
|
25
25
|
> = Prettify<{
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
26
|
+
// body: Route['body']
|
|
27
|
+
query: undefined extends Route['query']
|
|
28
|
+
? Record<string, string | undefined>
|
|
29
|
+
: Route['query']
|
|
30
|
+
params: undefined extends Route['params']
|
|
31
|
+
? Path extends `${string}/${':' | '*'}${string}`
|
|
32
|
+
? ResolvePath<Path>
|
|
33
|
+
: { [key in string]: string }
|
|
34
|
+
: Route['params']
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
// server: Server | null
|
|
37
|
+
redirect: Redirect
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Path extracted from incoming URL
|
|
41
|
+
*
|
|
42
|
+
* Represent a value extracted from URL
|
|
43
|
+
*
|
|
44
|
+
* @example '/id/9'
|
|
45
|
+
*/
|
|
46
|
+
path: string
|
|
47
|
+
/**
|
|
48
|
+
* Path as registered to router
|
|
49
|
+
*
|
|
50
|
+
* Represent a path registered to a router, not a URL
|
|
51
|
+
*
|
|
52
|
+
* @example '/id/:id'
|
|
53
|
+
*/
|
|
54
|
+
// route: string
|
|
55
|
+
request: SpiceflowRequest<Route['body']>
|
|
56
|
+
state: Singleton['state']
|
|
57
|
+
// response: Route['response']
|
|
58
58
|
}>
|
|
59
59
|
|
|
60
60
|
export type Context<
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
in out Route extends RouteSchema = {},
|
|
62
|
+
in out Singleton extends SingletonBase = {
|
|
63
|
+
state: {}
|
|
64
|
+
derive: {}
|
|
65
|
+
resolve: {}
|
|
66
|
+
},
|
|
67
|
+
Path extends string = '',
|
|
68
68
|
> = Prettify<{
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
69
|
+
query: undefined extends Route['query']
|
|
70
|
+
? Record<string, string | undefined>
|
|
71
|
+
: Route['query']
|
|
72
|
+
params: undefined extends Route['params']
|
|
73
|
+
? Path extends `${string}/${':' | '*'}${string}`
|
|
74
|
+
? ResolvePath<Path>
|
|
75
|
+
: never
|
|
76
|
+
: Route['params']
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
// server: Server | null
|
|
79
|
+
redirect: Redirect
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
path: string
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
request: SpiceflowRequest<Route['body']>
|
|
84
|
+
state: Singleton['state']
|
|
85
|
+
response?: Route['response']
|
|
86
86
|
}>
|
|
87
87
|
|
|
88
88
|
// Use to mimic request before mapping route
|
|
89
89
|
export type MiddlewareContext<
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
in out Singleton extends SingletonBase = {
|
|
91
|
+
state: {}
|
|
92
|
+
},
|
|
93
93
|
> = Prettify<{
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
94
|
+
state: Singleton['state']
|
|
95
|
+
request: Request
|
|
96
|
+
path: string
|
|
97
|
+
query?: Record<string, string | undefined>
|
|
98
|
+
params?: Record<string, string | undefined>
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
redirect: Redirect
|
|
101
|
+
// server: Server | null
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
103
|
+
// set: {
|
|
104
|
+
// headers: HTTPHeaders
|
|
105
|
+
// status?: number
|
|
106
|
+
// redirect?: string
|
|
107
|
+
// }
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
// error: typeof error
|
|
110
110
|
}>
|