@distilled.cloud/core 0.30.3 → 1.0.0-rc.10

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.
Files changed (216) hide show
  1. package/LICENSE +201 -0
  2. package/lib/api.d.ts +165 -0
  3. package/lib/api.d.ts.map +1 -0
  4. package/lib/api.js +190 -0
  5. package/lib/api.js.map +1 -0
  6. package/lib/category.d.ts +4 -4
  7. package/lib/category.js +4 -4
  8. package/lib/codegen/boolean-string-enums.d.ts +36 -0
  9. package/lib/codegen/boolean-string-enums.d.ts.map +1 -0
  10. package/lib/codegen/boolean-string-enums.js +94 -0
  11. package/lib/codegen/boolean-string-enums.js.map +1 -0
  12. package/lib/codegen/boolean-string-enums.test.d.ts +2 -0
  13. package/lib/codegen/boolean-string-enums.test.d.ts.map +1 -0
  14. package/lib/codegen/boolean-string-enums.test.js +147 -0
  15. package/lib/codegen/boolean-string-enums.test.js.map +1 -0
  16. package/lib/codegen/cli.d.ts +79 -0
  17. package/lib/codegen/cli.d.ts.map +1 -0
  18. package/lib/codegen/cli.js +149 -0
  19. package/lib/codegen/cli.js.map +1 -0
  20. package/lib/codegen/emit.d.ts +125 -0
  21. package/lib/codegen/emit.d.ts.map +1 -0
  22. package/lib/codegen/emit.js +101 -0
  23. package/lib/codegen/emit.js.map +1 -0
  24. package/lib/codegen/format.d.ts +23 -0
  25. package/lib/codegen/format.d.ts.map +1 -0
  26. package/lib/codegen/format.js +28 -0
  27. package/lib/codegen/format.js.map +1 -0
  28. package/lib/codegen/generator.d.ts +334 -0
  29. package/lib/codegen/generator.d.ts.map +1 -0
  30. package/lib/codegen/generator.js +813 -0
  31. package/lib/codegen/generator.js.map +1 -0
  32. package/lib/codegen/graph.d.ts +36 -0
  33. package/lib/codegen/graph.d.ts.map +1 -0
  34. package/lib/codegen/graph.js +136 -0
  35. package/lib/codegen/graph.js.map +1 -0
  36. package/lib/codegen/graphql-client.d.ts +62 -0
  37. package/lib/codegen/graphql-client.d.ts.map +1 -0
  38. package/lib/codegen/graphql-client.js +294 -0
  39. package/lib/codegen/graphql-client.js.map +1 -0
  40. package/lib/codegen/graphql-client.test.d.ts +2 -0
  41. package/lib/codegen/graphql-client.test.d.ts.map +1 -0
  42. package/lib/codegen/graphql-client.test.js +311 -0
  43. package/lib/codegen/graphql-client.test.js.map +1 -0
  44. package/lib/codegen/graphql.d.ts +207 -0
  45. package/lib/codegen/graphql.d.ts.map +1 -0
  46. package/lib/codegen/graphql.js +799 -0
  47. package/lib/codegen/graphql.js.map +1 -0
  48. package/lib/codegen/members.d.ts +25 -0
  49. package/lib/codegen/members.d.ts.map +1 -0
  50. package/lib/codegen/members.js +55 -0
  51. package/lib/codegen/members.js.map +1 -0
  52. package/lib/codegen/naming.d.ts +29 -0
  53. package/lib/codegen/naming.d.ts.map +1 -0
  54. package/lib/codegen/naming.js +74 -0
  55. package/lib/codegen/naming.js.map +1 -0
  56. package/lib/codegen/openapi-cli.d.ts +52 -0
  57. package/lib/codegen/openapi-cli.d.ts.map +1 -0
  58. package/lib/codegen/openapi-cli.js +109 -0
  59. package/lib/codegen/openapi-cli.js.map +1 -0
  60. package/lib/codegen/openapi.d.ts +178 -0
  61. package/lib/codegen/openapi.d.ts.map +1 -0
  62. package/lib/codegen/openapi.js +1377 -0
  63. package/lib/codegen/openapi.js.map +1 -0
  64. package/lib/codegen/operations.d.ts +24 -0
  65. package/lib/codegen/operations.d.ts.map +1 -0
  66. package/lib/codegen/operations.js +56 -0
  67. package/lib/codegen/operations.js.map +1 -0
  68. package/lib/codegen/pagination.d.ts +39 -0
  69. package/lib/codegen/pagination.d.ts.map +1 -0
  70. package/lib/codegen/pagination.js +33 -0
  71. package/lib/codegen/pagination.js.map +1 -0
  72. package/lib/codegen/patches.d.ts +65 -0
  73. package/lib/codegen/patches.d.ts.map +1 -0
  74. package/lib/codegen/patches.js +236 -0
  75. package/lib/codegen/patches.js.map +1 -0
  76. package/lib/codegen/patches.test.d.ts +2 -0
  77. package/lib/codegen/patches.test.d.ts.map +1 -0
  78. package/lib/codegen/patches.test.js +105 -0
  79. package/lib/codegen/patches.test.js.map +1 -0
  80. package/lib/codegen/prelude.d.ts +15 -0
  81. package/lib/codegen/prelude.d.ts.map +1 -0
  82. package/lib/codegen/prelude.js +60 -0
  83. package/lib/codegen/prelude.js.map +1 -0
  84. package/lib/codegen/proto.d.ts +121 -0
  85. package/lib/codegen/proto.d.ts.map +1 -0
  86. package/lib/codegen/proto.js +962 -0
  87. package/lib/codegen/proto.js.map +1 -0
  88. package/lib/codegen/rewrite-operation-ids.d.ts +131 -0
  89. package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -0
  90. package/lib/codegen/rewrite-operation-ids.js +1079 -0
  91. package/lib/codegen/rewrite-operation-ids.js.map +1 -0
  92. package/lib/codegen/rewrite-operation-ids.test.d.ts +2 -0
  93. package/lib/codegen/rewrite-operation-ids.test.d.ts.map +1 -0
  94. package/lib/codegen/rewrite-operation-ids.test.js +533 -0
  95. package/lib/codegen/rewrite-operation-ids.test.js.map +1 -0
  96. package/lib/codegen/spec-path.d.ts +16 -0
  97. package/lib/codegen/spec-path.d.ts.map +1 -0
  98. package/lib/codegen/spec-path.js +101 -0
  99. package/lib/codegen/spec-path.js.map +1 -0
  100. package/lib/error-category.d.ts +28 -0
  101. package/lib/error-category.d.ts.map +1 -0
  102. package/lib/error-category.js +46 -0
  103. package/lib/error-category.js.map +1 -0
  104. package/lib/errors.d.ts +1 -0
  105. package/lib/errors.d.ts.map +1 -1
  106. package/lib/errors.js +18 -13
  107. package/lib/errors.js.map +1 -1
  108. package/lib/graphql.d.ts +284 -0
  109. package/lib/graphql.d.ts.map +1 -0
  110. package/lib/graphql.fixture.d.ts +249 -0
  111. package/lib/graphql.fixture.d.ts.map +1 -0
  112. package/lib/graphql.fixture.js +240 -0
  113. package/lib/graphql.fixture.js.map +1 -0
  114. package/lib/graphql.js +718 -0
  115. package/lib/graphql.js.map +1 -0
  116. package/lib/graphql.test.d.ts +2 -0
  117. package/lib/graphql.test.d.ts.map +1 -0
  118. package/lib/graphql.test.js +780 -0
  119. package/lib/graphql.test.js.map +1 -0
  120. package/lib/graphql.types.d.ts +2 -0
  121. package/lib/graphql.types.d.ts.map +1 -0
  122. package/lib/graphql.types.js +45 -0
  123. package/lib/graphql.types.js.map +1 -0
  124. package/lib/json-patch.d.ts +30 -30
  125. package/lib/json-patch.d.ts.map +1 -1
  126. package/lib/json-patch.js +73 -107
  127. package/lib/json-patch.js.map +1 -1
  128. package/lib/pagination.d.ts +77 -51
  129. package/lib/pagination.d.ts.map +1 -1
  130. package/lib/pagination.js +162 -94
  131. package/lib/pagination.js.map +1 -1
  132. package/lib/protocol-http.d.ts +74 -0
  133. package/lib/protocol-http.d.ts.map +1 -0
  134. package/lib/protocol-http.js +590 -0
  135. package/lib/protocol-http.js.map +1 -0
  136. package/lib/protocol-http.test.d.ts +2 -0
  137. package/lib/protocol-http.test.d.ts.map +1 -0
  138. package/lib/protocol-http.test.js +88 -0
  139. package/lib/protocol-http.test.js.map +1 -0
  140. package/lib/protocol-rest.d.ts +134 -0
  141. package/lib/protocol-rest.d.ts.map +1 -0
  142. package/lib/protocol-rest.js +256 -0
  143. package/lib/protocol-rest.js.map +1 -0
  144. package/lib/retry.d.ts +8 -2
  145. package/lib/retry.d.ts.map +1 -1
  146. package/lib/retry.js +22 -16
  147. package/lib/retry.js.map +1 -1
  148. package/lib/schema.d.ts +8 -9
  149. package/lib/schema.d.ts.map +1 -1
  150. package/lib/schema.js +8 -9
  151. package/lib/schema.js.map +1 -1
  152. package/lib/trait.d.ts +174 -0
  153. package/lib/trait.d.ts.map +1 -0
  154. package/lib/trait.js +123 -0
  155. package/lib/trait.js.map +1 -0
  156. package/package.json +24 -78
  157. package/src/api.ts +460 -0
  158. package/src/category.ts +4 -4
  159. package/src/codegen/boolean-string-enums.test.ts +168 -0
  160. package/src/codegen/boolean-string-enums.ts +106 -0
  161. package/src/codegen/cli.ts +285 -0
  162. package/src/codegen/emit.ts +203 -0
  163. package/src/codegen/format.ts +47 -0
  164. package/src/codegen/generator.ts +1283 -0
  165. package/src/codegen/graph.ts +151 -0
  166. package/src/codegen/graphql-client.test.ts +386 -0
  167. package/src/codegen/graphql-client.ts +419 -0
  168. package/src/codegen/graphql.ts +1217 -0
  169. package/src/codegen/members.ts +71 -0
  170. package/src/codegen/naming.ts +86 -0
  171. package/src/codegen/openapi-cli.ts +182 -0
  172. package/src/codegen/openapi.ts +1689 -0
  173. package/src/codegen/operations.ts +76 -0
  174. package/src/codegen/pagination.ts +71 -0
  175. package/src/codegen/patches.test.ts +130 -0
  176. package/src/codegen/patches.ts +291 -0
  177. package/src/codegen/prelude.ts +70 -0
  178. package/src/codegen/proto.ts +1128 -0
  179. package/src/codegen/rewrite-operation-ids.test.ts +563 -0
  180. package/src/codegen/rewrite-operation-ids.ts +1206 -0
  181. package/src/codegen/spec-path.ts +115 -0
  182. package/src/error-category.ts +84 -0
  183. package/src/errors.ts +22 -25
  184. package/src/graphql.fixture.ts +371 -0
  185. package/src/graphql.test.ts +974 -0
  186. package/src/graphql.ts +1321 -0
  187. package/src/graphql.types.ts +185 -0
  188. package/src/json-patch.ts +95 -122
  189. package/src/pagination.ts +217 -146
  190. package/src/protocol-http.test.ts +107 -0
  191. package/src/protocol-http.ts +735 -0
  192. package/src/protocol-rest.ts +391 -0
  193. package/src/retry.ts +21 -22
  194. package/src/schema.ts +9 -10
  195. package/src/trait.ts +274 -0
  196. package/README.md +0 -30
  197. package/lib/client.d.ts +0 -167
  198. package/lib/client.d.ts.map +0 -1
  199. package/lib/client.js +0 -659
  200. package/lib/client.js.map +0 -1
  201. package/lib/schemas.d.ts +0 -60
  202. package/lib/schemas.d.ts.map +0 -1
  203. package/lib/schemas.js +0 -79
  204. package/lib/schemas.js.map +0 -1
  205. package/lib/sensitive.d.ts +0 -71
  206. package/lib/sensitive.d.ts.map +0 -1
  207. package/lib/sensitive.js +0 -96
  208. package/lib/sensitive.js.map +0 -1
  209. package/lib/traits.d.ts +0 -421
  210. package/lib/traits.d.ts.map +0 -1
  211. package/lib/traits.js +0 -737
  212. package/lib/traits.js.map +0 -1
  213. package/src/client.ts +0 -1177
  214. package/src/schemas.ts +0 -128
  215. package/src/sensitive.ts +0 -119
  216. package/src/traits.ts +0 -996
