@distilled.cloud/core 1.0.0-rc.2 → 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.
Files changed (321) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +33 -0
  3. package/lib/_bundle/aws.js +2 -0
  4. package/lib/_bundle/aws.js.map +1 -0
  5. package/lib/_bundle/base.js +5 -0
  6. package/lib/_bundle/base.js.map +1 -0
  7. package/lib/_bundle/graphql.js +3 -0
  8. package/lib/_bundle/graphql.js.map +1 -0
  9. package/lib/_bundle/grpc.js +3 -0
  10. package/lib/_bundle/grpc.js.map +1 -0
  11. package/lib/_bundle/rest.js +2 -0
  12. package/lib/_bundle/rest.js.map +1 -0
  13. package/lib/_bundle/xml.js +4 -0
  14. package/lib/_bundle/xml.js.map +1 -0
  15. package/lib/annotations.d.ts +17 -0
  16. package/lib/annotations.d.ts.map +1 -0
  17. package/lib/annotations.js +2 -0
  18. package/lib/annotations.js.map +1 -0
  19. package/lib/api.d.ts +220 -51
  20. package/lib/api.d.ts.map +1 -1
  21. package/lib/api.js +1 -178
  22. package/lib/category.d.ts +19 -16
  23. package/lib/category.d.ts.map +1 -1
  24. package/lib/category.js +1 -280
  25. package/lib/chunks.d.ts +3 -0
  26. package/lib/chunks.d.ts.map +1 -0
  27. package/lib/chunks.js +1 -0
  28. package/lib/config.d.ts +81 -0
  29. package/lib/config.d.ts.map +1 -0
  30. package/lib/config.js +1 -0
  31. package/lib/crypto.d.ts +76 -0
  32. package/lib/crypto.d.ts.map +1 -0
  33. package/lib/crypto.js +1 -0
  34. package/lib/error-category.d.ts +1 -1
  35. package/lib/error-category.d.ts.map +1 -1
  36. package/lib/error-category.js +1 -46
  37. package/lib/errors.d.ts +99 -74
  38. package/lib/errors.d.ts.map +1 -1
  39. package/lib/errors.js +1 -186
  40. package/lib/graphql.d.ts +420 -0
  41. package/lib/graphql.d.ts.map +1 -0
  42. package/lib/graphql.js +1 -0
  43. package/lib/graphql.types.d.ts +11 -0
  44. package/lib/graphql.types.d.ts.map +1 -0
  45. package/lib/graphql.types.js +1 -0
  46. package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
  47. package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
  48. package/lib/grpc-transport-cloudflare-http2.js +6 -0
  49. package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
  50. package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
  51. package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
  52. package/lib/grpc-transport-cloudflare-sockets.js +5 -0
  53. package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
  54. package/lib/grpc-transport-node-http2.d.ts +19 -0
  55. package/lib/grpc-transport-node-http2.d.ts.map +1 -0
  56. package/lib/grpc-transport-node-http2.js +2 -0
  57. package/lib/grpc-transport-node-http2.js.map +1 -0
  58. package/lib/grpc-transport.d.ts +111 -0
  59. package/lib/grpc-transport.d.ts.map +1 -0
  60. package/lib/grpc-transport.js +1 -0
  61. package/lib/hpack.d.ts +34 -0
  62. package/lib/hpack.d.ts.map +1 -0
  63. package/lib/hpack.js +2 -0
  64. package/lib/hpack.js.map +1 -0
  65. package/lib/index.d.ts +32 -0
  66. package/lib/index.d.ts.map +1 -0
  67. package/lib/index.js +17 -0
  68. package/lib/max-message-bytes.d.ts +42 -0
  69. package/lib/max-message-bytes.d.ts.map +1 -0
  70. package/lib/max-message-bytes.js +2 -0
  71. package/lib/max-message-bytes.js.map +1 -0
  72. package/lib/ndjson.d.ts +3 -0
  73. package/lib/ndjson.d.ts.map +1 -0
  74. package/lib/ndjson.js +2 -0
  75. package/lib/ndjson.js.map +1 -0
  76. package/lib/pagination.d.ts +105 -25
  77. package/lib/pagination.d.ts.map +1 -1
  78. package/lib/pagination.js +1 -182
  79. package/lib/plain-values.d.ts +21 -0
  80. package/lib/plain-values.d.ts.map +1 -0
  81. package/lib/plain-values.js +2 -0
  82. package/lib/plain-values.js.map +1 -0
  83. package/lib/protobuf.d.ts +85 -0
  84. package/lib/protobuf.d.ts.map +1 -0
  85. package/lib/protobuf.js +1 -0
  86. package/lib/protocol-grpc.d.ts +123 -0
  87. package/lib/protocol-grpc.d.ts.map +1 -0
  88. package/lib/protocol-grpc.js +1 -0
  89. package/lib/protocol-http.d.ts +206 -25
  90. package/lib/protocol-http.d.ts.map +1 -1
  91. package/lib/protocol-http.js +1 -554
  92. package/lib/protocol-rest.d.ts +258 -64
  93. package/lib/protocol-rest.d.ts.map +1 -1
  94. package/lib/protocol-rest.js +1 -242
  95. package/lib/protocol.d.ts +113 -0
  96. package/lib/protocol.d.ts.map +1 -0
  97. package/lib/protocol.js +0 -0
  98. package/lib/query.d.ts +29 -0
  99. package/lib/query.d.ts.map +1 -0
  100. package/lib/query.js +1 -0
  101. package/lib/response-validation-strict.d.ts +48 -0
  102. package/lib/response-validation-strict.d.ts.map +1 -0
  103. package/lib/response-validation-strict.js +1 -0
  104. package/lib/response-validation.d.ts +86 -0
  105. package/lib/response-validation.d.ts.map +1 -0
  106. package/lib/response-validation.js +1 -0
  107. package/lib/retry-after.d.ts +4 -4
  108. package/lib/retry-after.d.ts.map +1 -1
  109. package/lib/retry-after.js +1 -112
  110. package/lib/retry-state.d.ts +26 -0
  111. package/lib/retry-state.d.ts.map +1 -0
  112. package/lib/retry-state.js +2 -0
  113. package/lib/retry-state.js.map +1 -0
  114. package/lib/retry.d.ts +141 -42
  115. package/lib/retry.d.ts.map +1 -1
  116. package/lib/retry.js +1 -210
  117. package/lib/schema-lite-impl.d.ts +179 -0
  118. package/lib/schema-lite-impl.d.ts.map +1 -0
  119. package/lib/schema-lite-impl.js +1 -0
  120. package/lib/schema-lite-typed.d.ts +41 -0
  121. package/lib/schema-lite-typed.d.ts.map +1 -0
  122. package/lib/schema-lite-typed.js +1 -0
  123. package/lib/schema-lite.d.ts +41 -0
  124. package/lib/schema-lite.d.ts.map +1 -0
  125. package/lib/schema-lite.js +1 -0
  126. package/lib/schema.d.ts +21 -7
  127. package/lib/schema.d.ts.map +1 -1
  128. package/lib/schema.js +1 -48
  129. package/lib/sdk-error.d.ts +31 -0
  130. package/lib/sdk-error.d.ts.map +1 -0
  131. package/lib/sdk-error.js +1 -0
  132. package/lib/send.d.ts +37 -0
  133. package/lib/send.d.ts.map +1 -0
  134. package/lib/send.js +2 -0
  135. package/lib/send.js.map +1 -0
  136. package/lib/sensitive.d.ts +28 -0
  137. package/lib/sensitive.d.ts.map +1 -0
  138. package/lib/sensitive.js +2 -0
  139. package/lib/sensitive.js.map +1 -0
  140. package/lib/telemetry-debug.d.ts +6 -0
  141. package/lib/telemetry-debug.d.ts.map +1 -0
  142. package/lib/telemetry-debug.js +2 -0
  143. package/lib/telemetry-debug.js.map +1 -0
  144. package/lib/telemetry-hooks.d.ts +32 -0
  145. package/lib/telemetry-hooks.d.ts.map +1 -0
  146. package/lib/telemetry-hooks.js +2 -0
  147. package/lib/telemetry-hooks.js.map +1 -0
  148. package/lib/telemetry-host-rules.d.ts +23 -0
  149. package/lib/telemetry-host-rules.d.ts.map +1 -0
  150. package/lib/telemetry-host-rules.js +2 -0
  151. package/lib/telemetry-host-rules.js.map +1 -0
  152. package/lib/telemetry-lambda-context.d.ts +17 -0
  153. package/lib/telemetry-lambda-context.d.ts.map +1 -0
  154. package/lib/telemetry-lambda-context.js +2 -0
  155. package/lib/telemetry-lambda-context.js.map +1 -0
  156. package/lib/telemetry-message-context.d.ts +52 -0
  157. package/lib/telemetry-message-context.d.ts.map +1 -0
  158. package/lib/telemetry-message-context.js +2 -0
  159. package/lib/telemetry-message-context.js.map +1 -0
  160. package/lib/telemetry-metrics.d.ts +6 -0
  161. package/lib/telemetry-metrics.d.ts.map +1 -0
  162. package/lib/telemetry-metrics.js +2 -0
  163. package/lib/telemetry-metrics.js.map +1 -0
  164. package/lib/telemetry-name-template.d.ts +13 -0
  165. package/lib/telemetry-name-template.d.ts.map +1 -0
  166. package/lib/telemetry-name-template.js +2 -0
  167. package/lib/telemetry-name-template.js.map +1 -0
  168. package/lib/telemetry-stream.d.ts +11 -0
  169. package/lib/telemetry-stream.d.ts.map +1 -0
  170. package/lib/telemetry-stream.js +2 -0
  171. package/lib/telemetry-stream.js.map +1 -0
  172. package/lib/telemetry-xray.d.ts +28 -0
  173. package/lib/telemetry-xray.d.ts.map +1 -0
  174. package/lib/telemetry-xray.js +2 -0
  175. package/lib/telemetry-xray.js.map +1 -0
  176. package/lib/telemetry.d.ts +317 -0
  177. package/lib/telemetry.d.ts.map +1 -0
  178. package/lib/telemetry.js +3 -0
  179. package/lib/telemetry.js.map +1 -0
  180. package/lib/token-cache.d.ts +31 -0
  181. package/lib/token-cache.d.ts.map +1 -0
  182. package/lib/token-cache.js +2 -0
  183. package/lib/token-cache.js.map +1 -0
  184. package/lib/trait.d.ts +94 -20
  185. package/lib/trait.d.ts.map +1 -1
  186. package/lib/trait.js +1 -107
  187. package/lib/unknown-error.d.ts +18 -0
  188. package/lib/unknown-error.d.ts.map +1 -0
  189. package/lib/unknown-error.js +1 -0
  190. package/lib/xml.d.ts +44 -0
  191. package/lib/xml.d.ts.map +1 -0
  192. package/lib/xml.js +1 -0
  193. package/package.json +13 -13
  194. package/src/annotations.ts +53 -0
  195. package/src/api.ts +1597 -234
  196. package/src/category.ts +97 -134
  197. package/src/chunks.ts +30 -0
  198. package/src/config.ts +261 -0
  199. package/src/crypto.ts +202 -0
  200. package/src/error-category.ts +16 -29
  201. package/src/errors.ts +99 -77
  202. package/src/graphql.ts +1817 -0
  203. package/src/graphql.types.ts +120 -0
  204. package/src/grpc-transport-cloudflare-http2.ts +569 -0
  205. package/src/grpc-transport-cloudflare-sockets.ts +368 -0
  206. package/src/grpc-transport-node-http2.ts +307 -0
  207. package/src/grpc-transport.ts +287 -0
  208. package/src/hpack.ts +331 -0
  209. package/src/index.ts +31 -0
  210. package/src/max-message-bytes.ts +58 -0
  211. package/src/ndjson.ts +55 -0
  212. package/src/pagination.ts +379 -94
  213. package/src/plain-values.ts +70 -0
  214. package/src/protobuf.ts +1367 -0
  215. package/src/protocol-grpc.ts +811 -0
  216. package/src/protocol-http.ts +1045 -371
  217. package/src/protocol-rest.ts +908 -255
  218. package/src/protocol.ts +150 -0
  219. package/src/query.ts +39 -0
  220. package/src/response-validation-strict.ts +333 -0
  221. package/src/response-validation.ts +161 -0
  222. package/src/retry-after.ts +8 -14
  223. package/src/retry-state.ts +105 -0
  224. package/src/retry.ts +260 -145
  225. package/src/schema-lite-impl.ts +621 -0
  226. package/src/schema-lite-typed.ts +44 -0
  227. package/src/schema-lite.ts +45 -0
  228. package/src/schema.ts +259 -15
  229. package/src/sdk-error.ts +37 -0
  230. package/src/send.ts +156 -0
  231. package/src/sensitive.ts +188 -0
  232. package/src/telemetry-debug.ts +179 -0
  233. package/src/telemetry-hooks.ts +33 -0
  234. package/src/telemetry-host-rules.ts +39 -0
  235. package/src/telemetry-lambda-context.ts +46 -0
  236. package/src/telemetry-message-context.ts +170 -0
  237. package/src/telemetry-metrics.ts +69 -0
  238. package/src/telemetry-name-template.ts +26 -0
  239. package/src/telemetry-stream.ts +100 -0
  240. package/src/telemetry-xray.ts +66 -0
  241. package/src/telemetry.ts +800 -0
  242. package/src/token-cache.ts +117 -0
  243. package/src/trait.ts +139 -67
  244. package/src/unknown-error.ts +38 -0
  245. package/src/xml.ts +346 -0
  246. package/lib/api.js.map +0 -1
  247. package/lib/category.js.map +0 -1
  248. package/lib/codegen/cli.d.ts +0 -29
  249. package/lib/codegen/cli.d.ts.map +0 -1
  250. package/lib/codegen/cli.js +0 -165
  251. package/lib/codegen/cli.js.map +0 -1
  252. package/lib/codegen/emit.d.ts +0 -129
  253. package/lib/codegen/emit.d.ts.map +0 -1
  254. package/lib/codegen/emit.js +0 -105
  255. package/lib/codegen/emit.js.map +0 -1
  256. package/lib/codegen/format.d.ts +0 -23
  257. package/lib/codegen/format.d.ts.map +0 -1
  258. package/lib/codegen/format.js +0 -28
  259. package/lib/codegen/format.js.map +0 -1
  260. package/lib/codegen/generator.d.ts +0 -334
  261. package/lib/codegen/generator.d.ts.map +0 -1
  262. package/lib/codegen/generator.js +0 -691
  263. package/lib/codegen/generator.js.map +0 -1
  264. package/lib/codegen/graph.d.ts +0 -36
  265. package/lib/codegen/graph.d.ts.map +0 -1
  266. package/lib/codegen/graph.js +0 -136
  267. package/lib/codegen/graph.js.map +0 -1
  268. package/lib/codegen/members.d.ts +0 -25
  269. package/lib/codegen/members.d.ts.map +0 -1
  270. package/lib/codegen/members.js +0 -55
  271. package/lib/codegen/members.js.map +0 -1
  272. package/lib/codegen/naming.d.ts +0 -29
  273. package/lib/codegen/naming.d.ts.map +0 -1
  274. package/lib/codegen/naming.js +0 -74
  275. package/lib/codegen/naming.js.map +0 -1
  276. package/lib/codegen/openapi-cli.d.ts +0 -38
  277. package/lib/codegen/openapi-cli.d.ts.map +0 -1
  278. package/lib/codegen/openapi-cli.js +0 -107
  279. package/lib/codegen/openapi-cli.js.map +0 -1
  280. package/lib/codegen/openapi.d.ts +0 -115
  281. package/lib/codegen/openapi.d.ts.map +0 -1
  282. package/lib/codegen/openapi.js +0 -1220
  283. package/lib/codegen/openapi.js.map +0 -1
  284. package/lib/codegen/operations.d.ts +0 -24
  285. package/lib/codegen/operations.d.ts.map +0 -1
  286. package/lib/codegen/operations.js +0 -56
  287. package/lib/codegen/operations.js.map +0 -1
  288. package/lib/codegen/pagination.d.ts +0 -39
  289. package/lib/codegen/pagination.d.ts.map +0 -1
  290. package/lib/codegen/pagination.js +0 -33
  291. package/lib/codegen/pagination.js.map +0 -1
  292. package/lib/codegen/prelude.d.ts +0 -15
  293. package/lib/codegen/prelude.d.ts.map +0 -1
  294. package/lib/codegen/prelude.js +0 -60
  295. package/lib/codegen/prelude.js.map +0 -1
  296. package/lib/error-category.js.map +0 -1
  297. package/lib/errors.js.map +0 -1
  298. package/lib/json-patch.d.ts +0 -44
  299. package/lib/json-patch.d.ts.map +0 -1
  300. package/lib/json-patch.js +0 -174
  301. package/lib/json-patch.js.map +0 -1
  302. package/lib/pagination.js.map +0 -1
  303. package/lib/protocol-http.js.map +0 -1
  304. package/lib/protocol-rest.js.map +0 -1
  305. package/lib/retry-after.js.map +0 -1
  306. package/lib/retry.js.map +0 -1
  307. package/lib/schema.js.map +0 -1
  308. package/lib/trait.js.map +0 -1
  309. package/src/codegen/cli.ts +0 -268
  310. package/src/codegen/emit.ts +0 -207
  311. package/src/codegen/format.ts +0 -47
  312. package/src/codegen/generator.ts +0 -1153
  313. package/src/codegen/graph.ts +0 -151
  314. package/src/codegen/members.ts +0 -71
  315. package/src/codegen/naming.ts +0 -86
  316. package/src/codegen/openapi-cli.ts +0 -166
  317. package/src/codegen/openapi.ts +0 -1450
  318. package/src/codegen/operations.ts +0 -76
  319. package/src/codegen/pagination.ts +0 -71
  320. package/src/codegen/prelude.ts +0 -70
  321. package/src/json-patch.ts +0 -222
