spiceflow 1.0.8 → 1.1.0

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.
Files changed (71) hide show
  1. package/README.md +1 -1
  2. package/dist/client/index.d.ts +1 -1
  3. package/dist/client/index.d.ts.map +1 -1
  4. package/dist/client/index.js.map +1 -1
  5. package/dist/client/types.d.ts +1 -1
  6. package/dist/client/types.d.ts.map +1 -1
  7. package/dist/client/ws.d.ts +1 -1
  8. package/dist/client/ws.d.ts.map +1 -1
  9. package/dist/client.test.js +1 -18
  10. package/dist/client.test.js.map +1 -1
  11. package/dist/{elysia-fork/context.d.ts → context.d.ts} +8 -7
  12. package/dist/context.d.ts.map +1 -0
  13. package/dist/{elysia-fork/context.js.map → context.js.map} +1 -1
  14. package/dist/error.d.ts.map +1 -0
  15. package/dist/error.js.map +1 -0
  16. package/dist/index.d.ts +1 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/middleware.test.d.ts +2 -0
  21. package/dist/middleware.test.d.ts.map +1 -0
  22. package/dist/middleware.test.js +99 -0
  23. package/dist/middleware.test.js.map +1 -0
  24. package/dist/openapi.d.ts +4 -15
  25. package/dist/openapi.d.ts.map +1 -1
  26. package/dist/spiceflow.d.ts +39 -120
  27. package/dist/spiceflow.d.ts.map +1 -1
  28. package/dist/spiceflow.js +128 -165
  29. package/dist/spiceflow.js.map +1 -1
  30. package/dist/spiceflow.test.js +54 -16
  31. package/dist/spiceflow.test.js.map +1 -1
  32. package/dist/types.d.ts +406 -0
  33. package/dist/types.d.ts.map +1 -1
  34. package/dist/types.js +1 -1
  35. package/dist/types.js.map +1 -1
  36. package/dist/utils.d.ts +72 -0
  37. package/dist/utils.d.ts.map +1 -1
  38. package/dist/utils.js +69 -0
  39. package/dist/utils.js.map +1 -1
  40. package/package.json +1 -1
  41. package/src/client/index.ts +1 -3
  42. package/src/client/types.ts +6 -13
  43. package/src/client/ws.ts +1 -1
  44. package/src/client.test.ts +1 -19
  45. package/src/context.ts +128 -0
  46. package/src/index.ts +1 -2
  47. package/src/middleware.test.ts +107 -0
  48. package/src/openapi.ts +1 -1
  49. package/src/spiceflow.test.ts +74 -16
  50. package/src/spiceflow.ts +212 -385
  51. package/src/types.test.ts +1 -1
  52. package/src/types.ts +926 -0
  53. package/src/utils.ts +84 -0
  54. package/dist/elysia-fork/context.d.ts.map +0 -1
  55. package/dist/elysia-fork/error.d.ts.map +0 -1
  56. package/dist/elysia-fork/error.js.map +0 -1
  57. package/dist/elysia-fork/types.d.ts +0 -560
  58. package/dist/elysia-fork/types.d.ts.map +0 -1
  59. package/dist/elysia-fork/types.js +0 -2
  60. package/dist/elysia-fork/types.js.map +0 -1
  61. package/dist/elysia-fork/utils.d.ts +0 -73
  62. package/dist/elysia-fork/utils.d.ts.map +0 -1
  63. package/dist/elysia-fork/utils.js +0 -70
  64. package/dist/elysia-fork/utils.js.map +0 -1
  65. package/src/elysia-fork/context.ts +0 -166
  66. package/src/elysia-fork/types.ts +0 -1281
  67. package/src/elysia-fork/utils.ts +0 -85
  68. /package/dist/{elysia-fork/context.js → context.js} +0 -0
  69. /package/dist/{elysia-fork/error.d.ts → error.d.ts} +0 -0
  70. /package/dist/{elysia-fork/error.js → error.js} +0 -0
  71. /package/src/{elysia-fork/error.ts → error.ts} +0 -0