package/src/api.ts ADDED
@@ -0,0 +1,460 @@
1
+ import * as Context from "effect/Context";
2
+ import * as Effect from "effect/Effect";
3
+ import * as Layer from "effect/Layer";
4
+ import * as Option from "effect/Option";
5
+ import * as S from "effect/Schema";
6
+ import type * as AST from "effect/SchemaAST";
7
+ import { pipeArguments } from "effect/Pipeable";
8
+ import * as Ref from "effect/Ref";
9
+ import * as Scope from "effect/Scope";
10
+ import type * as Stream from "effect/Stream";
11
+ import { SingleShotGen } from "effect/Utils";
12
+ import * as Pagination from "./pagination.ts";
13
+ import { makeDefault, type Policy as RetryPolicy } from "./retry.ts";
14
+ import * as HttpClient from "effect/unstable/http/HttpClient";
15
+ import type * as HttpClientError from "effect/unstable/http/HttpClientError";
16
+ import type * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
17
+ import type * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
18
+
19
+ //#region Protocol
20
+
21
+ /**
22
+ * The view of an operation's config that protocols receive on every
23
+ * encode/decode call. The object is the same memoized config instance for
24
+ * every call to a given operation, so protocols can key per-operation
25
+ * preprocessing (handler construction, error maps) off its identity in a
26
+ * WeakMap.
27
+ */
28
+ export interface ProtocolOperationConfig {
29
+ readonly input?: S.Top;
30
+ readonly output?: S.Top;
31
+ readonly errors?: ReadonlyArray<ApiErrorClass>;
32
+ /**
33
+ * The wire operation name (e.g. the Smithy operation shape name). RPC-style
34
+ * protocols address operations by name (AWS JSON's `X-Amz-Target`, AWS
35
+ * Query's `Action=`) and unwrap responses by it; REST protocols ignore it.
36
+ */
37
+ readonly operationName?: string;
38
+ /**
39
+ * The smithy `smithy.api#endpoint` hostPrefix (e.g. `sync-` for Step
40
+ * Functions' StartSyncExecution). Protocols prepend it to the resolved
41
+ * endpoint host, substituting `{memberName}` labels from the input.
42
+ */
43
+ readonly endpointHostPrefix?: string;
44
+ readonly pagination?: Pagination.PaginatedTrait;
45
+ }
46
+
47
+ /**
48
+ * The Protocol service knows how to turn a value into an HTTP request using
49
+ * only the input schema's trait annotations, and how to turn a response back
50
+ * into an output value using the output schema's trait annotations.
51
+ *
52
+ * Swap implementations by providing a different `Layer<Protocol>`.
53
+ */
54
+ export class Protocol extends Context.Service<
55
+ Protocol,
56
+ {
57
+ readonly encode: (args: {
58
+ readonly input: unknown;
59
+ readonly inputAst: AST.AST;
60
+ /** The operation's config (memoized per operation — see {@link ProtocolOperationConfig}). */
61
+ readonly config: ProtocolOperationConfig;
62
+ }) => Effect.Effect<HttpClientRequest.HttpClientRequest>;
63
+ readonly decode: (args: {
64
+ readonly response: HttpClientResponse.HttpClientResponse;
65
+ readonly outputAst: AST.AST;
66
+ /**
67
+ * The operation's declared error classes (from `OperationConfig.errors`).
68
+ * Protocols use these to surface wire failures as the operation's typed
69
+ * errors — e.g. by consulting matcher metadata stamped on the class.
70
+ */
71
+ readonly errors: ReadonlyArray<ApiErrorClass>;
72
+ /** The operation's config (memoized per operation — see {@link ProtocolOperationConfig}). */
73
+ readonly config: ProtocolOperationConfig;
74
+ }) => Effect.Effect<unknown>;
75
+ }
76
+ >()("Protocol") {}
77
+
78
+ //#endregion
79
+
80
+ //#region Make
81
+
82
+ export type ApiErrorClass = {
83
+ new (...args: any[]): {
84
+ readonly _tag: string;
85
+ readonly message: string;
86
+ };
87
+ };
88
+
89
+ /**
90
+ * The shape of a generated SDK operation — usable two ways (mirrors the
91
+ * distilled repo's OperationMethod):
92
+ *
93
+ * 1. Direct call: `yield* operation(input)` — an Effect with requirements.
94
+ * 2. Yield first: `const fn = yield* operation` — captures the current
95
+ * context and returns a requirement-free call function.
96
+ *
97
+ * Generated service files annotate every exported operation with this type
98
+ * explicitly (against their hand-emitted interfaces), so the compiler never
99
+ * has to infer it back out of the schema generics.
100
+ */
101
+ export type OperationMethod<I, O, E, R> = Effect.Effect<
102
+ (input: I) => Effect.Effect<O, E, never>,
103
+ never,
104
+ R
105
+ > &
106
+ ((input: I) => Effect.Effect<O, E, R>);
107
+
108
+ // NOTE: request/response ASTs are handed to the protocol RAW (Suspend nodes
109
+ // included). Pre-resolving Suspends here would drop annotations that sit on
110
+ // intermediate nodes — e.g. a bare-payload response whose payload type is
111
+ // itself a suspended schema carries its EnvelopePayloadRoot marker on the
112
+ // inner Suspend, and unwrapping it silently degrades decode to the struct
113
+ // path. The protocol helpers (core/protocol-http getAnn/getProps) descend
114
+ // Suspends themselves, and `Suspend.thunk` memoizes, so raw ASTs cost
115
+ // nothing extra.
116
+
117
+ /**
118
+ * Protocol layers are built once per process and shared by every operation
119
+ * call (keyed by layer value identity — generated operations all reference the
120
+ * same module-level layer const). The build runs against a process-lifetime
121
+ * scope, and concurrent first calls are deduplicated through a private
122
+ * MemoMap.
123
+ *
124
+ * Contract for protocol implementations: the layer build must not capture
125
+ * per-call context. Anything call-dependent — credentials, per-request
126
+ * options — must be resolved inside `encode`/`decode`, which execute on the
127
+ * calling fiber and therefore see the caller's services on every request.
128
+ */
129
+ const protocolMemoMap = Layer.makeMemoMapUnsafe();
130
+ const protocolScope = Scope.makeUnsafe();
131
+ const protocolContexts = new WeakMap<
132
+ Layer.Layer<Protocol, any, any>,
133
+ Context.Context<Protocol>
134
+ >();
135
+
136
+ const protocolContext = <PE, PR>(
137
+ layer: Layer.Layer<Protocol, PE, PR>,
138
+ ): Effect.Effect<Context.Context<Protocol>, PE, PR> => {
139
+ const cached = protocolContexts.get(layer);
140
+ if (cached) return Effect.succeed(cached);
141
+ return Effect.map(
142
+ Layer.buildWithMemoMap(layer, protocolMemoMap, protocolScope),
143
+ (ctx) => {
144
+ protocolContexts.set(layer, ctx);
145
+ return ctx;
146
+ },
147
+ );
148
+ };
149
+
150
+ export interface OperationConfig<
151
+ I extends S.Top,
152
+ O extends S.Top,
153
+ PE,
154
+ PR,
155
+ E extends readonly ApiErrorClass[] = readonly ApiErrorClass[],
156
+ > {
157
+ input?: I;
158
+ output?: O;
159
+ errors?: E;
160
+ /**
161
+ * The protocol layer that knows how to encode/decode this operation's wire
162
+ * format. Built once per process and shared across all operations that
163
+ * reference the same layer value — it must resolve call-dependent services
164
+ * (credentials etc.) inside encode/decode, not at build time.
165
+ */
166
+ protocol: Layer.Layer<Protocol, PE, PR>;
167
+ /**
168
+ * SDK-specific retry service tag (see `core/retry`). When provided and the
169
+ * caller's context carries a policy under this tag, every call is retried
170
+ * per that policy (e.g. `Layer.succeed(Cloudflare.Retry.Retry, factory)`).
171
+ */
172
+ retry?: Context.Key<any, RetryPolicy>;
173
+ /**
174
+ * The wire operation name for RPC-style protocols (see
175
+ * {@link ProtocolOperationConfig.operationName}).
176
+ */
177
+ operationName?: string;
178
+ /**
179
+ * Host prefix from the smithy endpoint trait (see
180
+ * {@link ProtocolOperationConfig.endpointHostPrefix}).
181
+ */
182
+ endpointHostPrefix?: string;
183
+ }
184
+
185
+ /**
186
+ * Wrap one operation call with the caller-provided retry policy, if the
187
+ * operation declares a retry tag and the context carries a policy for it.
188
+ */
189
+ const applyRetry = (
190
+ base: Effect.Effect<any, any, any>,
191
+ retryKey: Context.Key<any, RetryPolicy> | undefined,
192
+ ): Effect.Effect<any, any, any> =>
193
+ retryKey === undefined
194
+ ? base
195
+ : Effect.flatMap(Effect.serviceOption(retryKey), (opt) =>
196
+ Effect.gen(function* () {
197
+ const lastError = yield* Ref.make<unknown>(undefined);
198
+ // No policy in context → makeDefault (transient/throttling/server
199
+ // errors, capped exponential backoff + jitter, honors server
200
+ // retryAfter hints). Mirrors the distilled client.
201
+ const policy = Option.isSome(opt) ? opt.value : makeDefault;
202
+ const opts =
203
+ typeof policy === "function" ? policy(lastError) : policy;
204
+ if (!opts.while) return yield* base;
205
+ return yield* base.pipe(
206
+ Effect.tapError((e) => Ref.set(lastError, e)),
207
+ Effect.retry({
208
+ while: (e: unknown) => opts.while!(e),
209
+ ...(opts.schedule ? { schedule: opts.schedule as any } : {}),
210
+ }),
211
+ );
212
+ }),
213
+ );
214
+
215
+ export function make<
216
+ I extends S.Top,
217
+ O extends S.Top,
218
+ PE,
219
+ PR,
220
+ const E extends readonly ApiErrorClass[] = readonly [],
221
+ >(
222
+ configFn: () => OperationConfig<I, O, PE, PR, E>,
223
+ ): OperationMethod<
224
+ S.Schema.Type<I>,
225
+ S.Schema.Type<O>,
226
+ InstanceType<E[number]> | PE | HttpClientError.HttpClientError,
227
+ PR | HttpClient.HttpClient
228
+ > {
229
+ // Lazily resolve the operation config + schema ASTs on first call, not at
230
+ // module-load time. Generated SDKs wrap each request/response schema in
231
+ // `Schema.suspend(() => ...)`; forcing them here (rather than when the
232
+ // `export const` is evaluated) keeps importing a service module cheap and
233
+ // only pays the schema-construction cost for operations that are actually
234
+ // called. Memoized so subsequent calls are free.
235
+ interface Prepared {
236
+ readonly cfg: OperationConfig<I, O, PE, PR, E>;
237
+ readonly inputAst: AST.AST;
238
+ readonly outputAst: AST.AST;
239
+ }
240
+ let prepared: Prepared | undefined;
241
+ const prepare = (): Prepared => {
242
+ if (prepared) return prepared;
243
+ const cfg = configFn();
244
+ prepared = {
245
+ cfg,
246
+ inputAst: cfg.input!.ast,
247
+ outputAst: cfg.output!.ast,
248
+ };
249
+ return prepared;
250
+ };
251
+ const fn = (input: unknown) =>
252
+ Effect.suspend(() => {
253
+ const { cfg, inputAst, outputAst } = prepare();
254
+ const call = Effect.flatMap(
255
+ protocolContext(cfg.protocol),
256
+ (protocolCtx) =>
257
+ Effect.gen(function* () {
258
+ const protocol = yield* Protocol;
259
+ const client = yield* HttpClient.HttpClient;
260
+ const request = yield* protocol.encode({
261
+ input,
262
+ inputAst,
263
+ config: cfg,
264
+ });
265
+ const response = yield* client.execute(request);
266
+ return yield* protocol.decode({
267
+ response,
268
+ outputAst,
269
+ errors: cfg.errors ?? [],
270
+ config: cfg,
271
+ });
272
+ }).pipe(Effect.provideContext(protocolCtx)),
273
+ );
274
+ return applyRetry(call, cfg.retry);
275
+ });
276
+
277
+ // Make the operation itself yieldable: `yield* operation` captures the
278
+ // current context and returns a requirement-free call function (mirrors
279
+ // the distilled repo's OperationMethod). The captured context is a
280
+ // FALLBACK, not a snapshot: entries present on the calling fiber at call
281
+ // time win, so per-call overrides — e.g. providing `Endpoint` with a
282
+ // discovered data-plane address around one invocation — take effect
283
+ // instead of being shadowed by the context captured at yield time.
284
+ const Proto = {
285
+ [Symbol.iterator](this: any) {
286
+ return new SingleShotGen(this.asEffect());
287
+ },
288
+ pipe(this: any) {
289
+ return pipeArguments(this.asEffect(), arguments);
290
+ },
291
+ asEffect() {
292
+ return Effect.map(
293
+ Effect.context(),
294
+ (context) => (input: unknown) =>
295
+ // `Context` is contravariant, so `Context<never>` is not assignable
296
+ // to the `Context<any>` updateContext expects — the widening cast is
297
+ // sound (the merge only ever adds entries).
298
+ Effect.updateContext(fn(input), (current): Context.Context<any> =>
299
+ Context.merge(context, current),
300
+ ),
301
+ );
302
+ },
303
+ };
304
+ Object.assign(fn, Proto);
305
+
306
+ // Debug/introspection surface carried over from the distilled client:
307
+ // the operation's metadata is readable off the exported callable
308
+ // (e.g. `iam.listRoles.input` / `.output` / `.errors` / `.operationName`
309
+ // / `.pagination`). Lazy getters, so importing a service module doesn't
310
+ // force its suspended schemas.
311
+ Object.defineProperties(fn, {
312
+ input: { get: () => prepare().cfg.input, configurable: true },
313
+ output: { get: () => prepare().cfg.output, configurable: true },
314
+ errors: { get: () => prepare().cfg.errors ?? [], configurable: true },
315
+ operationName: {
316
+ get: () => prepare().cfg.operationName,
317
+ configurable: true,
318
+ },
319
+ pagination: {
320
+ get: () =>
321
+ (prepare().cfg as PaginatedOperationConfig<I, O, PE, PR, E>).pagination,
322
+ configurable: true,
323
+ },
324
+ });
325
+
326
+ return fn as any;
327
+ }
328
+
329
+ //#endregion
330
+
331
+ //#region MakePaginated
332
+
333
+ /**
334
+ * STRUCTURAL fallback for the element type `.items()` yields: the array
335
+ * element of the page response itself, or of its `result` member.
336
+ *
337
+ * It can only see shapes it was taught, so any other envelope
338
+ * (`{ branches: […] }`, `{ data: […] }`, …) lands on `unknown` — which is
339
+ * why generators pass the item type EXPLICITLY as
340
+ * {@link PaginatedOperationMethod}'s `Item` argument, resolved from the
341
+ * pagination trait's `items` path against the response shape. This default
342
+ * only applies to hand-written operations.
343
+ */
344
+ export type PaginatedItem<A> =
345
+ A extends ReadonlyArray<infer Item>
346
+ ? Item
347
+ : A extends { result: ReadonlyArray<infer Item> }
348
+ ? Item
349
+ : A extends { result?: ReadonlyArray<infer Item> | null | undefined }
350
+ ? Item
351
+ : unknown;
352
+
353
+ /**
354
+ * The callable half of a paginated operation: the operation call itself, plus
355
+ * `.pages(input)` streaming every page response and `.items(input)` streaming
356
+ * the individual items across pages.
357
+ *
358
+ * Used twice in {@link PaginatedOperationMethod} — once with the operation's
359
+ * requirements (`R`) for the direct-call style, once with `R = never` for the
360
+ * value `yield* operation` hands back, which has the caller's context baked in.
361
+ */
362
+ export interface PaginatedCall<I, O, E, R, Item> {
363
+ (input: I): Effect.Effect<O, E, R>;
364
+ readonly pages: (input: I) => Stream.Stream<O, E, R>;
365
+ readonly items: (input: I) => Stream.Stream<Item, E, R>;
366
+ }
367
+
368
+ /**
369
+ * A paginated operation: usable both ways an {@link OperationMethod} is, with
370
+ * `.pages` / `.items` on BOTH — the operation object for
371
+ * `yield* op.items(input)`, and the requirement-free call function for
372
+ * `const op = yield* operation; yield* op.items(input)` (distilled #145).
373
+ */
374
+ export type PaginatedOperationMethod<
375
+ I,
376
+ O,
377
+ E,
378
+ R,
379
+ Item = PaginatedItem<O>,
380
+ > = Effect.Effect<PaginatedCall<I, O, E, never, Item>, never, R> &
381
+ PaginatedCall<I, O, E, R, Item>;
382
+
383
+ export interface PaginatedOperationConfig<
384
+ I extends S.Top,
385
+ O extends S.Top,
386
+ PE,
387
+ PR,
388
+ E extends readonly ApiErrorClass[] = readonly ApiErrorClass[],
389
+ > extends OperationConfig<I, O, PE, PR, E> {
390
+ /** How to advance between pages (mirrors the `smithy.api#paginated` trait). */
391
+ pagination: Pagination.PaginatedTrait;
392
+ }
393
+
394
+ /**
395
+ * Like {@link make}, plus `.pages()` / `.items()` streaming built on the
396
+ * operation's pagination trait. The SDK can pass a provider-specific
397
+ * {@link Pagination.PaginationStrategy} (e.g. Cloudflare stops page-mode
398
+ * traversal on the first empty page); the default dispatches on
399
+ * `pagination.mode`.
400
+ */
401
+ export function makePaginated<
402
+ I extends S.Top,
403
+ O extends S.Top,
404
+ PE,
405
+ PR,
406
+ const E extends readonly ApiErrorClass[] = readonly [],
407
+ >(
408
+ configFn: () => PaginatedOperationConfig<I, O, PE, PR, E>,
409
+ strategy?: Pagination.PaginationStrategy,
410
+ ): PaginatedOperationMethod<
411
+ S.Schema.Type<I>,
412
+ S.Schema.Type<O>,
413
+ InstanceType<E[number]> | PE | HttpClientError.HttpClientError,
414
+ PR | HttpClient.HttpClient
415
+ > {
416
+ const fn: any = make(configFn);
417
+ // configFn is a cheap object literal over already-constructed consts —
418
+ // re-invoking it here just reads the pagination trait (memoized).
419
+ let pagination: Pagination.PaginatedTrait | undefined;
420
+ const pag = () => (pagination ??= configFn().pagination);
421
+ const paginate = strategy ?? Pagination.paginateWithDefaults;
422
+
423
+ // The streams are built over a CALL FUNCTION, so the same code serves both
424
+ // call styles: the operation itself (requirements intact), and the
425
+ // context-bound function `yield* operation` hands back — whose streams
426
+ // inherit that captured context and so need nothing after the yield.
427
+ const withStreams = (
428
+ call: (input: any) => Effect.Effect<any, any, any>,
429
+ ): any => {
430
+ const pages = (input: Record<string, unknown>) =>
431
+ paginate(call, input, pag());
432
+ const items = (input: Record<string, unknown>) => {
433
+ const p = pag();
434
+ return p.items
435
+ ? Pagination.extractItems(pages(input), p.items)
436
+ : pages(input);
437
+ };
438
+ return Object.assign(call, { pages, items });
439
+ };
440
+
441
+ // `yield* operation` runs through `make`'s Proto.asEffect, which builds a
442
+ // fresh arrow and would drop `.pages` / `.items` (distilled #145). Override
443
+ // it here — Proto's `[Symbol.iterator]`/`pipe` dispatch on `this`, so both
444
+ // pick this up. Only paginated operations pay for it. Same fallback (not
445
+ // snapshot) semantics as Proto.asEffect: call-time fiber entries win over
446
+ // the captured context.
447
+ fn.asEffect = () =>
448
+ Effect.map(Effect.context(), (context) =>
449
+ withStreams((input: unknown) =>
450
+ // Same contravariance widening as Proto.asEffect above.
451
+ Effect.updateContext(fn(input), (current): Context.Context<any> =>
452
+ Context.merge(context, current),
453
+ ),
454
+ ),
455
+ );
456
+
457
+ return withStreams(fn);
458
+ }
459
+
460
+ //#endregion
package/src/category.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  * ```ts
10
10
  * import * as Category from "@distilled.cloud/core/category";
11
11
  *
12
- * export class Unauthorized extends Schema.TaggedErrorClass<Unauthorized>()(
12
+ * export class Unauthorized extends Schema.TaggedError<Unauthorized>()(
13
13
  * "Unauthorized",
14
14
  * { message: Schema.String },
15
15
  * ).pipe(Category.withAuthError) {}
@@ -92,7 +92,7 @@ export interface RetryableInfo {
92
92
  *
93
93
  * @example
94
94
  * ```ts
