@distilled.cloud/core 1.0.0-rc.15 → 1.0.0-rc.17
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/lib/_bundle/aws.js +2 -0
- package/lib/_bundle/aws.js.map +1 -0
- package/lib/_bundle/base.js +5 -0
- package/lib/_bundle/base.js.map +1 -0
- package/lib/_bundle/graphql.js +3 -0
- package/lib/_bundle/graphql.js.map +1 -0
- package/lib/_bundle/rest.js +2 -0
- package/lib/_bundle/rest.js.map +1 -0
- package/lib/_bundle/xml.js +4 -0
- package/lib/_bundle/xml.js.map +1 -0
- package/lib/api.d.ts +118 -13
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -188
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +1 -278
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +1 -0
- package/lib/codegen/boolean-string-enums.js +36 -36
- package/lib/codegen/chunk.d.ts +7 -0
- package/lib/codegen/chunk.d.ts.map +1 -0
- package/lib/codegen/chunk.js +139 -0
- package/lib/codegen/chunk.js.map +1 -0
- package/lib/codegen/cli.d.ts.map +1 -1
- package/lib/codegen/cli.js +28 -14
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/datafy.d.ts +14 -0
- package/lib/codegen/datafy.d.ts.map +1 -0
- package/lib/codegen/datafy.js +853 -0
- package/lib/codegen/datafy.js.map +1 -0
- package/lib/codegen/dedupe.d.ts +5 -0
- package/lib/codegen/dedupe.d.ts.map +1 -0
- package/lib/codegen/dedupe.js +153 -0
- package/lib/codegen/dedupe.js.map +1 -0
- package/lib/codegen/emit.js +56 -56
- package/lib/codegen/format.js +18 -18
- package/lib/codegen/generated-at.js +29 -29
- package/lib/codegen/generator.d.ts.map +1 -1
- package/lib/codegen/generator.js +92 -84
- package/lib/codegen/generator.js.map +1 -1
- package/lib/codegen/graph.js +36 -36
- package/lib/codegen/graphql-client.d.ts +2 -0
- package/lib/codegen/graphql-client.d.ts.map +1 -1
- package/lib/codegen/graphql-client.js +55 -26
- package/lib/codegen/graphql-client.js.map +1 -1
- package/lib/codegen/graphql.js +113 -113
- package/lib/codegen/latin1.d.ts +3 -0
- package/lib/codegen/latin1.d.ts.map +1 -0
- package/lib/codegen/latin1.js +126 -0
- package/lib/codegen/latin1.js.map +1 -0
- package/lib/codegen/members.d.ts +7 -0
- package/lib/codegen/members.d.ts.map +1 -1
- package/lib/codegen/members.js +26 -19
- package/lib/codegen/members.js.map +1 -1
- package/lib/codegen/model-diff.js +6 -6
- package/lib/codegen/naming.js +19 -19
- package/lib/codegen/openapi-cli.js +15 -15
- package/lib/codegen/openapi.d.ts +17 -0
- package/lib/codegen/openapi.d.ts.map +1 -1
- package/lib/codegen/openapi.js +323 -169
- package/lib/codegen/openapi.js.map +1 -1
- package/lib/codegen/operations.js +14 -14
- package/lib/codegen/pagination.js +11 -11
- package/lib/codegen/patch-audit.js +57 -57
- package/lib/codegen/patches.js +79 -79
- package/lib/codegen/prelude.js +19 -19
- package/lib/codegen/privatize.d.ts +21 -0
- package/lib/codegen/privatize.d.ts.map +1 -0
- package/lib/codegen/privatize.js +176 -0
- package/lib/codegen/privatize.js.map +1 -0
- package/lib/codegen/proto.js +25 -25
- package/lib/codegen/pure.d.ts +6 -0
- package/lib/codegen/pure.d.ts.map +1 -0
- package/lib/codegen/pure.js +91 -0
- package/lib/codegen/pure.js.map +1 -0
- package/lib/codegen/rewrite-operation-ids.js +108 -108
- package/lib/codegen/spec-path.js +57 -57
- package/lib/codegen/table.d.ts +5 -0
- package/lib/codegen/table.d.ts.map +1 -0
- package/lib/codegen/table.js +249 -0
- package/lib/codegen/table.js.map +1 -0
- package/lib/config.d.ts +27 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -0
- package/lib/crypto.d.ts +76 -0
- package/lib/crypto.d.ts.map +1 -0
- package/lib/crypto.js +1 -0
- package/lib/debug.d.ts +2 -0
- package/lib/debug.d.ts.map +1 -0
- package/lib/debug.js +1 -0
- package/lib/error-category.d.ts +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js +1 -46
- package/lib/errors.d.ts +62 -62
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -195
- package/lib/graphql.d.ts +31 -20
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.js +1 -1027
- package/lib/graphql.types.js +1 -5
- package/lib/index.d.ts +3 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -15
- package/lib/json-patch.d.ts +2 -2
- package/lib/json-patch.js +1 -226
- package/lib/json-patch.js.map +1 -1
- package/lib/pagination.d.ts +9 -25
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -266
- package/lib/plain-values.d.ts +10 -0
- package/lib/plain-values.d.ts.map +1 -0
- package/lib/plain-values.js +2 -0
- package/lib/plain-values.js.map +1 -0
- package/lib/protocol-http.d.ts +50 -14
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -639
- package/lib/protocol-rest.d.ts +38 -25
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -267
- package/lib/query.js +1 -27
- package/lib/response-validation-strict.d.ts +48 -0
- package/lib/response-validation-strict.d.ts.map +1 -0
- package/lib/response-validation-strict.js +1 -0
- package/lib/response-validation.d.ts +43 -25
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/response-validation.js +1 -76
- package/lib/retry-after.d.ts +3 -3
- package/lib/retry-after.js +1 -112
- package/lib/retry.d.ts +48 -29
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -209
- package/lib/schema-lite-impl.d.ts +158 -0
- package/lib/schema-lite-impl.d.ts.map +1 -0
- package/lib/schema-lite-impl.js +1 -0
- package/lib/schema-lite-typed.d.ts +41 -0
- package/lib/schema-lite-typed.d.ts.map +1 -0
- package/lib/schema-lite-typed.js +2 -0
- package/lib/schema-lite-typed.js.map +1 -0
- package/lib/schema-lite.d.ts +40 -0
- package/lib/schema-lite.d.ts.map +1 -0
- package/lib/schema-lite.js +1 -0
- package/lib/schema.d.ts +20 -6
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +1 -48
- package/lib/testing.d.ts +3 -2
- package/lib/testing.d.ts.map +1 -1
- package/lib/testing.js +1 -40
- package/lib/testing.js.map +1 -1
- package/lib/trait.d.ts +13 -13
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -131
- package/lib/xml.d.ts +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/lib/xml.js +1 -311
- package/package.json +9 -7
- package/src/api.ts +1080 -180
- package/src/category.ts +33 -33
- package/src/chunks.ts +30 -0
- package/src/codegen/chunk.ts +147 -0
- package/src/codegen/cli.ts +27 -1
- package/src/codegen/datafy.ts +874 -0
- package/src/codegen/dedupe.ts +173 -0
- package/src/codegen/generator.ts +10 -3
- package/src/codegen/graphql-client.ts +49 -15
- package/src/codegen/latin1.ts +131 -0
- package/src/codegen/members.ts +8 -0
- package/src/codegen/openapi.ts +194 -3
- package/src/codegen/privatize.ts +200 -0
- package/src/codegen/pure.ts +94 -0
- package/src/codegen/table.ts +254 -0
- package/src/config.ts +216 -0
- package/src/crypto.ts +202 -0
- package/src/debug.ts +8 -0
- package/src/error-category.ts +2 -1
- package/src/errors.ts +65 -56
- package/src/graphql.ts +109 -62
- package/src/graphql.types.ts +9 -7
- package/src/index.ts +3 -2
- package/src/json-patch.ts +2 -2
- package/src/pagination.ts +37 -18
- package/src/plain-values.ts +41 -0
- package/src/protocol-http.ts +378 -145
- package/src/protocol-rest.ts +337 -169
- package/src/response-validation-strict.ts +327 -0
- package/src/response-validation.ts +101 -50
- package/src/retry-after.ts +3 -3
- package/src/retry.ts +94 -93
- package/src/schema-lite-impl.ts +550 -0
- package/src/schema-lite-typed.ts +44 -0
- package/src/schema-lite.ts +44 -0
- package/src/schema.ts +258 -14
- package/src/testing.ts +4 -2
- package/src/trait.ts +36 -28
- package/src/xml.ts +31 -17
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/codegen/boolean-string-enums.test.d.ts +0 -2
- package/lib/codegen/boolean-string-enums.test.d.ts.map +0 -1
- package/lib/codegen/boolean-string-enums.test.js +0 -275
- package/lib/codegen/boolean-string-enums.test.js.map +0 -1
- package/lib/codegen/defer-naming.test.d.ts +0 -2
- package/lib/codegen/defer-naming.test.d.ts.map +0 -1
- package/lib/codegen/defer-naming.test.js +0 -105
- package/lib/codegen/defer-naming.test.js.map +0 -1
- package/lib/codegen/generated-at.test.d.ts +0 -2
- package/lib/codegen/generated-at.test.d.ts.map +0 -1
- package/lib/codegen/generated-at.test.js +0 -81
- package/lib/codegen/generated-at.test.js.map +0 -1
- package/lib/codegen/generator.test.d.ts +0 -2
- package/lib/codegen/generator.test.d.ts.map +0 -1
- package/lib/codegen/generator.test.js +0 -121
- package/lib/codegen/generator.test.js.map +0 -1
- package/lib/codegen/graphql-client.test.d.ts +0 -2
- package/lib/codegen/graphql-client.test.d.ts.map +0 -1
- package/lib/codegen/graphql-client.test.js +0 -171
- package/lib/codegen/graphql-client.test.js.map +0 -1
- package/lib/codegen/openapi-binary.test.d.ts +0 -2
- package/lib/codegen/openapi-binary.test.d.ts.map +0 -1
- package/lib/codegen/openapi-binary.test.js +0 -67
- package/lib/codegen/openapi-binary.test.js.map +0 -1
- package/lib/codegen/patch-audit.test.d.ts +0 -2
- package/lib/codegen/patch-audit.test.d.ts.map +0 -1
- package/lib/codegen/patch-audit.test.js +0 -141
- package/lib/codegen/patch-audit.test.js.map +0 -1
- package/lib/codegen/patches.test.d.ts +0 -2
- package/lib/codegen/patches.test.d.ts.map +0 -1
- package/lib/codegen/patches.test.js +0 -155
- package/lib/codegen/patches.test.js.map +0 -1
- package/lib/codegen/protocol-imports.test.d.ts +0 -2
- package/lib/codegen/protocol-imports.test.d.ts.map +0 -1
- package/lib/codegen/protocol-imports.test.js +0 -36
- package/lib/codegen/protocol-imports.test.js.map +0 -1
- package/lib/codegen/rewrite-operation-ids.test.d.ts +0 -2
- package/lib/codegen/rewrite-operation-ids.test.d.ts.map +0 -1
- package/lib/codegen/rewrite-operation-ids.test.js +0 -451
- package/lib/codegen/rewrite-operation-ids.test.js.map +0 -1
- package/lib/codegen/untagged-unions.test.d.ts +0 -2
- package/lib/codegen/untagged-unions.test.d.ts.map +0 -1
- package/lib/codegen/untagged-unions.test.js +0 -80
- package/lib/codegen/untagged-unions.test.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/fixtures/xml/upstream-validator.json +0 -397
- package/lib/graphql.js.map +0 -1
- package/lib/graphql.test.d.ts +0 -2
- package/lib/graphql.test.d.ts.map +0 -1
- package/lib/graphql.test.js +0 -151
- package/lib/graphql.test.js.map +0 -1
- package/lib/graphql.types.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/json-patch.test.d.ts +0 -2
- package/lib/json-patch.test.d.ts.map +0 -1
- package/lib/json-patch.test.js +0 -65
- package/lib/json-patch.test.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/pagination.test.d.ts +0 -2
- package/lib/pagination.test.d.ts.map +0 -1
- package/lib/pagination.test.js +0 -131
- package/lib/pagination.test.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-http.test.d.ts +0 -2
- package/lib/protocol-http.test.d.ts.map +0 -1
- package/lib/protocol-http.test.js +0 -228
- package/lib/protocol-http.test.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/protocol-rest.test.d.ts +0 -2
- package/lib/protocol-rest.test.d.ts.map +0 -1
- package/lib/protocol-rest.test.js +0 -498
- package/lib/protocol-rest.test.js.map +0 -1
- package/lib/query.js.map +0 -1
- package/lib/response-validation.js.map +0 -1
- package/lib/response-validation.test.d.ts +0 -2
- package/lib/response-validation.test.d.ts.map +0 -1
- package/lib/response-validation.test.js +0 -72
- package/lib/response-validation.test.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry-after.test.d.ts +0 -2
- package/lib/retry-after.test.d.ts.map +0 -1
- package/lib/retry-after.test.js +0 -204
- package/lib/retry-after.test.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/retry.test.d.ts +0 -2
- package/lib/retry.test.d.ts.map +0 -1
- package/lib/retry.test.js +0 -407
- package/lib/retry.test.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/sdks.test.d.ts +0 -2
- package/lib/sdks.test.d.ts.map +0 -1
- package/lib/sdks.test.js +0 -76
- package/lib/sdks.test.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/lib/xml.js.map +0 -1
- package/lib/xml.test.d.ts +0 -2
- package/lib/xml.test.d.ts.map +0 -1
- package/lib/xml.test.js +0 -210
- package/lib/xml.test.js.map +0 -1
- package/src/codegen/boolean-string-enums.test.ts +0 -299
- package/src/codegen/defer-naming.test.ts +0 -113
- package/src/codegen/generated-at.test.ts +0 -89
- package/src/codegen/generator.test.ts +0 -137
- package/src/codegen/graphql-client.test.ts +0 -189
- package/src/codegen/openapi-binary.test.ts +0 -91
- package/src/codegen/patch-audit.test.ts +0 -160
- package/src/codegen/patches.test.ts +0 -188
- package/src/codegen/protocol-imports.test.ts +0 -39
- package/src/codegen/rewrite-operation-ids.test.ts +0 -472
- package/src/codegen/untagged-unions.test.ts +0 -85
- package/src/fixtures/xml/upstream-validator.json +0 -397
- package/src/graphql.test.ts +0 -198
- package/src/json-patch.test.ts +0 -77
- package/src/pagination.test.ts +0 -169
- package/src/protocol-http.test.ts +0 -276
- package/src/protocol-rest.test.ts +0 -703
- package/src/response-validation.test.ts +0 -89
- package/src/retry-after.test.ts +0 -267
- package/src/retry.test.ts +0 -566
- package/src/sdks.test.ts +0 -87
- package/src/xml.test.ts +0 -251
package/lib/protocol-rest.d.ts
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generic REST protocol factory for simple bearer/header-auth JSON APIs
|
|
3
|
-
* (Neon, Fly.io, PlanetScale, Supabase, Turso,
|
|
3
|
+
* (Neon, Fly.io, PlanetScale, Supabase, Turso, ...).
|
|
4
4
|
*
|
|
5
5
|
* `makeRestProtocol(options)` builds a core {@link API.Protocol} layer:
|
|
6
6
|
*
|
|
7
7
|
* request: credentials resolved from the CALLING fiber's context on every
|
|
8
|
-
* request (the layer itself is memoized per process
|
|
8
|
+
* request (the layer itself is memoized per process -- see
|
|
9
9
|
* `core/api`), auth headers + base URL from the provider's
|
|
10
10
|
* options, request built by `buildRequest` from
|
|
11
11
|
* `core/protocol-http` (deep-unwrapping any `Redacted` input
|
|
12
12
|
* values first)
|
|
13
13
|
*
|
|
14
|
-
* response: 2xx JSON
|
|
15
|
-
* key mapping (`mapKeys`)
|
|
14
|
+
* response: 2xx JSON -> optional `transformResponse` -> recursive wire->TS
|
|
15
|
+
* key mapping (`mapKeys`) -> in strict mode, a schema check
|
|
16
16
|
* failing with the provider's `parseError` (see
|
|
17
|
-
* `core/response-validation`)
|
|
18
|
-
* marked with {@link SensitiveValue}; non-2xx
|
|
17
|
+
* `core/response-validation`) -> `Redacted` wrapping of members
|
|
18
|
+
* marked with {@link SensitiveValue}; non-2xx -> typed error:
|
|
19
19
|
* per-op matcher classes (`matchTypedError`), then the status
|
|
20
20
|
* map (default `HTTP_STATUS_MAP`), then an `InternalServerError`
|
|
21
21
|
* for unmapped 5xx, then the provider's `unknownError` fallback
|
|
22
|
-
*
|
|
22
|
+
* -- with `retryAfter` durations stamped from the standard hint
|
|
23
23
|
* headers on retryable statuses.
|
|
24
24
|
*
|
|
25
25
|
* Anything provider-specific (envelope quirks, bespoke error codes) belongs
|
|
26
|
-
* in the provider package
|
|
26
|
+
* in the provider package -- via these options or a hand-written protocol.
|
|
27
27
|
*/
|
|
28
|
+
import * as Context from "effect/Context";
|
|
28
29
|
import * as Effect from "effect/Effect";
|
|
29
30
|
import * as Layer from "effect/Layer";
|
|
30
31
|
import type * as AST from "effect/SchemaAST";
|
|
31
32
|
import * as API from "./api.ts";
|
|
33
|
+
import { unwrapRedactedDeep } from "./plain-values.ts";
|
|
32
34
|
export declare const sensitiveValueSymbol: unique symbol;
|
|
33
35
|
/**
|
|
34
36
|
* Marks a string member as sensitive (mirrors `smithy.api#sensitive`).
|
|
@@ -55,19 +57,19 @@ export declare const RawResponseRoot: () => import("./trait.ts").Annotation;
|
|
|
55
57
|
export declare const binaryResponseSymbol: unique symbol;
|
|
56
58
|
/** Decode a successful raw binary response as bytes, without text conversion. */
|
|
57
59
|
export declare const BinaryResponse: () => import("./trait.ts").Annotation;
|
|
58
|
-
|
|
59
|
-
* Deep-unwrap `Redacted` values in an input (plain objects/arrays only —
|
|
60
|
-
* class instances, files, and binary payloads pass through untouched).
|
|
61
|
-
* Sensitive input members accept `string | Redacted<string>`; the wire wants
|
|
62
|
-
* the raw string.
|
|
63
|
-
*/
|
|
64
|
-
export declare const unwrapRedactedDeep: (value: unknown) => unknown;
|
|
60
|
+
export { unwrapRedactedDeep };
|
|
65
61
|
/**
|
|
66
62
|
* Walk a decoded (TS-named) value alongside its schema AST, wrapping members
|
|
67
63
|
* marked {@link SensitiveValue} in `Redacted`. Keys the schema doesn't model
|
|
68
64
|
* pass through verbatim.
|
|
69
65
|
*/
|
|
70
66
|
export declare const wrapSensitive: (ast: AST.AST, value: unknown) => unknown;
|
|
67
|
+
/**
|
|
68
|
+
* Whether any member reachable from `ast` is marked {@link SensitiveValue}.
|
|
69
|
+
* Parse errors for such an output withhold the response body (see
|
|
70
|
+
* {@link RestParseErrorInfo}); hand-written protocols use this to do the same.
|
|
71
|
+
*/
|
|
72
|
+
export declare const hasSensitiveMember: (ast: AST.AST) => boolean;
|
|
71
73
|
/** What the wire said about a failure, for the `unknownError` fallback. */
|
|
72
74
|
export interface RestErrorInfo {
|
|
73
75
|
readonly status: number;
|
|
@@ -78,8 +80,12 @@ export interface RestErrorInfo {
|
|
|
78
80
|
readonly body: unknown;
|
|
79
81
|
readonly headers: Record<string, string | undefined>;
|
|
80
82
|
}
|
|
81
|
-
/**
|
|
83
|
+
/**
|
|
84
|
+
* A 2xx body that failed strict validation, or a non-JSON 2xx body for an
|
|
85
|
+
* output with sensitive members, for the `parseError` option.
|
|
86
|
+
*/
|
|
82
87
|
export interface RestParseErrorInfo {
|
|
88
|
+
/** `"[REDACTED]"` when the output has sensitive members. */
|
|
83
89
|
readonly body: unknown;
|
|
84
90
|
readonly cause: unknown;
|
|
85
91
|
}
|
|
@@ -89,16 +95,18 @@ export interface RestErrorEnvelope {
|
|
|
89
95
|
}
|
|
90
96
|
export interface RestProtocolOptions<C> {
|
|
91
97
|
/**
|
|
92
|
-
* Resolve credentials ON THE CALLING FIBER
|
|
98
|
+
* Resolve credentials ON THE CALLING FIBER -- evaluated per request, never
|
|
93
99
|
* at layer build time, so context-provided credentials and token refreshes
|
|
94
|
-
* are picked up.
|
|
95
|
-
*
|
|
96
|
-
*
|
|
100
|
+
* are picked up. Either an effect, or -- for the distilled
|
|
101
|
+
* credentials-service convention -- the `Credentials` service key whose
|
|
102
|
+
* value is that effect (read from the calling fiber's context; an
|
|
103
|
+
* already-resolved `Effect.succeed` value costs no extra fiber step). Its
|
|
104
|
+
* error/requirement channels are
|
|
97
105
|
* erased at the protocol boundary (Protocol effects carry none) and
|
|
98
106
|
* reintroduced for callers by the generated `<Sdk>OpError` /
|
|
99
107
|
* `<Sdk>OpContext` annotations.
|
|
100
108
|
*/
|
|
101
|
-
readonly credentials: Effect.Effect<C, any, any
|
|
109
|
+
readonly credentials: Effect.Effect<C, any, any> | Context.Key<any, Effect.Effect<C, any, any>>;
|
|
102
110
|
/**
|
|
103
111
|
* API base URL from the resolved credentials. Receives the operation's
|
|
104
112
|
* route so APIs with more than one endpoint (e.g. S2's account vs
|
|
@@ -120,7 +128,7 @@ export interface RestProtocolOptions<C> {
|
|
|
120
128
|
*/
|
|
121
129
|
readonly errorEnvelope?: (body: unknown) => RestErrorEnvelope | undefined;
|
|
122
130
|
/**
|
|
123
|
-
* HTTP status
|
|
131
|
+
* HTTP status -> error class constructed as `new Cls({ message, retryAfter
|
|
124
132
|
* })`. Default: core `HTTP_STATUS_MAP`. Consulted after per-op typed error
|
|
125
133
|
* matching; unmapped 5xx fall back to `InternalServerError`, everything
|
|
126
134
|
* else to {@link unknownError}.
|
|
@@ -131,8 +139,11 @@ export interface RestProtocolOptions<C> {
|
|
|
131
139
|
/**
|
|
132
140
|
* The SDK's `<Sdk>ParseError`, raised when a 2xx body does not match the
|
|
133
141
|
* operation's output schema in strict mode (see
|
|
134
|
-
* `core/response-validation`)
|
|
135
|
-
*
|
|
142
|
+
* `core/response-validation`), and in every mode when a 2xx body for an
|
|
143
|
+
* output with sensitive members is not JSON. `body` is the parsed JSON, or
|
|
144
|
+
* the raw text when the body wasn't JSON, and `"[REDACTED]"` for an output
|
|
145
|
+
* with sensitive members; `cause` is the schema error, or `"Invalid JSON
|
|
146
|
+
* response"`.
|
|
136
147
|
*/
|
|
137
148
|
readonly parseError: (info: RestParseErrorInfo) => unknown;
|
|
138
149
|
/** Transform the parsed 2xx JSON before decoding (e.g. stripNulls). */
|
|
@@ -142,9 +153,11 @@ export interface RestProtocolOptions<C> {
|
|
|
142
153
|
/** Passed through to `buildRequest` (wire names for unmodeled input keys). */
|
|
143
154
|
readonly unknownKeyToWire?: (key: string) => string;
|
|
144
155
|
}
|
|
156
|
+
/** Whether {@link wrapSensitive} can change any value of `root`. Cached per schema. */
|
|
157
|
+
export declare const hasSensitive: (root: AST.AST) => boolean;
|
|
145
158
|
/**
|
|
146
159
|
* Build a `Layer<Protocol>` for a simple REST JSON API. Assign the result to
|
|
147
|
-
* a module-level const in the provider's `protocol.ts`
|
|
160
|
+
* a module-level const in the provider's `protocol.ts` -- `API.make` memoizes
|
|
148
161
|
* protocol layers by value identity.
|
|
149
162
|
*/
|
|
150
163
|
export declare const makeRestProtocol: <C>(options: RestProtocolOptions<C>) => Layer.Layer<API.Protocol>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol-rest.d.ts","sourceRoot":"","sources":["../src/protocol-rest.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"protocol-rest.d.ts","sourceRoot":"","sources":["../src/protocol-rest.ts"],"names":[],"mappings":"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"}
|
package/lib/protocol-rest.js
CHANGED
|
@@ -1,267 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Generic REST protocol factory for simple bearer/header-auth JSON APIs
|
|
3
|
-
* (Neon, Fly.io, PlanetScale, Supabase, Turso, …).
|
|
4
|
-
*
|
|
5
|
-
* `makeRestProtocol(options)` builds a core {@link API.Protocol} layer:
|
|
6
|
-
*
|
|
7
|
-
* request: credentials resolved from the CALLING fiber's context on every
|
|
8
|
-
* request (the layer itself is memoized per process — see
|
|
9
|
-
* `core/api`), auth headers + base URL from the provider's
|
|
10
|
-
* options, request built by `buildRequest` from
|
|
11
|
-
* `core/protocol-http` (deep-unwrapping any `Redacted` input
|
|
12
|
-
* values first)
|
|
13
|
-
*
|
|
14
|
-
* response: 2xx JSON → optional `transformResponse` → recursive wire→TS
|
|
15
|
-
* key mapping (`mapKeys`) → in strict mode, a schema check
|
|
16
|
-
* failing with the provider's `parseError` (see
|
|
17
|
-
* `core/response-validation`) → `Redacted` wrapping of members
|
|
18
|
-
* marked with {@link SensitiveValue}; non-2xx → typed error:
|
|
19
|
-
* per-op matcher classes (`matchTypedError`), then the status
|
|
20
|
-
* map (default `HTTP_STATUS_MAP`), then an `InternalServerError`
|
|
21
|
-
* for unmapped 5xx, then the provider's `unknownError` fallback
|
|
22
|
-
* — with `retryAfter` durations stamped from the standard hint
|
|
23
|
-
* headers on retryable statuses.
|
|
24
|
-
*
|
|
25
|
-
* Anything provider-specific (envelope quirks, bespoke error codes) belongs
|
|
26
|
-
* in the provider package — via these options or a hand-written protocol.
|
|
27
|
-
*/
|
|
28
|
-
import * as Effect from "effect/Effect";
|
|
29
|
-
import * as Layer from "effect/Layer";
|
|
30
|
-
import * as Redacted from "effect/Redacted";
|
|
31
|
-
import * as API from "./api.js";
|
|
32
|
-
import { HTTP_STATUS_MAP, InternalServerError } from "./errors.js";
|
|
33
|
-
import { buildRequest, getAnn, getPropAnn, getProps, isOpaqueValue, mapKeys, matchTypedError, resolveNode, } from "./protocol-http.js";
|
|
34
|
-
import { validateResponse } from "./response-validation.js";
|
|
35
|
-
import { parseRetryAfterForStatus } from "./retry-after.js";
|
|
36
|
-
import { httpSymbol, makeAnnotation } from "./trait.js";
|
|
37
|
-
// =============================================================================
|
|
38
|
-
// Traits
|
|
39
|
-
// =============================================================================
|
|
40
|
-
export const sensitiveValueSymbol = Symbol.for("@distilled.cloud/core/sensitive-value");
|
|
41
|
-
/**
|
|
42
|
-
* Marks a string member as sensitive (mirrors `smithy.api#sensitive`).
|
|
43
|
-
* The REST protocol wraps decoded values in `Redacted` on the way out and
|
|
44
|
-
* accepts `string | Redacted<string>` on the way in (unwrapped before
|
|
45
|
-
* serialization). Takes an ignored argument so generators can inline the
|
|
46
|
-
* smithy trait value (`T.SensitiveValue({})`).
|
|
47
|
-
*/
|
|
48
|
-
export const SensitiveValue = (_value) => makeAnnotation(sensitiveValueSymbol, true);
|
|
49
|
-
export const rawResponseSymbol = Symbol.for("@distilled.cloud/core/raw-response");
|
|
50
|
-
/**
|
|
51
|
-
* Marks the sole output member that carries a bare (array/scalar) response
|
|
52
|
-
* body (mirrors `com.distilled.openapi#rawResponse`).
|
|
53
|
-
*/
|
|
54
|
-
export const RawResponse = (_value) => makeAnnotation(rawResponseSymbol, true);
|
|
55
|
-
export const rawResponseRootSymbol = Symbol.for("@distilled.cloud/core/raw-response-root");
|
|
56
|
-
/**
|
|
57
|
-
* Marks a response schema whose ENTIRE value is the response body (the
|
|
58
|
-
* generator's `rootPipe` for synthesized bare-payload wrappers): the emitted
|
|
59
|
-
* response type IS the payload type and the protocol returns the mapped body
|
|
60
|
-
* directly.
|
|
61
|
-
*/
|
|
62
|
-
export const RawResponseRoot = () => makeAnnotation(rawResponseRootSymbol, true);
|
|
63
|
-
export const binaryResponseSymbol = Symbol.for("@distilled.cloud/core/binary-response");
|
|
64
|
-
/** Decode a successful raw binary response as bytes, without text conversion. */
|
|
65
|
-
export const BinaryResponse = () => makeAnnotation(binaryResponseSymbol, true);
|
|
66
|
-
// =============================================================================
|
|
67
|
-
// Value helpers
|
|
68
|
-
// =============================================================================
|
|
69
|
-
const isPlainObject = (v) => v !== null &&
|
|
70
|
-
typeof v === "object" &&
|
|
71
|
-
!Array.isArray(v) &&
|
|
72
|
-
!isOpaqueValue(v) &&
|
|
73
|
-
(Object.getPrototypeOf(v) === Object.prototype || Object.getPrototypeOf(v) === null);
|
|
74
|
-
/**
|
|
75
|
-
* Deep-unwrap `Redacted` values in an input (plain objects/arrays only —
|
|
76
|
-
* class instances, files, and binary payloads pass through untouched).
|
|
77
|
-
* Sensitive input members accept `string | Redacted<string>`; the wire wants
|
|
78
|
-
* the raw string.
|
|
79
|
-
*/
|
|
80
|
-
export const unwrapRedactedDeep = (value) => {
|
|
81
|
-
if (Redacted.isRedacted(value))
|
|
82
|
-
return Redacted.value(value);
|
|
83
|
-
if (Array.isArray(value))
|
|
84
|
-
return value.map(unwrapRedactedDeep);
|
|
85
|
-
if (isPlainObject(value)) {
|
|
86
|
-
const out = {};
|
|
87
|
-
for (const [k, v] of Object.entries(value)) {
|
|
88
|
-
if (v === undefined)
|
|
89
|
-
continue;
|
|
90
|
-
out[k] = unwrapRedactedDeep(v);
|
|
91
|
-
}
|
|
92
|
-
return out;
|
|
93
|
-
}
|
|
94
|
-
return value;
|
|
95
|
-
};
|
|
96
|
-
/**
|
|
97
|
-
* Walk a decoded (TS-named) value alongside its schema AST, wrapping members
|
|
98
|
-
* marked {@link SensitiveValue} in `Redacted`. Keys the schema doesn't model
|
|
99
|
-
* pass through verbatim.
|
|
100
|
-
*/
|
|
101
|
-
export const wrapSensitive = (ast, value) => {
|
|
102
|
-
if (value === null || typeof value !== "object" || isOpaqueValue(value)) {
|
|
103
|
-
return value;
|
|
104
|
-
}
|
|
105
|
-
const node = resolveNode(ast);
|
|
106
|
-
if (node._tag === "Union") {
|
|
107
|
-
// Redact every possible sensitive member, including in partial responses.
|
|
108
|
-
return node.types.reduce((redacted, arm) => wrapSensitive(arm, redacted), value);
|
|
109
|
-
}
|
|
110
|
-
if (node._tag === "Arrays") {
|
|
111
|
-
if (!Array.isArray(value))
|
|
112
|
-
return value;
|
|
113
|
-
const elem = node.rest?.[0];
|
|
114
|
-
return elem ? value.map((v) => wrapSensitive(elem, v)) : value;
|
|
115
|
-
}
|
|
116
|
-
if (node._tag === "Objects" && !Array.isArray(value)) {
|
|
117
|
-
const props = getProps(node);
|
|
118
|
-
if (props.length === 0)
|
|
119
|
-
return value;
|
|
120
|
-
const byName = new Map(props.map((p) => [String(p.name), p]));
|
|
121
|
-
const out = {};
|
|
122
|
-
for (const [k, v] of Object.entries(value)) {
|
|
123
|
-
if (v === undefined)
|
|
124
|
-
continue;
|
|
125
|
-
const prop = byName.get(k);
|
|
126
|
-
if (!prop) {
|
|
127
|
-
out[k] = v;
|
|
128
|
-
}
|
|
129
|
-
else if (getPropAnn(prop, sensitiveValueSymbol) !== undefined && typeof v === "string") {
|
|
130
|
-
out[k] = Redacted.make(v);
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
out[k] = wrapSensitive(prop.type, v);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return out;
|
|
137
|
-
}
|
|
138
|
-
return value;
|
|
139
|
-
};
|
|
140
|
-
const defaultErrorEnvelope = (body) => {
|
|
141
|
-
if (body === null || typeof body !== "object")
|
|
142
|
-
return undefined;
|
|
143
|
-
const b = body;
|
|
144
|
-
const code = typeof b.code === "string" || typeof b.code === "number" ? b.code : undefined;
|
|
145
|
-
const message = typeof b.message === "string" ? b.message : typeof b.error === "string" ? b.error : undefined;
|
|
146
|
-
return { code, message };
|
|
147
|
-
};
|
|
148
|
-
// Bridge: Protocol.decode is typed as Effect<unknown> (no error channel),
|
|
149
|
-
// but REST failures are real typed errors that operations re-surface via
|
|
150
|
-
// their `errors: [...]` lists. Fail with the instance and erase the type
|
|
151
|
-
// here; the generated operation annotations reintroduce it for callers.
|
|
152
|
-
const fail = (e) => Effect.fail(e);
|
|
153
|
-
/**
|
|
154
|
-
* Build a `Layer<Protocol>` for a simple REST JSON API. Assign the result to
|
|
155
|
-
* a module-level const in the provider's `protocol.ts` — `API.make` memoizes
|
|
156
|
-
* protocol layers by value identity.
|
|
157
|
-
*/
|
|
158
|
-
export const makeRestProtocol = (options) => {
|
|
159
|
-
const errorEnvelope = options.errorEnvelope ?? defaultErrorEnvelope;
|
|
160
|
-
const statusMap = options.statusMap ?? HTTP_STATUS_MAP;
|
|
161
|
-
const encode = ({ input, inputAst }) => Effect.gen(function* () {
|
|
162
|
-
const creds = yield* options.credentials;
|
|
163
|
-
const http = getAnn(inputAst, httpSymbol);
|
|
164
|
-
let baseUrl;
|
|
165
|
-
try {
|
|
166
|
-
baseUrl = options.baseUrl(creds, {
|
|
167
|
-
uri: http?.uri ?? "",
|
|
168
|
-
method: http?.method ?? "",
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
catch (e) {
|
|
172
|
-
// A baseUrl that refuses the route (missing scope in the
|
|
173
|
-
// credentials) fails the call with that error, typed for callers by
|
|
174
|
-
// the operation's declared error channel.
|
|
175
|
-
return yield* Effect.fail(e);
|
|
176
|
-
}
|
|
177
|
-
return buildRequest({
|
|
178
|
-
input: unwrapRedactedDeep(input),
|
|
179
|
-
inputAst,
|
|
180
|
-
baseUrl,
|
|
181
|
-
headers: options.headers(creds),
|
|
182
|
-
mapMemberHeader: options.mapMemberHeader,
|
|
183
|
-
unknownKeyToWire: options.unknownKeyToWire,
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
const decode = ({ response, outputAst, errors: errorClasses, }) => Effect.gen(function* () {
|
|
187
|
-
if (response.status >= 200 &&
|
|
188
|
-
response.status < 300 &&
|
|
189
|
-
getAnn(outputAst, binaryResponseSymbol) !== undefined) {
|
|
190
|
-
const bytes = yield* response.arrayBuffer.pipe(Effect.orDie);
|
|
191
|
-
return new Uint8Array(bytes);
|
|
192
|
-
}
|
|
193
|
-
// Read as text and parse tolerantly — error pages are often non-JSON.
|
|
194
|
-
const text = (yield* response.text.pipe(Effect.orDie)) ?? "";
|
|
195
|
-
if (process.env.DISTILLED_DEBUG_HTTP) {
|
|
196
|
-
console.error(`[distilled] <- ${response.status}`);
|
|
197
|
-
}
|
|
198
|
-
let json;
|
|
199
|
-
let nonJson = false;
|
|
200
|
-
if (text.trim().length > 0) {
|
|
201
|
-
try {
|
|
202
|
-
json = JSON.parse(text);
|
|
203
|
-
}
|
|
204
|
-
catch {
|
|
205
|
-
nonJson = true;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
const status = response.status;
|
|
209
|
-
const headers = response.headers;
|
|
210
|
-
if (status >= 400) {
|
|
211
|
-
const env = (nonJson ? undefined : errorEnvelope(json)) ?? {};
|
|
212
|
-
const message = env.message ?? (nonJson && text.trim() ? text.trim() : `HTTP ${status}`);
|
|
213
|
-
// 1. Per-operation typed error (matcher metadata on the class).
|
|
214
|
-
const typed = matchTypedError(errorClasses, status, [
|
|
215
|
-
{
|
|
216
|
-
code: typeof env.code === "number" ? env.code : undefined,
|
|
217
|
-
message,
|
|
218
|
-
},
|
|
219
|
-
], { body: nonJson ? text : json, headers });
|
|
220
|
-
if (typed !== undefined)
|
|
221
|
-
return yield* fail(typed);
|
|
222
|
-
// 2. Status-mapped class (retryAfter only stamps on retryable
|
|
223
|
-
// statuses — parseRetryAfterForStatus gates on that).
|
|
224
|
-
const StatusErrorClass = statusMap[status];
|
|
225
|
-
if (StatusErrorClass) {
|
|
226
|
-
return yield* fail(new StatusErrorClass({
|
|
227
|
-
message,
|
|
228
|
-
retryAfter: parseRetryAfterForStatus(status, headers),
|
|
229
|
-
}));
|
|
230
|
-
}
|
|
231
|
-
// 3. Unmapped 5xx (e.g. proxy-specific statuses) → retryable server
|
|
232
|
-
// error rather than the unknown fallback.
|
|
233
|
-
if (status >= 500) {
|
|
234
|
-
return yield* fail(new InternalServerError({
|
|
235
|
-
message,
|
|
236
|
-
retryAfter: parseRetryAfterForStatus(status, headers),
|
|
237
|
-
}));
|
|
238
|
-
}
|
|
239
|
-
// 4. Provider fallback.
|
|
240
|
-
return yield* fail(options.unknownError({
|
|
241
|
-
status,
|
|
242
|
-
code: env.code,
|
|
243
|
-
message,
|
|
244
|
-
body: nonJson ? text : json,
|
|
245
|
-
headers,
|
|
246
|
-
}));
|
|
247
|
-
}
|
|
248
|
-
// 2xx: the response body IS the payload (no envelope). Wire→TS key
|
|
249
|
-
// mapping is schema-driven; `RawResponseRoot` responses are the body
|
|
250
|
-
// verbatim (mapKeys handles arrays/scalars structurally either way).
|
|
251
|
-
// Strict mode (core/response-validation) checks the mapped body against
|
|
252
|
-
// the output schema — a non-JSON body reaches it as a string and fails
|
|
253
|
-
// there unless the operation's output is itself a string.
|
|
254
|
-
let body = nonJson ? text : (json ?? {});
|
|
255
|
-
if (options.transformResponse)
|
|
256
|
-
body = options.transformResponse(body);
|
|
257
|
-
const mapped = yield* validateResponse(outputAst, mapKeys(outputAst, body, "decode"), (cause) => options.parseError({ body: nonJson ? text : json, cause })).pipe(Effect.catch(fail));
|
|
258
|
-
return wrapSensitive(outputAst, mapped);
|
|
259
|
-
});
|
|
260
|
-
return Layer.succeed(API.Protocol, API.Protocol.of({
|
|
261
|
-
// Erase encode's credentials requirement (resolved on the calling
|
|
262
|
-
// fiber; see RestProtocolOptions.credentials).
|
|
263
|
-
encode: (args) => encode(args),
|
|
264
|
-
decode,
|
|
265
|
-
}));
|
|
266
|
-
};
|
|
267
|
-
//# sourceMappingURL=protocol-rest.js.map
|
|
1
|
+
export { protocol_rest$BinaryResponse as BinaryResponse, protocol_rest$RawResponse as RawResponse, protocol_rest$RawResponseRoot as RawResponseRoot, protocol_rest$SensitiveValue as SensitiveValue, protocol_rest$binaryResponseSymbol as binaryResponseSymbol, protocol_rest$hasSensitive as hasSensitive, protocol_rest$hasSensitiveMember as hasSensitiveMember, protocol_rest$makeRestProtocol as makeRestProtocol, protocol_rest$rawResponseRootSymbol as rawResponseRootSymbol, protocol_rest$rawResponseSymbol as rawResponseSymbol, protocol_rest$sensitiveValueSymbol as sensitiveValueSymbol, protocol_rest$unwrapRedactedDeep as unwrapRedactedDeep, protocol_rest$wrapSensitive as wrapSensitive } from "./_bundle/rest.js";
|
package/lib/query.js
CHANGED
|
@@ -1,27 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Query combinators for GraphQL SDKs.
|
|
3
|
-
*
|
|
4
|
-
* Import this module next to a generated client:
|
|
5
|
-
*
|
|
6
|
-
* import { Query } from "@distilled.cloud/core/query"
|
|
7
|
-
* import { Railway } from "@distilled.cloud/railway"
|
|
8
|
-
*
|
|
9
|
-
* `Query.fn` takes a **builder** (plain function, not `Effect.gen`) that
|
|
10
|
-
* returns a plan of `Query<Value>` and `Effect` values. It walks Map /
|
|
11
|
-
* Filter / FlatMap / Of, posts one GraphQL document, then interprets the
|
|
12
|
-
* plan. Roots (`Railway.me`) come from the generated SDK; they are not
|
|
13
|
-
* combinators.
|
|
14
|
-
*
|
|
15
|
-
* @see {@link file://./graphql.ts} for the algebra, compiler, and transport.
|
|
16
|
-
*/
|
|
17
|
-
import { filterQuery, flatMapQuery, itemsQuery, mapQuery, ofQuery, pagesQuery, queryFn, } from "./graphql.js";
|
|
18
|
-
export const Query = {
|
|
19
|
-
fn: queryFn,
|
|
20
|
-
filter: filterQuery,
|
|
21
|
-
map: mapQuery,
|
|
22
|
-
flatMap: flatMapQuery,
|
|
23
|
-
of: ofQuery,
|
|
24
|
-
pages: pagesQuery,
|
|
25
|
-
items: itemsQuery,
|
|
26
|
-
};
|
|
27
|
-
//# sourceMappingURL=query.js.map
|
|
1
|
+
export { query$Query as Query } from "./_bundle/graphql.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The strict response checker: validates a decoded response against its
|
|
3
|
+
* output AST without Effect Schema.
|
|
4
|
+
*
|
|
5
|
+
* Generated output schemas are lite nodes (`schema-lite-impl.ts`); the
|
|
6
|
+
* checker accepts what Effect Schema's decoder accepted for the schema
|
|
7
|
+
* `toEffect` built from them (`response-validation-strict.test.ts` compares
|
|
8
|
+
* the two on random schemas and values). Each AST node compiles once to a
|
|
9
|
+
* closure, so a check walks the value only.
|
|
10
|
+
*
|
|
11
|
+
* Two modes: `additionalProperties` lets keys an object does not model
|
|
12
|
+
* through (Effect's default), `strict` (`exact`) rejects them (Effect's
|
|
13
|
+
* `onExcessProperty: "error"`). In both, an index signature covers only the
|
|
14
|
+
* keys the object does not declare (OpenAPI `additionalProperties`), and an
|
|
15
|
+
* empty struct accepts any object (generated code's opaque output).
|
|
16
|
+
*
|
|
17
|
+
* Hand-written protocols may pass real Effect ASTs. Their structure is
|
|
18
|
+
* checked the same way; refinement `checks` run, but a real `Declaration`
|
|
19
|
+
* with type parameters and `TemplateLiteral` contents are not checked.
|
|
20
|
+
*/
|
|
21
|
+
import type * as AST from "effect/SchemaAST";
|
|
22
|
+
/** A response that does not match its output schema (the `cause` of a strict `<Sdk>ParseError`). */
|
|
23
|
+
export declare class SchemaMismatch extends Error {
|
|
24
|
+
readonly name = "SchemaError";
|
|
25
|
+
/** Where in the response the mismatch is, outermost key first. */
|
|
26
|
+
readonly path: ReadonlyArray<PropertyKey>;
|
|
27
|
+
/** What the schema expected there, e.g. `string`. */
|
|
28
|
+
readonly expected: string;
|
|
29
|
+
readonly actual: unknown;
|
|
30
|
+
constructor(path: ReadonlyArray<PropertyKey>, expected: string, actual: unknown);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* `mismatch` with its offending value replaced by `"[REDACTED]"`, for a parse
|
|
34
|
+
* error that withholds the body (the value may be a secret).
|
|
35
|
+
*/
|
|
36
|
+
export declare const withholdActual: (mismatch: SchemaMismatch) => SchemaMismatch;
|
|
37
|
+
/**
|
|
38
|
+
* Validate `input` synchronously: `undefined` when it matches, else the
|
|
39
|
+
* mismatch.
|
|
40
|
+
*/
|
|
41
|
+
export type Decode = (input: unknown) => SchemaMismatch | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The checker for an output AST: compiled once per node and mode, then
|
|
44
|
+
* reused. `exact` also rejects keys an object does not model (the `strict`
|
|
45
|
+
* mode); without it they pass (`additionalProperties`).
|
|
46
|
+
*/
|
|
47
|
+
export declare const decoderFor: (ast: AST.AST, exact?: boolean) => Decode;
|
|
48
|
+
//# sourceMappingURL=response-validation-strict.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-validation-strict.d.ts","sourceRoot":"","sources":["../src/response-validation-strict.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,KAAK,KAAK,GAAG,MAAM,kBAAkB,CAAC;AAG7C,oGAAoG;AACpG,qBAAa,cAAe,SAAQ,KAAK;IACvC,SAAkB,IAAI,iBAAiB;IACvC,kEAAkE;IAClE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;IAC1C,qDAAqD;IACrD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,YAAY,IAAI,EAAE,aAAa,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAO9E;CACF;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,aAAc,cAAc,KAAG,cACU,CAAC;AAErE;;;GAGG;AACH,MAAM,MAAM,MAAM,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,cAAc,GAAG,SAAS,CAAC;AA2PpE;;;;GAIG;AACH,eAAO,MAAM,UAAU,QAAS,GAAG,CAAC,GAAG,sBAAkB,MAgBxD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { response_validation_strict$SchemaMismatch as SchemaMismatch, response_validation_strict$decoderFor as decoderFor, response_validation_strict$withholdActual as withholdActual } from "./_bundle/base.js";
|
|
@@ -7,14 +7,22 @@
|
|
|
7
7
|
* lenient (default): the response is returned as the protocol read it.
|
|
8
8
|
* A body that does not match the declared output type
|
|
9
9
|
* (a missing member, a wrong primitive, a non-JSON
|
|
10
|
-
* body) still succeeds
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
10
|
+
* body) still succeeds, except that core's REST
|
|
11
|
+
* protocol, Stripe and Fly.io fail a non-JSON body for
|
|
12
|
+
* an output with sensitive members in every mode.
|
|
13
|
+
* additionalProperties: the response is checked against the output
|
|
14
|
+
* schema, and a mismatch fails the call with the SDK's
|
|
15
|
+
* `<Sdk>ParseError` (AWS: `ParseError`). Keys the
|
|
16
|
+
* schema does not model are allowed and kept.
|
|
17
|
+
* strict: as `additionalProperties`, and a key the schema does
|
|
18
|
+
* not model also fails the call, at any depth. Open
|
|
19
|
+
* objects (an index signature, e.g. OpenAPI
|
|
20
|
+
* `additionalProperties`) and empty structs (opaque
|
|
21
|
+
* outputs) still accept any key.
|
|
14
22
|
*
|
|
15
23
|
* The mode is one {@link ResponseValidation} reference keyed by a string, so
|
|
16
|
-
* a single `Layer` switches every distilled SDK in a program
|
|
17
|
-
* Cloudflare, Neon,
|
|
24
|
+
* a single `Layer` switches every distilled SDK in a program -- AWS,
|
|
25
|
+
* Cloudflare, Neon, ... -- at once, and nests like any other Effect service:
|
|
18
26
|
*
|
|
19
27
|
* ```ts
|
|
20
28
|
* import { ResponseValidation } from "@distilled.cloud/core";
|
|
@@ -31,37 +39,47 @@
|
|
|
31
39
|
import * as Context from "effect/Context";
|
|
32
40
|
import * as Effect from "effect/Effect";
|
|
33
41
|
import * as Layer from "effect/Layer";
|
|
34
|
-
import * as Schema from "effect/Schema";
|
|
35
42
|
import type * as AST from "effect/SchemaAST";
|
|
36
|
-
|
|
43
|
+
import { type Decode, type SchemaMismatch } from "./response-validation-strict.ts";
|
|
44
|
+
export { SchemaMismatch, withholdActual } from "./response-validation-strict.ts";
|
|
45
|
+
export type Mode = "lenient" | "additionalProperties" | "strict";
|
|
37
46
|
/**
|
|
38
47
|
* The active validation mode. Read by protocols at call time on the calling
|
|
39
|
-
* fiber; `lenient` unless
|
|
48
|
+
* fiber; `lenient` unless an {@link additionalProperties} or {@link strict}
|
|
49
|
+
* layer is provided.
|
|
40
50
|
*/
|
|
41
51
|
export declare const ResponseValidation: Context.Reference<Mode>;
|
|
42
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* The checker the validating modes use (`exact`: reject unmodeled keys). The
|
|
54
|
+
* `additionalProperties` and `strict` layers provide it, so the checker is
|
|
55
|
+
* bundled only with programs that validate.
|
|
56
|
+
*/
|
|
57
|
+
export declare const StrictDecoder: Context.Reference<((ast: AST.AST, exact: boolean) => Decode) | undefined>;
|
|
58
|
+
/**
|
|
59
|
+
* Check every 2xx response against its output schema and fail on a
|
|
60
|
+
* mismatch, including any key the schema does not model.
|
|
61
|
+
*/
|
|
43
62
|
export declare const strict: Layer.Layer<never>;
|
|
63
|
+
/**
|
|
64
|
+
* Check every 2xx response against its output schema and fail on a
|
|
65
|
+
* mismatch; keys the schema does not model are allowed.
|
|
66
|
+
*/
|
|
67
|
+
export declare const additionalProperties: Layer.Layer<never>;
|
|
44
68
|
/** Return 2xx responses as read, without checking them against the schema. */
|
|
45
69
|
export declare const lenient: Layer.Layer<never>;
|
|
46
|
-
/** Whether the calling fiber
|
|
47
|
-
export declare const
|
|
70
|
+
/** Whether the calling fiber validates responses (any mode but lenient). */
|
|
71
|
+
export declare const isValidating: Effect.Effect<boolean>;
|
|
48
72
|
/**
|
|
49
73
|
* For a 2xx body the protocol could not read into the shape it transforms
|
|
50
|
-
* (e.g. invalid JSON): fail with `error`
|
|
74
|
+
* (e.g. invalid JSON): fail with `error` when validating, succeed with
|
|
51
75
|
* `asRead` (usually the body text) in lenient mode.
|
|
52
76
|
*/
|
|
53
|
-
export declare const
|
|
77
|
+
export declare const failUnlessLenient: <A, E>(error: E, asRead: A) => Effect.Effect<A, E>;
|
|
54
78
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* mismatch, fails with `onError(schemaError)` — protocols pass their SDK's
|
|
59
|
-
* `<Sdk>ParseError` constructor. On success the ORIGINAL value is returned
|
|
60
|
-
* (members the schema does not model are kept), so switching modes never
|
|
61
|
-
* changes what a successful call returns.
|
|
62
|
-
*
|
|
63
|
-
* `value` must already be in the schema's shape (TS member names), i.e.
|
|
64
|
-
* after any wire→TS key mapping and before `Redacted` wrapping.
|
|
79
|
+
* The schema error for `value` against `outputAst` when validating,
|
|
80
|
+
* `undefined` when it matches or the mode is lenient. Synchronous, for
|
|
81
|
+
* protocols that already hold the calling fiber's context.
|
|
65
82
|
*/
|
|
66
|
-
export declare const
|
|
83
|
+
export declare const checkResponse: (context: Context.Context<any>, outputAst: AST.AST, value: unknown) => SchemaMismatch | undefined;
|
|
84
|
+
export declare const validateResponse: <E>(outputAst: AST.AST, value: unknown, onError: (cause: SchemaMismatch) => E) => Effect.Effect<unknown, E>;
|
|
67
85
|
//# sourceMappingURL=response-validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response-validation.d.ts","sourceRoot":"","sources":["../src/response-validation.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"response-validation.d.ts","sourceRoot":"","sources":["../src/response-validation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,KAAK,GAAG,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,KAAK,MAAM,EAAc,KAAK,cAAc,EAAE,MAAM,iCAAiC,CAAC;AAE/F,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AAEjF,MAAM,MAAM,IAAI,GAAG,SAAS,GAAG,sBAAsB,GAAG,QAAQ,CAAC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,yBAG9B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,2BACjB,GAAG,CAAC,GAAG,SAAS,OAAO,KAAK,MAAM,cACmD,CAAC;AAoB/F;;;GAGG;AACH,eAAO,MAAM,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAMrC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,oBAAoB,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAMnD,CAAC;AAEF,8EAA8E;AAC9E,eAAO,MAAM,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAEtC,CAAC;AAEF,4EAA4E;AAC5E,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,CAG/C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,EAAE,CAAC,SAAS,CAAC,UAAU,CAAC,KAAG,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAG7E,CAAC;AAEJ;;;;GAIG;AACH,eAAO,MAAM,aAAa,YACf,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,aAClB,GAAG,CAAC,GAAG,SACX,OAAO,KACb,cAAc,GAAG,SAQnB,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,CAAC,aACrB,GAAG,CAAC,GAAG,SACX,OAAO,WACL,CAAC,KAAK,EAAE,cAAc,KAAK,CAAC,KACpC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAIvB,CAAC"}
|