-
-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathHttpApiSchema.ts
More file actions
684 lines (626 loc) · 17.5 KB
/
Copy pathHttpApiSchema.ts
File metadata and controls
684 lines (626 loc) · 17.5 KB
1
2
3
4
5
6
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/**
* @since 1.0.0
*/
import type { Brand } from "effect/Brand"
import * as Effect from "effect/Effect"
import * as Effectable from "effect/Effectable"
import type { LazyArg } from "effect/Function"
import { constant, constVoid, dual } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
import * as Option from "effect/Option"
import { hasProperty } from "effect/Predicate"
import * as Schema from "effect/Schema"
import * as AST from "effect/SchemaAST"
import * as Struct from "effect/Struct"
import type * as Unify from "effect/Unify"
import type * as FileSystem from "./FileSystem.js"
import type * as Multipart_ from "./Multipart.js"
/**
* @since 1.0.0
* @category annotations
*/
export const AnnotationMultipart: unique symbol = Symbol.for(
"@effect/platform/HttpApiSchema/AnnotationMultipart"
)
/**
* @since 1.0.0
* @category annotations
*/
export const AnnotationMultipartStream: unique symbol = Symbol.for(
"@effect/platform/HttpApiSchema/AnnotationMultipartStream"
)
/**
* @since 1.0.0
* @category annotations
*/
export const AnnotationStatus: unique symbol = Symbol.for("@effect/platform/HttpApiSchema/AnnotationStatus")
/**
* @since 1.0.0
* @category annotations
*/
export const AnnotationEmptyDecodeable: unique symbol = Symbol.for(
"@effect/platform/HttpApiSchema/AnnotationEmptyDecodeable"
)
/**
* @since 1.0.0
* @category annotations
*/
export const AnnotationEncoding: unique symbol = Symbol.for("@effect/platform/HttpApiSchema/AnnotationEncoding")
/**
* @since 1.0.0
* @category annotations
*/
export const AnnotationParam: unique symbol = Symbol.for(
"@effect/platform/HttpApiSchema/AnnotationParam"
)
/**
* @since 1.0.0
* @category annotations
*/
export const extractAnnotations = (ast: AST.Annotations): AST.Annotations => {
const result: Record<symbol, unknown> = {}
if (AnnotationStatus in ast) {
result[AnnotationStatus] = ast[AnnotationStatus]
}
if (AnnotationEmptyDecodeable in ast) {
result[AnnotationEmptyDecodeable] = ast[AnnotationEmptyDecodeable]
}
if (AnnotationEncoding in ast) {
result[AnnotationEncoding] = ast[AnnotationEncoding]
}
if (AnnotationParam in ast) {
result[AnnotationParam] = ast[AnnotationParam]
}
if (AnnotationMultipart in ast) {
result[AnnotationMultipart] = ast[AnnotationMultipart]
}
if (AnnotationMultipartStream in ast) {
result[AnnotationMultipartStream] = ast[AnnotationMultipartStream]
}
return result
}
const mergedAnnotations = (ast: AST.AST): Record<symbol, unknown> =>
ast._tag === "Transformation" ?
{
...ast.to.annotations,
...ast.annotations
} :
ast.annotations
const getAnnotation = <A>(ast: AST.AST, key: symbol): A | undefined => mergedAnnotations(ast)[key] as A
/**
* @since 1.0.0
* @category annotations
*/
export const getStatus = (ast: AST.AST, defaultStatus: number): number =>
getAnnotation<number>(ast, AnnotationStatus) ?? defaultStatus
/**
* @since 1.0.0
* @category annotations
*/
export const getEmptyDecodeable = (ast: AST.AST): boolean =>
getAnnotation<boolean>(ast, AnnotationEmptyDecodeable) ?? false
/**
* @since 1.0.0
* @category annotations
*/
export const getMultipart = (ast: AST.AST): Multipart_.withLimits.Options | undefined =>
getAnnotation<Multipart_.withLimits.Options>(ast, AnnotationMultipart)
/**
* @since 1.0.0
* @category annotations
*/
export const getMultipartStream = (ast: AST.AST): Multipart_.withLimits.Options | undefined =>
getAnnotation<Multipart_.withLimits.Options>(ast, AnnotationMultipartStream)
const encodingJson: Encoding = {
kind: "Json",
contentType: "application/json"
}
/**
* @since 1.0.0
* @category annotations
*/
export const getEncoding = (ast: AST.AST, fallback = encodingJson): Encoding =>
getAnnotation<Encoding>(ast, AnnotationEncoding) ?? fallback
/**
* @since 1.0.0
* @category annotations
*/
export const getParam = (ast: AST.AST | Schema.PropertySignature.AST): string | undefined => {
const annotations = ast._tag === "PropertySignatureTransformation" ? ast.to.annotations : ast.annotations
return (annotations[AnnotationParam] as any)?.name as string | undefined
}
/**
* @since 1.0.0
* @category annotations
*/
export const annotations = <A>(
annotations: Schema.Annotations.Schema<NoInfer<A>> & {
readonly status?: number | undefined
}
): Schema.Annotations.Schema<A> => {
const result: Record<symbol, unknown> = Struct.omit(annotations, "status")
if (annotations.status !== undefined) {
result[AnnotationStatus] = annotations.status
}
return result
}
/**
* @since 1.0.0
* @category reflection
*/
export const isVoid = (ast: AST.AST): boolean => {
switch (ast._tag) {
case "VoidKeyword": {
return true
}
case "Transformation": {
return isVoid(ast.from)
}
case "Suspend": {
return isVoid(ast.f())
}
default: {
return false
}
}
}
/**
* @since 1.0.0
* @category reflection
*/
export const getStatusSuccessAST = (ast: AST.AST): number => getStatus(ast, isVoid(ast) ? 204 : 200)
/**
* @since 1.0.0
* @category reflection
*/
export const getStatusSuccess = <A extends Schema.Schema.Any>(self: A): number => getStatusSuccessAST(self.ast)
/**
* @since 1.0.0
* @category reflection
*/
export const getStatusErrorAST = (ast: AST.AST): number => getStatus(ast, 500)
/**
* @since 1.0.0
* @category reflection
*/
export const getStatusError = <A extends Schema.Schema.All>(self: A): number => getStatusErrorAST(self.ast)
/**
* Extracts all individual types from a union type recursively.
*
* **Details**
*
* This function traverses an AST and collects all the types within a union,
* even if they are nested. It ensures that every type in a union (including
* deeply nested unions) is included in the resulting array. The returned array
* contains each type as an individual AST node, preserving the order in which
* they appear.
*
* @internal
*/
export const extractUnionTypes = (ast: AST.AST): ReadonlyArray<AST.AST> => {
if (AST.isUnion(ast)) {
const types = ast.types.flatMap((type) => extractUnionTypes(type))
if (Reflect.ownKeys(ast.annotations).length === 0) {
return types
} else {
return [AST.Union.make(types, ast.annotations)]
}
} else {
return [ast]
}
}
/** @internal */
export const UnionUnifyAST = (self: AST.AST, that: AST.AST): AST.AST =>
AST.Union.make(Array.from(new Set<AST.AST>([...extractUnionTypes(self), ...extractUnionTypes(that)])))
/**
* @since 1.0.0
*/
export const UnionUnify = <A extends Schema.Schema.All, B extends Schema.Schema.All>(self: A, that: B): Schema.Schema<
A["Type"] | B["Type"],
A["Encoded"] | B["Encoded"],
A["Context"] | B["Context"]
> => Schema.make(UnionUnifyAST(self.ast, that.ast))
type Void$ = typeof Schema.Void
/**
* @since 1.0.0
* @category path params
*/
export interface Param<Name extends string, S extends Schema.Schema.Any | Schema.PropertySignature.Any>
extends Schema.Schema<Schema.Schema.Type<S>, Schema.Schema.Encoded<S>, Schema.Schema.Context<S>>
{
readonly [AnnotationParam]: {
readonly name: Name
readonly schema: S
}
}
/**
* @since 1.0.0
* @category path params
*/
export const param: {
<Name extends string>(
name: Name
): <S extends Schema.Schema.Any | Schema.PropertySignature.Any>(
schema:
& S
& ([Schema.Schema.Encoded<S> & {}] extends [string] ? unknown : "Schema must be encodable to a string")
) => Param<Name, S>
<Name extends string, S extends Schema.Schema.Any | Schema.PropertySignature.Any>(
name: Name,
schema:
& S
& ([Schema.Schema.Encoded<S> & {}] extends [string] ? unknown : "Schema must be encodable to a string")
): Param<Name, S>
} = dual(
2,
<Name extends string, S extends Schema.Schema.Any | Schema.PropertySignature.Any>(
name: Name,
schema: S
): Param<Name, S> => {
const annotations: Record<string | symbol, unknown> = {
[AnnotationParam]: { name, schema }
}
if (Schema.isSchema(schema)) {
const identifier = AST.getIdentifierAnnotation(schema.ast)
if (Option.isSome(identifier)) {
annotations[AST.IdentifierAnnotationId] = identifier.value
}
}
return schema.annotations(annotations) as any
}
)
/**
* @since 1.0.0
* @category empty response
*/
export const Empty = (status: number): typeof Schema.Void => Schema.Void.annotations(annotations({ status }))
/**
* @since 1.0.0
* @category empty response
*/
export interface asEmpty<
S extends Schema.Schema.Any
> extends Schema.transform<typeof Schema.Void, S> {}
/**
* @since 1.0.0
* @category empty response
*/
export const asEmpty: {
<S extends Schema.Schema.Any>(options: {
readonly status: number
readonly decode: LazyArg<Schema.Schema.Type<S>>
}): (self: S) => asEmpty<S>
<S extends Schema.Schema.Any>(
self: S,
options: {
readonly status: number
readonly decode: LazyArg<Schema.Schema.Type<S>>
}
): asEmpty<S>
} = dual(
2,
<S extends Schema.Schema.Any>(
self: S,
options: {
readonly status: number
readonly decode: LazyArg<Schema.Schema.Type<S>>
}
): asEmpty<S> =>
Schema.transform(
Schema.Void.annotations(self.ast.annotations),
Schema.typeSchema(self),
{
decode: options.decode,
encode: constVoid
}
).annotations(annotations({
status: options.status,
[AnnotationEmptyDecodeable]: true
})) as any
)
/**
* @since 1.0.0
* @category empty response
*/
export interface Created extends Void$ {
readonly _: unique symbol
}
/**
* @since 1.0.0
* @category empty response
*/
export const Created: Created = Empty(201) as any
/**
* @since 1.0.0
* @category empty response
*/
export interface Accepted extends Void$ {
readonly _: unique symbol
}
/**
* @since 1.0.0
* @category empty response
*/
export const Accepted: Accepted = Empty(202) as any
/**
* @since 1.0.0
* @category empty response
*/
export interface NoContent extends Void$ {
readonly _: unique symbol
}
/**
* @since 1.0.0
* @category empty response
*/
export const NoContent: NoContent = Empty(204) as any
/**
* @since 1.0.0
* @category multipart
*/
export const MultipartTypeId: unique symbol = Symbol.for("@effect/platform/HttpApiSchema/Multipart")
/**
* @since 1.0.0
* @category multipart
*/
export type MultipartTypeId = typeof MultipartTypeId
/**
* @since 1.0.0
* @category multipart
*/
export interface Multipart<S extends Schema.Schema.Any>
extends
Schema.Schema<Schema.Schema.Type<S> & Brand<MultipartTypeId>, Schema.Schema.Encoded<S>, Schema.Schema.Context<S>>
{}
/**
* @since 1.0.0
* @category multipart
*/
export const Multipart = <S extends Schema.Schema.Any>(self: S, options?: {
readonly maxParts?: Option.Option<number> | undefined
readonly maxFieldSize?: FileSystem.SizeInput | undefined
readonly maxFileSize?: Option.Option<FileSystem.SizeInput> | undefined
readonly maxTotalSize?: Option.Option<FileSystem.SizeInput> | undefined
readonly fieldMimeTypes?: ReadonlyArray<string> | undefined
}): Multipart<S> =>
self.annotations({
[AnnotationMultipart]: options ?? {}
}) as any
/**
* @since 1.0.0
* @category multipart
*/
export const MultipartStreamTypeId: unique symbol = Symbol.for("@effect/platform/HttpApiSchema/MultipartStream")
/**
* @since 1.0.0
* @category multipart
*/
export type MultipartStreamTypeId = typeof MultipartStreamTypeId
/**
* @since 1.0.0
* @category multipart
*/
export interface MultipartStream<S extends Schema.Schema.Any> extends
Schema.Schema<
Schema.Schema.Type<S> & Brand<MultipartStreamTypeId>,
Schema.Schema.Encoded<S>,
Schema.Schema.Context<S>
>
{}
/**
* @since 1.0.0
* @category multipart
*/
export const MultipartStream = <S extends Schema.Schema.Any>(self: S, options?: {
readonly maxParts?: Option.Option<number> | undefined
readonly maxFieldSize?: FileSystem.SizeInput | undefined
readonly maxFileSize?: Option.Option<FileSystem.SizeInput> | undefined
readonly maxTotalSize?: Option.Option<FileSystem.SizeInput> | undefined
readonly fieldMimeTypes?: ReadonlyArray<string> | undefined
}): MultipartStream<S> =>
self.annotations({
[AnnotationMultipartStream]: options ?? {}
}) as any
const defaultContentType = (encoding: Encoding["kind"]) => {
switch (encoding) {
case "Json": {
return "application/json"
}
case "UrlParams": {
return "application/x-www-form-urlencoded"
}
case "Uint8Array": {
return "application/octet-stream"
}
case "Text": {
return "text/plain"
}
}
}
/**
* @since 1.0.0
* @category encoding
*/
export interface Encoding {
readonly kind: "Json" | "UrlParams" | "Uint8Array" | "Text"
readonly contentType: string
}
/**
* @since 1.0.0
* @category encoding
*/
export declare namespace Encoding {
/**
* @since 1.0.0
* @category encoding
*/
export type Validate<A extends Schema.Schema.Any, Kind extends Encoding["kind"]> = Kind extends "Json" ? {}
: Kind extends "UrlParams" ? [A["Encoded"]] extends [Readonly<Record<string, string | undefined>>] ? {}
: `'UrlParams' kind can only be encoded to 'Record<string, string | undefined>'`
: Kind extends "Uint8Array" ?
[A["Encoded"]] extends [Uint8Array] ? {} : `'Uint8Array' kind can only be encoded to 'Uint8Array'`
: Kind extends "Text" ? [A["Encoded"]] extends [string] ? {} : `'Text' kind can only be encoded to 'string'`
: never
}
/**
* @since 1.0.0
* @category encoding
*/
export const withEncoding: {
<A extends Schema.Schema.Any, Kind extends Encoding["kind"]>(
options: {
readonly kind: Kind
readonly contentType?: string | undefined
} & Encoding.Validate<A, Kind>
): (self: A) => A
<A extends Schema.Schema.Any, Kind extends Encoding["kind"]>(
self: A,
options: {
readonly kind: Kind
readonly contentType?: string | undefined
} & Encoding.Validate<A, Kind>
): A
} = dual(2, <A extends Schema.Schema.Any>(self: A, options: {
readonly kind: Encoding["kind"]
readonly contentType?: string | undefined
}): A =>
self.annotations({
[AnnotationEncoding]: {
kind: options.kind,
contentType: options.contentType ?? defaultContentType(options.kind)
},
...(options.kind === "Uint8Array" ?
{
jsonSchema: {
type: "string",
format: "binary"
}
} :
undefined)
}) as any)
/**
* @since 1.0.0
* @category encoding
*/
export const Text = (options?: {
readonly contentType?: string
}): typeof Schema.String => withEncoding(Schema.String, { kind: "Text", ...options })
/**
* @since 1.0.0
* @category encoding
*/
export const Uint8Array = (options?: {
readonly contentType?: string
}): typeof Schema.Uint8ArrayFromSelf => withEncoding(Schema.Uint8ArrayFromSelf, { kind: "Uint8Array", ...options })
const astCache = globalValue(
"@effect/platform/HttpApiSchema/astCache",
() => new WeakMap<AST.AST, Schema.Schema.Any>()
)
/**
* @since 1.0.0
*/
export const deunionize = (
schemas: Set<Schema.Schema.Any>,
schema: Schema.Schema.Any
): void => {
if (astCache.has(schema.ast)) {
schemas.add(astCache.get(schema.ast)!)
return
}
const ast = schema.ast
if (ast._tag === "Union") {
for (const astType of ast.types) {
if (astCache.has(astType)) {
schemas.add(astCache.get(astType)!)
continue
}
const memberSchema = Schema.make(AST.annotations(astType, {
...ast.annotations,
...astType.annotations
}))
astCache.set(astType, memberSchema)
schemas.add(memberSchema)
}
} else {
astCache.set(ast, schema)
schemas.add(schema)
}
}
/**
* @since 1.0.0
* @category empty errors
*/
export interface EmptyError<Self, Tag> extends Effect.Effect<never, Self> {
readonly _tag: Tag
[Unify.typeSymbol]?: unknown
[Unify.unifySymbol]?: EmptyErrorUnify<this>
[Unify.ignoreSymbol]?: EmptyErrorUnifyIgnore
}
/**
* @category models
* @since 1.0.0
*/
export interface EmptyErrorUnify<A extends { [Unify.typeSymbol]?: any }> extends Effect.EffectUnify<A> {
EmptyError?: () => A[Unify.typeSymbol] extends EmptyError<infer Self, infer _Tag> | infer _ ? Self
: never
}
/**
* @since 1.0.0
* @category empty errors
*/
export interface EmptyErrorClass<Self, Tag> extends Schema.Schema<Self, void> {
new(_: void): EmptyError<Self, Tag>
}
/**
* @category models
* @since 1.0.0
*/
export interface EmptyErrorUnifyIgnore extends Effect.EffectUnifyIgnore {
Effect?: true
}
/**
* @since 1.0.0
* @category empty errors
*/
export const EmptyError = <Self>() =>
<const Tag extends string>(options: {
readonly tag: Tag
readonly status: number
}): EmptyErrorClass<Self, Tag> => {
const symbol = Symbol.for(`@effect/platform/HttpApiSchema/EmptyError/${options.tag}`)
class EmptyError extends Effectable.StructuralClass<never, Self> {
readonly _tag: Tag = options.tag
commit(): Effect.Effect<never, Self> {
return Effect.fail(this) as any
}
}
;(EmptyError as any).prototype[symbol] = symbol
Object.assign(EmptyError, {
[Schema.TypeId]: Schema.Void[Schema.TypeId],
pipe: Schema.Void.pipe,
annotations(this: any, annotations: any) {
return Schema.make(this.ast).annotations(annotations)
}
})
let transform: Schema.Schema.Any | undefined
Object.defineProperty(EmptyError, "ast", {
get() {
if (transform) {
return transform.ast
}
const self = this as any
transform = asEmpty(
Schema.declare((u) => hasProperty(u, symbol), {
identifier: options.tag,
title: options.tag
}),
{
status: options.status,
decode: constant(new self())
}
)
return transform.ast
}
})
return EmptyError as any
}