95
- * export class MyError extends Schema.TaggedErrorClass<MyError>()(
95
+ * export class MyError extends Schema.TaggedError<MyError>()(
96
96
  * "MyError",
97
97
  * { message: Schema.String },
98
98
  * ).pipe(Category.withCategory(Category.AuthError)) {}
@@ -123,13 +123,13 @@ export const withCategory =
123
123
  * @example
124
124
  * ```ts
125
125
  * // Standard retryable error
126
- * export class TransientError extends Schema.TaggedErrorClass<TransientError>()(
126
+ * export class TransientError extends Schema.TaggedError<TransientError>()(
127
127
  * "TransientError",
128
128
  * { message: Schema.String },
129
129
  * ).pipe(Category.withRetryable()) {}
130
130
  *
131
131
  * // Throttling error (uses longer backoff)
132
- * export class RateLimitError extends Schema.TaggedErrorClass<RateLimitError>()(
132
+ * export class RateLimitError extends Schema.TaggedError<RateLimitError>()(
133
133
  * "RateLimitError",
134
134
  * { message: Schema.String },
135
135
  * ).pipe(Category.withRetryable({ throttling: true })) {}
@@ -0,0 +1,168 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import {
3
+ booleanStringEnums,
4
+ STRING_ENCODED_TRAIT,
5
+ } from "./boolean-string-enums.ts";
6
+ import { generateService } from "./generator.ts";
7
+
8
+ const boolEnum = {
9
+ type: "enum",
10
+ members: {
11
+ TRUE: {
12
+ target: "smithy.api#Unit",
13
+ traits: { "smithy.api#enumValue": "true" },
14
+ },
15
+ FALSE: {
16
+ target: "smithy.api#Unit",
17
+ traits: { "smithy.api#enumValue": "false" },
18
+ },
19
+ },
20
+ };
21
+
22
+ const model = (shapes: Record<string, any>) => ({ smithy: "2.0", shapes });
23
+
24
+ describe("booleanStringEnums", () => {
25
+ test("retargets a request member and stamps the trait", () => {
26
+ const m = model({
27
+ "com.example.x#Flag": boolEnum,
28
+ "com.example.x#Request": {
29
+ type: "structure",
30
+ traits: { "smithy.api#input": {} },
31
+ members: { validation_enabled: { target: "com.example.x#Flag" } },
32
+ },
33
+ });
34
+ expect(booleanStringEnums(m)).toEqual({ members: 1, lists: 0 });
35
+ const member = m.shapes["com.example.x#Request"].members.validation_enabled;
36
+ expect(member.target).toBe("smithy.api#Boolean");
37
+ expect(member.traits[STRING_ENCODED_TRAIT]).toEqual({});
38
+ });
39
+
40
+ test("leaves response members alone", () => {
41
+ const m = model({
42
+ "com.example.x#Flag": boolEnum,
43
+ "com.example.x#Response": {
44
+ type: "structure",
45
+ members: { active: { target: "com.example.x#Flag" } },
46
+ },
47
+ });
48
+ expect(booleanStringEnums(m)).toEqual({ members: 0, lists: 0 });
49
+ expect(m.shapes["com.example.x#Response"].members.active.target).toBe(
50
+ "com.example.x#Flag",
51
+ );
52
+ });
53
+
54
+ test("retargets a request-only list's element type", () => {
55
+ const m = model({
56
+ "com.example.x#Flag": boolEnum,
57
+ "com.example.x#FlagList": {
58
+ type: "list",
59
+ member: { target: "com.example.x#Flag" },
60
+ },
61
+ "com.example.x#Request": {
62
+ type: "structure",
63
+ traits: { "smithy.api#input": {} },
64
+ members: { unique_entries: { target: "com.example.x#FlagList" } },
65
+ },
66
+ });
67
+ expect(booleanStringEnums(m)).toEqual({ members: 1, lists: 1 });
68
+ expect(m.shapes["com.example.x#FlagList"].member.target).toBe(
69
+ "smithy.api#Boolean",
70
+ );
71
+ });
72
+
73
+ test("leaves a list a response also uses", () => {
74
+ const m = model({
75
+ "com.example.x#Flag": boolEnum,
76
+ "com.example.x#FlagList": {
77
+ type: "list",
78
+ member: { target: "com.example.x#Flag" },
79
+ },
80
+ "com.example.x#Request": {
81
+ type: "structure",
82
+ traits: { "smithy.api#input": {} },
83
+ members: { unique_entries: { target: "com.example.x#FlagList" } },
84
+ },
85
+ "com.example.x#Response": {
86
+ type: "structure",
87
+ members: { seen: { target: "com.example.x#FlagList" } },
88
+ },
89
+ });
90
+ expect(booleanStringEnums(m)).toEqual({ members: 0, lists: 0 });
91
+ expect(m.shapes["com.example.x#FlagList"].member.target).toBe(
92
+ "com.example.x#Flag",
93
+ );
94
+ });
95
+
96
+ test("leaves an enum that is not exactly true/false", () => {
97
+ const m = model({
98
+ "com.example.x#Tri": {
99
+ type: "enum",
100
+ members: {
101
+ TRUE: {
102
+ target: "smithy.api#Unit",
103
+ traits: { "smithy.api#enumValue": "true" },
104
+ },
105
+ FALSE: {
106
+ target: "smithy.api#Unit",
107
+ traits: { "smithy.api#enumValue": "false" },
108
+ },
109
+ AUTO: {
110
+ target: "smithy.api#Unit",
111
+ traits: { "smithy.api#enumValue": "auto" },
112
+ },
113
+ },
114
+ },
115
+ "com.example.x#Request": {
116
+ type: "structure",
117
+ traits: { "smithy.api#input": {} },
118
+ members: { mode: { target: "com.example.x#Tri" } },
119
+ },
120
+ });
121
+ expect(booleanStringEnums(m)).toEqual({ members: 0, lists: 0 });
122
+ });
123
+ });
124
+
125
+ describe("generateService", () => {
126
+ test("emits a boolean piped through T.StringEncoded()", () => {
127
+ const { code } = generateService(
128
+ model({
129
+ "com.example.x#Example": {
130
+ type: "service",
131
+ version: "2024-01-01",
132
+ operations: [{ target: "com.example.x#GetThing" }],
133
+ },
134
+ "com.example.x#GetThing": {
135
+ type: "operation",
136
+ input: { target: "com.example.x#GetThingRequest" },
137
+ output: { target: "smithy.api#Unit" },
138
+ traits: { "smithy.api#http": { method: "GET", uri: "/things" } },
139
+ },
140
+ "com.example.x#GetThingRequest": {
141
+ type: "structure",
142
+ traits: { "smithy.api#input": {} },
143
+ members: {
144
+ verbose: {
145
+ target: "com.example.x#Flag",
146
+ traits: { "smithy.api#httpQuery": "verbose" },
147
+ },
148
+ },
149
+ },
150
+ "com.example.x#Flag": boolEnum,
151
+ }),
152
+ {
153
+ operationDecl: {
154
+ contextType: "ExampleOpContext",
155
+ commonErrorType: "ExampleOpError",
156
+ commonErrorClasses: [],
157
+ protocol: "ExampleProtocol",
158
+ retry: "Retry.Retry",
159
+ },
160
+ },
161
+ );
162
+ expect(code).toContain(
163
+ '"verbose": S.optional(S.Boolean.pipe(T.Query(), T.StringEncoded())),',
164
+ );
165
+ expect(code).toContain("verbose?: boolean;");
166
+ expect(code).not.toContain('"true" | "false"');
167
+ });
168
+ });