@distilled.cloud/core 1.0.0-rc.20 → 1.0.0-rc.21
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/README.md +33 -0
- package/lib/_bundle/aws.js +1 -1
- package/lib/_bundle/aws.js.map +1 -1
- package/lib/_bundle/base.js +2 -2
- package/lib/_bundle/base.js.map +1 -1
- package/lib/_bundle/graphql.js +2 -2
- package/lib/_bundle/graphql.js.map +1 -1
- package/lib/_bundle/grpc.js +2 -1
- package/lib/_bundle/grpc.js.map +1 -1
- package/lib/_bundle/rest.js +1 -1
- package/lib/_bundle/rest.js.map +1 -1
- package/lib/annotations.d.ts.map +1 -1
- package/lib/api.d.ts +71 -58
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -1
- package/lib/category.d.ts +15 -12
- package/lib/category.d.ts.map +1 -1
- package/lib/chunks.d.ts.map +1 -1
- package/lib/config.d.ts +13 -0
- package/lib/config.d.ts.map +1 -1
- package/lib/crypto.d.ts.map +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/errors.d.ts +35 -2
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -1
- package/lib/graphql.d.ts +21 -4
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.types.d.ts +0 -1
- package/lib/graphql.types.d.ts.map +1 -1
- package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -1
- package/lib/grpc-transport-cloudflare-http2.js +2 -2
- package/lib/grpc-transport-cloudflare-http2.js.map +1 -1
- package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -1
- package/lib/grpc-transport-cloudflare-sockets.js +4 -4
- package/lib/grpc-transport-cloudflare-sockets.js.map +1 -1
- package/lib/grpc-transport-node-http2.d.ts.map +1 -1
- package/lib/grpc-transport-node-http2.js +1 -1
- package/lib/grpc-transport-node-http2.js.map +1 -1
- package/lib/grpc-transport.d.ts +53 -18
- package/lib/grpc-transport.d.ts.map +1 -1
- package/lib/grpc-transport.js +1 -1
- package/lib/hpack.d.ts.map +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/max-message-bytes.d.ts +42 -0
- package/lib/max-message-bytes.d.ts.map +1 -0
- package/lib/max-message-bytes.js +2 -0
- package/lib/max-message-bytes.js.map +1 -0
- package/lib/ndjson.d.ts +3 -0
- package/lib/ndjson.d.ts.map +1 -0
- package/lib/ndjson.js +2 -0
- package/lib/ndjson.js.map +1 -0
- package/lib/pagination.d.ts +4 -1
- package/lib/pagination.d.ts.map +1 -1
- package/lib/plain-values.d.ts +11 -0
- package/lib/plain-values.d.ts.map +1 -1
- package/lib/plain-values.js +1 -1
- package/lib/plain-values.js.map +1 -1
- package/lib/protobuf.d.ts +12 -19
- package/lib/protobuf.d.ts.map +1 -1
- package/lib/protobuf.js +1 -1
- package/lib/protocol-grpc.d.ts +47 -10
- package/lib/protocol-grpc.d.ts.map +1 -1
- package/lib/protocol-grpc.js +1 -1
- package/lib/protocol-http.d.ts +110 -8
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +1 -1
- package/lib/protocol-rest.d.ts +161 -37
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +1 -1
- package/lib/protocol.d.ts +113 -0
- package/lib/protocol.d.ts.map +1 -0
- package/lib/protocol.js +0 -0
- package/lib/query.d.ts.map +1 -1
- package/lib/response-validation-strict.d.ts.map +1 -1
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/retry-after.d.ts +1 -1
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-state.d.ts +26 -0
- package/lib/retry-state.d.ts.map +1 -0
- package/lib/retry-state.js +2 -0
- package/lib/retry-state.js.map +1 -0
- package/lib/retry.d.ts +100 -20
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -1
- package/lib/schema-lite-impl.d.ts +25 -4
- package/lib/schema-lite-impl.d.ts.map +1 -1
- package/lib/schema-lite-impl.js +1 -1
- package/lib/schema-lite-typed.d.ts.map +1 -1
- package/lib/schema-lite.d.ts +1 -0
- package/lib/schema-lite.d.ts.map +1 -1
- package/lib/schema-lite.js +1 -1
- package/lib/schema.d.ts.map +1 -1
- package/lib/sdk-error.d.ts +4 -0
- package/lib/sdk-error.d.ts.map +1 -1
- package/lib/send.d.ts +37 -0
- package/lib/send.d.ts.map +1 -0
- package/lib/send.js +2 -0
- package/lib/send.js.map +1 -0
- package/lib/sensitive.d.ts.map +1 -1
- package/lib/sensitive.js +1 -1
- package/lib/sensitive.js.map +1 -1
- package/lib/telemetry-debug.d.ts.map +1 -1
- package/lib/telemetry-debug.js +1 -1
- package/lib/telemetry-debug.js.map +1 -1
- package/lib/telemetry-hooks.d.ts.map +1 -1
- package/lib/telemetry-host-rules.d.ts.map +1 -1
- package/lib/telemetry-lambda-context.d.ts.map +1 -1
- package/lib/telemetry-message-context.d.ts.map +1 -1
- package/lib/telemetry-metrics.d.ts.map +1 -1
- package/lib/telemetry-metrics.js +1 -1
- package/lib/telemetry-metrics.js.map +1 -1
- package/lib/telemetry-name-template.d.ts.map +1 -1
- package/lib/telemetry-stream.d.ts.map +1 -1
- package/lib/telemetry-stream.js +1 -1
- package/lib/telemetry-stream.js.map +1 -1
- package/lib/telemetry-xray.d.ts.map +1 -1
- package/lib/telemetry.d.ts +29 -0
- package/lib/telemetry.d.ts.map +1 -1
- package/lib/telemetry.js +2 -2
- package/lib/telemetry.js.map +1 -1
- package/lib/token-cache.d.ts +31 -0
- package/lib/token-cache.d.ts.map +1 -0
- package/lib/token-cache.js +2 -0
- package/lib/token-cache.js.map +1 -0
- package/lib/trait.d.ts +24 -1
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -1
- package/lib/unknown-error.d.ts.map +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/api.ts +402 -209
- package/src/category.ts +21 -28
- package/src/config.ts +17 -1
- package/src/errors.ts +24 -2
- package/src/graphql.ts +65 -26
- package/src/graphql.types.ts +15 -0
- package/src/grpc-transport-cloudflare-http2.ts +374 -299
- package/src/grpc-transport-cloudflare-sockets.ts +150 -81
- package/src/grpc-transport-node-http2.ts +119 -46
- package/src/grpc-transport.ts +211 -42
- package/src/max-message-bytes.ts +58 -0
- package/src/ndjson.ts +55 -0
- package/src/pagination.ts +26 -15
- package/src/plain-values.ts +30 -1
- package/src/protobuf.ts +40 -5
- package/src/protocol-grpc.ts +518 -348
- package/src/protocol-http.ts +474 -100
- package/src/protocol-rest.ts +580 -140
- package/src/protocol.ts +150 -0
- package/src/response-validation-strict.ts +9 -3
- package/src/retry-after.ts +1 -1
- package/src/retry-state.ts +105 -0
- package/src/retry.ts +176 -47
- package/src/schema-lite-impl.ts +81 -10
- package/src/schema-lite.ts +1 -0
- package/src/sdk-error.ts +4 -0
- package/src/send.ts +156 -0
- package/src/sensitive.ts +49 -15
- package/src/telemetry.ts +2 -34
- package/src/token-cache.ts +117 -0
- package/src/trait.ts +34 -1
package/src/category.ts
CHANGED
|
@@ -254,35 +254,28 @@ const isHttpTransportError = (error: unknown): boolean =>
|
|
|
254
254
|
HttpClientError.isHttpClientError(error) && error.reason._tag === "TransportError";
|
|
255
255
|
|
|
256
256
|
/**
|
|
257
|
-
* Check if an error
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* -
|
|
261
|
-
*
|
|
262
|
-
* -
|
|
263
|
-
*
|
|
264
|
-
* -
|
|
265
|
-
*
|
|
266
|
-
* - Effect `HttpClientError` with `reason._tag === "TransportError"`
|
|
267
|
-
* (
|
|
268
|
-
*
|
|
257
|
+
* Check if an error should be retried automatically. Only errors that say
|
|
258
|
+
* so are retried; an HTTP status alone does not make an error retryable:
|
|
259
|
+
*
|
|
260
|
+
* - errors marked with `withRetryable()` (Smithy `@retryable`, a patch, or
|
|
261
|
+
* the SDK's own error classes);
|
|
262
|
+
* - the `RetryableError` and `ThrottlingError` categories (`ThrottlingError`
|
|
263
|
+
* is HTTP 429, the server's own "try again later");
|
|
264
|
+
* - the `NetworkError` category: the request failed on the way, before any
|
|
265
|
+
* server answered;
|
|
266
|
+
* - an Effect `HttpClientError` with `reason._tag === "TransportError"`
|
|
267
|
+
* (connect timeout, socket reset, DNS failure, premature stream close),
|
|
268
|
+
* which no spec can mark.
|
|
269
|
+
*
|
|
270
|
+
* `ServerError`, `TimeoutError` and `LockedError` describe an
|
|
271
|
+
* error; they do not retry it.
|
|
269
272
|
*/
|
|
270
|
-
export const isTransientError = (error: unknown): boolean =>
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return (
|
|
277
|
-
hasCategory(error, RetryableError) ||
|
|
278
|
-
hasCategory(error, ThrottlingError) ||
|
|
279
|
-
hasCategory(error, ServerError) ||
|
|
280
|
-
hasCategory(error, NetworkError) ||
|
|
281
|
-
hasCategory(error, TimeoutError) ||
|
|
282
|
-
hasCategory(error, LockedError) ||
|
|
283
|
-
isHttpTransportError(error)
|
|
284
|
-
);
|
|
285
|
-
};
|
|
273
|
+
export const isTransientError = (error: unknown): boolean =>
|
|
274
|
+
isRetryable(error) ||
|
|
275
|
+
hasCategory(error, RetryableError) ||
|
|
276
|
+
hasCategory(error, ThrottlingError) ||
|
|
277
|
+
hasCategory(error, NetworkError) ||
|
|
278
|
+
isHttpTransportError(error);
|
|
286
279
|
|
|
287
280
|
// ============================================================================
|
|
288
281
|
// Category Type Utilities
|
package/src/config.ts
CHANGED
|
@@ -42,6 +42,19 @@ export class ConfigError
|
|
|
42
42
|
key: /*@__PURE__*/ Schema.optional(Schema.String),
|
|
43
43
|
}).pipe(Category.withConfigurationError) {}
|
|
44
44
|
|
|
45
|
+
declare const checked: unique symbol;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A configuration its SDK has checked. `ConfigError` is a setup error: a
|
|
49
|
+
* layer reads and checks what the user configured when the layer is built,
|
|
50
|
+
* and fails there. The SDK's `check(config)` is the one place that returns
|
|
51
|
+
* a `Checked<Config>`, and its `Credentials` service holds one, so the code
|
|
52
|
+
* that builds a request can use the values without checking them again and
|
|
53
|
+
* no call fails for a malformed one. A `Credentials` layer written by hand
|
|
54
|
+
* has to go through `check` too.
|
|
55
|
+
*/
|
|
56
|
+
export type Checked<Config> = Config & { readonly [checked]: true };
|
|
57
|
+
|
|
45
58
|
const CONFIG_PROVIDER_KEY = "effect/ConfigProvider";
|
|
46
59
|
|
|
47
60
|
interface ProvidedConfigProvider {
|
|
@@ -55,7 +68,10 @@ const fromEnv = (name: string): string | undefined =>
|
|
|
55
68
|
|
|
56
69
|
/** A required value that is missing, inside a synchronous read. */
|
|
57
70
|
class Missing {
|
|
58
|
-
|
|
71
|
+
readonly name: string;
|
|
72
|
+
constructor(name: string) {
|
|
73
|
+
this.name = name;
|
|
74
|
+
}
|
|
59
75
|
}
|
|
60
76
|
|
|
61
77
|
/**
|
package/src/errors.ts
CHANGED
|
@@ -24,6 +24,16 @@ import * as Schema from "./schema-lite-typed.ts";
|
|
|
24
24
|
*/
|
|
25
25
|
export const DurationSchema = /*@__PURE__*/ Schema.declare<Duration.Duration>(Duration.isDuration);
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Fields every error built from an HTTP response declares: the parsed body
|
|
29
|
+
* (the raw text when it wasn't JSON) and the response headers, for whatever
|
|
30
|
+
* the SDK does not type -- a request ID, nested details.
|
|
31
|
+
*/
|
|
32
|
+
export const ResponseErrorFields = {
|
|
33
|
+
body: /*@__PURE__*/ Schema.optional(Schema.Unknown),
|
|
34
|
+
headers: /*@__PURE__*/ Schema.optional(/*@__PURE__*/ Schema.Record(Schema.String, Schema.String)),
|
|
35
|
+
};
|
|
36
|
+
|
|
27
37
|
// ============================================================================
|
|
28
38
|
// Common HTTP Status Error Classes
|
|
29
39
|
// ============================================================================
|
|
@@ -34,6 +44,7 @@ export const DurationSchema = /*@__PURE__*/ Schema.declare<Duration.Duration>(Du
|
|
|
34
44
|
export class Unauthorized
|
|
35
45
|
extends /*@__PURE__*/ Schema.TaggedError<Unauthorized>()("Unauthorized", {
|
|
36
46
|
message: Schema.String,
|
|
47
|
+
...ResponseErrorFields,
|
|
37
48
|
}).pipe(Category.withAuthError) {}
|
|
38
49
|
|
|
39
50
|
/**
|
|
@@ -42,6 +53,7 @@ export class Unauthorized
|
|
|
42
53
|
export class Forbidden
|
|
43
54
|
extends /*@__PURE__*/ Schema.TaggedError<Forbidden>()("Forbidden", {
|
|
44
55
|
message: Schema.String,
|
|
56
|
+
...ResponseErrorFields,
|
|
45
57
|
}).pipe(Category.withAuthError) {}
|
|
46
58
|
|
|
47
59
|
/**
|
|
@@ -50,6 +62,7 @@ export class Forbidden
|
|
|
50
62
|
export class NotFound
|
|
51
63
|
extends /*@__PURE__*/ Schema.TaggedError<NotFound>()("NotFound", {
|
|
52
64
|
message: Schema.String,
|
|
65
|
+
...ResponseErrorFields,
|
|
53
66
|
}).pipe(Category.withNotFoundError) {}
|
|
54
67
|
|
|
55
68
|
/**
|
|
@@ -58,6 +71,7 @@ export class NotFound
|
|
|
58
71
|
export class BadRequest
|
|
59
72
|
extends /*@__PURE__*/ Schema.TaggedError<BadRequest>()("BadRequest", {
|
|
60
73
|
message: Schema.String,
|
|
74
|
+
...ResponseErrorFields,
|
|
61
75
|
}).pipe(Category.withBadRequestError) {}
|
|
62
76
|
|
|
63
77
|
/**
|
|
@@ -66,6 +80,7 @@ export class BadRequest
|
|
|
66
80
|
export class Conflict
|
|
67
81
|
extends /*@__PURE__*/ Schema.TaggedError<Conflict>()("Conflict", {
|
|
68
82
|
message: Schema.String,
|
|
83
|
+
...ResponseErrorFields,
|
|
69
84
|
}).pipe(Category.withConflictError) {}
|
|
70
85
|
|
|
71
86
|
/**
|
|
@@ -74,6 +89,7 @@ export class Conflict
|
|
|
74
89
|
export class UnprocessableEntity
|
|
75
90
|
extends /*@__PURE__*/ Schema.TaggedError<UnprocessableEntity>()("UnprocessableEntity", {
|
|
76
91
|
message: Schema.String,
|
|
92
|
+
...ResponseErrorFields,
|
|
77
93
|
}).pipe(Category.withBadRequestError) {}
|
|
78
94
|
|
|
79
95
|
/**
|
|
@@ -82,6 +98,7 @@ export class UnprocessableEntity
|
|
|
82
98
|
export class TooManyRequests
|
|
83
99
|
extends /*@__PURE__*/ Schema.TaggedError<TooManyRequests>()("TooManyRequests", {
|
|
84
100
|
message: Schema.String,
|
|
101
|
+
...ResponseErrorFields,
|
|
85
102
|
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
86
103
|
}).pipe(
|
|
87
104
|
Category.withThrottlingError,
|
|
@@ -94,6 +111,7 @@ export class TooManyRequests
|
|
|
94
111
|
export class Locked
|
|
95
112
|
extends /*@__PURE__*/ Schema.TaggedError<Locked>()("Locked", {
|
|
96
113
|
message: Schema.String,
|
|
114
|
+
...ResponseErrorFields,
|
|
97
115
|
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
98
116
|
}).pipe(Category.withLockedError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
99
117
|
|
|
@@ -108,6 +126,7 @@ export class Locked
|
|
|
108
126
|
export class InternalServerError
|
|
109
127
|
extends /*@__PURE__*/ Schema.TaggedError<InternalServerError>()("InternalServerError", {
|
|
110
128
|
message: Schema.String,
|
|
129
|
+
...ResponseErrorFields,
|
|
111
130
|
code: /*@__PURE__*/ Schema.optional(Schema.Number),
|
|
112
131
|
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
113
132
|
}).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
@@ -118,6 +137,7 @@ export class InternalServerError
|
|
|
118
137
|
export class BadGateway
|
|
119
138
|
extends /*@__PURE__*/ Schema.TaggedError<BadGateway>()("BadGateway", {
|
|
120
139
|
message: Schema.String,
|
|
140
|
+
...ResponseErrorFields,
|
|
121
141
|
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
122
142
|
}).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
123
143
|
|
|
@@ -127,6 +147,7 @@ export class BadGateway
|
|
|
127
147
|
export class ServiceUnavailable
|
|
128
148
|
extends /*@__PURE__*/ Schema.TaggedError<ServiceUnavailable>()("ServiceUnavailable", {
|
|
129
149
|
message: Schema.String,
|
|
150
|
+
...ResponseErrorFields,
|
|
130
151
|
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
131
152
|
}).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
132
153
|
|
|
@@ -136,6 +157,7 @@ export class ServiceUnavailable
|
|
|
136
157
|
export class GatewayTimeout
|
|
137
158
|
extends /*@__PURE__*/ Schema.TaggedError<GatewayTimeout>()("GatewayTimeout", {
|
|
138
159
|
message: Schema.String,
|
|
160
|
+
...ResponseErrorFields,
|
|
139
161
|
retryAfter: /*@__PURE__*/ Schema.optional(DurationSchema),
|
|
140
162
|
}).pipe(Category.withServerError, /*@__PURE__*/ Category.withRetryable()) {}
|
|
141
163
|
|
|
@@ -186,8 +208,8 @@ export const DEFAULT_ERROR_STATUSES = /*@__PURE__*/ new Set([401, 429, 500, 502,
|
|
|
186
208
|
|
|
187
209
|
/**
|
|
188
210
|
* HTTP status codes whose corresponding error class in {@link HTTP_STATUS_MAP}
|
|
189
|
-
* declares a `retryAfter` field. The retry policy
|
|
190
|
-
*
|
|
211
|
+
* declares a `retryAfter` field. The retry policy waits at least
|
|
212
|
+
* `error.retryAfter` for these.
|
|
191
213
|
*
|
|
192
214
|
* `matchError` implementations should only pass `retryAfter` into the
|
|
193
215
|
* constructor when the status is in this set; passing it on non-retryable
|
package/src/graphql.ts
CHANGED
|
@@ -229,7 +229,7 @@ export type FieldMeta = {
|
|
|
229
229
|
readonly kind: "scalar" | "object" | "list" | "connection";
|
|
230
230
|
readonly of?: TypeMeta;
|
|
231
231
|
readonly argTypes?: ArgMeta;
|
|
232
|
-
/** The value is a secret: extracted
|
|
232
|
+
/** The value is a secret: extracted values are wrapped in `Redacted`. */
|
|
233
233
|
readonly sensitive?: boolean;
|
|
234
234
|
};
|
|
235
235
|
|
|
@@ -240,16 +240,18 @@ export type TypeMeta = {
|
|
|
240
240
|
|
|
241
241
|
export const scalarField = (
|
|
242
242
|
name: string,
|
|
243
|
-
options: { readonly sensitive?: boolean } = {},
|
|
243
|
+
options: { readonly sensitive?: boolean; readonly argTypes?: ArgMeta } = {},
|
|
244
244
|
): FieldMeta => ({
|
|
245
245
|
name,
|
|
246
246
|
kind: "scalar",
|
|
247
|
+
...(options.argTypes ? { argTypes: options.argTypes } : {}),
|
|
247
248
|
...(options.sensitive ? { sensitive: true } : {}),
|
|
248
249
|
});
|
|
249
|
-
export const objectField = (name: string, objectType: TypeMeta): FieldMeta => ({
|
|
250
|
+
export const objectField = (name: string, objectType: TypeMeta, argTypes?: ArgMeta): FieldMeta => ({
|
|
250
251
|
name,
|
|
251
252
|
kind: "object",
|
|
252
253
|
of: objectType,
|
|
254
|
+
...(argTypes ? { argTypes } : {}),
|
|
253
255
|
});
|
|
254
256
|
export const listField = (name: string, itemType: TypeMeta, argTypes?: ArgMeta): FieldMeta => ({
|
|
255
257
|
name,
|
|
@@ -355,8 +357,9 @@ const log = (..._args: Array<unknown>): void => {};
|
|
|
355
357
|
*/
|
|
356
358
|
type QueryFields<Value, Error> = [NonNullable<Value>] extends [ReadonlyArray<infer Item>]
|
|
357
359
|
? {
|
|
358
|
-
readonly [Field in keyof NonNullable<Item>]:
|
|
359
|
-
NonNullable<Item>[Field]
|
|
360
|
+
readonly [Field in keyof NonNullable<Item>]: FieldQuery<
|
|
361
|
+
NonNullable<Item>[Field],
|
|
362
|
+
Nullish<Item>,
|
|
360
363
|
Error
|
|
361
364
|
>;
|
|
362
365
|
} & {
|
|
@@ -364,13 +367,36 @@ type QueryFields<Value, Error> = [NonNullable<Value>] extends [ReadonlyArray<inf
|
|
|
364
367
|
}
|
|
365
368
|
: [NonNullable<Value>] extends [object]
|
|
366
369
|
? {
|
|
367
|
-
readonly [Field in keyof NonNullable<Value>]:
|
|
368
|
-
NonNullable<Value>[Field]
|
|
370
|
+
readonly [Field in keyof NonNullable<Value>]: FieldQuery<
|
|
371
|
+
NonNullable<Value>[Field],
|
|
372
|
+
Nullish<Value>,
|
|
369
373
|
Error
|
|
370
374
|
>;
|
|
371
375
|
}
|
|
372
376
|
: {};
|
|
373
377
|
|
|
378
|
+
/**
|
|
379
|
+
* A field that takes arguments, as it appears in a generated data type.
|
|
380
|
+
* Through a Query it is a call: `account.byId({ accountId })`. When every
|
|
381
|
+
* argument is optional (`Required` false) the field also reads bare.
|
|
382
|
+
*/
|
|
383
|
+
export interface FieldWithArgs<Value, Args, Required extends boolean = true> {
|
|
384
|
+
readonly "~fieldArgs": {
|
|
385
|
+
readonly value: Value;
|
|
386
|
+
readonly args: Args;
|
|
387
|
+
readonly required: Required;
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** The Query for one field: a call for a field with arguments, else a property. */
|
|
392
|
+
type FieldQuery<Field, ParentNull, Error> = [Field] extends [
|
|
393
|
+
FieldWithArgs<infer Value, infer Args, infer Required>,
|
|
394
|
+
]
|
|
395
|
+
? [Required] extends [true]
|
|
396
|
+
? (args: Args) => Query<Value | ParentNull, Error>
|
|
397
|
+
: Query<Value | ParentNull, Error> & ((args?: Args) => Query<Value | ParentNull, Error>)
|
|
398
|
+
: Query<Field | ParentNull, Error>;
|
|
399
|
+
|
|
374
400
|
/** A field read through a `null` parent is `null`. */
|
|
375
401
|
type Nullish<Value> = Extract<Value, null | undefined>;
|
|
376
402
|
|
|
@@ -862,7 +888,9 @@ const paintAbs = (forest: Forest, expr: Expr): Path => {
|
|
|
862
888
|
isList: expr.field.kind === "list",
|
|
863
889
|
sensitive: expr.field.sensitive,
|
|
864
890
|
});
|
|
865
|
-
const
|
|
891
|
+
const key = node.alias ?? node.field;
|
|
892
|
+
forest.rootAlias.set(expr, key);
|
|
893
|
+
const path = [...parentPath, key];
|
|
866
894
|
return expr.field.kind === "connection" ? [...path, ...paintConnection(forest, node)] : path;
|
|
867
895
|
}
|
|
868
896
|
default:
|
|
@@ -892,13 +920,14 @@ const connectionNodeSel = (connection: SelNode): SelNode => {
|
|
|
892
920
|
return node;
|
|
893
921
|
};
|
|
894
922
|
|
|
895
|
-
const relSel = (expr: Expr, listSel: SelNode): SelNode => {
|
|
923
|
+
const relSel = (forest: Forest, expr: Expr, listSel: SelNode): SelNode => {
|
|
896
924
|
switch (expr._tag) {
|
|
897
925
|
case "Item":
|
|
898
926
|
return listSel;
|
|
899
927
|
case "Prop": {
|
|
900
|
-
const parent = relSel(expr.parent, listSel);
|
|
901
|
-
const
|
|
928
|
+
const parent = relSel(forest, expr.parent, listSel);
|
|
929
|
+
const key = forest.rootAlias.get(expr) ?? expr.field.name;
|
|
930
|
+
const child = [...parent.children.values()].find((c) => (c.alias ?? c.field) === key);
|
|
902
931
|
if (!child) {
|
|
903
932
|
throw new GqlError(`missing ${expr.field.name} in list selection`);
|
|
904
933
|
}
|
|
@@ -916,13 +945,14 @@ const paintRel = (forest: Forest, expr: Expr, listSel: SelNode): void => {
|
|
|
916
945
|
return;
|
|
917
946
|
case "Prop": {
|
|
918
947
|
paintRel(forest, expr.parent, listSel);
|
|
919
|
-
const child = ensureChild(forest, relSel(expr.parent, listSel), expr.field.name, {
|
|
948
|
+
const child = ensureChild(forest, relSel(forest, expr.parent, listSel), expr.field.name, {
|
|
920
949
|
args: expr.args,
|
|
921
950
|
argTypes: expr.field.argTypes,
|
|
922
951
|
isScalar: expr.field.kind === "scalar",
|
|
923
952
|
isList: expr.field.kind === "list",
|
|
924
953
|
sensitive: expr.field.sensitive,
|
|
925
954
|
});
|
|
955
|
+
forest.rootAlias.set(expr, child.alias ?? child.field);
|
|
926
956
|
if (expr.field.kind === "connection") paintConnection(forest, child);
|
|
927
957
|
return;
|
|
928
958
|
}
|
|
@@ -932,11 +962,11 @@ const paintRel = (forest: Forest, expr: Expr, listSel: SelNode): void => {
|
|
|
932
962
|
return;
|
|
933
963
|
case "MapItems":
|
|
934
964
|
paintRel(forest, expr.parent, listSel);
|
|
935
|
-
visitRel(expr.mapped, forest, relSel(expr.parent, listSel));
|
|
965
|
+
visitRel(expr.mapped, forest, relSel(forest, expr.parent, listSel));
|
|
936
966
|
return;
|
|
937
967
|
case "Filter":
|
|
938
968
|
paintRel(forest, expr.parent, listSel);
|
|
939
|
-
paintRel(forest, expr.predicate, relSel(expr.parent, listSel));
|
|
969
|
+
paintRel(forest, expr.predicate, relSel(forest, expr.parent, listSel));
|
|
940
970
|
return;
|
|
941
971
|
case "Root":
|
|
942
972
|
throw new GqlError("root inside list item");
|
|
@@ -1102,22 +1132,25 @@ const readField = (parent: unknown, name: string): unknown => {
|
|
|
1102
1132
|
return (parent as Record<string, unknown>)[name];
|
|
1103
1133
|
};
|
|
1104
1134
|
|
|
1105
|
-
/**
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1135
|
+
/**
|
|
1136
|
+
* Wrap a sensitive scalar in `Redacted`: a string, a JSON object, or each
|
|
1137
|
+
* item of a list of them.
|
|
1138
|
+
*/
|
|
1139
|
+
const redactValues = (value: unknown): unknown =>
|
|
1140
|
+
value === null || value === undefined
|
|
1141
|
+
? value
|
|
1109
1142
|
: Array.isArray(value)
|
|
1110
|
-
? value.map(
|
|
1111
|
-
: value;
|
|
1143
|
+
? value.map(redactValues)
|
|
1144
|
+
: Redacted.make(value);
|
|
1112
1145
|
|
|
1113
|
-
/** Copy `data` with the
|
|
1146
|
+
/** Copy `data` with the value of every sensitive field `sel` selects wrapped in `Redacted`. */
|
|
1114
1147
|
const redactSelected = (sel: SelNode, data: unknown): unknown => {
|
|
1115
1148
|
if (sel.children.size === 0 || data === null || typeof data !== "object") return data;
|
|
1116
1149
|
if (Array.isArray(data)) return data.map((item) => redactSelected(sel, item));
|
|
1117
1150
|
const out: Record<string, unknown> = { ...data };
|
|
1118
1151
|
for (const child of sel.children.values()) {
|
|
1119
1152
|
const key = child.alias ?? child.field;
|
|
1120
|
-
out[key] = child.sensitive ?
|
|
1153
|
+
out[key] = child.sensitive ? redactValues(out[key]) : redactSelected(child, out[key]);
|
|
1121
1154
|
}
|
|
1122
1155
|
return out;
|
|
1123
1156
|
};
|
|
@@ -1129,7 +1162,10 @@ const extractAbs = (expr: Expr, data: unknown, rootAlias: WeakMap<object, string
|
|
|
1129
1162
|
return expr.connection ? nodesFromConnection(raw) : raw;
|
|
1130
1163
|
}
|
|
1131
1164
|
case "Prop": {
|
|
1132
|
-
const value = readField(
|
|
1165
|
+
const value = readField(
|
|
1166
|
+
extractAbs(expr.parent, data, rootAlias),
|
|
1167
|
+
rootAlias.get(expr) ?? expr.field.name,
|
|
1168
|
+
);
|
|
1133
1169
|
return expr.field.kind === "connection" ? nodesFromConnection(value) : value;
|
|
1134
1170
|
}
|
|
1135
1171
|
default:
|
|
@@ -1137,12 +1173,15 @@ const extractAbs = (expr: Expr, data: unknown, rootAlias: WeakMap<object, string
|
|
|
1137
1173
|
}
|
|
1138
1174
|
};
|
|
1139
1175
|
|
|
1140
|
-
const extractRel = (expr: Expr, item: unknown): unknown => {
|
|
1176
|
+
const extractRel = (expr: Expr, item: unknown, rootAlias: WeakMap<object, string>): unknown => {
|
|
1141
1177
|
switch (expr._tag) {
|
|
1142
1178
|
case "Item":
|
|
1143
1179
|
return item;
|
|
1144
1180
|
case "Prop": {
|
|
1145
|
-
const value = readField(
|
|
1181
|
+
const value = readField(
|
|
1182
|
+
extractRel(expr.parent, item, rootAlias),
|
|
1183
|
+
rootAlias.get(expr) ?? expr.field.name,
|
|
1184
|
+
);
|
|
1146
1185
|
return expr.field.kind === "connection" ? nodesFromConnection(value) : value;
|
|
1147
1186
|
}
|
|
1148
1187
|
default:
|
|
@@ -1178,7 +1217,7 @@ const interpretExpr = (
|
|
|
1178
1217
|
return item;
|
|
1179
1218
|
case "Root":
|
|
1180
1219
|
case "Prop":
|
|
1181
|
-
return inItem(expr) ? extractRel(expr, item) : extractAbs(expr, data, rootAlias);
|
|
1220
|
+
return inItem(expr) ? extractRel(expr, item, rootAlias) : extractAbs(expr, data, rootAlias);
|
|
1182
1221
|
case "MapValue":
|
|
1183
1222
|
return expr.mapFn(interpretExpr(expr.parent, data, rootAlias, item));
|
|
1184
1223
|
case "Filter": {
|
package/src/graphql.types.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
UnknownGraphQLError,
|
|
10
10
|
} from "./graphql.ts";
|
|
11
11
|
/** Compile-only: Query.fn unwraps Query/Effect fields on the returned plan. */
|
|
12
|
+
import type { FieldWithArgs } from "./graphql.ts";
|
|
12
13
|
import type { PlanError, Query, QueryError, UnwrapPlan } from "./query.ts";
|
|
13
14
|
|
|
14
15
|
type Plan = {
|
|
@@ -103,3 +104,17 @@ const popular = /*@__PURE__*/ service.stars.pipe(/*@__PURE__*/ mapQuery((stars)
|
|
|
103
104
|
type _ScalarMap = Assert<Equal<UnwrapPlan<typeof popular>, boolean>>;
|
|
104
105
|
const labels = /*@__PURE__*/ service.tags.pipe(/*@__PURE__*/ mapQuery((tag) => tag.label));
|
|
105
106
|
type _ListMap = Assert<Equal<UnwrapPlan<typeof labels>, ReadonlyArray<string>>>;
|
|
107
|
+
|
|
108
|
+
// Fields with arguments: a required argument makes the field a call only;
|
|
109
|
+
// all-optional arguments also allow the bare read.
|
|
110
|
+
declare const accountQuery: Query<{
|
|
111
|
+
byId: FieldWithArgs<{ name: string }, { readonly accountId: string }>;
|
|
112
|
+
avatarUrl: FieldWithArgs<string, { readonly size?: number | undefined }, false>;
|
|
113
|
+
}>;
|
|
114
|
+
type _ById = Assert<Equal<UnwrapPlan<ReturnType<typeof accountQuery.byId>["name"]>, string>>;
|
|
115
|
+
type _AvatarCall = Assert<Equal<UnwrapPlan<ReturnType<typeof accountQuery.avatarUrl>>, string>>;
|
|
116
|
+
type _AvatarBare = Assert<Equal<UnwrapPlan<typeof accountQuery.avatarUrl>, string>>;
|
|
117
|
+
// @ts-expect-error a required argument cannot be left out
|
|
118
|
+
accountQuery.byId({});
|
|
119
|
+
// @ts-expect-error a field with a required argument has no bare read
|
|
120
|
+
type _NoBareRead = (typeof accountQuery.byId)["pipe"];
|