effect 4.0.0-beta.51 → 4.0.0-beta.52
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/dist/Data.js +2 -2
- package/dist/Data.js.map +1 -1
- package/dist/Schema.d.ts +0 -1
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +82 -11
- package/dist/Schema.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/schema/representation.js +1 -2
- package/dist/internal/schema/representation.js.map +1 -1
- package/dist/internal/schema/schema.d.ts.map +1 -1
- package/dist/internal/schema/schema.js +37 -0
- package/dist/internal/schema/schema.js.map +1 -1
- package/dist/unstable/ai/McpServer.d.ts.map +1 -1
- package/dist/unstable/ai/McpServer.js +7 -0
- package/dist/unstable/ai/McpServer.js.map +1 -1
- package/dist/unstable/cli/index.d.ts +2 -0
- package/dist/unstable/cli/index.d.ts.map +1 -1
- package/dist/unstable/cli/index.js +2 -0
- package/dist/unstable/cli/index.js.map +1 -1
- package/dist/unstable/httpapi/HttpApi.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.js +11 -6
- package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiClient.d.ts +5 -6
- package/dist/unstable/httpapi/HttpApiClient.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiEndpoint.d.ts +17 -18
- package/dist/unstable/httpapi/HttpApiEndpoint.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiEndpoint.js +1 -2
- package/dist/unstable/httpapi/HttpApiEndpoint.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiError.d.ts +28 -8
- package/dist/unstable/httpapi/HttpApiError.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiError.js +28 -15
- package/dist/unstable/httpapi/HttpApiError.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.d.ts +4 -3
- package/dist/unstable/httpapi/HttpApiMiddleware.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.js +2 -2
- package/dist/unstable/httpapi/HttpApiMiddleware.js.map +1 -1
- package/dist/unstable/httpapi/OpenApi.js.map +1 -1
- package/package.json +1 -1
- package/src/Data.ts +1 -1
- package/src/Schema.ts +96 -12
- package/src/index.ts +3 -0
- package/src/internal/schema/representation.ts +1 -2
- package/src/internal/schema/schema.ts +43 -0
- package/src/unstable/ai/McpServer.ts +8 -0
- package/src/unstable/cli/index.ts +2 -0
- package/src/unstable/httpapi/HttpApi.ts +1 -1
- package/src/unstable/httpapi/HttpApiBuilder.ts +13 -6
- package/src/unstable/httpapi/HttpApiClient.ts +5 -6
- package/src/unstable/httpapi/HttpApiEndpoint.ts +6 -7
- package/src/unstable/httpapi/HttpApiError.ts +43 -22
- package/src/unstable/httpapi/HttpApiMiddleware.ts +6 -5
- package/src/unstable/httpapi/OpenApi.ts +1 -1
- package/dist/internal/schema/to-codec.d.ts +0 -2
- package/dist/internal/schema/to-codec.d.ts.map +0 -1
- package/dist/internal/schema/to-codec.js +0 -126
- package/dist/internal/schema/to-codec.js.map +0 -1
- package/src/internal/schema/to-codec.ts +0 -138
|
@@ -23,7 +23,6 @@ import * as HttpMethod from "../http/HttpMethod.ts"
|
|
|
23
23
|
import * as UrlParams from "../http/UrlParams.ts"
|
|
24
24
|
import * as HttpApi from "./HttpApi.ts"
|
|
25
25
|
import * as HttpApiEndpoint from "./HttpApiEndpoint.ts"
|
|
26
|
-
import type { BadRequest } from "./HttpApiError.ts"
|
|
27
26
|
import type * as HttpApiGroup from "./HttpApiGroup.ts"
|
|
28
27
|
import type * as HttpApiMiddleware from "./HttpApiMiddleware.ts"
|
|
29
28
|
import * as HttpApiSchema from "./HttpApiSchema.ts"
|
|
@@ -32,7 +31,7 @@ import * as HttpApiSchema from "./HttpApiSchema.ts"
|
|
|
32
31
|
* @since 4.0.0
|
|
33
32
|
* @category models
|
|
34
33
|
*/
|
|
35
|
-
export type Client<Groups extends HttpApiGroup.Any, E =
|
|
34
|
+
export type Client<Groups extends HttpApiGroup.Any, E = never, R = never> = Simplify<
|
|
36
35
|
& {
|
|
37
36
|
readonly [Group in Extract<Groups, { readonly topLevel: false }> as HttpApiGroup.Name<Group>]: Client.Group<
|
|
38
37
|
Group,
|
|
@@ -50,7 +49,7 @@ export type Client<Groups extends HttpApiGroup.Any, E = BadRequest, R = never> =
|
|
|
50
49
|
* @since 4.0.0
|
|
51
50
|
* @category models
|
|
52
51
|
*/
|
|
53
|
-
export type ForApi<Api extends HttpApi.Any, E =
|
|
52
|
+
export type ForApi<Api extends HttpApi.Any, E = never, R = never> = Api extends
|
|
54
53
|
HttpApi.HttpApi<infer _Id, infer Groups> ? Client<Groups, E, R> :
|
|
55
54
|
never
|
|
56
55
|
|
|
@@ -405,7 +404,7 @@ export const makeWith = <ApiId extends string, Groups extends HttpApiGroup.Any,
|
|
|
405
404
|
| undefined
|
|
406
405
|
readonly baseUrl?: URL | string | undefined
|
|
407
406
|
}
|
|
408
|
-
): Effect.Effect<Client<Groups,
|
|
407
|
+
): Effect.Effect<Client<Groups, E, R>, never, HttpApiGroup.MiddlewareClient<Groups>> => {
|
|
409
408
|
const client: Record<string, Record<string, any>> = {}
|
|
410
409
|
return makeClient(api, {
|
|
411
410
|
...options,
|
|
@@ -440,7 +439,7 @@ export const group = <
|
|
|
440
439
|
readonly baseUrl?: URL | string | undefined
|
|
441
440
|
}
|
|
442
441
|
): Effect.Effect<
|
|
443
|
-
Client.Group<Groups, GroupName,
|
|
442
|
+
Client.Group<Groups, GroupName, E, R>,
|
|
444
443
|
never,
|
|
445
444
|
HttpApiGroup.MiddlewareClient<HttpApiGroup.WithName<Groups, GroupName>>
|
|
446
445
|
> => {
|
|
@@ -480,7 +479,7 @@ export const endpoint = <
|
|
|
480
479
|
): Effect.Effect<
|
|
481
480
|
Client.Method<
|
|
482
481
|
HttpApiEndpoint.WithName<HttpApiGroup.Endpoints<HttpApiGroup.WithName<Groups, GroupName>>, EndpointName>,
|
|
483
|
-
|
|
482
|
+
E,
|
|
484
483
|
R
|
|
485
484
|
>,
|
|
486
485
|
never,
|
|
@@ -16,7 +16,6 @@ import * as HttpRouter from "../http/HttpRouter.ts"
|
|
|
16
16
|
import type { HttpServerRequest } from "../http/HttpServerRequest.ts"
|
|
17
17
|
import type { HttpServerResponse } from "../http/HttpServerResponse.ts"
|
|
18
18
|
import type * as Multipart from "../http/Multipart.ts"
|
|
19
|
-
import { BadRequestFromSchemaError, type BadRequestNoContent } from "./HttpApiError.ts"
|
|
20
19
|
import type * as HttpApiGroup from "./HttpApiGroup.ts"
|
|
21
20
|
import type * as HttpApiMiddleware from "./HttpApiMiddleware.ts"
|
|
22
21
|
import * as HttpApiSchema from "./HttpApiSchema.ts"
|
|
@@ -54,7 +53,7 @@ export interface HttpApiEndpoint<
|
|
|
54
53
|
out Payload extends Schema.Top = never,
|
|
55
54
|
out Headers extends Schema.Top = never,
|
|
56
55
|
out Success extends Schema.Top = typeof HttpApiSchema.NoContent,
|
|
57
|
-
out Error extends Schema.Top =
|
|
56
|
+
out Error extends Schema.Top = never,
|
|
58
57
|
in out Middleware = never,
|
|
59
58
|
out MiddlewareR = never
|
|
60
59
|
> extends Pipeable {
|
|
@@ -172,7 +171,7 @@ export function getSuccessSchemas(endpoint: AnyWithProps): [Schema.Top, ...Array
|
|
|
172
171
|
}
|
|
173
172
|
|
|
174
173
|
/** @internal */
|
|
175
|
-
export function getErrorSchemas(endpoint: AnyWithProps):
|
|
174
|
+
export function getErrorSchemas(endpoint: AnyWithProps): Array<Schema.Top> {
|
|
176
175
|
const schemas = new Set<Schema.Top>(endpoint.error)
|
|
177
176
|
for (const middleware of endpoint.middlewares) {
|
|
178
177
|
const key = middleware as any as HttpApiMiddleware.AnyService
|
|
@@ -180,7 +179,7 @@ export function getErrorSchemas(endpoint: AnyWithProps): [Schema.Top, ...Array<S
|
|
|
180
179
|
schemas.add(schema)
|
|
181
180
|
}
|
|
182
181
|
}
|
|
183
|
-
return
|
|
182
|
+
return Array.from(schemas)
|
|
184
183
|
}
|
|
185
184
|
|
|
186
185
|
/**
|
|
@@ -951,7 +950,7 @@ export const make = <Method extends HttpMethod>(method: Method): {
|
|
|
951
950
|
: StringTree<ExtractSchemaOrArray<Payload>>,
|
|
952
951
|
StringTree<Headers extends Schema.Struct.Fields ? Schema.Struct<Headers> : Headers>,
|
|
953
952
|
Json<Success extends ReadonlyArray<Schema.Top> ? Success[number] : Success>,
|
|
954
|
-
Json<
|
|
953
|
+
Json<Error extends ReadonlyArray<Schema.Top> ? Error[number] : Error>
|
|
955
954
|
>
|
|
956
955
|
<
|
|
957
956
|
const Name extends string,
|
|
@@ -983,7 +982,7 @@ export const make = <Method extends HttpMethod>(method: Method): {
|
|
|
983
982
|
ExtractSchemaOrArray<Payload>,
|
|
984
983
|
ExtractSchemaOrArray<Headers>,
|
|
985
984
|
Success extends ReadonlyArray<Schema.Top> ? Success[number] : Success,
|
|
986
|
-
|
|
985
|
+
Error extends ReadonlyArray<Schema.Top> ? Error[number] : Error
|
|
987
986
|
>
|
|
988
987
|
} =>
|
|
989
988
|
<
|
|
@@ -1018,7 +1017,7 @@ export const make = <Method extends HttpMethod>(method: Method): {
|
|
|
1018
1017
|
: Payload,
|
|
1019
1018
|
Headers extends Schema.Struct.Fields ? Schema.Struct<Headers> : Headers,
|
|
1020
1019
|
Success extends ReadonlyArray<Schema.Top> ? Success[number] : Success,
|
|
1021
|
-
|
|
1020
|
+
Error extends ReadonlyArray<Schema.Top> ? Error[number] : Error
|
|
1022
1021
|
> => {
|
|
1023
1022
|
const disableCodecs = options?.disableCodecs ?? false
|
|
1024
1023
|
const transformStringTree = disableCodecs ? identity : Schema.toCodecStringTree
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 4.0.0
|
|
3
3
|
*/
|
|
4
|
+
import * as Data from "../../Data.ts"
|
|
4
5
|
import * as Effect from "../../Effect.ts"
|
|
5
6
|
import * as ErrorReporter from "../../ErrorReporter.ts"
|
|
6
|
-
import {
|
|
7
|
+
import { hasProperty } from "../../Predicate.ts"
|
|
7
8
|
import * as Schema from "../../Schema.ts"
|
|
8
|
-
import * as Transformation from "../../SchemaTransformation.ts"
|
|
9
9
|
import * as HttpServerRespondable from "../http/HttpServerRespondable.ts"
|
|
10
10
|
import * as HttpServerResponse from "../http/HttpServerResponse.ts"
|
|
11
11
|
import * as HttpApiSchema from "./HttpApiSchema.ts"
|
|
@@ -48,26 +48,6 @@ export const BadRequestNoContent = BadRequest.pipe(HttpApiSchema.asNoContent({
|
|
|
48
48
|
decode: () => new BadRequest({})
|
|
49
49
|
}))
|
|
50
50
|
|
|
51
|
-
/**
|
|
52
|
-
* @category Built-in errors
|
|
53
|
-
* @since 4.0.0
|
|
54
|
-
*/
|
|
55
|
-
export const BadRequestFromSchemaError = BadRequest.pipe(
|
|
56
|
-
Schema.decodeTo(
|
|
57
|
-
Schema.Union([Schema.declare(Schema.isSchemaError), BadRequest]),
|
|
58
|
-
Transformation.transform({
|
|
59
|
-
encode: (_) => BadRequest.singleton,
|
|
60
|
-
decode: identity
|
|
61
|
-
})
|
|
62
|
-
),
|
|
63
|
-
HttpApiSchema.asNoContent({
|
|
64
|
-
decode: () => new BadRequest({})
|
|
65
|
-
})
|
|
66
|
-
).annotate({
|
|
67
|
-
httpApiStatus: 400,
|
|
68
|
-
description: "BadRequest"
|
|
69
|
-
})
|
|
70
|
-
|
|
71
51
|
/**
|
|
72
52
|
* @category Built-in errors
|
|
73
53
|
* @since 4.0.0
|
|
@@ -332,3 +312,44 @@ export class ServiceUnavailable
|
|
|
332
312
|
export const ServiceUnavailableNoContent = ServiceUnavailable.pipe(HttpApiSchema.asNoContent({
|
|
333
313
|
decode: () => new ServiceUnavailable({})
|
|
334
314
|
}))
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* @category Parsing errors
|
|
318
|
+
* @since 4.0.0
|
|
319
|
+
*/
|
|
320
|
+
export type HttpApiSchemaErrorTypeId = "~effect/httpapi/HttpApiError/HttpApiSchemaError"
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @category Parsing errors
|
|
324
|
+
* @since 4.0.0
|
|
325
|
+
*/
|
|
326
|
+
export const HttpApiSchemaErrorTypeId: HttpApiSchemaErrorTypeId = "~effect/httpapi/HttpApiError/HttpApiSchemaError"
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* @category Parsing errors
|
|
330
|
+
* @since 4.0.0
|
|
331
|
+
*/
|
|
332
|
+
export class HttpApiSchemaError extends Data.TaggedClass("HttpApiSchemaError")<{
|
|
333
|
+
readonly kind: "Params" | "Headers" | "Query" | "Body" | "Payload"
|
|
334
|
+
readonly cause: Schema.SchemaError
|
|
335
|
+
}> {
|
|
336
|
+
readonly [HttpApiSchemaErrorTypeId]: HttpApiSchemaErrorTypeId = HttpApiSchemaErrorTypeId
|
|
337
|
+
|
|
338
|
+
static is(u: unknown): u is HttpApiSchemaError {
|
|
339
|
+
return hasProperty(u, HttpApiSchemaErrorTypeId)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
static wrap<A, R>(
|
|
343
|
+
kind: HttpApiSchemaError["kind"],
|
|
344
|
+
effect: Effect.Effect<A, Schema.SchemaError, R>
|
|
345
|
+
): Effect.Effect<A, HttpApiSchemaError, R> {
|
|
346
|
+
return Effect.mapError(effect, (error) => new HttpApiSchemaError({ kind, cause: error }))
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
readonly name = "HttpApiSchemaError"
|
|
350
|
+
readonly message = this.kind;
|
|
351
|
+
|
|
352
|
+
[HttpServerRespondable.symbol]() {
|
|
353
|
+
return Effect.succeed(badRequestResponse)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
@@ -5,7 +5,7 @@ import * as Context from "../../Context.ts"
|
|
|
5
5
|
import * as Effect from "../../Effect.ts"
|
|
6
6
|
import * as Layer from "../../Layer.ts"
|
|
7
7
|
import { hasProperty } from "../../Predicate.ts"
|
|
8
|
-
import * as Schema from "../../Schema.ts"
|
|
8
|
+
import type * as Schema from "../../Schema.ts"
|
|
9
9
|
import { Scope } from "../../Scope.ts"
|
|
10
10
|
import type { unhandled } from "../../Types.ts"
|
|
11
11
|
import type * as HttpClientError from "../http/HttpClientError.ts"
|
|
@@ -14,6 +14,7 @@ import type * as HttpClientResponse from "../http/HttpClientResponse.ts"
|
|
|
14
14
|
import type * as HttpRouter from "../http/HttpRouter.ts"
|
|
15
15
|
import type { HttpServerResponse } from "../http/HttpServerResponse.ts"
|
|
16
16
|
import type * as HttpApiEndpoint from "./HttpApiEndpoint.ts"
|
|
17
|
+
import { HttpApiSchemaError } from "./HttpApiError.ts"
|
|
17
18
|
import type * as HttpApiGroup from "./HttpApiGroup.ts"
|
|
18
19
|
import type * as HttpApiSecurity from "./HttpApiSecurity.ts"
|
|
19
20
|
|
|
@@ -333,11 +334,11 @@ function getError(error: ErrorConstraint | undefined): ReadonlySet<Schema.Top> {
|
|
|
333
334
|
export const layerSchemaErrorTransform = <Id, E extends ErrorConstraint, Requires>(
|
|
334
335
|
service: Context.Service<Id, HttpApiMiddleware<never, E, Requires>>,
|
|
335
336
|
transform: (
|
|
336
|
-
error:
|
|
337
|
+
error: HttpApiSchemaError,
|
|
337
338
|
context: { readonly endpoint: HttpApiEndpoint.AnyWithProps; readonly group: HttpApiGroup.AnyWithProps }
|
|
338
339
|
) => Effect.Effect<
|
|
339
340
|
HttpServerResponse,
|
|
340
|
-
ErrorSchemaFromConstraint<E>["Type"] |
|
|
341
|
+
ErrorSchemaFromConstraint<E>["Type"] | HttpApiSchemaError,
|
|
341
342
|
Requires | HttpRouter.Provided
|
|
342
343
|
>
|
|
343
344
|
): Layer.Layer<Id> =>
|
|
@@ -348,9 +349,9 @@ export const layerSchemaErrorTransform = <Id, E extends ErrorConstraint, Require
|
|
|
348
349
|
httpEffect,
|
|
349
350
|
(e): Effect.Effect<
|
|
350
351
|
HttpServerResponse,
|
|
351
|
-
unhandled |
|
|
352
|
+
unhandled | HttpApiSchemaError | ErrorSchemaFromConstraint<E>["Type"],
|
|
352
353
|
Requires | HttpRouter.Provided
|
|
353
|
-
> =>
|
|
354
|
+
> => HttpApiSchemaError.is(e) ? transform(e, options) : Effect.fail(e)
|
|
354
355
|
)
|
|
355
356
|
)
|
|
356
357
|
|
|
@@ -524,7 +524,7 @@ type ResponseBodies = Map<
|
|
|
524
524
|
>
|
|
525
525
|
|
|
526
526
|
function extractResponseBodies(
|
|
527
|
-
schemas:
|
|
527
|
+
schemas: Array<Schema.Top>,
|
|
528
528
|
getStatus: (ast: AST.AST) => number,
|
|
529
529
|
getDescription: (ast: AST.AST) => string | undefined
|
|
530
530
|
): ResponseBodies {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"to-codec.d.ts","sourceRoot":"","sources":["../../../src/internal/schema/to-codec.ts"],"names":[],"mappings":""}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { memoize } from "../../Function.js";
|
|
2
|
-
import * as Predicate from "../../Predicate.js";
|
|
3
|
-
import * as AST from "../../SchemaAST.js";
|
|
4
|
-
import * as InternalSchema from "./schema.js";
|
|
5
|
-
/** @internal */
|
|
6
|
-
export const toCodecJson = /*#__PURE__*/AST.toCodec(ast => {
|
|
7
|
-
const out = toCodecJsonBase(ast);
|
|
8
|
-
if (out !== ast && AST.isOptional(ast)) {
|
|
9
|
-
return AST.optionalKeyLastLink(out);
|
|
10
|
-
}
|
|
11
|
-
return out;
|
|
12
|
-
});
|
|
13
|
-
function toCodecJsonBase(ast) {
|
|
14
|
-
switch (ast._tag) {
|
|
15
|
-
case "Declaration":
|
|
16
|
-
{
|
|
17
|
-
const getLink = ast.annotations?.toCodecJson ?? ast.annotations?.toCodec;
|
|
18
|
-
if (Predicate.isFunction(getLink)) {
|
|
19
|
-
const tps = AST.isDeclaration(ast) ? ast.typeParameters.map(tp => InternalSchema.make(AST.toEncoded(tp))) : [];
|
|
20
|
-
const link = getLink(tps);
|
|
21
|
-
const to = toCodecJson(link.to);
|
|
22
|
-
return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)]);
|
|
23
|
-
}
|
|
24
|
-
return AST.replaceEncoding(ast, [AST.unknownToNull]);
|
|
25
|
-
}
|
|
26
|
-
case "Unknown":
|
|
27
|
-
case "ObjectKeyword":
|
|
28
|
-
return AST.replaceEncoding(ast, [AST.unknownToJson]);
|
|
29
|
-
case "Undefined":
|
|
30
|
-
case "Void":
|
|
31
|
-
case "Literal":
|
|
32
|
-
case "Number":
|
|
33
|
-
return ast.toCodecJson();
|
|
34
|
-
case "UniqueSymbol":
|
|
35
|
-
case "Symbol":
|
|
36
|
-
case "BigInt":
|
|
37
|
-
return ast.toCodecStringTree();
|
|
38
|
-
case "Objects":
|
|
39
|
-
{
|
|
40
|
-
if (ast.propertySignatures.some(ps => typeof ps.name !== "string")) {
|
|
41
|
-
throw new globalThis.Error("Objects property names must be strings", {
|
|
42
|
-
cause: ast
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
return ast.recur(toCodecJson);
|
|
46
|
-
}
|
|
47
|
-
case "Union":
|
|
48
|
-
{
|
|
49
|
-
const sortedTypes = jsonReorder(ast.types);
|
|
50
|
-
if (sortedTypes !== ast.types) {
|
|
51
|
-
return new AST.Union(sortedTypes, ast.mode, ast.annotations, ast.checks, ast.encoding, ast.context).recur(toCodecJson);
|
|
52
|
-
}
|
|
53
|
-
return ast.recur(toCodecJson);
|
|
54
|
-
}
|
|
55
|
-
case "Arrays":
|
|
56
|
-
case "Suspend":
|
|
57
|
-
return ast.recur(toCodecJson);
|
|
58
|
-
}
|
|
59
|
-
// `Schema.Any` is used as an escape hatch
|
|
60
|
-
return ast;
|
|
61
|
-
}
|
|
62
|
-
/** @internal */
|
|
63
|
-
export const jsonReorder = /*#__PURE__*/makeReorder(getJsonPriority);
|
|
64
|
-
function getJsonPriority(ast) {
|
|
65
|
-
switch (ast._tag) {
|
|
66
|
-
case "BigInt":
|
|
67
|
-
case "Symbol":
|
|
68
|
-
case "UniqueSymbol":
|
|
69
|
-
return 0;
|
|
70
|
-
default:
|
|
71
|
-
return 1;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
/** @internal */
|
|
75
|
-
export function makeReorder(getPriority) {
|
|
76
|
-
return types => {
|
|
77
|
-
// Create a map of original indices for O(1) lookup
|
|
78
|
-
const indexMap = new Map();
|
|
79
|
-
for (let i = 0; i < types.length; i++) {
|
|
80
|
-
indexMap.set(AST.toEncoded(types[i]), i);
|
|
81
|
-
}
|
|
82
|
-
// Create a sorted copy of the types array
|
|
83
|
-
const sortedTypes = [...types].sort((a, b) => {
|
|
84
|
-
a = AST.toEncoded(a);
|
|
85
|
-
b = AST.toEncoded(b);
|
|
86
|
-
const pa = getPriority(a);
|
|
87
|
-
const pb = getPriority(b);
|
|
88
|
-
if (pa !== pb) return pa - pb;
|
|
89
|
-
// If priorities are equal, maintain original order (stable sort)
|
|
90
|
-
return indexMap.get(a) - indexMap.get(b);
|
|
91
|
-
});
|
|
92
|
-
// Check if order changed by comparing arrays
|
|
93
|
-
const orderChanged = sortedTypes.some((ast, index) => ast !== types[index]);
|
|
94
|
-
if (!orderChanged) return types;
|
|
95
|
-
return sortedTypes;
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
/** @internal */
|
|
99
|
-
export const toCodecIso = /*#__PURE__*/memoize(ast => {
|
|
100
|
-
const out = toCodecIsoBase(ast);
|
|
101
|
-
if (out !== ast && AST.isOptional(ast)) {
|
|
102
|
-
return AST.optionalKeyLastLink(out);
|
|
103
|
-
}
|
|
104
|
-
return out;
|
|
105
|
-
});
|
|
106
|
-
function toCodecIsoBase(ast) {
|
|
107
|
-
switch (ast._tag) {
|
|
108
|
-
case "Declaration":
|
|
109
|
-
{
|
|
110
|
-
const getLink = ast.annotations?.toCodecIso ?? ast.annotations?.toCodec;
|
|
111
|
-
if (Predicate.isFunction(getLink)) {
|
|
112
|
-
const link = getLink(ast.typeParameters.map(tp => InternalSchema.make(tp)));
|
|
113
|
-
const to = toCodecIso(link.to);
|
|
114
|
-
return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)]);
|
|
115
|
-
}
|
|
116
|
-
return ast;
|
|
117
|
-
}
|
|
118
|
-
case "Arrays":
|
|
119
|
-
case "Objects":
|
|
120
|
-
case "Union":
|
|
121
|
-
case "Suspend":
|
|
122
|
-
return ast.recur(toCodecIso);
|
|
123
|
-
}
|
|
124
|
-
return ast;
|
|
125
|
-
}
|
|
126
|
-
//# sourceMappingURL=to-codec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"to-codec.js","names":["memoize","Predicate","AST","InternalSchema","toCodecJson","toCodec","ast","out","toCodecJsonBase","isOptional","optionalKeyLastLink","_tag","getLink","annotations","isFunction","tps","isDeclaration","typeParameters","map","tp","make","toEncoded","link","to","replaceEncoding","Link","transformation","unknownToNull","unknownToJson","toCodecStringTree","propertySignatures","some","ps","name","globalThis","Error","cause","recur","sortedTypes","jsonReorder","types","Union","mode","checks","encoding","context","makeReorder","getJsonPriority","getPriority","indexMap","Map","i","length","set","sort","a","b","pa","pb","get","orderChanged","index","toCodecIso","toCodecIsoBase"],"sources":["../../../src/internal/schema/to-codec.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,OAAO,QAAQ,mBAAmB;AAC3C,OAAO,KAAKC,SAAS,MAAM,oBAAoB;AAC/C,OAAO,KAAKC,GAAG,MAAM,oBAAoB;AACzC,OAAO,KAAKC,cAAc,MAAM,aAAa;AAE7C;AACA,OAAO,MAAMC,WAAW,gBAAGF,GAAG,CAACG,OAAO,CAAEC,GAAG,IAAI;EAC7C,MAAMC,GAAG,GAAGC,eAAe,CAACF,GAAG,CAAC;EA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AAG,IAAIJ,GAAG,CAACO,UAAU,CAACH,GAAG,CAAC,EAAE;IACtC,OAAOJ,GAAG,CAACQ,mBAAmB,CAACH,GAAG,CAAC;EACrC;EACA,OAAOA,GAAG;AACZ,CAAC,CAAC;AAEF,SAASwD,cAAcA,CAACzD,GAAY;EAClC,QAAQA,GAAG,CAACK,IAAI;IACd,KAAK,aAAa;MAAE;QAClB,MAAMC,OAAO,GAAGN,GAAG,CAACO,WAAW,EAAEiD,UAAU,IAAIxD,GAAG,CAACO,WAAW,EAAER,OAAO;QACvE,IAAIJ,SAAS,CAACa,UAAU,CAACF,OAAO,CAAC,EAAE;UACjC,MAAMU,IAAI,GAAGV,OAAO,CAACN,GAAG,CAACW,cAAc,CAACC,GAAG,CAAEC,EAAE,IAAKhB,cAAc,CAACiB,IAAI,CAACD,EAAE,CAAC,CAAC,CAAC;UAC7E,MAAMI,EAAE,GAAGuC,UAAU,CAACxC,IAAI,CAACC,EAAE,CAAC;UAC9B,OAAOrB,GAAG,CAACsB,eAAe,CAAClB,GAAG,EAAEiB,EAAE,KAAKD,IAAI,CAACC,EAAE,GAAG,CAACD,IAAI,CAAC,GAAG,CAAC,IAAIpB,GAAG,CAACuB,IAAI,CAACF,EAAE,EAAED,IAAI,CAACI,cAAc,CAAC,CAAC,CAAC;QACpG;QACA,OAAOpB,GAAG;MACZ;IACA,KAAK,QAAQ;IACb,KAAK,SAAS;IACd,KAAK,OAAO;IACZ,KAAK,SAAS;MACZ,OAAOA,GAAG,CAAC+B,KAAK,CAACyB,UAAU,CAAC;EAChC;EACA,OAAOxD,GAAG;AACZ","ignoreList":[]}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { memoize } from "../../Function.ts"
|
|
2
|
-
import * as Predicate from "../../Predicate.ts"
|
|
3
|
-
import * as AST from "../../SchemaAST.ts"
|
|
4
|
-
import * as InternalSchema from "./schema.ts"
|
|
5
|
-
|
|
6
|
-
/** @internal */
|
|
7
|
-
export const toCodecJson = AST.toCodec((ast) => {
|
|
8
|
-
const out = toCodecJsonBase(ast)
|
|
9
|
-
if (out !== ast && AST.isOptional(ast)) {
|
|
10
|
-
return AST.optionalKeyLastLink(out)
|
|
11
|
-
}
|
|
12
|
-
return out
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
function toCodecJsonBase(ast: AST.AST): AST.AST {
|
|
16
|
-
switch (ast._tag) {
|
|
17
|
-
case "Declaration": {
|
|
18
|
-
const getLink = ast.annotations?.toCodecJson ?? ast.annotations?.toCodec
|
|
19
|
-
if (Predicate.isFunction(getLink)) {
|
|
20
|
-
const tps = AST.isDeclaration(ast)
|
|
21
|
-
? ast.typeParameters.map((tp) => InternalSchema.make(AST.toEncoded(tp)))
|
|
22
|
-
: []
|
|
23
|
-
const link = getLink(tps)
|
|
24
|
-
const to = toCodecJson(link.to)
|
|
25
|
-
return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)])
|
|
26
|
-
}
|
|
27
|
-
return AST.replaceEncoding(ast, [AST.unknownToNull])
|
|
28
|
-
}
|
|
29
|
-
case "Unknown":
|
|
30
|
-
case "ObjectKeyword":
|
|
31
|
-
return AST.replaceEncoding(ast, [AST.unknownToJson])
|
|
32
|
-
case "Undefined":
|
|
33
|
-
case "Void":
|
|
34
|
-
case "Literal":
|
|
35
|
-
case "Number":
|
|
36
|
-
return ast.toCodecJson()
|
|
37
|
-
case "UniqueSymbol":
|
|
38
|
-
case "Symbol":
|
|
39
|
-
case "BigInt":
|
|
40
|
-
return ast.toCodecStringTree()
|
|
41
|
-
case "Objects": {
|
|
42
|
-
if (ast.propertySignatures.some((ps) => typeof ps.name !== "string")) {
|
|
43
|
-
throw new globalThis.Error("Objects property names must be strings", { cause: ast })
|
|
44
|
-
}
|
|
45
|
-
return ast.recur(toCodecJson)
|
|
46
|
-
}
|
|
47
|
-
case "Union": {
|
|
48
|
-
const sortedTypes = jsonReorder(ast.types)
|
|
49
|
-
if (sortedTypes !== ast.types) {
|
|
50
|
-
return new AST.Union(
|
|
51
|
-
sortedTypes,
|
|
52
|
-
ast.mode,
|
|
53
|
-
ast.annotations,
|
|
54
|
-
ast.checks,
|
|
55
|
-
ast.encoding,
|
|
56
|
-
ast.context
|
|
57
|
-
).recur(toCodecJson)
|
|
58
|
-
}
|
|
59
|
-
return ast.recur(toCodecJson)
|
|
60
|
-
}
|
|
61
|
-
case "Arrays":
|
|
62
|
-
case "Suspend":
|
|
63
|
-
return ast.recur(toCodecJson)
|
|
64
|
-
}
|
|
65
|
-
// `Schema.Any` is used as an escape hatch
|
|
66
|
-
return ast
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** @internal */
|
|
70
|
-
export const jsonReorder = makeReorder(getJsonPriority)
|
|
71
|
-
|
|
72
|
-
function getJsonPriority(ast: AST.AST): number {
|
|
73
|
-
switch (ast._tag) {
|
|
74
|
-
case "BigInt":
|
|
75
|
-
case "Symbol":
|
|
76
|
-
case "UniqueSymbol":
|
|
77
|
-
return 0
|
|
78
|
-
default:
|
|
79
|
-
return 1
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** @internal */
|
|
84
|
-
export function makeReorder(getPriority: (ast: AST.AST) => number) {
|
|
85
|
-
return (types: ReadonlyArray<AST.AST>): ReadonlyArray<AST.AST> => {
|
|
86
|
-
// Create a map of original indices for O(1) lookup
|
|
87
|
-
const indexMap = new Map<AST.AST, number>()
|
|
88
|
-
for (let i = 0; i < types.length; i++) {
|
|
89
|
-
indexMap.set(AST.toEncoded(types[i]), i)
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Create a sorted copy of the types array
|
|
93
|
-
const sortedTypes = [...types].sort((a, b) => {
|
|
94
|
-
a = AST.toEncoded(a)
|
|
95
|
-
b = AST.toEncoded(b)
|
|
96
|
-
const pa = getPriority(a)
|
|
97
|
-
const pb = getPriority(b)
|
|
98
|
-
if (pa !== pb) return pa - pb
|
|
99
|
-
// If priorities are equal, maintain original order (stable sort)
|
|
100
|
-
return indexMap.get(a)! - indexMap.get(b)!
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
// Check if order changed by comparing arrays
|
|
104
|
-
const orderChanged = sortedTypes.some((ast, index) => ast !== types[index])
|
|
105
|
-
|
|
106
|
-
if (!orderChanged) return types
|
|
107
|
-
return sortedTypes
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** @internal */
|
|
112
|
-
export const toCodecIso = memoize((ast: AST.AST): AST.AST => {
|
|
113
|
-
const out = toCodecIsoBase(ast)
|
|
114
|
-
if (out !== ast && AST.isOptional(ast)) {
|
|
115
|
-
return AST.optionalKeyLastLink(out)
|
|
116
|
-
}
|
|
117
|
-
return out
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
function toCodecIsoBase(ast: AST.AST): AST.AST {
|
|
121
|
-
switch (ast._tag) {
|
|
122
|
-
case "Declaration": {
|
|
123
|
-
const getLink = ast.annotations?.toCodecIso ?? ast.annotations?.toCodec
|
|
124
|
-
if (Predicate.isFunction(getLink)) {
|
|
125
|
-
const link = getLink(ast.typeParameters.map((tp) => InternalSchema.make(tp)))
|
|
126
|
-
const to = toCodecIso(link.to)
|
|
127
|
-
return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)])
|
|
128
|
-
}
|
|
129
|
-
return ast
|
|
130
|
-
}
|
|
131
|
-
case "Arrays":
|
|
132
|
-
case "Objects":
|
|
133
|
-
case "Union":
|
|
134
|
-
case "Suspend":
|
|
135
|
-
return ast.recur(toCodecIso)
|
|
136
|
-
}
|
|
137
|
-
return ast
|
|
138
|
-
}
|