@@ -0,0 +1,117 @@
1
+ /**
2
+ * A token or credentials cache shared by every call on one layer value.
3
+ *
4
+ * The first call mints, later calls reuse the value until it goes stale, and
5
+ * calls that find a cold or stale cache while another call mints wait for
6
+ * that mint instead of starting their own: only one mint runs at a time
7
+ * (request coalescing). A waiting call never waits on the minting call
8
+ * itself: it sleeps on its own timer and checks again. In workerd one
9
+ * request's events never wake another request, and a request that waits on
10
+ * nothing of its own is cancelled as hung, so a lock, `Deferred`,
11
+ * `SynchronizedRef` or `Effect.cachedWithTTL` shared between requests fails
12
+ * there.
13
+ *
14
+ * Every mint has a time limit. A mint that runs past it is interrupted and
15
+ * fails with the caller's timeout error, and so does every call waiting on
16
+ * it. A mint that fails is not cached: the next call mints again.
17
+ *
18
+ * A mint whose owner disappears without finishing (workerd drops the
19
+ * request) can never run again. Once its time limit and
20
+ * {@link TAKEOVER_MARGIN_MS} have passed, the next waiting call mints in its
21
+ * place, and the old mint may no longer write the cache.
22
+ */
23
+ import * as Duration from "effect/Duration";
24
+ import * as Effect from "effect/Effect";
25
+ import * as Exit from "effect/Exit";
26
+
27
+ /** How often a call waiting on another call's mint checks again. */
28
+ export const POLL_MS = 20;
29
+
30
+ /**
31
+ * Default time limit of one mint. A token endpoint answers in well under a
32
+ * second; this is a ceiling for a hung endpoint, not a latency target. A
33
+ * provider whose own retries can take longer passes its own limit.
34
+ */
35
+ export const DEFAULT_MINT_TIMEOUT_MS = 60_000;
36
+
37
+ /**
38
+ * How long after a mint's time limit a waiting call takes its place. The
39
+ * owner's timeout normally ends the mint first; the margin covers a timer
40
+ * that fires late.
41
+ */
42
+ export const TAKEOVER_MARGIN_MS = 1_000;
43
+
44
+ export interface RefreshingOptions<E> {
45
+ /** Time limit of one mint, in milliseconds. @default DEFAULT_MINT_TIMEOUT_MS */
46
+ readonly timeoutMs?: number;
47
+ /** The error a mint fails with when it runs past `timeoutMs`. */
48
+ readonly onTimeout: (timeoutMs: number) => E;
49
+ }
50
+
51
+ /**
52
+ * Cache what `mint` produces. `refreshAt` gives the epoch milliseconds after
53
+ * which a value is stale (`Infinity` for never); `undefined` keeps no copy,
54
+ * so every call mints, with no waiting.
55
+ *
56
+ * Create the cache once per layer value, outside `Layer.effect`, so every
57
+ * build of the layer shares it.
58
+ */
59
+ export const refreshing = <A, E, R>(
60
+ mint: Effect.Effect<A, E, R>,
61
+ refreshAt: (value: A) => number | undefined,
62
+ options: RefreshingOptions<E>,
63
+ ): Effect.Effect<A, E, R> => {
64
+ const timeoutMs = options.timeoutMs ?? DEFAULT_MINT_TIMEOUT_MS;
65
+ const limited = Effect.timeoutOrElse(mint, {
66
+ duration: Duration.millis(timeoutMs),
67
+ orElse: () => Effect.fail(options.onTimeout(timeoutMs)),
68
+ });
69
+
70
+ let cached: { readonly value: A; readonly refreshAt: number } | undefined;
71
+ let keepsNoCopy = false;
72
+ /** The mint in progress; `exit` is set when it succeeds or fails. */
73
+ let minting: { readonly since: number; exit?: Exit.Exit<A, E> } | undefined;
74
+
75
+ const fresh = () => cached !== undefined && Date.now() < cached.refreshAt;
76
+ const takenOver = (mine: NonNullable<typeof minting>) =>
77
+ Date.now() - mine.since >= timeoutMs + TAKEOVER_MARGIN_MS;
78
+
79
+ const store = (value: A) => {
80
+ const at = refreshAt(value);
81
+ keepsNoCopy = at === undefined;
82
+ cached = at === undefined ? undefined : { value, refreshAt: at };
83
+ return value;
84
+ };
85
+
86
+ const mintAndStore = Effect.suspend(() => {
87
+ const mine: NonNullable<typeof minting> = { since: Date.now() };
88
+ minting = mine;
89
+ return limited.pipe(
90
+ // A mint another call has taken over no longer writes the cache.
91
+ Effect.map((value) => (minting === mine ? store(value) : value)),
92
+ Effect.onExit((exit) =>
93
+ Effect.sync(() => {
94
+ if (minting === mine) minting = undefined;
95
+ // An interrupted mint has no result to share: its waiters mint again.
96
+ if (!Exit.hasInterrupts(exit)) mine.exit = exit;
97
+ }),
98
+ ),
99
+ );
100
+ });
101
+
102
+ const waitFor = (mine: NonNullable<typeof minting>): Effect.Effect<A, E, R> =>
103
+ Effect.suspend(() => {
104
+ if (fresh()) return Effect.succeed(cached!.value);
105
+ if (mine.exit !== undefined) return mine.exit;
106
+ if (minting !== mine || takenOver(mine)) return get;
107
+ return Effect.andThen(Effect.sleep(POLL_MS), waitFor(mine));
108
+ });
109
+
110
+ const get: Effect.Effect<A, E, R> = Effect.suspend(() => {
111
+ if (fresh()) return Effect.succeed(cached!.value);
112
+ if (keepsNoCopy) return Effect.map(limited, store);
113
+ if (minting !== undefined && !takenOver(minting)) return waitFor(minting);
114
+ return mintAndStore;
115
+ });
116
+ return get;
117
+ };
package/src/trait.ts CHANGED
@@ -1,8 +1,8 @@
1
- export const annotationMetaSymbol = Symbol.for(
1
+ export const annotationMetaSymbol = /*@__PURE__*/ Symbol.for(
2
2
  "@distilled.cloud/core/annotation-meta",
3
3
  );
4
4
 
5
- type Annotatable = {
5
+ export type Annotatable = {
6
6
  annotate(annotations: any): Annotatable;
7
7
  };
8
8
 
@@ -20,19 +20,18 @@ export interface Annotation {
20
20
  * Build a pipeable schema annotation carrying `value` under `sym`. Exported
21
21
  * so SDK packages can define their own protocol traits (e.g. cloudflare's
22
22
  * envelope traits, aws's smithy traits) with the same mechanics as the
23
- * generic ones here. Keys may be symbols or plain strings — effect Schema
23
+ * generic ones here. Keys may be symbols or plain strings -- effect Schema
24
24
  * annotation dictionaries accept both.
25
25
  */
26
26
  export function makeAnnotation<T>(sym: symbol | string, value: T): Annotation {
27
- const fn = <A extends Annotatable>(schema: A): A =>
28
- schema.annotate({ [sym]: value }) as A;
27
+ const fn = <A extends Annotatable>(schema: A): A => schema.annotate({ [sym]: value }) as A;
29
28
  (fn as any)[annotationMetaSymbol] = [{ symbol: sym, value }];
30
29
  (fn as any)[sym] = value;
31
30
  return fn as Annotation;
32
31
  }
33
32
 
34
33
  /**
35
- * Combine several annotations into one — needed where only a single
34
+ * Combine several annotations into one -- needed where only a single
36
35
  * annotations object can be supplied (e.g. the second argument of
37
36
  * `S.Class`).
38
37
  */
@@ -59,21 +58,14 @@ export function all(...annotations: Annotation[]): Annotation {
59
58
  //#region Generic Http traits
60
59
 
61
60
  export interface HttpTrait {
62
- readonly method:
63
- | "GET"
64
- | "POST"
65
- | "PUT"
66
- | "PATCH"
67
- | "DELETE"
68
- | "HEAD"
69
- | "OPTIONS";
61
+ readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
70
62
  /** URI template relative to the service base URL, e.g. `/accounts/{account_id}/foo`. */
71
63
  readonly uri: string;
72
64
  /** Default success status code (a `ResponseCode()` member can still read the actual code). */
73
65
  readonly code?: number;
74
66
  /**
75
67
  * Request body encoding. Default is JSON; `"multipart"` sends
76
- * multipart/form-data — body members become form parts (objects
68
+ * multipart/form-data -- body members become form parts (objects
77
69
  * JSON-encoded) and members marked `FormDataFile()` append their files.
78
70
  * `"form-urlencoded"` sends application/x-www-form-urlencoded with
79
71
  * Stripe-style bracket notation for nested objects and arrays (see
@@ -90,13 +82,21 @@ export interface HttpTrait {
90
82
  /**
91
83
  * Explicit `Accept` for the operation, when the API serves more than one
92
84
  * response media type. Atlas pins an API version this way (e.g.
93
- * `application/vnd.atlas.2024-05-30+json` — `application/json` alone is a
85
+ * `application/vnd.atlas.2024-05-30+json` -- `application/json` alone is a
94
86
  * 406). Protocols that don't read it fall back to their own default.
95
87
  */
96
88
  readonly accept?: string;
89
+ /**
90
+ * Name of the host the operation runs on, when the API serves it from a
91
+ * host other than the service's main one (DigitalOcean's `"inference"`
92
+ * for `inference.do-ai.run`). It names a host and is never a URL: the
93
+ * protocol maps it to a base URL from its credentials, so the caller's
94
+ * configuration decides where the request and its credentials go.
95
+ */
96
+ readonly server?: string;
97
97
  }
98
98
 
99
- export const httpSymbol = Symbol.for("@distilled.cloud/core/http");
99
+ export const httpSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http");
100
100
  /**
101
101
  * Operation-level HTTP binding: the request method + URI template. Stamped on
102
102
  * the input schema so the protocol can build the request line. URI `{labels}`
@@ -104,52 +104,92 @@ export const httpSymbol = Symbol.for("@distilled.cloud/core/http");
104
104
  */
105
105
  export const Http = (trait: HttpTrait) => makeAnnotation(httpSymbol, trait);
106
106
 
107
- export const labelSymbol = Symbol.for("@distilled.cloud/core/http/label");
107
+ export const labelSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/label");
108
108
  /** Bind a member to a `{name}` placeholder in the operation's URI template. */
109
- export const Label = (name?: string) =>
110
- makeAnnotation(labelSymbol, name ?? true);
109
+ export const Label = (name?: string) => makeAnnotation(labelSymbol, name ?? true);
111
110
 
112
- export const responseCodeSymbol = Symbol.for(
111
+ export const labelEncodingSymbol = /*@__PURE__*/ Symbol.for(
112
+ "@distilled.cloud/core/http/label-encoding",
113
+ );
114
+ /** Preserve selected RFC 3986 pchar delimiters inside a single URI label. */
115
+ export const LabelEncoding = (options: { readonly preserve: string }) => {
116
+ if (!/^[!$&'()*+,;=:@]*$/.test(options.preserve)) {
117
+ throw new TypeError("LabelEncoding can preserve only URI path-segment delimiters");
118
+ }
119
+ return makeAnnotation(labelEncodingSymbol, options.preserve);
120
+ };
121
+
122
+ export const responseCodeSymbol = /*@__PURE__*/ Symbol.for(
113
123
  "@distilled.cloud/core/http/response-code",
114
124
  );
115
125
  export const ResponseCode = () => makeAnnotation(responseCodeSymbol, true);
116
126
 
117
- export const headerSymbol = Symbol.for("@distilled.cloud/core/http/header");
118
- export const Header = (name?: string) =>
119
- makeAnnotation(headerSymbol, name ?? true);
127
+ export const headerSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/header");
128
+ export const Header = (name?: string) => makeAnnotation(headerSymbol, name ?? true);
120
129
 
121
- export const bodySymbol = Symbol.for("@distilled.cloud/core/http/body");
130
+ export const bodySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/body");
122
131
  export const Body = (name?: string) => makeAnnotation(bodySymbol, name ?? true);
123
132
 
124
- export const querySymbol = Symbol.for("@distilled.cloud/core/http/query");
125
- export const Query = (name?: string) =>
126
- makeAnnotation(querySymbol, name ?? true);
133
+ export const querySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/query");
134
+ export const Query = (name?: string) => makeAnnotation(querySymbol, name ?? true);
135
+
136
+ export const idempotencyTokenSymbol = /*@__PURE__*/ Symbol.for(
137
+ "@distilled.cloud/core/idempotency-token",
138
+ );
139
+ /**
140
+ * `smithy.api#idempotencyToken`: an input member the API uses to recognize
141
+ * a retried request. When the caller leaves it out, each call fills it with
142
+ * a new UUID, and every automatic retry of that call sends the same one.
143
+ * The UUID is random (v4), or time-ordered (v7, RFC 9562) with `"uuidv7"`
144
+ * for APIs that require it (`distilled.idempotency#format` in the model).
145
+ */
146
+ export const IdempotencyToken = (format?: "uuidv7") =>
147
+ makeAnnotation(idempotencyTokenSymbol, format ?? true);
127
148
 
128
- export const deepQuerySymbol = Symbol.for(
129
- "@distilled.cloud/core/http/deep-query",
149
+ export const commaDelimitedSymbol = /*@__PURE__*/ Symbol.for(
150
+ "@distilled.cloud/core/http/comma-delimited",
130
151
  );
152
+ /**
153
+ * Sends a list-valued `Query` member as ONE comma-separated parameter
154
+ * (`?state=running,paused`) instead of one parameter per item -- OpenAPI's
155
+ * `style: form, explode: false`. Takes the trait value the codegen inlines.
156
+ */
157
+ export const CommaDelimited = (_trait?: unknown) => makeAnnotation(commaDelimitedSymbol, true);
158
+
159
+ export const deepQuerySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http/deep-query");
131
160
  /**
132
161
  * Binds a struct-valued input member to a family of DOTTED query parameters:
133
162
  * `{ account: { id, name } }` with `DeepQuery("account")` serializes as
134
- * `?account.id=…&account.name=…` (null/undefined entries skipped). This is
163
+ * `?account.id=...&account.name=...` (null/undefined entries skipped). This is
135
164
  * how several list endpoints model their filters on the wire (e.g.
136
165
  * Cloudflare zones list `account.id` / `account.name`) while the TS surface
137
166
  * keeps the nested-object shape (v0 parity).
138
167
  */
139
- export const DeepQuery = (name?: string) =>
140
- makeAnnotation(deepQuerySymbol, name ?? true);
168
+ export const DeepQuery = (name?: string) => makeAnnotation(deepQuerySymbol, name ?? true);
141
169
 
142
- export const httpBodySymbol = Symbol.for("@distilled.cloud/core/http-body");
170
+ export const httpBodySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/http-body");
143
171
  /**
144
172
  * Marks the input member whose value IS the entire request body (raw arrays/
145
- * scalars — e.g. endpoints that POST a bare JSON array). Mirrors
173
+ * scalars -- e.g. endpoints that POST a bare JSON array). Mirrors
146
174
  * `smithy.api#httpPayload`.
147
175
  */
148
176
  export const HttpBody = () => makeAnnotation(httpBodySymbol, true);
149
177
 
150
- export const formDataFileSymbol = Symbol.for(
151
- "@distilled.cloud/core/form-data-file",
178
+ export const stringEncodedSymbol = /*@__PURE__*/ Symbol.for(
179
+ "@distilled.cloud/core/http/string-encoded",
152
180
  );
181
+ /**
182
+ * Marks a member the API only accepts as the STRING spelling of its value --
183
+ * `true` travels as `"true"`, `3` as `"3"` -- while the TS surface keeps the
184
+ * natural type (e.g. Cloudflare's multipart `validation_enabled`, which the
185
+ * docs model as the enum `"true" | "false"`). The protocol stringifies body
186
+ * members on encode, element-wise for lists; `null` is left alone. Query and
187
+ * header bindings already serialize their values as strings, so the trait is
188
+ * documentation there.
189
+ */
190
+ export const StringEncoded = () => makeAnnotation(stringEncodedSymbol, true);
191
+
192
+ export const formDataFileSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/form-data-file");
153
193
  /**
154
194
  * Marks an input member holding `File`/`Blob` parts for a multipart upload
155
195
  * (`Http({ contentType: "multipart" })`). Each file is appended to the form
@@ -160,11 +200,9 @@ export const FormDataFile = () => makeAnnotation(formDataFileSymbol, true);
160
200
 
161
201
  //#region Generic JSON traits
162
202
 
163
- export const keyDictionarySymbol = Symbol.for(
164
- "@distilled.cloud/core/key-dictionary",
165
- );
203
+ export const keyDictionarySymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/key-dictionary");
166
204
  /**
167
- * Deep TS-name→wire-name key dictionary for members whose full structure is
205
+ * Deep TS-name->wire-name key dictionary for members whose full structure is
168
206
  * not modeled (opaque `Document` content). The protocol renames any matching
169
207
  * key at any depth on encode (and the reverse on decode); keys not in the
170
208
  * dictionary pass through verbatim.
@@ -175,64 +213,98 @@ export const keyDictionarySymbol = Symbol.for(
175
213
  * wire name used on encode; decode maps every listed spelling back to the
176
214
  * TS name.
177
215
  */
178
- export type KeyDictionaryEntries = Record<
179
- string,
180
- string | ReadonlyArray<string>
181
- >;
216
+ export type KeyDictionaryEntries = Record<string, string | ReadonlyArray<string>>;
182
217
 
183
218
  export const KeyDictionary = (dict: KeyDictionaryEntries) =>
184
219
  makeAnnotation(keyDictionarySymbol, dict);
185
220
 
186
- export const unionCasesSymbol = Symbol.for("@distilled.cloud/core/union-cases");
221
+ export const unionCasesSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/union-cases");
222
+
223
+ /**
224
+ * The literal member every case of a union fixes to its own value -- what the
225
+ * docs call the resource `type` or the action `id`. `values[i]` is the value
226
+ * case `i` carries.
227
+ */
228
+ export interface UnionDiscriminator {
229
+ readonly key: string;
230
+ readonly values: ReadonlyArray<string>;
231
+ }
232
+
187
233
  /**
188
234
  * Marks an opaque schema standing in for a discriminated union of object
189
235
  * cases, carrying each case's TS-facing key set. For APIs that return every
190
236
  * case's keys with `null` for the inactive ones, the protocol uses these key
191
237
  * sets to pick the active case and drop the others, so consumers' `"key" in
192
238
  * value` discrimination works.
239
+ *
240
+ * Key sets alone cannot tell apart cases that share them -- the 41 resource
241
+ * kinds of `resource_tagging`, the 34 page-rule actions -- so when the union
242
+ * has a discriminator the protocol reads the case off it and only falls back
243
+ * to key-set scoring for a value whose tag is missing or unknown.
244
+ *
245
+ * `dictionaries[i]` holds case `i`'s member-level `T.KeyDictionary`s by TS
246
+ * member name, which the opaque schema cannot carry on the members
247
+ * themselves. An empty one (`{}`) keeps a member's content keys as written:
248
+ * a Worker binding's `json` or `props` is the caller's own JSON. A union
249
+ * with dictionaries is mapped per case in both directions.
193
250
  */
194
- export const UnionCases = (cases: ReadonlyArray<ReadonlyArray<string>>) =>
195
- makeAnnotation(unionCasesSymbol, cases);
251
+ export const UnionCases = (
252
+ cases: ReadonlyArray<ReadonlyArray<string>>,
253
+ discriminator?: UnionDiscriminator,
254
+ dictionaries?: ReadonlyArray<Readonly<Record<string, KeyDictionaryEntries>>>,
255
+ ) =>
256
+ makeAnnotation(unionCasesSymbol, {
257
+ cases,
258
+ ...(discriminator ? { discriminator } : {}),
259
+ ...(dictionaries ? { dictionaries } : {}),
260
+ });
196
261
  //#endregion
197
262
 
198
263
  //#region Error matcher traits
199
264
 
200
- export const errorMatchersSymbol = Symbol.for(
201
- "@distilled.cloud/core/error-matchers",
202
- );
265
+ export const errorMatchersSymbol = /*@__PURE__*/ Symbol.for("@distilled.cloud/core/error-matchers");
266
+
267
+ /** Exact text, or the conjunction of substring and regular-expression constraints. */
268
+ export type ErrorTextMatcher = string | { readonly includes?: string; readonly matches?: string };
203
269
 
204
270
  /**
205
- * One wire-matching rule for a typed error class. A matcher matches a wire
206
- * failure when every present field matches: `code` equals the wire error's
207
- * code, `status` equals the HTTP status, and `message` either equals the
208
- * error message (string form) or satisfies `includes` (substring) /
209
- * `matches` (regex). A matcher with no fields matches nothing.
271
+ * One wire-matching rule. All supplied constraints must match; separate
272
+ * matchers on a class are alternatives. Empty matchers match nothing.
273
+ *
274
+ * `body` maps RFC 6901 JSON Pointers to scalar constraints. For example,
275
+ * `{ "/success": false, "/result/status": "error" }`. Strings also accept
276
+ * `includes` / `matches`. Missing fields never match, including against null.
277
+ * The empty pointer addresses the entire body; array indices are supported.
278
+ * Header names are case-insensitive; their value constraints are case-sensitive.
279
+ * Each body path and header adds one specificity point, like code/status/message.
280
+ * Protocols evaluate these rules only after identifying a failed response.
281
+ *
282
+ * @example
283
+ * ```ts
284
+ * { status: 200, body: { "/success": false, "/result/status": "error" } }
285
+ * { status: 409, headers: { "x-error-type": { includes: "Conflict" } } }
286
+ * ```
210
287
  */
211
288
  export interface ErrorMatcher {
212
289
  readonly code?: number;
213
290
  readonly status?: number;
214
- readonly message?:
215
- | string
216
- | { readonly includes?: string; readonly matches?: string };
291
+ readonly message?: ErrorTextMatcher;
292
+ readonly body?: Readonly<Record<string, ErrorTextMatcher | number | boolean | null>>;
293
+ readonly headers?: Readonly<Record<string, ErrorTextMatcher>>;
217
294
  }
218
295
 
219
296
  /**
220
297
  * Stamp wire-matching rules onto a generated error class. Protocols consult
221
298
  * these to decide which of an operation's declared error classes a failed
222
299
  * response should surface as (most specific matcher wins; ties break by
223
- * declaration order — see `matchTypedError` in `core/protocol-http`).
300
+ * declaration order -- see `matchTypedError` in `core/protocol-http`).
224
301
  */
225
- export const applyErrorMatchers = <C>(
226
- cls: C,
227
- matchers: ReadonlyArray<ErrorMatcher>,
228
- ): C => {
302
+ export const applyErrorMatchers = <C>(cls: C, matchers: ReadonlyArray<ErrorMatcher>): C => {
229
303
  (cls as any)[errorMatchersSymbol] = matchers;
230
304
  return cls;
231
305
  };
232
306
 
233
307
  /** Read the matchers stamped on an error class, if any. */
234
- export const getErrorMatchers = (
235
- cls: unknown,
236
- ): ReadonlyArray<ErrorMatcher> | undefined =>
308
+ export const getErrorMatchers = (cls: unknown): ReadonlyArray<ErrorMatcher> | undefined =>
237
309
  (cls as any)?.[errorMatchersSymbol];
238
310
  //#endregion
@@ -0,0 +1,38 @@
1
+ /**
2
+ * The fields every SDK's `Unknown<Sdk>Error` carries: what the response said
3
+ * about a failure that no typed error matched. An SDK spreads
4
+ * {@link UnknownErrorFields} into its own class and adds the fields its
5
+ * provider sends (Stripe's `type` and `param`, AWS's `service` and
6
+ * `operation`, ...):
7
+ *
8
+ * ```ts
9
+ * export class UnknownNeonError
10
+ * extends Schema.TaggedError<UnknownNeonError>()("UnknownNeonError", UnknownErrorFields) {}
11
+ * ```
12
+ *
13
+ * The class carries no error category: which statuses reach it, and whether
14
+ * they are worth a retry, is the protocol's call.
15
+ *
16
+ * Defined on its own, in core's `base` bundle group, so AWS uses it without
17
+ * loading the REST modules.
18
+ */
19
+ import * as Schema from "./schema-lite-typed.ts";
20
+
21
+ export const UnknownErrorFields = {
22
+ /** HTTP status of the response that carried the failure. */
23
+ status: Schema.Number,
24
+ /** The provider's machine-readable error code, when the response has one. */
25
+ code: /*@__PURE__*/ Schema.optional(Schema.String),
26
+ /** The provider's error message, when the response has one. */
27
+ message: /*@__PURE__*/ Schema.optional(Schema.String),
28
+ /** Parsed JSON body, or the raw text when the body wasn't JSON. */
29
+ body: Schema.Unknown,
30
+ };
31
+
32
+ /** The values of {@link UnknownErrorFields}. */
33
+ export interface UnknownErrorProps {
34
+ readonly status: number;
35
+ readonly code?: string | undefined;
36
+ readonly message?: string | undefined;
37
+ readonly body: unknown;
38
+ }