@distilled.cloud/core 1.0.0-rc.2 → 1.0.0-rc.20
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/LICENSE +201 -0
- 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/grpc.js +2 -0
- package/lib/_bundle/grpc.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/annotations.d.ts +17 -0
- package/lib/annotations.d.ts.map +1 -0
- package/lib/annotations.js +2 -0
- package/lib/annotations.js.map +1 -0
- package/lib/api.d.ts +174 -18
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -178
- package/lib/category.d.ts +4 -4
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +1 -280
- package/lib/chunks.d.ts +3 -0
- package/lib/chunks.d.ts.map +1 -0
- package/lib/chunks.js +1 -0
- package/lib/config.d.ts +68 -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/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 +64 -72
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -186
- package/lib/graphql.d.ts +403 -0
- package/lib/graphql.d.ts.map +1 -0
- package/lib/graphql.js +1 -0
- package/lib/graphql.types.d.ts +12 -0
- package/lib/graphql.types.d.ts.map +1 -0
- package/lib/graphql.types.js +1 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-http2.js +6 -0
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
- package/lib/grpc-transport-cloudflare-sockets.js +5 -0
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
- package/lib/grpc-transport-node-http2.d.ts +19 -0
- package/lib/grpc-transport-node-http2.d.ts.map +1 -0
- package/lib/grpc-transport-node-http2.js +2 -0
- package/lib/grpc-transport-node-http2.js.map +1 -0
- package/lib/grpc-transport.d.ts +76 -0
- package/lib/grpc-transport.d.ts.map +1 -0
- package/lib/grpc-transport.js +1 -0
- package/lib/hpack.d.ts +34 -0
- package/lib/hpack.d.ts.map +1 -0
- package/lib/hpack.js +2 -0
- package/lib/hpack.js.map +1 -0
- package/lib/index.d.ts +32 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +17 -0
- package/lib/pagination.d.ts +101 -24
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -182
- 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/protobuf.d.ts +92 -0
- package/lib/protobuf.d.ts.map +1 -0
- package/lib/protobuf.js +1 -0
- package/lib/protocol-grpc.d.ts +86 -0
- package/lib/protocol-grpc.d.ts.map +1 -0
- package/lib/protocol-grpc.js +1 -0
- package/lib/protocol-http.d.ts +102 -23
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -554
- package/lib/protocol-rest.d.ts +120 -50
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -242
- package/lib/query.d.ts +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +1 -0
- 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 +86 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +1 -0
- package/lib/retry-after.d.ts +3 -3
- package/lib/retry-after.d.ts.map +1 -1
- 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 -210
- 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 +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 +21 -7
- package/lib/schema.d.ts.map +1 -1
- package/lib/schema.js +1 -48
- package/lib/sdk-error.d.ts +27 -0
- package/lib/sdk-error.d.ts.map +1 -0
- package/lib/sdk-error.js +1 -0
- package/lib/sensitive.d.ts +28 -0
- package/lib/sensitive.d.ts.map +1 -0
- package/lib/sensitive.js +2 -0
- package/lib/sensitive.js.map +1 -0
- package/lib/telemetry-debug.d.ts +6 -0
- package/lib/telemetry-debug.d.ts.map +1 -0
- package/lib/telemetry-debug.js +2 -0
- package/lib/telemetry-debug.js.map +1 -0
- package/lib/telemetry-hooks.d.ts +32 -0
- package/lib/telemetry-hooks.d.ts.map +1 -0
- package/lib/telemetry-hooks.js +2 -0
- package/lib/telemetry-hooks.js.map +1 -0
- package/lib/telemetry-host-rules.d.ts +23 -0
- package/lib/telemetry-host-rules.d.ts.map +1 -0
- package/lib/telemetry-host-rules.js +2 -0
- package/lib/telemetry-host-rules.js.map +1 -0
- package/lib/telemetry-lambda-context.d.ts +17 -0
- package/lib/telemetry-lambda-context.d.ts.map +1 -0
- package/lib/telemetry-lambda-context.js +2 -0
- package/lib/telemetry-lambda-context.js.map +1 -0
- package/lib/telemetry-message-context.d.ts +52 -0
- package/lib/telemetry-message-context.d.ts.map +1 -0
- package/lib/telemetry-message-context.js +2 -0
- package/lib/telemetry-message-context.js.map +1 -0
- package/lib/telemetry-metrics.d.ts +6 -0
- package/lib/telemetry-metrics.d.ts.map +1 -0
- package/lib/telemetry-metrics.js +2 -0
- package/lib/telemetry-metrics.js.map +1 -0
- package/lib/telemetry-name-template.d.ts +13 -0
- package/lib/telemetry-name-template.d.ts.map +1 -0
- package/lib/telemetry-name-template.js +2 -0
- package/lib/telemetry-name-template.js.map +1 -0
- package/lib/telemetry-stream.d.ts +11 -0
- package/lib/telemetry-stream.d.ts.map +1 -0
- package/lib/telemetry-stream.js +2 -0
- package/lib/telemetry-stream.js.map +1 -0
- package/lib/telemetry-xray.d.ts +28 -0
- package/lib/telemetry-xray.d.ts.map +1 -0
- package/lib/telemetry-xray.js +2 -0
- package/lib/telemetry-xray.js.map +1 -0
- package/lib/telemetry.d.ts +288 -0
- package/lib/telemetry.d.ts.map +1 -0
- package/lib/telemetry.js +3 -0
- package/lib/telemetry.js.map +1 -0
- package/lib/trait.d.ts +71 -20
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -107
- package/lib/unknown-error.d.ts +18 -0
- package/lib/unknown-error.d.ts.map +1 -0
- package/lib/unknown-error.js +1 -0
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +1 -0
- package/package.json +13 -13
- package/src/annotations.ts +53 -0
- package/src/api.ts +1370 -200
- package/src/category.ts +76 -106
- package/src/chunks.ts +30 -0
- package/src/config.ts +245 -0
- package/src/crypto.ts +202 -0
- package/src/error-category.ts +16 -29
- package/src/errors.ts +75 -75
- package/src/graphql.ts +1778 -0
- package/src/graphql.types.ts +105 -0
- package/src/grpc-transport-cloudflare-http2.ts +494 -0
- package/src/grpc-transport-cloudflare-sockets.ts +299 -0
- package/src/grpc-transport-node-http2.ts +234 -0
- package/src/grpc-transport.ts +118 -0
- package/src/hpack.ts +331 -0
- package/src/index.ts +31 -0
- package/src/pagination.ts +368 -94
- package/src/plain-values.ts +41 -0
- package/src/protobuf.ts +1332 -0
- package/src/protocol-grpc.ts +641 -0
- package/src/protocol-http.ts +622 -322
- package/src/protocol-rest.ts +440 -227
- package/src/query.ts +39 -0
- package/src/response-validation-strict.ts +327 -0
- package/src/response-validation.ts +161 -0
- package/src/retry-after.ts +7 -13
- package/src/retry.ts +96 -110
- 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 +259 -15
- package/src/sdk-error.ts +33 -0
- package/src/sensitive.ts +154 -0
- package/src/telemetry-debug.ts +179 -0
- package/src/telemetry-hooks.ts +33 -0
- package/src/telemetry-host-rules.ts +39 -0
- package/src/telemetry-lambda-context.ts +46 -0
- package/src/telemetry-message-context.ts +170 -0
- package/src/telemetry-metrics.ts +69 -0
- package/src/telemetry-name-template.ts +26 -0
- package/src/telemetry-stream.ts +100 -0
- package/src/telemetry-xray.ts +66 -0
- package/src/telemetry.ts +832 -0
- package/src/trait.ts +106 -67
- package/src/unknown-error.ts +38 -0
- package/src/xml.ts +346 -0
- package/lib/api.js.map +0 -1
- package/lib/category.js.map +0 -1
- package/lib/codegen/cli.d.ts +0 -29
- package/lib/codegen/cli.d.ts.map +0 -1
- package/lib/codegen/cli.js +0 -165
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/emit.d.ts +0 -129
- package/lib/codegen/emit.d.ts.map +0 -1
- package/lib/codegen/emit.js +0 -105
- package/lib/codegen/emit.js.map +0 -1
- package/lib/codegen/format.d.ts +0 -23
- package/lib/codegen/format.d.ts.map +0 -1
- package/lib/codegen/format.js +0 -28
- package/lib/codegen/format.js.map +0 -1
- package/lib/codegen/generator.d.ts +0 -334
- package/lib/codegen/generator.d.ts.map +0 -1
- package/lib/codegen/generator.js +0 -691
- package/lib/codegen/generator.js.map +0 -1
- package/lib/codegen/graph.d.ts +0 -36
- package/lib/codegen/graph.d.ts.map +0 -1
- package/lib/codegen/graph.js +0 -136
- package/lib/codegen/graph.js.map +0 -1
- package/lib/codegen/members.d.ts +0 -25
- package/lib/codegen/members.d.ts.map +0 -1
- package/lib/codegen/members.js +0 -55
- package/lib/codegen/members.js.map +0 -1
- package/lib/codegen/naming.d.ts +0 -29
- package/lib/codegen/naming.d.ts.map +0 -1
- package/lib/codegen/naming.js +0 -74
- package/lib/codegen/naming.js.map +0 -1
- package/lib/codegen/openapi-cli.d.ts +0 -38
- package/lib/codegen/openapi-cli.d.ts.map +0 -1
- package/lib/codegen/openapi-cli.js +0 -107
- package/lib/codegen/openapi-cli.js.map +0 -1
- package/lib/codegen/openapi.d.ts +0 -115
- package/lib/codegen/openapi.d.ts.map +0 -1
- package/lib/codegen/openapi.js +0 -1220
- package/lib/codegen/openapi.js.map +0 -1
- package/lib/codegen/operations.d.ts +0 -24
- package/lib/codegen/operations.d.ts.map +0 -1
- package/lib/codegen/operations.js +0 -56
- package/lib/codegen/operations.js.map +0 -1
- package/lib/codegen/pagination.d.ts +0 -39
- package/lib/codegen/pagination.d.ts.map +0 -1
- package/lib/codegen/pagination.js +0 -33
- package/lib/codegen/pagination.js.map +0 -1
- package/lib/codegen/prelude.d.ts +0 -15
- package/lib/codegen/prelude.d.ts.map +0 -1
- package/lib/codegen/prelude.js +0 -60
- package/lib/codegen/prelude.js.map +0 -1
- package/lib/error-category.js.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/json-patch.d.ts +0 -44
- package/lib/json-patch.d.ts.map +0 -1
- package/lib/json-patch.js +0 -174
- package/lib/json-patch.js.map +0 -1
- package/lib/pagination.js.map +0 -1
- package/lib/protocol-http.js.map +0 -1
- package/lib/protocol-rest.js.map +0 -1
- package/lib/retry-after.js.map +0 -1
- package/lib/retry.js.map +0 -1
- package/lib/schema.js.map +0 -1
- package/lib/trait.js.map +0 -1
- package/src/codegen/cli.ts +0 -268
- package/src/codegen/emit.ts +0 -207
- package/src/codegen/format.ts +0 -47
- package/src/codegen/generator.ts +0 -1153
- package/src/codegen/graph.ts +0 -151
- package/src/codegen/members.ts +0 -71
- package/src/codegen/naming.ts +0 -86
- package/src/codegen/openapi-cli.ts +0 -166
- package/src/codegen/openapi.ts +0 -1450
- package/src/codegen/operations.ts +0 -76
- package/src/codegen/pagination.ts +0 -71
- package/src/codegen/prelude.ts +0 -70
- package/src/json-patch.ts +0 -222
package/lib/protocol-rest.d.ts
CHANGED
|
@@ -1,36 +1,17 @@
|
|
|
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`) → `Redacted` wrapping of members
|
|
16
|
-
* marked with {@link SensitiveValue}; non-2xx → typed error:
|
|
17
|
-
* per-op matcher classes (`matchTypedError`), then the status
|
|
18
|
-
* map (default `HTTP_STATUS_MAP`), then an `InternalServerError`
|
|
19
|
-
* for unmapped 5xx, then the provider's `unknownError` fallback
|
|
20
|
-
* — with `retryAfter` durations stamped from the standard hint
|
|
21
|
-
* headers on retryable statuses.
|
|
22
|
-
*
|
|
23
|
-
* Anything provider-specific (envelope quirks, bespoke error codes) belongs
|
|
24
|
-
* in the provider package — via these options or a hand-written protocol.
|
|
25
|
-
*/
|
|
1
|
+
import * as Context from "effect/Context";
|
|
26
2
|
import * as Effect from "effect/Effect";
|
|
27
3
|
import * as Layer from "effect/Layer";
|
|
28
4
|
import type * as AST from "effect/SchemaAST";
|
|
5
|
+
import { rawResponseRootSymbol } from "./annotations.ts";
|
|
29
6
|
import * as API from "./api.ts";
|
|
30
|
-
|
|
7
|
+
import { unwrapRedactedDeep } from "./plain-values.ts";
|
|
8
|
+
import { sensitiveValueSymbol, wrapSensitive } from "./sensitive.ts";
|
|
9
|
+
import type { UnknownErrorProps } from "./unknown-error.ts";
|
|
10
|
+
export { sensitiveValueSymbol };
|
|
31
11
|
/**
|
|
32
|
-
* Marks a string member as sensitive (mirrors
|
|
33
|
-
* The REST protocol wraps decoded values in
|
|
12
|
+
* Marks a string member, map value or list element as sensitive (mirrors
|
|
13
|
+
* `smithy.api#sensitive`). The REST protocol wraps decoded values in
|
|
14
|
+
* `Redacted` on the way out (an untyped `S.Unknown` value whole) and
|
|
34
15
|
* accepts `string | Redacted<string>` on the way in (unwrapped before
|
|
35
16
|
* serialization). Takes an ignored argument so generators can inline the
|
|
36
17
|
* smithy trait value (`T.SensitiveValue({})`).
|
|
@@ -42,27 +23,26 @@ export declare const rawResponseSymbol: unique symbol;
|
|
|
42
23
|
* body (mirrors `com.distilled.openapi#rawResponse`).
|
|
43
24
|
*/
|
|
44
25
|
export declare const RawResponse: (_value?: unknown) => import("./trait.ts").Annotation;
|
|
45
|
-
export declare const rawResponseRootSymbol: unique symbol;
|
|
46
26
|
/**
|
|
47
27
|
* Marks a response schema whose ENTIRE value is the response body (the
|
|
48
28
|
* generator's `rootPipe` for synthesized bare-payload wrappers): the emitted
|
|
49
29
|
* response type IS the payload type and the protocol returns the mapped body
|
|
50
30
|
* directly.
|
|
51
31
|
*/
|
|
32
|
+
export { rawResponseRootSymbol };
|
|
52
33
|
export declare const RawResponseRoot: () => import("./trait.ts").Annotation;
|
|
34
|
+
export declare const binaryResponseSymbol: unique symbol;
|
|
35
|
+
/** Decode a successful raw binary response as bytes, without text conversion. */
|
|
36
|
+
export declare const BinaryResponse: () => import("./trait.ts").Annotation;
|
|
37
|
+
export { unwrapRedactedDeep };
|
|
38
|
+
export { wrapSensitive };
|
|
53
39
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*/
|
|
59
|
-
export declare const unwrapRedactedDeep: (value: unknown) => unknown;
|
|
60
|
-
/**
|
|
61
|
-
* Walk a decoded (TS-named) value alongside its schema AST, wrapping members
|
|
62
|
-
* marked {@link SensitiveValue} in `Redacted`. Keys the schema doesn't model
|
|
63
|
-
* pass through verbatim.
|
|
40
|
+
* Whether any member, map value or list element reachable from `ast` is
|
|
41
|
+
* marked {@link SensitiveValue}. Such an output is decoded through
|
|
42
|
+
* {@link wrapSensitive}, and its parse errors withhold the response body (see
|
|
43
|
+
* {@link checkSuccess}, which every REST-style protocol's success path uses).
|
|
64
44
|
*/
|
|
65
|
-
export declare const
|
|
45
|
+
export declare const hasSensitiveMember: (ast: AST.AST) => boolean;
|
|
66
46
|
/** What the wire said about a failure, for the `unknownError` fallback. */
|
|
67
47
|
export interface RestErrorInfo {
|
|
68
48
|
readonly status: number;
|
|
@@ -73,24 +53,51 @@ export interface RestErrorInfo {
|
|
|
73
53
|
readonly body: unknown;
|
|
74
54
|
readonly headers: Record<string, string | undefined>;
|
|
75
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* The core {@link UnknownErrorProps} of a failure, for an SDK's
|
|
58
|
+
* `unknownError`: `new UnknownNeonError(unknownErrorProps(info))`. A number
|
|
59
|
+
* code becomes its string.
|
|
60
|
+
*/
|
|
61
|
+
export declare const unknownErrorProps: (info: RestErrorInfo) => UnknownErrorProps;
|
|
62
|
+
/**
|
|
63
|
+
* A 2xx body that failed strict validation, or a non-JSON 2xx body for an
|
|
64
|
+
* output with sensitive members, for the `parseError` option.
|
|
65
|
+
*/
|
|
66
|
+
export interface RestParseErrorInfo {
|
|
67
|
+
/** `"[REDACTED]"` when the output has sensitive members. */
|
|
68
|
+
readonly body: unknown;
|
|
69
|
+
readonly cause: unknown;
|
|
70
|
+
}
|
|
76
71
|
export interface RestErrorEnvelope {
|
|
77
72
|
readonly code?: string | number;
|
|
78
73
|
readonly message?: string;
|
|
79
74
|
}
|
|
80
75
|
export interface RestProtocolOptions<C> {
|
|
81
76
|
/**
|
|
82
|
-
* Resolve credentials ON THE CALLING FIBER
|
|
77
|
+
* Resolve credentials ON THE CALLING FIBER -- evaluated per request, never
|
|
83
78
|
* at layer build time, so context-provided credentials and token refreshes
|
|
84
|
-
* are picked up.
|
|
85
|
-
*
|
|
86
|
-
*
|
|
79
|
+
* are picked up. Either an effect, or -- for the distilled
|
|
80
|
+
* credentials-service convention -- the `Credentials` service key whose
|
|
81
|
+
* value is that effect (read from the calling fiber's context; an
|
|
82
|
+
* already-resolved `Effect.succeed` value costs no extra fiber step). Its
|
|
83
|
+
* error/requirement channels are
|
|
87
84
|
* erased at the protocol boundary (Protocol effects carry none) and
|
|
88
85
|
* reintroduced for callers by the generated `<Sdk>OpError` /
|
|
89
86
|
* `<Sdk>OpContext` annotations.
|
|
90
87
|
*/
|
|
91
|
-
readonly credentials: Effect.Effect<C, any, any
|
|
92
|
-
/**
|
|
93
|
-
|
|
88
|
+
readonly credentials: Effect.Effect<C, any, any> | Context.Key<any, Effect.Effect<C, any, any>>;
|
|
89
|
+
/**
|
|
90
|
+
* API base URL from the resolved credentials. Receives the operation's
|
|
91
|
+
* route so APIs with more than one endpoint (e.g. S2's account vs
|
|
92
|
+
* per-basin hosts, chosen by path) can route per request; single-endpoint
|
|
93
|
+
* providers ignore the argument. A THROWN error (e.g. a required scope
|
|
94
|
+
* missing from the credentials) is caught and surfaced on the calling
|
|
95
|
+
* effect's error channel rather than dying as a defect.
|
|
96
|
+
*/
|
|
97
|
+
readonly baseUrl: (credentials: C, target: {
|
|
98
|
+
readonly uri: string;
|
|
99
|
+
readonly method: string;
|
|
100
|
+
}) => string;
|
|
94
101
|
/** Auth (and any fixed) headers from the resolved credentials. */
|
|
95
102
|
readonly headers: (credentials: C) => Record<string, string>;
|
|
96
103
|
/**
|
|
@@ -100,24 +107,87 @@ export interface RestProtocolOptions<C> {
|
|
|
100
107
|
*/
|
|
101
108
|
readonly errorEnvelope?: (body: unknown) => RestErrorEnvelope | undefined;
|
|
102
109
|
/**
|
|
103
|
-
* HTTP status
|
|
110
|
+
* HTTP status -> error class constructed as `new Cls({ message, retryAfter
|
|
104
111
|
* })`. Default: core `HTTP_STATUS_MAP`. Consulted after per-op typed error
|
|
105
112
|
* matching; unmapped 5xx fall back to `InternalServerError`, everything
|
|
106
113
|
* else to {@link unknownError}.
|
|
107
114
|
*/
|
|
108
115
|
readonly statusMap?: Readonly<Record<number, new (args: any) => any>>;
|
|
109
|
-
/**
|
|
116
|
+
/**
|
|
117
|
+
* The provider's own reading of a failure, consulted before per-op typed
|
|
118
|
+
* matching and the status map: return the error to fail with, or
|
|
119
|
+
* `undefined` to decode the failure as usual. For rules a status alone
|
|
120
|
+
* can't express, e.g. a 403 that is a rate limit because of its headers.
|
|
121
|
+
*/
|
|
122
|
+
readonly classifyError?: (info: RestErrorInfo) => unknown;
|
|
123
|
+
/**
|
|
124
|
+
* Fallback error for failures nothing else matched: the SDK's
|
|
125
|
+
* `Unknown<Sdk>Error`, built from {@link unknownErrorProps}. Only non-5xx
|
|
126
|
+
* statuses reach it (unmapped 5xx are `InternalServerError`), so the class
|
|
127
|
+
* carries no retryable category.
|
|
128
|
+
*/
|
|
110
129
|
readonly unknownError: (info: RestErrorInfo) => unknown;
|
|
130
|
+
/**
|
|
131
|
+
* The SDK's `<Sdk>ParseError`, raised when a 2xx body does not match the
|
|
132
|
+
* operation's output schema in strict mode (see
|
|
133
|
+
* `core/response-validation`), and in every mode when a 2xx body for an
|
|
134
|
+
* output with sensitive members is not JSON. `body` is the parsed JSON, or
|
|
135
|
+
* the raw text when the body wasn't JSON, and `"[REDACTED]"` for an output
|
|
136
|
+
* with sensitive members; `cause` is the schema error, or `"Invalid JSON
|
|
137
|
+
* response"`.
|
|
138
|
+
*/
|
|
139
|
+
readonly parseError: (info: RestParseErrorInfo) => unknown;
|
|
111
140
|
/** Transform the parsed 2xx JSON before decoding (e.g. stripNulls). */
|
|
112
141
|
readonly transformResponse?: (body: unknown) => unknown;
|
|
113
142
|
/** Passed through to `buildRequest` (member-header transforms). */
|
|
114
143
|
readonly mapMemberHeader?: (name: string, value: string) => string;
|
|
115
144
|
/** Passed through to `buildRequest` (wire names for unmodeled input keys). */
|
|
116
145
|
readonly unknownKeyToWire?: (key: string) => string;
|
|
146
|
+
/** Passed through to `buildRequest`: send `{}` when a bodied call has no body members. */
|
|
147
|
+
readonly emptyJsonBody?: boolean;
|
|
117
148
|
}
|
|
149
|
+
/** A 2xx body a protocol read, for {@link checkSuccess}. */
|
|
150
|
+
export interface SuccessBody {
|
|
151
|
+
/** The value to check and return: the body mapped onto the output's TS names. */
|
|
152
|
+
readonly value: unknown;
|
|
153
|
+
/** What a parse error reports as `body` for an output without sensitive members. */
|
|
154
|
+
readonly body: unknown;
|
|
155
|
+
/** Whether the body was not JSON. */
|
|
156
|
+
readonly nonJson: boolean;
|
|
157
|
+
}
|
|
158
|
+
/** The outcome of {@link checkSuccess}: the value, or the parse error's fields. */
|
|
159
|
+
export type SuccessCheck = {
|
|
160
|
+
readonly ok: true;
|
|
161
|
+
readonly value: unknown;
|
|
162
|
+
} | {
|
|
163
|
+
readonly ok: false;
|
|
164
|
+
readonly error: RestParseErrorInfo;
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Check a 2xx body against the operation's output schema and deliver its
|
|
168
|
+
* sensitive values as `Redacted`. Every REST-style protocol ends its success
|
|
169
|
+
* path here, so all of them treat a body that can hold a secret alike:
|
|
170
|
+
*
|
|
171
|
+
* - The checked value comes back through {@link wrapSensitive}.
|
|
172
|
+
* - A mismatch (validating modes, see `core/response-validation`) reports
|
|
173
|
+
* `read.body` and the schema error.
|
|
174
|
+
* - For an output with sensitive members ({@link hasSensitiveMember}), a
|
|
175
|
+
* non-JSON body fails in every mode with `"Invalid JSON response"`, and a
|
|
176
|
+
* mismatch reports `body: "[REDACTED]"` and the schema error without the
|
|
177
|
+
* value it found (`withholdActual`).
|
|
178
|
+
*
|
|
179
|
+
* Synchronous, for protocols that already hold the calling fiber's context;
|
|
180
|
+
* {@link validateSuccess} is the `Effect` form.
|
|
181
|
+
*/
|
|
182
|
+
export declare const checkSuccess: (context: Context.Context<any>, outputAst: AST.AST, read: SuccessBody) => SuccessCheck;
|
|
183
|
+
/**
|
|
184
|
+
* {@link checkSuccess} on the calling fiber, failing with `parseError` (the
|
|
185
|
+
* SDK's `<Sdk>ParseError`) built from the withheld-or-not fields.
|
|
186
|
+
*/
|
|
187
|
+
export declare const validateSuccess: <E>(outputAst: AST.AST, read: SuccessBody, parseError: (info: RestParseErrorInfo) => E) => Effect.Effect<unknown, E>;
|
|
118
188
|
/**
|
|
119
189
|
* Build a `Layer<Protocol>` for a simple REST JSON API. Assign the result to
|
|
120
|
-
* a module-level const in the provider's `protocol.ts`
|
|
190
|
+
* a module-level const in the provider's `protocol.ts` -- `API.make` memoizes
|
|
121
191
|
* protocol layers by value identity.
|
|
122
192
|
*/
|
|
123
193
|
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
|
|
1
|
+
{"version":3,"file":"protocol-rest.d.ts","sourceRoot":"","sources":["../src/protocol-rest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AAIxC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,KAAK,GAAG,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,qBAAqB,EAAe,MAAM,kBAAkB,CAAC;AACt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AAG,CAAC,GAAG,QACZ,WAAW,cACL,CAAC,IAAI,EAAE,kBAAkB,KAAK,CAAC,KAC1C,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAIvB,CAAC;AA0CL;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,WAAW,mBAAmB,CAAC,CAAC,CAAC,KAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CA2N7F,CAAC"}
|
package/lib/protocol-rest.js
CHANGED
|
@@ -1,242 +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`) → `Redacted` wrapping of members
|
|
16
|
-
* marked with {@link SensitiveValue}; non-2xx → typed error:
|
|
17
|
-
* per-op matcher classes (`matchTypedError`), then the status
|
|
18
|
-
* map (default `HTTP_STATUS_MAP`), then an `InternalServerError`
|
|
19
|
-
* for unmapped 5xx, then the provider's `unknownError` fallback
|
|
20
|
-
* — with `retryAfter` durations stamped from the standard hint
|
|
21
|
-
* headers on retryable statuses.
|
|
22
|
-
*
|
|
23
|
-
* Anything provider-specific (envelope quirks, bespoke error codes) belongs
|
|
24
|
-
* in the provider package — via these options or a hand-written protocol.
|
|
25
|
-
*/
|
|
26
|
-
import * as Effect from "effect/Effect";
|
|
27
|
-
import * as Layer from "effect/Layer";
|
|
28
|
-
import * as Redacted from "effect/Redacted";
|
|
29
|
-
import * as API from "./api.js";
|
|
30
|
-
import { makeAnnotation } from "./trait.js";
|
|
31
|
-
import { buildRequest, getPropAnn, getProps, isOpaqueValue, mapKeys, matchTypedError, resolveNode, } from "./protocol-http.js";
|
|
32
|
-
import { HTTP_STATUS_MAP, InternalServerError } from "./errors.js";
|
|
33
|
-
import { parseRetryAfterForStatus } from "./retry-after.js";
|
|
34
|
-
// =============================================================================
|
|
35
|
-
// Traits
|
|
36
|
-
// =============================================================================
|
|
37
|
-
export const sensitiveValueSymbol = Symbol.for("@distilled.cloud/core/sensitive-value");
|
|
38
|
-
/**
|
|
39
|
-
* Marks a string member as sensitive (mirrors `smithy.api#sensitive`).
|
|
40
|
-
* The REST protocol wraps decoded values in `Redacted` on the way out and
|
|
41
|
-
* accepts `string | Redacted<string>` on the way in (unwrapped before
|
|
42
|
-
* serialization). Takes an ignored argument so generators can inline the
|
|
43
|
-
* smithy trait value (`T.SensitiveValue({})`).
|
|
44
|
-
*/
|
|
45
|
-
export const SensitiveValue = (_value) => makeAnnotation(sensitiveValueSymbol, true);
|
|
46
|
-
export const rawResponseSymbol = Symbol.for("@distilled.cloud/core/raw-response");
|
|
47
|
-
/**
|
|
48
|
-
* Marks the sole output member that carries a bare (array/scalar) response
|
|
49
|
-
* body (mirrors `com.distilled.openapi#rawResponse`).
|
|
50
|
-
*/
|
|
51
|
-
export const RawResponse = (_value) => makeAnnotation(rawResponseSymbol, true);
|
|
52
|
-
export const rawResponseRootSymbol = Symbol.for("@distilled.cloud/core/raw-response-root");
|
|
53
|
-
/**
|
|
54
|
-
* Marks a response schema whose ENTIRE value is the response body (the
|
|
55
|
-
* generator's `rootPipe` for synthesized bare-payload wrappers): the emitted
|
|
56
|
-
* response type IS the payload type and the protocol returns the mapped body
|
|
57
|
-
* directly.
|
|
58
|
-
*/
|
|
59
|
-
export const RawResponseRoot = () => makeAnnotation(rawResponseRootSymbol, true);
|
|
60
|
-
// =============================================================================
|
|
61
|
-
// Value helpers
|
|
62
|
-
// =============================================================================
|
|
63
|
-
const isPlainObject = (v) => v !== null &&
|
|
64
|
-
typeof v === "object" &&
|
|
65
|
-
!Array.isArray(v) &&
|
|
66
|
-
!isOpaqueValue(v) &&
|
|
67
|
-
(Object.getPrototypeOf(v) === Object.prototype ||
|
|
68
|
-
Object.getPrototypeOf(v) === null);
|
|
69
|
-
/**
|
|
70
|
-
* Deep-unwrap `Redacted` values in an input (plain objects/arrays only —
|
|
71
|
-
* class instances, files, and binary payloads pass through untouched).
|
|
72
|
-
* Sensitive input members accept `string | Redacted<string>`; the wire wants
|
|
73
|
-
* the raw string.
|
|
74
|
-
*/
|
|
75
|
-
export const unwrapRedactedDeep = (value) => {
|
|
76
|
-
if (Redacted.isRedacted(value))
|
|
77
|
-
return Redacted.value(value);
|
|
78
|
-
if (Array.isArray(value))
|
|
79
|
-
return value.map(unwrapRedactedDeep);
|
|
80
|
-
if (isPlainObject(value)) {
|
|
81
|
-
const out = {};
|
|
82
|
-
for (const [k, v] of Object.entries(value)) {
|
|
83
|
-
if (v === undefined)
|
|
84
|
-
continue;
|
|
85
|
-
out[k] = unwrapRedactedDeep(v);
|
|
86
|
-
}
|
|
87
|
-
return out;
|
|
88
|
-
}
|
|
89
|
-
return value;
|
|
90
|
-
};
|
|
91
|
-
/**
|
|
92
|
-
* Walk a decoded (TS-named) value alongside its schema AST, wrapping members
|
|
93
|
-
* marked {@link SensitiveValue} in `Redacted`. Keys the schema doesn't model
|
|
94
|
-
* pass through verbatim.
|
|
95
|
-
*/
|
|
96
|
-
export const wrapSensitive = (ast, value) => {
|
|
97
|
-
if (value === null || typeof value !== "object" || isOpaqueValue(value)) {
|
|
98
|
-
return value;
|
|
99
|
-
}
|
|
100
|
-
const node = resolveNode(ast);
|
|
101
|
-
if (node._tag === "Arrays") {
|
|
102
|
-
if (!Array.isArray(value))
|
|
103
|
-
return value;
|
|
104
|
-
const elem = node.rest?.[0];
|
|
105
|
-
return elem ? value.map((v) => wrapSensitive(elem, v)) : value;
|
|
106
|
-
}
|
|
107
|
-
if (node._tag === "Objects" && !Array.isArray(value)) {
|
|
108
|
-
const props = getProps(node);
|
|
109
|
-
if (props.length === 0)
|
|
110
|
-
return value;
|
|
111
|
-
const byName = new Map(props.map((p) => [String(p.name), p]));
|
|
112
|
-
const out = {};
|
|
113
|
-
for (const [k, v] of Object.entries(value)) {
|
|
114
|
-
if (v === undefined)
|
|
115
|
-
continue;
|
|
116
|
-
const prop = byName.get(k);
|
|
117
|
-
if (!prop) {
|
|
118
|
-
out[k] = v;
|
|
119
|
-
}
|
|
120
|
-
else if (getPropAnn(prop, sensitiveValueSymbol) !== undefined &&
|
|
121
|
-
typeof v === "string") {
|
|
122
|
-
out[k] = Redacted.make(v);
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
out[k] = wrapSensitive(prop.type, v);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return out;
|
|
129
|
-
}
|
|
130
|
-
return value;
|
|
131
|
-
};
|
|
132
|
-
const defaultErrorEnvelope = (body) => {
|
|
133
|
-
if (body === null || typeof body !== "object")
|
|
134
|
-
return undefined;
|
|
135
|
-
const b = body;
|
|
136
|
-
const code = typeof b.code === "string" || typeof b.code === "number"
|
|
137
|
-
? b.code
|
|
138
|
-
: undefined;
|
|
139
|
-
const message = typeof b.message === "string"
|
|
140
|
-
? b.message
|
|
141
|
-
: typeof b.error === "string"
|
|
142
|
-
? b.error
|
|
143
|
-
: undefined;
|
|
144
|
-
return { code, message };
|
|
145
|
-
};
|
|
146
|
-
// Bridge: Protocol.decode is typed as Effect<unknown> (no error channel),
|
|
147
|
-
// but REST failures are real typed errors that operations re-surface via
|
|
148
|
-
// their `errors: [...]` lists. Fail with the instance and erase the type
|
|
149
|
-
// here; the generated operation annotations reintroduce it for callers.
|
|
150
|
-
const fail = (e) => Effect.fail(e);
|
|
151
|
-
/**
|
|
152
|
-
* Build a `Layer<Protocol>` for a simple REST JSON API. Assign the result to
|
|
153
|
-
* a module-level const in the provider's `protocol.ts` — `API.make` memoizes
|
|
154
|
-
* protocol layers by value identity.
|
|
155
|
-
*/
|
|
156
|
-
export const makeRestProtocol = (options) => {
|
|
157
|
-
const errorEnvelope = options.errorEnvelope ?? defaultErrorEnvelope;
|
|
158
|
-
const statusMap = options.statusMap ?? HTTP_STATUS_MAP;
|
|
159
|
-
const encode = ({ input, inputAst, }) => Effect.gen(function* () {
|
|
160
|
-
const creds = yield* options.credentials;
|
|
161
|
-
return buildRequest({
|
|
162
|
-
input: unwrapRedactedDeep(input),
|
|
163
|
-
inputAst,
|
|
164
|
-
baseUrl: options.baseUrl(creds),
|
|
165
|
-
headers: options.headers(creds),
|
|
166
|
-
mapMemberHeader: options.mapMemberHeader,
|
|
167
|
-
unknownKeyToWire: options.unknownKeyToWire,
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
const decode = ({ response, outputAst, errors: errorClasses, }) => Effect.gen(function* () {
|
|
171
|
-
// Read as text and parse tolerantly — error pages are often non-JSON.
|
|
172
|
-
const text = (yield* response.text.pipe(Effect.orDie)) ?? "";
|
|
173
|
-
if (process.env.DISTILLED_DEBUG_HTTP) {
|
|
174
|
-
console.error(`[distilled] <- ${response.status} ${text.slice(0, 400)}`);
|
|
175
|
-
}
|
|
176
|
-
let json;
|
|
177
|
-
let nonJson = false;
|
|
178
|
-
if (text.trim().length > 0) {
|
|
179
|
-
try {
|
|
180
|
-
json = JSON.parse(text);
|
|
181
|
-
}
|
|
182
|
-
catch {
|
|
183
|
-
nonJson = true;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
const status = response.status;
|
|
187
|
-
const headers = response.headers;
|
|
188
|
-
if (status >= 400) {
|
|
189
|
-
const env = (nonJson ? undefined : errorEnvelope(json)) ?? {};
|
|
190
|
-
const message = env.message ??
|
|
191
|
-
(nonJson && text.trim() ? text.trim() : `HTTP ${status}`);
|
|
192
|
-
// 1. Per-operation typed error (matcher metadata on the class).
|
|
193
|
-
const typed = matchTypedError(errorClasses, status, [
|
|
194
|
-
{
|
|
195
|
-
code: typeof env.code === "number" ? env.code : undefined,
|
|
196
|
-
message,
|
|
197
|
-
},
|
|
198
|
-
]);
|
|
199
|
-
if (typed !== undefined)
|
|
200
|
-
return yield* fail(typed);
|
|
201
|
-
// 2. Status-mapped class (retryAfter only stamps on retryable
|
|
202
|
-
// statuses — parseRetryAfterForStatus gates on that).
|
|
203
|
-
const StatusErrorClass = statusMap[status];
|
|
204
|
-
if (StatusErrorClass) {
|
|
205
|
-
return yield* fail(new StatusErrorClass({
|
|
206
|
-
message,
|
|
207
|
-
retryAfter: parseRetryAfterForStatus(status, headers),
|
|
208
|
-
}));
|
|
209
|
-
}
|
|
210
|
-
// 3. Unmapped 5xx (e.g. proxy-specific statuses) → retryable server
|
|
211
|
-
// error rather than the unknown fallback.
|
|
212
|
-
if (status >= 500) {
|
|
213
|
-
return yield* fail(new InternalServerError({
|
|
214
|
-
message,
|
|
215
|
-
retryAfter: parseRetryAfterForStatus(status, headers),
|
|
216
|
-
}));
|
|
217
|
-
}
|
|
218
|
-
// 4. Provider fallback.
|
|
219
|
-
return yield* fail(options.unknownError({
|
|
220
|
-
status,
|
|
221
|
-
code: env.code,
|
|
222
|
-
message,
|
|
223
|
-
body: nonJson ? text : json,
|
|
224
|
-
headers,
|
|
225
|
-
}));
|
|
226
|
-
}
|
|
227
|
-
// 2xx: the response body IS the payload (no envelope). Wire→TS key
|
|
228
|
-
// mapping is schema-driven; `RawResponseRoot` responses are the body
|
|
229
|
-
// verbatim (mapKeys handles arrays/scalars structurally either way).
|
|
230
|
-
let body = nonJson ? text : (json ?? {});
|
|
231
|
-
if (options.transformResponse)
|
|
232
|
-
body = options.transformResponse(body);
|
|
233
|
-
return wrapSensitive(outputAst, mapKeys(outputAst, body, "decode"));
|
|
234
|
-
});
|
|
235
|
-
return Layer.succeed(API.Protocol, API.Protocol.of({
|
|
236
|
-
// Erase encode's credentials requirement (resolved on the calling
|
|
237
|
-
// fiber; see RestProtocolOptions.credentials).
|
|
238
|
-
encode: (args) => encode(args),
|
|
239
|
-
decode,
|
|
240
|
-
}));
|
|
241
|
-
};
|
|
242
|
-
//# 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$checkSuccess as checkSuccess, 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$unknownErrorProps as unknownErrorProps, protocol_rest$unwrapRedactedDeep as unwrapRedactedDeep, protocol_rest$validateSuccess as validateSuccess, protocol_rest$wrapSensitive as wrapSensitive } from "./_bundle/rest.js";
|
package/lib/query.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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, type Query as QueryType } from "./graphql.ts";
|
|
18
|
+
export type Query<Value, Error = never> = QueryType<Value, Error>;
|
|
19
|
+
export type { PlanError, QueryError, UnwrapPlan } from "./graphql.ts";
|
|
20
|
+
export declare const Query: {
|
|
21
|
+
fn: typeof queryFn;
|
|
22
|
+
filter: typeof filterQuery;
|
|
23
|
+
map: typeof mapQuery;
|
|
24
|
+
flatMap: typeof flatMapQuery;
|
|
25
|
+
of: typeof ofQuery;
|
|
26
|
+
pages: typeof pagesQuery;
|
|
27
|
+
items: typeof itemsQuery;
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=query.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EACL,WAAW,EACX,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,OAAO,EACP,UAAU,EACV,OAAO,EACP,KAAK,KAAK,IAAI,SAAS,EACxB,MAAM,cAAc,CAAC;AAEtB,MAAM,MAAM,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,IAAI,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAClE,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEtE,eAAO,MAAM,KAAK;IAChB,EAAE;IACF,MAAM;IACN,GAAG;IACH,OAAO;IACP,EAAE;IACF,KAAK;IACL,KAAK;CACN,CAAC"}
|
package/lib/query.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
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";
|