@distilled.cloud/core 1.0.0-rc.18 → 1.0.0-rc.19
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/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 -0
- package/lib/_bundle/grpc.js.map +1 -0
- package/lib/_bundle/rest.js +1 -1
- package/lib/_bundle/rest.js.map +1 -1
- package/lib/_bundle/xml.js +1 -1
- package/lib/_bundle/xml.js.map +1 -1
- package/lib/annotations.d.ts +2 -0
- package/lib/annotations.d.ts.map +1 -1
- package/lib/annotations.js +1 -1
- package/lib/annotations.js.map +1 -1
- package/lib/api.d.ts +37 -1
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +1 -1
- package/lib/errors.d.ts +1 -0
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +1 -1
- package/lib/graphql.d.ts +12 -5
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.types.d.ts.map +1 -1
- 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 +3 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -0
- package/lib/pagination.d.ts +22 -2
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +1 -1
- 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 +11 -6
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-rest.d.ts +12 -8
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/response-validation.d.ts +1 -1
- package/lib/response-validation.d.ts.map +1 -1
- 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 +2 -10
- 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-stream.d.ts +3 -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.d.ts +23 -4
- package/lib/telemetry.d.ts.map +1 -1
- package/lib/telemetry.js +2 -2
- package/lib/telemetry.js.map +1 -1
- package/lib/trait.d.ts +7 -0
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/annotations.ts +16 -0
- package/src/api.ts +199 -25
- package/src/errors.ts +4 -0
- package/src/graphql.ts +68 -45
- package/src/graphql.types.ts +5 -1
- 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 +3 -0
- package/src/pagination.ts +75 -3
- package/src/protobuf.ts +1332 -0
- package/src/protocol-grpc.ts +641 -0
- package/src/protocol-http.ts +30 -30
- package/src/protocol-rest.ts +49 -118
- package/src/response-validation.ts +4 -2
- package/src/sdk-error.ts +33 -0
- package/src/sensitive.ts +154 -0
- package/src/telemetry-debug.ts +65 -21
- package/src/telemetry-stream.ts +16 -7
- package/src/telemetry.ts +91 -27
- package/src/trait.ts +10 -0
- package/src/xml.ts +10 -1
- package/lib/debug.d.ts +0 -2
- package/lib/debug.d.ts.map +0 -1
- package/lib/debug.js +0 -1
- package/src/debug.ts +0 -11
package/src/annotations.ts
CHANGED
|
@@ -35,3 +35,19 @@ export const getAnn = (ast: AST.AST, symbol: symbol): unknown => {
|
|
|
35
35
|
}
|
|
36
36
|
return undefined;
|
|
37
37
|
};
|
|
38
|
+
|
|
39
|
+
/** Resolve wrappers to the real node: Suspend, encoding, optional/null unions. */
|
|
40
|
+
export const resolveNode = (ast: AST.AST): AST.AST => {
|
|
41
|
+
if (ast._tag === "Suspend") return resolveNode(ast.thunk());
|
|
42
|
+
if (ast.encoding && ast.encoding.length > 0) return resolveNode(ast.encoding[0]!.to);
|
|
43
|
+
if (ast._tag === "Union") {
|
|
44
|
+
const real = (ast as AST.Union).types.filter(
|
|
45
|
+
(t) =>
|
|
46
|
+
t._tag !== "Undefined" &&
|
|
47
|
+
t._tag !== "Null" &&
|
|
48
|
+
!(t._tag === "Literal" && (t as any).literal === null),
|
|
49
|
+
);
|
|
50
|
+
if (real.length === 1) return resolveNode(real[0]!);
|
|
51
|
+
}
|
|
52
|
+
return ast;
|
|
53
|
+
};
|
package/src/api.ts
CHANGED
|
@@ -13,7 +13,7 @@ import * as Ref from "effect/Ref";
|
|
|
13
13
|
import * as S from "effect/Schema";
|
|
14
14
|
import type * as AST from "effect/SchemaAST";
|
|
15
15
|
import * as Scope from "effect/Scope";
|
|
16
|
-
import
|
|
16
|
+
import * as Stream from "effect/Stream";
|
|
17
17
|
import { SingleShotGen } from "effect/Utils";
|
|
18
18
|
import { getAnn, rawResponseRootSymbol } from "./annotations.ts";
|
|
19
19
|
import { isThrottling } from "./category.ts";
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
resolveServerRetryHintCapMs,
|
|
27
27
|
serverHintMillis,
|
|
28
28
|
} from "./retry.ts";
|
|
29
|
+
import { SdkError } from "./sdk-error.ts";
|
|
29
30
|
import * as Telemetry from "./telemetry.ts";
|
|
30
31
|
|
|
31
32
|
//#region Protocol
|
|
@@ -94,6 +95,16 @@ export class Protocol
|
|
|
94
95
|
* produce the same request, result and errors as encode/decode.
|
|
95
96
|
*/
|
|
96
97
|
readonly call?: (operation: PreparedOperation, input: unknown) => Effect.Effect<unknown>;
|
|
98
|
+
/**
|
|
99
|
+
* Server streaming (see {@link makeStreaming}): each response message
|
|
100
|
+
* as it arrives, decoded as one element of the output's `items`.
|
|
101
|
+
* Ending the stream early cancels the call. Protocols that can't
|
|
102
|
+
* stream leave it out.
|
|
103
|
+
*/
|
|
104
|
+
readonly stream?: (
|
|
105
|
+
operation: PreparedOperation,
|
|
106
|
+
input: unknown,
|
|
107
|
+
) => Stream.Stream<unknown, unknown>;
|
|
97
108
|
}
|
|
98
109
|
>()("Protocol") {}
|
|
99
110
|
|
|
@@ -429,6 +440,22 @@ const prepare = (state: OperationState): Prepared => {
|
|
|
429
440
|
});
|
|
430
441
|
};
|
|
431
442
|
|
|
443
|
+
/**
|
|
444
|
+
* {@link prepare} for the call paths: a config that can't be built (a
|
|
445
|
+
* generated config that throws, a lazy service whose module doesn't match
|
|
446
|
+
* its stub) fails the call with {@link SdkError} instead of crashing it.
|
|
447
|
+
*/
|
|
448
|
+
const prepareOr = (state: OperationState): Prepared | SdkError => {
|
|
449
|
+
if (state.prepared) return state.prepared;
|
|
450
|
+
try {
|
|
451
|
+
return prepare(state);
|
|
452
|
+
} catch (cause) {
|
|
453
|
+
return new SdkError({
|
|
454
|
+
message: `the operation could not be loaded: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
432
459
|
const stateOf = (op: unknown): OperationState => (op as Record<symbol, OperationState>)[stateKey]!;
|
|
433
460
|
|
|
434
461
|
/**
|
|
@@ -472,7 +499,8 @@ const callUntraced = (
|
|
|
472
499
|
const p = state.prepared;
|
|
473
500
|
if (p?.service !== undefined) return applyRetry(runWith(p, p.service, input), p.cfg.retry, debug);
|
|
474
501
|
return Effect.suspend(() => {
|
|
475
|
-
const prepared =
|
|
502
|
+
const prepared = prepareOr(state);
|
|
503
|
+
if (prepared instanceof SdkError) return Effect.fail(prepared);
|
|
476
504
|
const call = Effect.flatMap(
|
|
477
505
|
debug === undefined
|
|
478
506
|
? protocolContext(prepared.cfg.protocol)
|
|
@@ -501,8 +529,16 @@ const callOperation = (
|
|
|
501
529
|
const callSite = Telemetry.captureCallSite(entry);
|
|
502
530
|
return Effect.withFiber((fiber) => {
|
|
503
531
|
const debug = fiber.getRef(Telemetry.Debug);
|
|
504
|
-
|
|
505
|
-
|
|
532
|
+
const prepared = prepareOr(state);
|
|
533
|
+
if (prepared instanceof SdkError) return Effect.fail(prepared);
|
|
534
|
+
return Telemetry.operationSpan(
|
|
535
|
+
fiber,
|
|
536
|
+
debug,
|
|
537
|
+
infoOf(prepared),
|
|
538
|
+
callSite,
|
|
539
|
+
input,
|
|
540
|
+
(sent) => callUntraced(state, sent, debug),
|
|
541
|
+
debug === undefined ? undefined : { input: prepared.inputAst, output: prepared.outputAst },
|
|
506
542
|
);
|
|
507
543
|
});
|
|
508
544
|
};
|
|
@@ -656,21 +692,28 @@ export interface PaginatedOperationConfig<
|
|
|
656
692
|
|
|
657
693
|
/** `.pages` / `.items` over any call function (the operation itself or a context-bound one). */
|
|
658
694
|
const streamsOf = (state: OperationState, call: (input: any) => Effect.Effect<any, any, any>) => {
|
|
659
|
-
const
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
Pagination.paginateWithDefaults;
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
695
|
+
const pagesOf = (prepared: Prepared, input: Record<string, unknown>) => {
|
|
696
|
+
const cfg = prepared.cfg as PaginatedOperationConfig<any, any, any, any> & {
|
|
697
|
+
strategy?: Pagination.PaginationStrategy;
|
|
698
|
+
};
|
|
699
|
+
// Data operations carry their strategy in the config.
|
|
700
|
+
const paginate = state.strategy ?? cfg.strategy ?? Pagination.paginateWithDefaults;
|
|
701
|
+
return paginate(call, input, cfg.pagination);
|
|
702
|
+
};
|
|
703
|
+
const pages = (input: Record<string, unknown>): Stream.Stream<any, any, any> => {
|
|
704
|
+
const prepared = prepareOr(state);
|
|
705
|
+
return prepared instanceof SdkError ? Stream.fail(prepared) : pagesOf(prepared, input);
|
|
706
|
+
};
|
|
707
|
+
const items = (input: Record<string, unknown>): Stream.Stream<any, any, any> => {
|
|
708
|
+
const prepared = prepareOr(state);
|
|
709
|
+
if (prepared instanceof SdkError) return Stream.fail(prepared);
|
|
710
|
+
const p = (prepared.cfg as PaginatedOperationConfig<any, any, any, any>).pagination;
|
|
711
|
+
if (!p.items) return pagesOf(prepared, input);
|
|
669
712
|
// A bare-array response's schema is its sole member's value
|
|
670
713
|
// (`RawResponseRoot`), so the member `items` names is the page itself.
|
|
671
|
-
return getAnn(
|
|
672
|
-
? Pagination.extractArrayItems(
|
|
673
|
-
: Pagination.extractItems(
|
|
714
|
+
return getAnn(prepared.outputAst, rawResponseRootSymbol) !== undefined
|
|
715
|
+
? Pagination.extractArrayItems(pagesOf(prepared, input))
|
|
716
|
+
: Pagination.extractItems(pagesOf(prepared, input), p.items);
|
|
674
717
|
};
|
|
675
718
|
return { pages, items };
|
|
676
719
|
};
|
|
@@ -733,6 +776,114 @@ export function makePaginated<
|
|
|
733
776
|
|
|
734
777
|
//#endregion
|
|
735
778
|
|
|
779
|
+
//#region MakeStreaming
|
|
780
|
+
|
|
781
|
+
/** The message type of a server-streaming output, `{ items: Message[] }`. */
|
|
782
|
+
export type StreamedItem<O> = O extends { readonly items: ReadonlyArray<infer Item> }
|
|
783
|
+
? Item
|
|
784
|
+
: O extends { readonly items?: ReadonlyArray<infer Item> | undefined }
|
|
785
|
+
? Item
|
|
786
|
+
: unknown;
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* The callable half of a server-streaming operation: the call, which
|
|
790
|
+
* collects the whole stream into `{ items }`, plus `.stream(input)`, which
|
|
791
|
+
* emits each message as it arrives.
|
|
792
|
+
*/
|
|
793
|
+
export interface StreamingCall<I, O, E, R, Item> {
|
|
794
|
+
(input: I): Effect.Effect<O, E, R>;
|
|
795
|
+
readonly stream: (input: I) => Stream.Stream<Item, E, R>;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* A server-streaming operation: usable both ways an {@link OperationMethod}
|
|
800
|
+
* is, with `.stream` on both the operation and the requirement-free call
|
|
801
|
+
* function `yield* operation` returns.
|
|
802
|
+
*/
|
|
803
|
+
export type StreamingOperationMethod<I, O, E, R, Item = StreamedItem<O>> = Effect.Effect<
|
|
804
|
+
StreamingCall<I, O, E, never, Item>,
|
|
805
|
+
never,
|
|
806
|
+
R
|
|
807
|
+
> &
|
|
808
|
+
StreamingCall<I, O, E, R, Item> &
|
|
809
|
+
OperationSchemas<I, O>;
|
|
810
|
+
|
|
811
|
+
/** `.stream(input)` over an operation's protocol. */
|
|
812
|
+
const streamCall =
|
|
813
|
+
(state: OperationState) =>
|
|
814
|
+
(input: unknown): Stream.Stream<any, any, any> =>
|
|
815
|
+
Stream.unwrap(
|
|
816
|
+
Effect.suspend(() => {
|
|
817
|
+
const prepared = prepareOr(state);
|
|
818
|
+
if (prepared instanceof SdkError) return Effect.succeed(Stream.fail(prepared));
|
|
819
|
+
const run = (service: ProtocolShape) =>
|
|
820
|
+
service.stream === undefined
|
|
821
|
+
? Stream.fail(
|
|
822
|
+
new SdkError({
|
|
823
|
+
message: `${prepared.cfg.operationName ?? "this operation"}: its protocol can't stream`,
|
|
824
|
+
}),
|
|
825
|
+
)
|
|
826
|
+
: service.stream(prepared, input);
|
|
827
|
+
return prepared.service !== undefined
|
|
828
|
+
? Effect.succeed(run(prepared.service))
|
|
829
|
+
: Effect.map(protocolContext(prepared.cfg.protocol), (ctx) =>
|
|
830
|
+
run((prepared.service = Context.get(ctx, Protocol))),
|
|
831
|
+
);
|
|
832
|
+
}),
|
|
833
|
+
);
|
|
834
|
+
|
|
835
|
+
const StreamingOperationProto: object = /*@__PURE__*/ Object.create(OperationProto, {
|
|
836
|
+
stream: {
|
|
837
|
+
get(this: any) {
|
|
838
|
+
const fn = streamCall(stateOf(this));
|
|
839
|
+
Object.defineProperty(this, "stream", { value: fn, configurable: true });
|
|
840
|
+
return fn;
|
|
841
|
+
},
|
|
842
|
+
configurable: true,
|
|
843
|
+
},
|
|
844
|
+
// `yield* operation` returns a context-bound call; its `.stream` gets the
|
|
845
|
+
// same fallback (not snapshot) context.
|
|
846
|
+
asEffect: {
|
|
847
|
+
value(this: any) {
|
|
848
|
+
const state = stateOf(this);
|
|
849
|
+
return Effect.map(Effect.context(), (context) => {
|
|
850
|
+
const merge = (current: Context.Context<any>): Context.Context<any> =>
|
|
851
|
+
Context.merge(context, current);
|
|
852
|
+
const call = (input: unknown) => Effect.updateContext(this(input), merge);
|
|
853
|
+
return Object.assign(call, {
|
|
854
|
+
stream: (input: unknown) => Stream.updateContext(streamCall(state)(input), merge),
|
|
855
|
+
});
|
|
856
|
+
});
|
|
857
|
+
},
|
|
858
|
+
configurable: true,
|
|
859
|
+
writable: true,
|
|
860
|
+
},
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
/**
|
|
864
|
+
* Like {@link make}, for a server-streaming RPC whose output is `{ items }`:
|
|
865
|
+
* calling it collects the stream, and `.stream(input)` emits each message as
|
|
866
|
+
* the protocol receives it (see {@link ProtocolShape.stream}).
|
|
867
|
+
*/
|
|
868
|
+
export function makeStreaming<
|
|
869
|
+
I extends S.Top,
|
|
870
|
+
O extends S.Top,
|
|
871
|
+
PE,
|
|
872
|
+
PR,
|
|
873
|
+
const E extends readonly ApiErrorClass[] = readonly [],
|
|
874
|
+
>(
|
|
875
|
+
configFn: () => OperationConfig<I, O, PE, PR, E>,
|
|
876
|
+
): StreamingOperationMethod<
|
|
877
|
+
S.Schema.Type<I>,
|
|
878
|
+
S.Schema.Type<O>,
|
|
879
|
+
InstanceType<E[number]> | PE | HttpClientError.HttpClientError,
|
|
880
|
+
PR | HttpClient.HttpClient
|
|
881
|
+
> {
|
|
882
|
+
return makeOperation({ configFn }, StreamingOperationProto);
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
//#endregion
|
|
886
|
+
|
|
736
887
|
//#region Data operations
|
|
737
888
|
|
|
738
889
|
/**
|
|
@@ -879,11 +1030,13 @@ const dataState = function (this: DataFn): OperationState {
|
|
|
879
1030
|
};
|
|
880
1031
|
let dataProto: object | undefined;
|
|
881
1032
|
let dataPaginatedProto: object | undefined;
|
|
1033
|
+
let dataStreamingProto: object | undefined;
|
|
882
1034
|
/**
|
|
883
1035
|
* While a lazy service loads its full module (see {@link lazyService}), the module's
|
|
884
|
-
* definitions go to it instead (kind 0 operation, 1 paginated, 2 error class
|
|
1036
|
+
* definitions go to it instead (kind 0 operation, 1 paginated, 2 error class,
|
|
1037
|
+
* 3 streaming).
|
|
885
1038
|
*/
|
|
886
|
-
let definer: ((fn: DataFn, kind: 0 | 1 | 2) => unknown) | undefined;
|
|
1039
|
+
let definer: ((fn: DataFn, kind: 0 | 1 | 2 | 3) => unknown) | undefined;
|
|
887
1040
|
|
|
888
1041
|
/** Runs `f` with `d` receiving the definitions it evaluates. */
|
|
889
1042
|
const defining = (d: typeof definer, f: () => void): void => {
|
|
@@ -921,12 +1074,23 @@ const asOpPaginated = (fn: DataFn): any =>
|
|
|
921
1074
|
})),
|
|
922
1075
|
);
|
|
923
1076
|
|
|
1077
|
+
const asOpStreaming = (fn: DataFn): any =>
|
|
1078
|
+
Object.setPrototypeOf(
|
|
1079
|
+
fn,
|
|
1080
|
+
(dataStreamingProto ??= Object.create(StreamingOperationProto, {
|
|
1081
|
+
[stateKey]: { get: dataState, configurable: true },
|
|
1082
|
+
})),
|
|
1083
|
+
);
|
|
1084
|
+
|
|
924
1085
|
/** An operation described by data (see the region header). */
|
|
925
1086
|
export const op = (fn: DataFn): any => (definer ? definer(fn, 0) : asOp(fn));
|
|
926
1087
|
|
|
927
1088
|
/** A paginated operation described by data; its config may name a `strategy`. */
|
|
928
1089
|
export const opPaginated = (fn: DataFn): any => (definer ? definer(fn, 1) : asOpPaginated(fn));
|
|
929
1090
|
|
|
1091
|
+
/** A server-streaming operation described by data. */
|
|
1092
|
+
export const opStreaming = (fn: DataFn): any => (definer ? definer(fn, 3) : asOpStreaming(fn));
|
|
1093
|
+
|
|
930
1094
|
/** The call path of a data operation. */
|
|
931
1095
|
export const call = (op: unknown, input: unknown): Effect.Effect<any, any, any> =>
|
|
932
1096
|
callOperation(stateOf(op), input, op as Function);
|
|
@@ -1007,7 +1171,7 @@ export const newError = (props: unknown, newTarget: Function | undefined, data:
|
|
|
1007
1171
|
* which passes its own URL and the namespaces every stub imports.
|
|
1008
1172
|
*
|
|
1009
1173
|
* `kinds` has one letter per operation definition of the full module: `o`
|
|
1010
|
-
* plain, `p` paginated. While the stub's load evaluates the full module,
|
|
1174
|
+
* plain, `p` paginated, `s` server-streaming. While the stub's load evaluates the full module,
|
|
1011
1175
|
* `op` / `opPaginated` / `error` hand back the stub's placeholders in
|
|
1012
1176
|
* definition order instead of new values, and each placeholder takes the
|
|
1013
1177
|
* data function it stands for: the full module's operations, error lists
|
|
@@ -1039,7 +1203,10 @@ interface LazyService {
|
|
|
1039
1203
|
ei: number;
|
|
1040
1204
|
}
|
|
1041
1205
|
|
|
1042
|
-
|
|
1206
|
+
/** The stub letter of each operation kind (see {@link lazyService}). */
|
|
1207
|
+
const KIND_LETTERS = ["o", "p", "", "s"] as const;
|
|
1208
|
+
|
|
1209
|
+
const define = (svc: LazyService, fn: DataFn, kind: 0 | 1 | 2 | 3): any => {
|
|
1043
1210
|
if (kind === 2) {
|
|
1044
1211
|
const i = svc.ei++;
|
|
1045
1212
|
if (i >= svc.e.length) throw new Error(`distilled: ${svc.spec}: error ${i} has no stub`);
|
|
@@ -1047,7 +1214,7 @@ const define = (svc: LazyService, fn: DataFn, kind: 0 | 1 | 2): any => {
|
|
|
1047
1214
|
return svc.e[i];
|
|
1048
1215
|
}
|
|
1049
1216
|
const i = svc.oi++;
|
|
1050
|
-
if (i >= svc.o.length ||
|
|
1217
|
+
if (i >= svc.o.length || svc.kinds[i] !== KIND_LETTERS[kind])
|
|
1051
1218
|
throw new Error(`distilled: ${svc.spec}: operation ${i} does not match its stub`);
|
|
1052
1219
|
svc.od![i] = fn;
|
|
1053
1220
|
return svc.o[i];
|
|
@@ -1150,12 +1317,17 @@ const sliced = (svc: LazyService): SliceIndex | false => {
|
|
|
1150
1317
|
svc.ix = false;
|
|
1151
1318
|
if (svc.at === undefined || svc.od) return false;
|
|
1152
1319
|
try {
|
|
1320
|
+
// oxlint-disable-next-line distilled/no-function-constructor -- probes whether the host allows it (a CSP may not)
|
|
1153
1321
|
Function("");
|
|
1154
1322
|
const file = fileOf(svc);
|
|
1155
1323
|
svc.ix = indexOf(file, read({ file }, svc.at, svc.len!));
|
|
1156
1324
|
svc.od = [];
|
|
1157
1325
|
svc.ed = [];
|
|
1158
|
-
} catch {
|
|
1326
|
+
} catch {
|
|
1327
|
+
// Code from strings is blocked, or the slice can't be read: load the
|
|
1328
|
+
// whole module instead (`svc.ix` stays `false`).
|
|
1329
|
+
svc.ix = false;
|
|
1330
|
+
}
|
|
1159
1331
|
return svc.ix;
|
|
1160
1332
|
};
|
|
1161
1333
|
|
|
@@ -1259,6 +1431,7 @@ const evaluate = (svc: LazyService, ix: SliceIndex, k: number): any => {
|
|
|
1259
1431
|
});
|
|
1260
1432
|
text += read(ix, at, end! - at);
|
|
1261
1433
|
} else text = read(ix, start!, end! - start!);
|
|
1434
|
+
// oxlint-disable-next-line distilled/no-function-constructor -- the distilled-lazy loader compiles one declaration of a bundled service
|
|
1262
1435
|
return Function(...names, `"use strict";return(${text}\n)${url}`)(...values);
|
|
1263
1436
|
};
|
|
1264
1437
|
|
|
@@ -1278,6 +1451,7 @@ const compileBody = (
|
|
|
1278
1451
|
): Function => {
|
|
1279
1452
|
const text = read(ix, start, end - start);
|
|
1280
1453
|
const keep = names.map((n) => !own.includes(n));
|
|
1454
|
+
// oxlint-disable-next-line distilled/no-function-constructor -- the distilled-lazy loader compiles one declaration of a bundled service
|
|
1281
1455
|
return Function(
|
|
1282
1456
|
...names.filter((_, i) => keep[i]),
|
|
1283
1457
|
...own,
|
|
@@ -1374,11 +1548,11 @@ export const lazyService = (
|
|
|
1374
1548
|
if (!primed && at !== undefined) prime(svc);
|
|
1375
1549
|
return {
|
|
1376
1550
|
o: () => {
|
|
1377
|
-
const
|
|
1551
|
+
const kind = kinds[svc.o.length];
|
|
1378
1552
|
const f: DataFn = function op(x) {
|
|
1379
1553
|
return x === D ? dataOf(svc, 0, svc.o.indexOf(op))(D) : call(op, x);
|
|
1380
1554
|
};
|
|
1381
|
-
svc.o.push(
|
|
1555
|
+
svc.o.push(kind === "p" ? asOpPaginated(f) : kind === "s" ? asOpStreaming(f) : asOp(f));
|
|
1382
1556
|
return f;
|
|
1383
1557
|
},
|
|
1384
1558
|
e: (name: string) => {
|
package/src/errors.ts
CHANGED
|
@@ -147,6 +147,10 @@ export { ConfigError } from "./config.ts";
|
|
|
147
147
|
// with it without importing every class in this module.
|
|
148
148
|
export { InvalidInput } from "./protocol-http.ts";
|
|
149
149
|
|
|
150
|
+
// Defined in the `base` bundle group (`sdk-error.ts`), with pagination and
|
|
151
|
+
// the operation runtime that fail with it.
|
|
152
|
+
export { SdkError } from "./sdk-error.ts";
|
|
153
|
+
|
|
150
154
|
// ============================================================================
|
|
151
155
|
// Error Maps
|
|
152
156
|
// ============================================================================
|
package/src/graphql.ts
CHANGED
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
* {@link GqlTransport.execute} posts `{ query, variables, operationName }`.
|
|
45
45
|
* The Effect it returns may require Credentials / HttpClient; those leak
|
|
46
46
|
* into `Query.fn`'s requirements. It returns the parsed `data` and raw
|
|
47
|
-
* `errors`; `Query.fn` classifies the errors (see "Errors" below).
|
|
48
|
-
*
|
|
47
|
+
* `errors`; `Query.fn` classifies the errors (see "Errors" below). A plan
|
|
48
|
+
* that can't be compiled fails with {@link GqlError}.
|
|
49
49
|
*
|
|
50
50
|
* Generated SDKs export *roots* (`Railway.me`, `Railway.project`).
|
|
51
51
|
* Combinators live in `@distilled.cloud/core/query`.
|
|
@@ -64,15 +64,29 @@ import * as Telemetry from "./telemetry.ts";
|
|
|
64
64
|
export const QuerySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/graphql/Query");
|
|
65
65
|
const inspect = /*@__PURE__*/ Symbol.for("nodejs.util.inspect.custom");
|
|
66
66
|
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
/**
|
|
68
|
+
* A plan that cannot be compiled (a programming error in the query). The
|
|
69
|
+
* compiler throws it; `Query.fn`, `Query.pages` and `Query.items` fail with
|
|
70
|
+
* it (see {@link compiling}).
|
|
71
|
+
*/
|
|
72
|
+
export class GqlError
|
|
73
|
+
extends /*@__PURE__*/ Data.TaggedError("GqlError")<{
|
|
74
|
+
readonly message: string;
|
|
75
|
+
}>
|
|
76
|
+
{
|
|
71
77
|
constructor(message: string) {
|
|
72
|
-
|
|
78
|
+
super({ message });
|
|
73
79
|
}
|
|
74
80
|
}
|
|
75
81
|
|
|
82
|
+
/** `f()`, with what the compiler throws failed as a {@link GqlError}. */
|
|
83
|
+
const compiling = <A>(f: () => A): Effect.Effect<A, GqlError> =>
|
|
84
|
+
Effect.try({
|
|
85
|
+
try: f,
|
|
86
|
+
catch: (e) =>
|
|
87
|
+
e instanceof GqlError ? e : new GqlError(e instanceof Error ? e.message : String(e)),
|
|
88
|
+
});
|
|
89
|
+
|
|
76
90
|
// U+2500U+2500 Errors U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500U+2500
|
|
77
91
|
//
|
|
78
92
|
// Every entry of a GraphQL `errors` array becomes one typed issue. Generated
|
|
@@ -395,7 +409,8 @@ export type QueryError<Error> =
|
|
|
395
409
|
| Error
|
|
396
410
|
| GraphQLFailure<Extract<Error, GraphQLIssue> | UnknownGraphQLError>
|
|
397
411
|
| UnknownGraphQLError
|
|
398
|
-
| GraphQLTransportError
|
|
412
|
+
| GraphQLTransportError
|
|
413
|
+
| GqlError;
|
|
399
414
|
|
|
400
415
|
type Expr =
|
|
401
416
|
| RootExpr
|
|
@@ -1212,13 +1227,15 @@ const interpretPlan = (
|
|
|
1212
1227
|
if (isQuery(plan)) {
|
|
1213
1228
|
const expr = (plan as QueryNode).expr;
|
|
1214
1229
|
if (expr._tag === "FlatMap") {
|
|
1215
|
-
const resolved =
|
|
1230
|
+
const resolved = yield* compiling(() =>
|
|
1231
|
+
interpretExpr(expr.parent, data, rootAlias, undefined),
|
|
1232
|
+
);
|
|
1216
1233
|
const next = expr.flatMapFn(resolved);
|
|
1217
1234
|
if (Effect.isEffect(next)) return yield* next;
|
|
1218
1235
|
if (isQuery(next)) return yield* interpretPlan(next, data, rootAlias);
|
|
1219
1236
|
return next;
|
|
1220
1237
|
}
|
|
1221
|
-
return interpretExpr(expr, data, rootAlias, undefined);
|
|
1238
|
+
return yield* compiling(() => interpretExpr(expr, data, rootAlias, undefined));
|
|
1222
1239
|
}
|
|
1223
1240
|
if (Effect.isEffect(plan)) return yield* plan;
|
|
1224
1241
|
if (plan !== null && typeof plan === "object" && !Array.isArray(plan)) {
|
|
@@ -1240,11 +1257,13 @@ const evaluatePlan = <Plan>(
|
|
|
1240
1257
|
): Effect.Effect<UnwrapPlan<Plan>, QueryError<PlanError<Plan>>, GqlTransport> =>
|
|
1241
1258
|
Effect.gen(function* () {
|
|
1242
1259
|
const forest = emptyForest();
|
|
1243
|
-
|
|
1260
|
+
const compiled = yield* compiling(() => {
|
|
1261
|
+
visitPlan(plan, forest);
|
|
1262
|
+
return forest.roots.children.size > 0 ? emit(forest) : undefined;
|
|
1263
|
+
});
|
|
1244
1264
|
let data: unknown = {};
|
|
1245
1265
|
let rootAlias: WeakMap<object, string> = new WeakMap();
|
|
1246
|
-
if (
|
|
1247
|
-
const compiled = emit(forest);
|
|
1266
|
+
if (compiled !== undefined) {
|
|
1248
1267
|
log("document\n" + compiled.document);
|
|
1249
1268
|
data = yield* execute(compiled);
|
|
1250
1269
|
rootAlias = compiled.rootAlias;
|
|
@@ -1678,15 +1697,17 @@ const fetchPage = <Item>(
|
|
|
1678
1697
|
state: PageState,
|
|
1679
1698
|
) =>
|
|
1680
1699
|
Effect.gen(function* () {
|
|
1681
|
-
const pageExpr =
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1700
|
+
const { pageExpr, connectionPath, compiled } = yield* compiling(() => {
|
|
1701
|
+
const pageExpr = withCursor(query.expr, connection, state.after);
|
|
1702
|
+
const forest = emptyForest();
|
|
1703
|
+
visitExpr(forest, pageExpr);
|
|
1704
|
+
const nodePath = paintAbs(forest, withCursor(connection, connection, state.after));
|
|
1705
|
+
const connectionPath = nodePath.slice(0, -2);
|
|
1706
|
+
const pageInfo = ensureChild(forest, selAt(forest.roots, connectionPath), "pageInfo", {});
|
|
1707
|
+
ensureChild(forest, pageInfo, "hasNextPage", { isScalar: true });
|
|
1708
|
+
ensureChild(forest, pageInfo, "endCursor", { isScalar: true });
|
|
1709
|
+
return { pageExpr, connectionPath, compiled: emit(forest) };
|
|
1710
|
+
});
|
|
1690
1711
|
log("page\n" + compiled.document);
|
|
1691
1712
|
const data = yield* execute(compiled);
|
|
1692
1713
|
const items = (yield* interpretPlan(make(pageExpr), data, compiled.rootAlias)) as
|
|
@@ -1723,29 +1744,31 @@ export const pagesQuery = <Item, Error>(
|
|
|
1723
1744
|
query: QueryNode<ReadonlyArray<Item> | null, Error>,
|
|
1724
1745
|
): Stream.Stream<ReadonlyArray<Item>, QueryError<Error> | GraphQLPaginationError, GqlTransport> =>
|
|
1725
1746
|
Stream.unwrap(
|
|
1726
|
-
Effect.
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1747
|
+
Effect.map(
|
|
1748
|
+
compiling(() => connectionOf(query.expr)),
|
|
1749
|
+
(connection) => {
|
|
1750
|
+
const after = connection.args?.after;
|
|
1751
|
+
return Stream.paginate<
|
|
1752
|
+
PageState,
|
|
1753
|
+
ReadonlyArray<Item>,
|
|
1754
|
+
QueryError<Error> | GraphQLPaginationError,
|
|
1755
|
+
GqlTransport
|
|
1756
|
+
>(
|
|
1757
|
+
{
|
|
1758
|
+
after: typeof after === "string" ? after : undefined,
|
|
1759
|
+
seen: new Set(),
|
|
1760
|
+
},
|
|
1761
|
+
(state) =>
|
|
1762
|
+
fetchPage(query, connection, state).pipe(
|
|
1763
|
+
Effect.map(([items, next]) => [[items], next] as const),
|
|
1764
|
+
) as Effect.Effect<
|
|
1765
|
+
readonly [ReadonlyArray<ReadonlyArray<Item>>, Option.Option<PageState>],
|
|
1766
|
+
QueryError<Error> | GraphQLPaginationError,
|
|
1767
|
+
GqlTransport
|
|
1768
|
+
>,
|
|
1769
|
+
);
|
|
1770
|
+
},
|
|
1771
|
+
),
|
|
1749
1772
|
);
|
|
1750
1773
|
|
|
1751
1774
|
/** Every node of a Relay connection, across all pages. */
|
package/src/graphql.types.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type * as Stream from "effect/Stream";
|
|
|
3
3
|
import { mapQuery } from "./graphql.ts";
|
|
4
4
|
import type {
|
|
5
5
|
itemsQuery,
|
|
6
|
+
GqlError,
|
|
6
7
|
GraphQLFailure,
|
|
7
8
|
GraphQLTransportError,
|
|
8
9
|
UnknownGraphQLError,
|
|
@@ -58,7 +59,10 @@ type _QueryError = Assert<
|
|
|
58
59
|
Equal<
|
|
59
60
|
Exclude<
|
|
60
61
|
QueryError<NotFound>,
|
|
61
|
-
GraphQLFailure<NotFound | UnknownGraphQLError>
|
|
62
|
+
| GraphQLFailure<NotFound | UnknownGraphQLError>
|
|
63
|
+
| UnknownGraphQLError
|
|
64
|
+
| GraphQLTransportError
|
|
65
|
+
| GqlError
|
|
62
66
|
>,
|
|
63
67
|
NotFound
|
|
64
68
|
>
|