@@ -1,1281 +0,0 @@
1
- // https://github.com/remorses/elysia/blob/main/src/types.ts#L6
2
- /// <reference types="bun-types" />
3
- import z from 'zod'
4
-
5
- import type { BunFile, Server } from 'bun'
6
- /* eslint-disable @typescript-eslint/no-unused-vars */
7
-
8
- import type {
9
- OptionalKind,
10
- Static,
11
- StaticDecode,
12
- TAnySchema,
13
- TObject,
14
- TSchema,
15
- } from '@sinclair/typebox'
16
- import type { TypeCheck, ValueError } from '@sinclair/typebox/compiler'
17
-
18
- import type { OpenAPIV3 } from 'openapi-types'
19
-
20
- import { Spiceflow } from '../spiceflow.js'
21
- import type { Context, ErrorContext, PreContext } from './context.js'
22
- import {
23
- ELYSIA_RESPONSE,
24
- InternalServerError,
25
- NotFoundError,
26
- ParseError,
27
- ValidationError,
28
- } from './error.js'
29
- import { ZodTypeAny, ZodObject } from 'zod'
30
-
31
- export type MaybeArray<T> = T | T[]
32
- export type MaybePromise<T> = T | Promise<T>
33
-
34
- export type ObjectValues<T extends object> = T[keyof T]
35
-
36
- type IsPathParameter<Part extends string> = Part extends `:${infer Parameter}`
37
- ? Parameter
38
- : Part extends `*`
39
- ? '*'
40
- : never
41
-
42
- export type GetPathParameter<Path extends string> =
43
- Path extends `${infer A}/${infer B}`
44
- ? IsPathParameter<A> | GetPathParameter<B>
45
- : IsPathParameter<Path>
46
-
47
- export type ResolvePath<Path extends string> = Prettify<
48
- {
49
- [Param in GetPathParameter<Path> as Param extends `${string}?`
50
- ? never
51
- : Param]: string
52
- } & {
53
- [Param in GetPathParameter<Path> as Param extends `${infer OptionalParam}?`
54
- ? OptionalParam
55
- : never]?: string
56
- }
57
- >
58
-
59
- // https://twitter.com/mattpocockuk/status/1622730173446557697?s=20
60
- export type Prettify<T> = {
61
- [K in keyof T]: T[K]
62
- } & {}
63
-
64
- export type Prettify2<T> = {
65
- [K in keyof T]: Prettify<T[K]>
66
- } & {}
67
-
68
- export type Partial2<T> = {
69
- [K in keyof T]?: Partial<T[K]>
70
- }
71
-
72
- export type NeverKey<T> = {
73
- [K in keyof T]?: T[K]
74
- } & {}
75
-
76
- type IsBothObject<A, B> = A extends Record<string | number | symbol, unknown>
77
- ? B extends Record<string | number | symbol, unknown>
78
- ? IsClass<A> extends false
79
- ? IsClass<B> extends false
80
- ? true
81
- : false
82
- : false
83
- : false
84
- : false
85
-
86
- type IsClass<V> = V extends abstract new (...args: any) => any ? true : false
87
- type And<A, B> = A extends true ? (B extends true ? true : false) : false
88
-
89
- export type Reconcile<
90
- A extends Object,
91
- B extends Object,
92
- Override extends boolean = false,
93
- // Detect Stack limit, eg. circular dependency
94
- Stack extends number[] = [],
95
- > = Stack['length'] extends 16
96
- ? A
97
- : Override extends true
98
- ? {
99
- [key in keyof A as key extends keyof B ? never : key]: A[key]
100
- } extends infer Collision
101
- ? {} extends Collision
102
- ? {
103
- [key in keyof B]: IsBothObject<
104
- // @ts-ignore trust me bro
105
- A[key],
106
- B[key]
107
- > extends true
108
- ? Reconcile<
109
- // @ts-ignore trust me bro
110
- A[key],
111
- B[key],
112
- Override,
113
- [0, ...Stack]
114
- >
115
- : B[key]
116
- }
117
- : Prettify<
118
- Collision & {
119
- [key in keyof B]: B[key]
120
- }
121
- >
122
- : never
123
- : {
124
- [key in keyof B as key extends keyof A ? never : key]: B[key]
125
- } extends infer Collision
126
- ? {} extends Collision
127
- ? {
128
- [key in keyof A]: IsBothObject<
129
- A[key],
130
- // @ts-ignore trust me bro
131
- B[key]
132
- > extends true
133
- ? Reconcile<
134
- // @ts-ignore trust me bro
135
- A[key],
136
- // @ts-ignore trust me bro
137
- B[key],
138
- Override,
139
- [0, ...Stack]
140
- >
141
- : A[key]
142
- }
143
- : Prettify<
144
- {
145
- [key in keyof A]: A[key]
146
- } & Collision
147
- >
148
- : never
149
-
150
- export interface SingletonBase {
151
- decorator: Record<string, unknown>
152
- store: Record<string, unknown>
153
- derive: Record<string, unknown>
154
- resolve: Record<string, unknown>
155
- }
156
-
157
- export interface EphemeralType {
158
- derive: SingletonBase['derive']
159
- resolve: SingletonBase['resolve']
160
- schema: MetadataBase['schema']
161
- }
162
-
163
- export interface DefinitionBase {
164
- type: Record<string, unknown>
165
- error: Record<string, Error>
166
- }
167
-
168
- export type RouteBase = Record<string, unknown>
169
-
170
- export interface MetadataBase {
171
- schema: RouteSchema
172
- macro: BaseMacro
173
- macroFn: BaseMacroFn
174
- }
175
-
176
- export interface RouteSchema {
177
- body?: unknown
178
- // headers?: unknown
179
- query?: unknown
180
- params?: unknown
181
- // cookie?: unknown
182
- response?: unknown
183
- }
184
-
185
- type OptionalField = {
186
- [OptionalKind]: 'Optional'
187
- }
188
-
189
- export type TypeSchema = TSchema | ZodTypeAny
190
-
191
- export type TypeObject = TObject | ZodObject<any, any, any>
192
-
193
- export type UnwrapSchema<
194
- Schema extends TypeSchema | string | undefined,
195
- Definitions extends Record<string, unknown> = {},
196
- > = undefined extends Schema
197
- ? unknown
198
- : Schema extends ZodTypeAny
199
- ? z.infer<Schema>
200
- : Schema extends TSchema
201
- ? Schema extends OptionalField
202
- ? Prettify<Partial<Static<Schema>>>
203
- : StaticDecode<Schema>
204
- : Schema extends string
205
- ? Definitions extends Record<Schema, infer NamedSchema>
206
- ? NamedSchema
207
- : Definitions
208
- : unknown
209
-
210
- export interface UnwrapRoute<
211
- in out Schema extends InputSchema<any>,
212
- in out Definitions extends DefinitionBase['type'] = {},
213
- > {
214
- body: UnwrapSchema<Schema['body'], Definitions>
215
- // headers: UnwrapSchema<Schema['headers'], Definitions>
216
- query: UnwrapSchema<Schema['query'], Definitions>
217
- params: UnwrapSchema<Schema['params'], Definitions>
218
- // cookie: UnwrapSchema<Schema['cookie'], Definitions>
219
- response: Schema['response'] extends TypeSchema | string
220
- ? {
221
- 200: CoExist<
222
- UnwrapSchema<Schema['response'], Definitions>,
223
- File,
224
- BunFile
225
- >
226
- }
227
- : Schema['response'] extends Record<number, TypeSchema | string>
228
- ? {
229
- [k in keyof Schema['response']]: CoExist<
230
- UnwrapSchema<Schema['response'][k], Definitions>,
231
- File,
232
- BunFile
233
- >
234
- }
235
- : unknown | void
236
- }
237
-
238
- export type HookContainer<T extends Function = Function> = {
239
- checksum?: number
240
- scope?: LifeCycleType
241
- subType?: 'derive' | 'resolve' | 'mapDerive' | 'mapResolve' | (string & {})
242
- fn: T
243
- }
244
-
245
- export interface LifeCycleStore {
246
- type?: ContentType
247
- start: HookContainer<GracefulHandler<any>>[]
248
- request: HookContainer<PreHandler<any, any>>[]
249
- parse: HookContainer<BodyHandler<any, any>>[]
250
- transform: HookContainer<TransformHandler<any, any>>[]
251
- beforeHandle: HookContainer<OptionalHandler<any, any>>[]
252
- afterHandle: HookContainer<AfterHandler<any, any>>[]
253
- mapResponse: HookContainer<MapResponse<any, any>>[]
254
- afterResponse: HookContainer<AfterResponseHandler<any, any>>[]
255
- // trace: HookContainer<TraceHandler<any, any>>[]
256
- error: HookContainer<ErrorHandler<any, any, any>>[]
257
- stop: HookContainer<GracefulHandler<any>>[]
258
- }
259
-
260
- export type LifeCycleEvent =
261
- | 'start'
262
- | 'request'
263
- | 'parse'
264
- | 'transform'
265
- | 'beforeHandle'
266
- | 'afterHandle'
267
- | 'response'
268
- | 'error'
269
- | 'stop'
270
-
271
- export type ContentType = MaybeArray<
272
- | (string & {})
273
- | 'none'
274
- | 'text'
275
- | 'json'
276
- | 'formdata'
277
- | 'urlencoded'
278
- | 'arrayBuffer'
279
- | 'text/plain'
280
- | 'application/json'
281
- | 'multipart/form-data'
282
- | 'application/x-www-form-urlencoded'
283
- >
284
-
285
- export type HTTPMethod =
286
- | (string & {})
287
- | 'ACL'
288
- | 'BIND'
289
- | 'CHECKOUT'
290
- | 'CONNECT'
291
- | 'COPY'
292
- | 'DELETE'
293
- | 'GET'
294
- | 'HEAD'
295
- | 'LINK'
296
- | 'LOCK'
297
- | 'M-SEARCH'
298
- | 'MERGE'
299
- | 'MKACTIVITY'
300
- | 'MKCALENDAR'
301
- | 'MKCOL'
302
- | 'MOVE'
303
- | 'NOTIFY'
304
- | 'OPTIONS'
305
- | 'PATCH'
306
- | 'POST'
307
- | 'PROPFIND'
308
- | 'PROPPATCH'
309
- | 'PURGE'
310
- | 'PUT'
311
- | 'REBIND'
312
- | 'REPORT'
313
- | 'SEARCH'
314
- | 'SOURCE'
315
- | 'SUBSCRIBE'
316
- | 'TRACE'
317
- | 'UNBIND'
318
- | 'UNLINK'
319
- | 'UNLOCK'
320
- | 'UNSUBSCRIBE'
321
- | 'ALL'
322
-
323
- export interface InputSchema<Name extends string = string> {
324
- body?: TypeSchema | Name
325
- // headers?: TObject | TNull | TUndefined | Name
326
- query?: TypeObject | Name
327
- params?: TypeObject | Name
328
- // cookie?: TObject | TNull | TUndefined | Name
329
- response?:
330
- | TypeSchema
331
- | Record<number, TypeSchema>
332
- | Name
333
- | Record<number, Name | TypeSchema>
334
- }
335
-
336
- export interface MergeSchema<
337
- in out A extends RouteSchema,
338
- in out B extends RouteSchema,
339
- > {
340
- body: undefined extends A['body'] ? B['body'] : A['body']
341
- // headers: undefined extends A['headers'] ? B['headers'] : A['headers']
342
- query: undefined extends A['query'] ? B['query'] : A['query']
343
- params: undefined extends A['params'] ? B['params'] : A['params']
344
- // cookie: undefined extends A['cookie'] ? B['cookie'] : A['cookie']
345
- response: {} extends A['response']
346
- ? {} extends B['response']
347
- ? {}
348
- : B['response']
349
- : {} extends B['response']
350
- ? A['response']
351
- : A['response'] & Omit<B['response'], keyof A['response']>
352
- }
353
-
354
- export type Handler<
355
- in out Route extends RouteSchema = {},
356
- in out Singleton extends SingletonBase = {
357
- decorator: {}
358
- store: {}
359
- derive: {}
360
- resolve: {}
361
- },
362
- Path extends string = '',
363
- > = (
364
- context: Context<Route, Singleton, Path>,
365
- ) => MaybePromise<
366
- {} extends Route['response']
367
- ? unknown
368
- : Route['response'][keyof Route['response']]
369
- >
370
-
371
- export type Replace<Original, Target, With> = IsAny<Target> extends true
372
- ? Original
373
- : Original extends Record<string, unknown>
374
- ? {
375
- [K in keyof Original]: Original[K] extends Target
376
- ? With
377
- : Original[K]
378
- }
379
- : Original extends Target
380
- ? With
381
- : Original
382
-
383
- export type IsAny<T> = 0 extends 1 & T ? true : false
384
-
385
- export type CoExist<Original, Target, With> = IsAny<Target> extends true
386
- ? Original
387
- : Original extends Record<string, unknown>
388
- ? {
389
- [K in keyof Original]: Original[K] extends Target
390
- ? Original[K] | With
391
- : Original[K]
392
- }
393
- : Original extends Target
394
- ? Original | With
395
- : Original
396
-
397
- export type InlineHandler<
398
- Route extends RouteSchema = {},
399
- Singleton extends SingletonBase = {
400
- decorator: {}
401
- store: {}
402
- derive: {}
403
- resolve: {}
404
- },
405
- Path extends string = '',
406
- MacroContext = {},
407
- > = (
408
- context: MacroContext extends Record<string | number | symbol, unknown>
409
- ? Prettify<MacroContext & Context<Route, Singleton, Path>>
410
- : Context<Route, Singleton, Path>,
411
- ) =>
412
- | Response
413
- | MaybePromise<
414
- {} extends Route['response']
415
- ? unknown
416
- :
417
- | (Route['response'] extends { 200: any }
418
- ? Route['response']
419
- : string | number | boolean | Object)
420
- | Route['response'][keyof Route['response']]
421
- | {
422
- [Status in keyof Route['response']]: {
423
- _type: Record<
424
- Status,
425
- Route['response'][Status]
426
- >
427
- [ELYSIA_RESPONSE]: Status
428
- }
429
- }[keyof Route['response']]
430
- >
431
-
432
- export type OptionalHandler<
433
- in out Route extends RouteSchema = {},
434
- in out Singleton extends SingletonBase = {
435
- decorator: {}
436
- store: {}
437
- derive: {}
438
- resolve: {}
439
- },
440
- Path extends string = '',
441
- > = Handler<Route, Singleton, Path> extends (
442
- context: infer Context,
443
- ) => infer Returned
444
- ? (context: Context) => Returned | MaybePromise<void>
445
- : never
446
-
447
- export type AfterHandler<
448
- in out Route extends RouteSchema = {},
449
- in out Singleton extends SingletonBase = {
450
- decorator: {}
451
- store: {}
452
- derive: {}
453
- resolve: {}
454
- },
455
- Path extends string = '',
456
- > = Handler<Route, Singleton, Path> extends (
457
- context: infer Context,
458
- ) => infer Returned
459
- ? (
460
- context: Prettify<
461
- {
462
- response: Route['response']
463
- } & Context
464
- >,
465
- ) => Returned | MaybePromise<void>
466
- : never
467
-
468
- export type MapResponse<
469
- in out Route extends RouteSchema = {},
470
- in out Singleton extends SingletonBase = {
471
- decorator: {}
472
- store: {}
473
- derive: {}
474
- resolve: {}
475
- },
476
- Path extends string = '',
477
- > = Handler<
478
- Omit<Route, 'response'> & {
479
- response: MaybePromise<Response | undefined | unknown>
480
- },
481
- Singleton & {
482
- derive: {
483
- response: Route['response']
484
- }
485
- },
486
- Path
487
- >
488
-
489
- export type VoidHandler<
490
- in out Route extends RouteSchema = {},
491
- in out Singleton extends SingletonBase = {
492
- decorator: {}
493
- store: {}
494
- derive: {}
495
- resolve: {}
496
- },
497
- > = (context: Context<Route, Singleton>) => MaybePromise<void>
498
-
499
- export type TransformHandler<
500
- in out Route extends RouteSchema = {},
501
- in out Singleton extends SingletonBase = {
502
- decorator: {}
503
- store: {}
504
- derive: {}
505
- resolve: {}
506
- },
507
- BasePath extends string = '',
508
- > = {
509
- (
510
- context: Prettify<
511
- Context<
512
- Route,
513
- Omit<Singleton, 'resolve'> & {
514
- resolve: {}
515
- },
516
- BasePath
517
- >
518
- >,
519
- ): MaybePromise<void>
520
- }
521
-
522
- export type BodyHandler<
523
- in out Route extends RouteSchema = {},
524
- in out Singleton extends SingletonBase = {
525
- decorator: {}
526
- store: {}
527
- derive: {}
528
- resolve: {}
529
- },
530
- Path extends string = '',
531
- > = (
532
- context: Prettify<
533
- {
534
- contentType: string
535
- } & Context<Route, Singleton, Path>
536
- >,
537
- /**
538
- * @deprecated
539
- *
540
- * use `context.contentType` instead
541
- *
542
- * @example
543
- * ```ts
544
- * new Spiceflow()
545
- * .onParse(({ contentType, request }) => {
546
- * if (contentType === 'application/json')
547
- * return request.json()
548
- * })
549
- * ```
550
- */
551
- contentType: string,
552
- ) => MaybePromise<any>
553
-
554
- export type PreHandler<
555
- in out Route extends RouteSchema = {},
556
- in out Singleton extends SingletonBase = {
557
- decorator: {}
558
- store: {}
559
- derive: {}
560
- resolve: {}
561
- },
562
- > = (context: PreContext<Singleton>) => MaybePromise<Route['response'] | void>
563
-
564
- export type AfterResponseHandler<
565
- in out Route extends RouteSchema = {},
566
- in out Singleton extends SingletonBase = {
567
- decorator: {}
568
- store: {}
569
- derive: {}
570
- resolve: {}
571
- },
572
- > = (
573
- context: Prettify<
574
- Context<Route, Singleton> & {
575
- response: Route['response']
576
- }
577
- >,
578
- ) => MaybePromise<void>
579
-
580
- export type GracefulHandler<
581
- in Instance extends Spiceflow<any, any, any, any, any, any, any, any>,
582
- > = (data: Instance) => any
583
-
584
- export type ErrorHandler<
585
- in out T extends Record<string, Error> = {},
586
- in out Route extends RouteSchema = {},
587
- in out Singleton extends SingletonBase = {
588
- decorator: {}
589
- store: {}
590
- derive: {}
591
- resolve: {}
592
- },
593
- // ? scoped
594
- in out Ephemeral extends EphemeralType = {
595
- derive: {}
596
- resolve: {}
597
- schema: {}
598
- },
599
- // ? local
600
- in out Volatile extends EphemeralType = {
601
- derive: {}
602
- resolve: {}
603
- schema: {}
604
- },
605
- > = (
606
- context: ErrorContext<
607
- Route,
608
- {
609
- store: Singleton['store']
610
- decorator: Singleton['decorator']
611
- derive: {}
612
- resolve: {}
613
- }
614
- > &
615
- (
616
- | Prettify<
617
- {
618
- request: Request
619
- code: 'UNKNOWN'
620
- error: Readonly<Error>
621
- } & Partial<
622
- Singleton['derive'] &
623
- Ephemeral['derive'] &
624
- Volatile['derive']
625
- > &
626
- Partial<
627
- Singleton['derive'] &
628
- Ephemeral['resolve'] &
629
- Volatile['resolve']
630
- >
631
- >
632
- | Prettify<
633
- {
634
- request: Request
635
- code: 'VALIDATION'
636
- error: Readonly<ValidationError>
637
- } & Singleton['derive'] &
638
- Ephemeral['derive'] &
639
- Volatile['derive'] &
640
- NeverKey<
641
- Singleton['derive'] &
642
- Ephemeral['resolve'] &
643
- Volatile['resolve']
644
- >
645
- >
646
- | Prettify<
647
- {
648
- request: Request
649
- code: 'NOT_FOUND'
650
- error: Readonly<NotFoundError>
651
- } & NeverKey<
652
- Singleton['derive'] &
653
- Ephemeral['derive'] &
654
- Volatile['derive']
655
- > &
656
- NeverKey<
657
- Singleton['derive'] &
658
- Ephemeral['resolve'] &
659
- Volatile['resolve']
660
- >
661
- >
662
- | Prettify<
663
- {
664
- request: Request
665
- code: 'PARSE'
666
- error: Readonly<ParseError>
667
- } & Singleton['derive'] &
668
- Ephemeral['derive'] &
669
- Volatile['derive'] &
670
- NeverKey<
671
- Singleton['derive'] &
672
- Ephemeral['resolve'] &
673
- Volatile['resolve']
674
- >
675
- >
676
- | Prettify<
677
- {
678
- request: Request
679
- code: 'INTERNAL_SERVER_ERROR'
680
- error: Readonly<InternalServerError>
681
- } & Partial<
682
- Singleton['derive'] &
683
- Ephemeral['derive'] &
684
- Volatile['derive']
685
- > &
686
- Partial<
687
- Singleton['derive'] &
688
- Ephemeral['resolve'] &
689
- Volatile['resolve']
690
- >
691
- >
692
- // | Prettify<
693
- // {
694
- // request: Request
695
- // code: 'INVALID_COOKIE_SIGNATURE'
696
- // error: Readonly<InvalidCookieSignature>
697
- // } & NeverKey<
698
- // Singleton['derive'] &
699
- // Ephemeral['derive'] &
700
- // Volatile['derive']
701
- // > &
702
- // NeverKey<
703
- // Singleton['derive'] &
704
- // Ephemeral['resolve'] &
705
- // Volatile['resolve']
706
- // >
707
- // >
708
- | Prettify<
709
- {
710
- [K in keyof T]: {
711
- request: Request
712
- code: K
713
- error: Readonly<T[K]>
714
- }
715
- }[keyof T] &
716
- Partial<
717
- Singleton['derive'] &
718
- Ephemeral['derive'] &
719
- Volatile['derive']
720
- > &
721
- Partial<
722
- Singleton['derive'] &
723
- Ephemeral['resolve'] &
724
- Volatile['resolve']
725
- >
726
- >
727
- ),
728
- ) => any | Promise<any>
729
-
730
- export type Isolate<T> = {
731
- [P in keyof T]: T[P]
732
- }
733
-
734
- export type DocumentDecoration = Partial<OpenAPIV3.OperationObject> & {
735
- /**
736
- * Pass `true` to hide route from OpenAPI/swagger document
737
- * */
738
- hide?: boolean
739
- }
740
-
741
- export type LocalHook<
742
- LocalSchema extends InputSchema,
743
- Schema extends RouteSchema,
744
- Singleton extends SingletonBase,
745
- Errors extends Record<string, Error>,
746
- Extension extends BaseMacro,
747
- Path extends string = '',
748
- TypedRoute extends RouteSchema = Schema extends {
749
- params: Record<string, unknown>
750
- }
751
- ? Schema
752
- : Schema & {
753
- params: undefined extends Schema['params']
754
- ? ResolvePath<Path>
755
- : Schema['params']
756
- },
757
- > = (LocalSchema extends {} ? LocalSchema : Isolate<LocalSchema>) &
758
- Extension & {
759
- /**
760
- * Short for 'Content-Type'
761
- *
762
- * Available:
763
- * - 'none': do not parse body
764
- * - 'text' / 'text/plain': parse body as string
765
- * - 'json' / 'application/json': parse body as json
766
- * - 'formdata' / 'multipart/form-data': parse body as form-data
767
- * - 'urlencoded' / 'application/x-www-form-urlencoded: parse body as urlencoded
768
- * - 'arraybuffer': parse body as readable stream
769
- */
770
- type?: ContentType
771
- detail?: DocumentDecoration
772
- // /**
773
- // * Custom body parser
774
- // */
775
- // parse?: MaybeArray<BodyHandler<TypedRoute, Singleton>>
776
- // /**
777
- // * Transform context's value
778
- // */
779
- // transform?: MaybeArray<TransformHandler<TypedRoute, Singleton>>
780
- // /**
781
- // * Execute before main handler
782
- // */
783
- // beforeHandle?: MaybeArray<OptionalHandler<TypedRoute, Singleton>>
784
- // /**
785
- // * Execute after main handler
786
- // */
787
- // afterHandle?: MaybeArray<AfterHandler<TypedRoute, Singleton>>
788
- // /**
789
- // * Execute after main handler
790
- // */
791
- // mapResponse?: MaybeArray<MapResponse<TypedRoute, Singleton>>
792
- // /**
793
- // * Execute after response is sent
794
- // */
795
- // afterResponse?: MaybeArray<VoidHandler<TypedRoute, Singleton>>
796
- // /**
797
- // * Catch error
798
- // */
799
- // error?: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>
800
- // tags?: DocumentDecoration['tags']
801
- }
802
-
803
- export type ComposedHandler = (context: Context) => MaybePromise<Response>
804
-
805
- export interface InternalRoute {
806
- method: HTTPMethod
807
- path: string
808
- composed: ComposedHandler | Response | null
809
- handler: Handler
810
- hooks: LocalHook<any, any, any, any, any, any, any>
811
- }
812
-
813
- export type ListenCallback = (server: Server) => MaybePromise<void>
814
-
815
- export type AddPrefix<Prefix extends string, T> = {
816
- [K in keyof T as Prefix extends string ? `${Prefix}${K & string}` : K]: T[K]
817
- }
818
-
819
- export type AddPrefixCapitalize<Prefix extends string, T> = {
820
- [K in keyof T as `${Prefix}${Capitalize<K & string>}`]: T[K]
821
- }
822
-
823
- export type AddSuffix<Suffix extends string, T> = {
824
- [K in keyof T as `${K & string}${Suffix}`]: T[K]
825
- }
826
-
827
- export type AddSuffixCapitalize<Suffix extends string, T> = {
828
- [K in keyof T as `${K & string}${Capitalize<Suffix>}`]: T[K]
829
- }
830
-
831
- export type Checksum = {
832
- name?: string
833
- seed?: unknown
834
- checksum: number
835
- stack?: string
836
- routes?: InternalRoute[]
837
- decorators?: SingletonBase['decorator']
838
- store?: SingletonBase['store']
839
- type?: DefinitionBase['type']
840
- error?: DefinitionBase['error']
841
- dependencies?: Record<string, Checksum[]>
842
- derive?: {
843
- fn: string
844
- stack: string
845
- }[]
846
- resolve?: {
847
- fn: string
848
- stack: string
849
- }[]
850
- }
851
-
852
- export type BaseMacro = Record<
853
- string,
854
- string | number | boolean | Object | undefined | null
855
- >
856
- export type BaseMacroFn = Record<string, (...a: any) => unknown>
857
-
858
- export type MacroToProperty<in out T extends BaseMacroFn> = Prettify<{
859
- [K in keyof T]: T[K] extends Function
860
- ? T[K] extends (a: infer Params) => any
861
- ? Params | undefined
862
- : T[K]
863
- : T[K]
864
- }>
865
-
866
- interface MacroOptions {
867
- insert?: 'before' | 'after'
868
- stack?: 'global' | 'local'
869
- }
870
-
871
- export interface MacroManager<
872
- in out TypedRoute extends RouteSchema = {},
873
- in out Singleton extends SingletonBase = {
874
- decorator: {}
875
- store: {}
876
- derive: {}
877
- resolve: {}
878
- },
879
- in out Errors extends Record<string, Error> = {},
880
- > {
881
- onParse(fn: MaybeArray<BodyHandler<TypedRoute, Singleton>>): unknown
882
- onParse(
883
- options: MacroOptions,
884
- fn: MaybeArray<BodyHandler<TypedRoute, Singleton>>,
885
- ): unknown
886
-
887
- onTransform(fn: MaybeArray<VoidHandler<TypedRoute, Singleton>>): unknown
888
- onTransform(
889
- options: MacroOptions,
890
- fn: MaybeArray<VoidHandler<TypedRoute, Singleton>>,
891
- ): unknown
892
-
893
- onBeforeHandle(
894
- fn: MaybeArray<OptionalHandler<TypedRoute, Singleton>>,
895
- ): unknown
896
- onBeforeHandle(
897
- options: MacroOptions,
898
- fn: MaybeArray<OptionalHandler<TypedRoute, Singleton>>,
899
- ): unknown
900
-
901
- onAfterHandle(fn: MaybeArray<AfterHandler<TypedRoute, Singleton>>): unknown
902
- onAfterHandle(
903
- options: MacroOptions,
904
- fn: MaybeArray<AfterHandler<TypedRoute, Singleton>>,
905
- ): unknown
906
-
907
- onError(
908
- fn: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>,
909
- ): unknown
910
- onError(
911
- options: MacroOptions,
912
- fn: MaybeArray<ErrorHandler<Errors, TypedRoute, Singleton>>,
913
- ): unknown
914
-
915
- mapResponse(fn: MaybeArray<MapResponse<TypedRoute, Singleton>>): unknown
916
- mapResponse(
917
- options: MacroOptions,
918
- fn: MaybeArray<MapResponse<TypedRoute, Singleton>>,
919
- ): unknown
920
-
921
- onAfterResponse(
922
- fn: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>,
923
- ): unknown
924
- onAfterResponse(
925
- options: MacroOptions,
926
- fn: MaybeArray<AfterResponseHandler<TypedRoute, Singleton>>,
927
- ): unknown
928
-
929
- events: {
930
- global: Prettify<LifeCycleStore & RouteSchema>
931
- local: Prettify<LifeCycleStore & RouteSchema>
932
- }
933
- }
934
-
935
- export type MacroQueue = HookContainer<
936
- (manager: MacroManager<any, any, any>) => unknown
937
- >
938
-
939
- type _CreateEden<
940
- Path extends string,
941
- Property extends Record<string, unknown> = {},
942
- > = Path extends `${infer Start}/${infer Rest}`
943
- ? {
944
- [x in Start]: _CreateEden<Rest, Property>
945
- }
946
- : {
947
- [x in Path]: Property
948
- }
949
-
950
- export type CreateEden<
951
- Path extends string,
952
- Property extends Record<string, unknown> = {},
953
- > = Path extends `/${infer Rest}`
954
- ? _CreateEden<Rest, Property>
955
- : Path extends ''
956
- ? _CreateEden<'index', Property>
957
- : _CreateEden<Path, Property>
958
-
959
- export type ComposeSpiceflowResponse<Response, Handle> = Handle extends (
960
- ...a: any[]
961
- ) => infer A
962
- ? _ComposeSpiceflowResponse<Response, Replace<Awaited<A>, BunFile, File>>
963
- : _ComposeSpiceflowResponse<
964
- Response,
965
- Replace<Awaited<Handle>, BunFile, File>
966
- >
967
-
968
- type _ComposeSpiceflowResponse<Response, Handle> = Prettify<
969
- {} extends Response
970
- ? {
971
- 200: Exclude<Handle, { [ELYSIA_RESPONSE]: any }>
972
- } & {
973
- [ErrorResponse in Extract<
974
- Handle,
975
- { response: any }
976
- > as ErrorResponse extends {
977
- [ELYSIA_RESPONSE]: infer Status extends number
978
- }
979
- ? Status
980
- : never]: ErrorResponse['response']
981
- }
982
- : Response
983
- >
984
-
985
- export type MergeSpiceflowInstances<
986
- Instances extends Spiceflow<any, any, any, any, any, any>[] = [],
987
- Prefix extends string = '',
988
- Scoped extends boolean = false,
989
- Singleton extends SingletonBase = {
990
- decorator: {}
991
- store: {}
992
- derive: {}
993
- resolve: {}
994
- },
995
- Definitions extends DefinitionBase = {
996
- type: {}
997
- error: {}
998
- },
999
- Metadata extends MetadataBase = {
1000
- schema: {}
1001
- macro: {}
1002
- macroFn: {}
1003
- },
1004
- Routes extends RouteBase = {},
1005
- > = Instances extends [
1006
- infer Current extends Spiceflow<any, any, any, any, any, any>,
1007
- ...infer Rest extends Spiceflow<any, any, any, any, any, any>[],
1008
- ]
1009
- ? Current['_types']['Scoped'] extends true
1010
- ? MergeSpiceflowInstances<
1011
- Rest,
1012
- Prefix,
1013
- Scoped,
1014
- Singleton,
1015
- Definitions,
1016
- Metadata,
1017
- Routes
1018
- >
1019
- : MergeSpiceflowInstances<
1020
- Rest,
1021
- Prefix,
1022
- Scoped,
1023
- Singleton & Current['_types']['Singleton'],
1024
- Definitions & Current['_types']['Definitions'],
1025
- Metadata & Current['_types']['Metadata'],
1026
- Routes &
1027
- (Prefix extends ``
1028
- ? Current['_routes']
1029
- : AddPrefix<Prefix, Current['_routes']>)
1030
- >
1031
- : Spiceflow<
1032
- Prefix,
1033
- Scoped,
1034
- {
1035
- decorator: Prettify<Singleton['decorator']>
1036
- store: Prettify<Singleton['store']>
1037
- derive: Prettify<Singleton['derive']>
1038
- resolve: Prettify<Singleton['resolve']>
1039
- },
1040
- {
1041
- type: Prettify<Definitions['type']>
1042
- error: Prettify<Definitions['error']>
1043
- },
1044
- {
1045
- schema: Prettify<Metadata['schema']>
1046
- macro: Prettify<Metadata['macro']>
1047
- macroFn: Prettify<Metadata['macroFn']>
1048
- },
1049
- Routes
1050
- >
1051
-
1052
- export type LifeCycleType = 'global' | 'local' | 'scoped'
1053
-
1054
- // Exclude return error()
1055
- export type ExcludeSpiceflowResponse<T> = Exclude<
1056
- undefined extends Awaited<T> ? Partial<Awaited<T>> : Awaited<T>,
1057
- { [ELYSIA_RESPONSE]: any }
1058
- >
1059
-
1060
- export type InferContext<
1061
- T extends Spiceflow<any, any, any, any, any, any, any, any>,
1062
- Path extends string = T['_types']['Prefix'],
1063
- Schema extends RouteSchema = T['_types']['Metadata']['schema'],
1064
- > = Context<
1065
- MergeSchema<Schema, T['_types']['Metadata']['schema']>,
1066
- T['_types']['Singleton'] & {
1067
- derive: T['_ephemeral']['derive'] & T['_volatile']['derive']
1068
- resolve: T['_ephemeral']['resolve'] & T['_volatile']['resolve']
1069
- },
1070
- Path
1071
- >
1072
-
1073
- export type InferHandler<
1074
- T extends Spiceflow<any, any, any, any, any, any, any, any>,
1075
- Path extends string = T['_types']['Prefix'],
1076
- Schema extends RouteSchema = T['_types']['Metadata']['schema'],
1077
- > = InlineHandler<
1078
- MergeSchema<Schema, T['_types']['Metadata']['schema']>,
1079
- T['_types']['Singleton'] & {
1080
- derive: T['_ephemeral']['derive'] & T['_volatile']['derive']
1081
- resolve: T['_ephemeral']['resolve'] & T['_volatile']['resolve']
1082
- },
1083
- Path
1084
- >
1085
-
1086
- export interface ModelValidatorError extends ValueError {
1087
- summary: string
1088
- }
1089
-
1090
- // @ts-ignore trust me bro
1091
- export interface ModelValidator<T> extends TypeCheck<T> {
1092
- parse(a: T): T
1093
- safeParse(a: T):
1094
- | { success: true; data: T; error: null }
1095
- | {
1096
- success: true
1097
- data: null
1098
- error: string
1099
- errors: ModelValidatorError[]
1100
- }
1101
- }
1102
-
1103
- export type UnionToIntersect<U> = (
1104
- U extends unknown ? (arg: U) => 0 : never
1105
- ) extends (arg: infer I) => 0
1106
- ? I
1107
- : never
1108
-
1109
- export type ResolveMacroContext<
1110
- Macro extends BaseMacro,
1111
- MacroFn extends BaseMacroFn,
1112
- > = UnionToIntersect<
1113
- {
1114
- [K in keyof Macro]-?: undefined extends Macro[K]
1115
- ? never
1116
- : K extends keyof MacroFn
1117
- ? ReturnType<MacroFn[K]> extends infer A extends Record<
1118
- string | number | symbol,
1119
- unknown
1120
- >
1121
- ? A
1122
- : never
1123
- : never
1124
- }[keyof Macro]
1125
- >
1126
-
1127
- export type ContextAppendType = 'append' | 'override'
1128
-
1129
- export type HigherOrderFunction<
1130
- T extends (...arg: unknown[]) => Function = (...arg: unknown[]) => Function,
1131
- > = (fn: T, request: Request) => ReturnType<T>
1132
-
1133
- // new Spiceflow()
1134
- // .wrap((fn) => {
1135
- // return fn()
1136
- // })
1137
-
1138
- export type HTTPHeaders = Record<string, string> & {
1139
- // Authentication
1140
- 'www-authenticate'?: string
1141
- authorization?: string
1142
- 'proxy-authenticate'?: string
1143
- 'proxy-authorization'?: string
1144
-
1145
- // Caching
1146
- age?: string
1147
- 'cache-control'?: string
1148
- 'clear-site-data'?: string
1149
- expires?: string
1150
- 'no-vary-search'?: string
1151
- pragma?: string
1152
-
1153
- // Conditionals
1154
- 'last-modified'?: string
1155
- etag?: string
1156
- 'if-match'?: string
1157
- 'if-none-match'?: string
1158
- 'if-modified-since'?: string
1159
- 'if-unmodified-since'?: string
1160
- vary?: string
1161
-
1162
- // Connection management
1163
- connection?: string
1164
- 'keep-alive'?: string
1165
-
1166
- // Content negotiation
1167
- accept?: string
1168
- 'accept-encoding'?: string
1169
- 'accept-language'?: string
1170
-
1171
- // Controls
1172
- expect?: string
1173
- 'max-forwards'?: string
1174
-
1175
- // Cokies
1176
- cookie?: string
1177
- 'set-cookie'?: string | string[]
1178
-
1179
- // CORS
1180
- 'access-control-allow-origin'?: string
1181
- 'access-control-allow-credentials'?: string
1182
- 'access-control-allow-headers'?: string
1183
- 'access-control-allow-methods'?: string
1184
- 'access-control-expose-headers'?: string
1185
- 'access-control-max-age'?: string
1186
- 'access-control-request-headers'?: string
1187
- 'access-control-request-method'?: string
1188
- origin?: string
1189
- 'timing-allow-origin'?: string
1190
-
1191
- // Downloads
1192
- 'content-disposition'?: string
1193
-
1194
- // Message body information
1195
- 'content-length'?: string
1196
- 'content-type'?: string
1197
- 'content-encoding'?: string
1198
- 'content-language'?: string
1199
- 'content-location'?: string
1200
-
1201
- // Proxies
1202
- forwarded?: string
1203
- via?: string
1204
-
1205
- // Redirects
1206
- location?: string
1207
- refresh?: string
1208
-
1209
- // Request context
1210
- // from?: string
1211
- // host?: string
1212
- // referer?: string
1213
- // 'user-agent'?: string
1214
-
1215
- // Response context
1216
- allow?: string
1217
- server?: 'spiceflow' | (string & {})
1218
-
1219
- // Range requests
1220
- 'accept-ranges'?: string
1221
- range?: string
1222
- 'if-range'?: string
1223
- 'content-range'?: string
1224
-
1225
- // Security
1226
- 'content-security-policy'?: string
1227
- 'content-security-policy-report-only'?: string
1228
- 'cross-origin-embedder-policy'?: string
1229
- 'cross-origin-opener-policy'?: string
1230
- 'cross-origin-resource-policy'?: string
1231
- 'expect-ct'?: string
1232
- 'permission-policy'?: string
1233
- 'strict-transport-security'?: string
1234
- 'upgrade-insecure-requests'?: string
1235
- 'x-content-type-options'?: string
1236
- 'x-frame-options'?: string
1237
- 'x-xss-protection'?: string
1238
-
1239
- // Server-sent events
1240
- 'last-event-id'?: string
1241
- 'ping-from'?: string
1242
- 'ping-to'?: string
1243
- 'report-to'?: string
1244
-
1245
- // Transfer coding
1246
- te?: string
1247
- trailer?: string
1248
- 'transfer-encoding'?: string
1249
-
1250
- // Other
1251
- 'alt-svg'?: string
1252
- 'alt-used'?: string
1253
- date?: string
1254
- dnt?: string
1255
- 'early-data'?: string
1256
- 'large-allocation'?: string
1257
- link?: string
1258
- 'retry-after'?: string
1259
- 'service-worker-allowed'?: string
1260
- 'source-map'?: string
1261
- upgrade?: string
1262
-
1263
- // Non-standard
1264
- 'x-dns-prefetch-control'?: string
1265
- 'x-forwarded-for'?: string
1266
- 'x-forwarded-host'?: string
1267
- 'x-forwarded-proto'?: string
1268
- 'x-powered-by'?: 'spiceflow' | (string & {})
1269
- 'x-request-id'?: string
1270
- 'x-requested-with'?: string
1271
- 'x-robots-tag'?: string
1272
- 'x-ua-compatible'?: string
1273
- }
1274
-
1275
- export type JoinPath<A extends string, B extends string> = `${A}${B extends '/'
1276
- ? '/index'
1277
- : B extends ''
1278
- ? B
1279
- : B extends `/${string}`
1280
- ? B
1281
- : B}`