@distilled.cloud/core 1.0.0-rc.15 → 1.0.0-rc.17

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 (318) hide show
  1. package/lib/_bundle/aws.js +2 -0
  2. package/lib/_bundle/aws.js.map +1 -0
  3. package/lib/_bundle/base.js +5 -0
  4. package/lib/_bundle/base.js.map +1 -0
  5. package/lib/_bundle/graphql.js +3 -0
  6. package/lib/_bundle/graphql.js.map +1 -0
  7. package/lib/_bundle/rest.js +2 -0
  8. package/lib/_bundle/rest.js.map +1 -0
  9. package/lib/_bundle/xml.js +4 -0
  10. package/lib/_bundle/xml.js.map +1 -0
  11. package/lib/api.d.ts +118 -13
  12. package/lib/api.d.ts.map +1 -1
  13. package/lib/api.js +1 -188
  14. package/lib/category.d.ts.map +1 -1
  15. package/lib/category.js +1 -278
  16. package/lib/chunks.d.ts +3 -0
  17. package/lib/chunks.d.ts.map +1 -0
  18. package/lib/chunks.js +1 -0
  19. package/lib/codegen/boolean-string-enums.js +36 -36
  20. package/lib/codegen/chunk.d.ts +7 -0
  21. package/lib/codegen/chunk.d.ts.map +1 -0
  22. package/lib/codegen/chunk.js +139 -0
  23. package/lib/codegen/chunk.js.map +1 -0
  24. package/lib/codegen/cli.d.ts.map +1 -1
  25. package/lib/codegen/cli.js +28 -14
  26. package/lib/codegen/cli.js.map +1 -1
  27. package/lib/codegen/datafy.d.ts +14 -0
  28. package/lib/codegen/datafy.d.ts.map +1 -0
  29. package/lib/codegen/datafy.js +853 -0
  30. package/lib/codegen/datafy.js.map +1 -0
  31. package/lib/codegen/dedupe.d.ts +5 -0
  32. package/lib/codegen/dedupe.d.ts.map +1 -0
  33. package/lib/codegen/dedupe.js +153 -0
  34. package/lib/codegen/dedupe.js.map +1 -0
  35. package/lib/codegen/emit.js +56 -56
  36. package/lib/codegen/format.js +18 -18
  37. package/lib/codegen/generated-at.js +29 -29
  38. package/lib/codegen/generator.d.ts.map +1 -1
  39. package/lib/codegen/generator.js +92 -84
  40. package/lib/codegen/generator.js.map +1 -1
  41. package/lib/codegen/graph.js +36 -36
  42. package/lib/codegen/graphql-client.d.ts +2 -0
  43. package/lib/codegen/graphql-client.d.ts.map +1 -1
  44. package/lib/codegen/graphql-client.js +55 -26
  45. package/lib/codegen/graphql-client.js.map +1 -1
  46. package/lib/codegen/graphql.js +113 -113
  47. package/lib/codegen/latin1.d.ts +3 -0
  48. package/lib/codegen/latin1.d.ts.map +1 -0
  49. package/lib/codegen/latin1.js +126 -0
  50. package/lib/codegen/latin1.js.map +1 -0
  51. package/lib/codegen/members.d.ts +7 -0
  52. package/lib/codegen/members.d.ts.map +1 -1
  53. package/lib/codegen/members.js +26 -19
  54. package/lib/codegen/members.js.map +1 -1
  55. package/lib/codegen/model-diff.js +6 -6
  56. package/lib/codegen/naming.js +19 -19
  57. package/lib/codegen/openapi-cli.js +15 -15
  58. package/lib/codegen/openapi.d.ts +17 -0
  59. package/lib/codegen/openapi.d.ts.map +1 -1
  60. package/lib/codegen/openapi.js +323 -169
  61. package/lib/codegen/openapi.js.map +1 -1
  62. package/lib/codegen/operations.js +14 -14
  63. package/lib/codegen/pagination.js +11 -11
  64. package/lib/codegen/patch-audit.js +57 -57
  65. package/lib/codegen/patches.js +79 -79
  66. package/lib/codegen/prelude.js +19 -19
  67. package/lib/codegen/privatize.d.ts +21 -0
  68. package/lib/codegen/privatize.d.ts.map +1 -0
  69. package/lib/codegen/privatize.js +176 -0
  70. package/lib/codegen/privatize.js.map +1 -0
  71. package/lib/codegen/proto.js +25 -25
  72. package/lib/codegen/pure.d.ts +6 -0
  73. package/lib/codegen/pure.d.ts.map +1 -0
  74. package/lib/codegen/pure.js +91 -0
  75. package/lib/codegen/pure.js.map +1 -0
  76. package/lib/codegen/rewrite-operation-ids.js +108 -108
  77. package/lib/codegen/spec-path.js +57 -57
  78. package/lib/codegen/table.d.ts +5 -0
  79. package/lib/codegen/table.d.ts.map +1 -0
  80. package/lib/codegen/table.js +249 -0
  81. package/lib/codegen/table.js.map +1 -0
  82. package/lib/config.d.ts +27 -0
  83. package/lib/config.d.ts.map +1 -0
  84. package/lib/config.js +1 -0
  85. package/lib/crypto.d.ts +76 -0
  86. package/lib/crypto.d.ts.map +1 -0
  87. package/lib/crypto.js +1 -0
  88. package/lib/debug.d.ts +2 -0
  89. package/lib/debug.d.ts.map +1 -0
  90. package/lib/debug.js +1 -0
  91. package/lib/error-category.d.ts +1 -1
  92. package/lib/error-category.d.ts.map +1 -1
  93. package/lib/error-category.js +1 -46
  94. package/lib/errors.d.ts +62 -62
  95. package/lib/errors.d.ts.map +1 -1
  96. package/lib/errors.js +1 -195
  97. package/lib/graphql.d.ts +31 -20
  98. package/lib/graphql.d.ts.map +1 -1
  99. package/lib/graphql.js +1 -1027
  100. package/lib/graphql.types.js +1 -5
  101. package/lib/index.d.ts +3 -2
  102. package/lib/index.d.ts.map +1 -1
  103. package/lib/index.js +1 -15
  104. package/lib/json-patch.d.ts +2 -2
  105. package/lib/json-patch.js +1 -226
  106. package/lib/json-patch.js.map +1 -1
  107. package/lib/pagination.d.ts +9 -25
  108. package/lib/pagination.d.ts.map +1 -1
  109. package/lib/pagination.js +1 -266
  110. package/lib/plain-values.d.ts +10 -0
  111. package/lib/plain-values.d.ts.map +1 -0
  112. package/lib/plain-values.js +2 -0
  113. package/lib/plain-values.js.map +1 -0
  114. package/lib/protocol-http.d.ts +50 -14
  115. package/lib/protocol-http.d.ts.map +1 -1
  116. package/lib/protocol-http.js +1 -639
  117. package/lib/protocol-rest.d.ts +38 -25
  118. package/lib/protocol-rest.d.ts.map +1 -1
  119. package/lib/protocol-rest.js +1 -267
  120. package/lib/query.js +1 -27
  121. package/lib/response-validation-strict.d.ts +48 -0
  122. package/lib/response-validation-strict.d.ts.map +1 -0
  123. package/lib/response-validation-strict.js +1 -0
  124. package/lib/response-validation.d.ts +43 -25
  125. package/lib/response-validation.d.ts.map +1 -1
  126. package/lib/response-validation.js +1 -76
  127. package/lib/retry-after.d.ts +3 -3
  128. package/lib/retry-after.js +1 -112
  129. package/lib/retry.d.ts +48 -29
  130. package/lib/retry.d.ts.map +1 -1
  131. package/lib/retry.js +1 -209
  132. package/lib/schema-lite-impl.d.ts +158 -0
  133. package/lib/schema-lite-impl.d.ts.map +1 -0
  134. package/lib/schema-lite-impl.js +1 -0
  135. package/lib/schema-lite-typed.d.ts +41 -0
  136. package/lib/schema-lite-typed.d.ts.map +1 -0
  137. package/lib/schema-lite-typed.js +2 -0
  138. package/lib/schema-lite-typed.js.map +1 -0
  139. package/lib/schema-lite.d.ts +40 -0
  140. package/lib/schema-lite.d.ts.map +1 -0
  141. package/lib/schema-lite.js +1 -0
  142. package/lib/schema.d.ts +20 -6
  143. package/lib/schema.d.ts.map +1 -1
  144. package/lib/schema.js +1 -48
  145. package/lib/testing.d.ts +3 -2
  146. package/lib/testing.d.ts.map +1 -1
  147. package/lib/testing.js +1 -40
  148. package/lib/testing.js.map +1 -1
  149. package/lib/trait.d.ts +13 -13
  150. package/lib/trait.d.ts.map +1 -1
  151. package/lib/trait.js +1 -131
  152. package/lib/xml.d.ts +1 -1
  153. package/lib/xml.d.ts.map +1 -1
  154. package/lib/xml.js +1 -311
  155. package/package.json +9 -7
  156. package/src/api.ts +1080 -180
  157. package/src/category.ts +33 -33
  158. package/src/chunks.ts +30 -0
  159. package/src/codegen/chunk.ts +147 -0
  160. package/src/codegen/cli.ts +27 -1
  161. package/src/codegen/datafy.ts +874 -0
  162. package/src/codegen/dedupe.ts +173 -0
  163. package/src/codegen/generator.ts +10 -3
  164. package/src/codegen/graphql-client.ts +49 -15
  165. package/src/codegen/latin1.ts +131 -0
  166. package/src/codegen/members.ts +8 -0
  167. package/src/codegen/openapi.ts +194 -3
  168. package/src/codegen/privatize.ts +200 -0
  169. package/src/codegen/pure.ts +94 -0
  170. package/src/codegen/table.ts +254 -0
  171. package/src/config.ts +216 -0
  172. package/src/crypto.ts +202 -0
  173. package/src/debug.ts +8 -0
  174. package/src/error-category.ts +2 -1
  175. package/src/errors.ts +65 -56
  176. package/src/graphql.ts +109 -62
  177. package/src/graphql.types.ts +9 -7
  178. package/src/index.ts +3 -2
  179. package/src/json-patch.ts +2 -2
  180. package/src/pagination.ts +37 -18
  181. package/src/plain-values.ts +41 -0
  182. package/src/protocol-http.ts +378 -145
  183. package/src/protocol-rest.ts +337 -169
  184. package/src/response-validation-strict.ts +327 -0
  185. package/src/response-validation.ts +101 -50
  186. package/src/retry-after.ts +3 -3
  187. package/src/retry.ts +94 -93
  188. package/src/schema-lite-impl.ts +550 -0
  189. package/src/schema-lite-typed.ts +44 -0
  190. package/src/schema-lite.ts +44 -0
  191. package/src/schema.ts +258 -14
  192. package/src/testing.ts +4 -2
  193. package/src/trait.ts +36 -28
  194. package/src/xml.ts +31 -17
  195. package/lib/api.js.map +0 -1
  196. package/lib/category.js.map +0 -1
  197. package/lib/codegen/boolean-string-enums.test.d.ts +0 -2
  198. package/lib/codegen/boolean-string-enums.test.d.ts.map +0 -1
  199. package/lib/codegen/boolean-string-enums.test.js +0 -275
  200. package/lib/codegen/boolean-string-enums.test.js.map +0 -1
  201. package/lib/codegen/defer-naming.test.d.ts +0 -2
  202. package/lib/codegen/defer-naming.test.d.ts.map +0 -1
  203. package/lib/codegen/defer-naming.test.js +0 -105
  204. package/lib/codegen/defer-naming.test.js.map +0 -1
  205. package/lib/codegen/generated-at.test.d.ts +0 -2
  206. package/lib/codegen/generated-at.test.d.ts.map +0 -1
  207. package/lib/codegen/generated-at.test.js +0 -81
  208. package/lib/codegen/generated-at.test.js.map +0 -1
  209. package/lib/codegen/generator.test.d.ts +0 -2
  210. package/lib/codegen/generator.test.d.ts.map +0 -1
  211. package/lib/codegen/generator.test.js +0 -121
  212. package/lib/codegen/generator.test.js.map +0 -1
  213. package/lib/codegen/graphql-client.test.d.ts +0 -2
  214. package/lib/codegen/graphql-client.test.d.ts.map +0 -1
  215. package/lib/codegen/graphql-client.test.js +0 -171
  216. package/lib/codegen/graphql-client.test.js.map +0 -1
  217. package/lib/codegen/openapi-binary.test.d.ts +0 -2
  218. package/lib/codegen/openapi-binary.test.d.ts.map +0 -1
  219. package/lib/codegen/openapi-binary.test.js +0 -67
  220. package/lib/codegen/openapi-binary.test.js.map +0 -1
  221. package/lib/codegen/patch-audit.test.d.ts +0 -2
  222. package/lib/codegen/patch-audit.test.d.ts.map +0 -1
  223. package/lib/codegen/patch-audit.test.js +0 -141
  224. package/lib/codegen/patch-audit.test.js.map +0 -1
  225. package/lib/codegen/patches.test.d.ts +0 -2
  226. package/lib/codegen/patches.test.d.ts.map +0 -1
  227. package/lib/codegen/patches.test.js +0 -155
  228. package/lib/codegen/patches.test.js.map +0 -1
  229. package/lib/codegen/protocol-imports.test.d.ts +0 -2
  230. package/lib/codegen/protocol-imports.test.d.ts.map +0 -1
  231. package/lib/codegen/protocol-imports.test.js +0 -36
  232. package/lib/codegen/protocol-imports.test.js.map +0 -1
  233. package/lib/codegen/rewrite-operation-ids.test.d.ts +0 -2
  234. package/lib/codegen/rewrite-operation-ids.test.d.ts.map +0 -1
  235. package/lib/codegen/rewrite-operation-ids.test.js +0 -451
  236. package/lib/codegen/rewrite-operation-ids.test.js.map +0 -1
  237. package/lib/codegen/untagged-unions.test.d.ts +0 -2
  238. package/lib/codegen/untagged-unions.test.d.ts.map +0 -1
  239. package/lib/codegen/untagged-unions.test.js +0 -80
  240. package/lib/codegen/untagged-unions.test.js.map +0 -1
  241. package/lib/error-category.js.map +0 -1
  242. package/lib/errors.js.map +0 -1
  243. package/lib/fixtures/xml/upstream-validator.json +0 -397
  244. package/lib/graphql.js.map +0 -1
  245. package/lib/graphql.test.d.ts +0 -2
  246. package/lib/graphql.test.d.ts.map +0 -1
  247. package/lib/graphql.test.js +0 -151
  248. package/lib/graphql.test.js.map +0 -1
  249. package/lib/graphql.types.js.map +0 -1
  250. package/lib/index.js.map +0 -1
  251. package/lib/json-patch.test.d.ts +0 -2
  252. package/lib/json-patch.test.d.ts.map +0 -1
  253. package/lib/json-patch.test.js +0 -65
  254. package/lib/json-patch.test.js.map +0 -1
  255. package/lib/pagination.js.map +0 -1
  256. package/lib/pagination.test.d.ts +0 -2
  257. package/lib/pagination.test.d.ts.map +0 -1
  258. package/lib/pagination.test.js +0 -131
  259. package/lib/pagination.test.js.map +0 -1
  260. package/lib/protocol-http.js.map +0 -1
  261. package/lib/protocol-http.test.d.ts +0 -2
  262. package/lib/protocol-http.test.d.ts.map +0 -1
  263. package/lib/protocol-http.test.js +0 -228
  264. package/lib/protocol-http.test.js.map +0 -1
  265. package/lib/protocol-rest.js.map +0 -1
  266. package/lib/protocol-rest.test.d.ts +0 -2
  267. package/lib/protocol-rest.test.d.ts.map +0 -1
  268. package/lib/protocol-rest.test.js +0 -498
  269. package/lib/protocol-rest.test.js.map +0 -1
  270. package/lib/query.js.map +0 -1
  271. package/lib/response-validation.js.map +0 -1
  272. package/lib/response-validation.test.d.ts +0 -2
  273. package/lib/response-validation.test.d.ts.map +0 -1
  274. package/lib/response-validation.test.js +0 -72
  275. package/lib/response-validation.test.js.map +0 -1
  276. package/lib/retry-after.js.map +0 -1
  277. package/lib/retry-after.test.d.ts +0 -2
  278. package/lib/retry-after.test.d.ts.map +0 -1
  279. package/lib/retry-after.test.js +0 -204
  280. package/lib/retry-after.test.js.map +0 -1
  281. package/lib/retry.js.map +0 -1
  282. package/lib/retry.test.d.ts +0 -2
  283. package/lib/retry.test.d.ts.map +0 -1
  284. package/lib/retry.test.js +0 -407
  285. package/lib/retry.test.js.map +0 -1
  286. package/lib/schema.js.map +0 -1
  287. package/lib/sdks.test.d.ts +0 -2
  288. package/lib/sdks.test.d.ts.map +0 -1
  289. package/lib/sdks.test.js +0 -76
  290. package/lib/sdks.test.js.map +0 -1
  291. package/lib/trait.js.map +0 -1
  292. package/lib/xml.js.map +0 -1
  293. package/lib/xml.test.d.ts +0 -2
  294. package/lib/xml.test.d.ts.map +0 -1
  295. package/lib/xml.test.js +0 -210
  296. package/lib/xml.test.js.map +0 -1
  297. package/src/codegen/boolean-string-enums.test.ts +0 -299
  298. package/src/codegen/defer-naming.test.ts +0 -113
  299. package/src/codegen/generated-at.test.ts +0 -89
  300. package/src/codegen/generator.test.ts +0 -137
  301. package/src/codegen/graphql-client.test.ts +0 -189
  302. package/src/codegen/openapi-binary.test.ts +0 -91
  303. package/src/codegen/patch-audit.test.ts +0 -160
  304. package/src/codegen/patches.test.ts +0 -188
  305. package/src/codegen/protocol-imports.test.ts +0 -39
  306. package/src/codegen/rewrite-operation-ids.test.ts +0 -472
  307. package/src/codegen/untagged-unions.test.ts +0 -85
  308. package/src/fixtures/xml/upstream-validator.json +0 -397
  309. package/src/graphql.test.ts +0 -198
  310. package/src/json-patch.test.ts +0 -77
  311. package/src/pagination.test.ts +0 -169
  312. package/src/protocol-http.test.ts +0 -276
  313. package/src/protocol-rest.test.ts +0 -703
  314. package/src/response-validation.test.ts +0 -89
  315. package/src/retry-after.test.ts +0 -267
  316. package/src/retry.test.ts +0 -566
  317. package/src/sdks.test.ts +0 -87
  318. package/src/xml.test.ts +0 -251
package/src/retry.test.ts DELETED
@@ -1,566 +0,0 @@
1
- import * as Clock from "effect/Clock";
2
- import * as ConfigProvider from "effect/ConfigProvider";
3
- import * as Duration from "effect/Duration";
4
- import * as Effect from "effect/Effect";
5
- import * as Fiber from "effect/Fiber";
6
- import * as HttpClient from "effect/http/HttpClient";
7
- import * as HttpClientError from "effect/http/HttpClientError";
8
- import * as HttpClientRequest from "effect/http/HttpClientRequest";
9
- import * as HttpClientResponse from "effect/http/HttpClientResponse";
10
- import * as Layer from "effect/Layer";
11
- import * as Ref from "effect/Ref";
12
- import * as Schedule from "effect/Schedule";
13
- import * as Schema from "effect/Schema";
14
- import * as TestClock from "effect/testing/TestClock";
15
- import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
16
- import * as API from "./api.ts";
17
- import * as Category from "./category.ts";
18
- import {
19
- BadGateway,
20
- BadRequest,
21
- Conflict,
22
- Forbidden,
23
- GatewayTimeout,
24
- InternalServerError,
25
- Locked,
26
- NotFound,
27
- ServiceUnavailable,
28
- TooManyRequests,
29
- Unauthorized,
30
- } from "./errors.ts";
31
- import { makeRestProtocol } from "./protocol-rest.ts";
32
- import * as Retry from "./retry.ts";
33
- import * as S from "./schema.ts";
34
- import * as T from "./trait.ts";
35
-
36
- // =============================================================================
37
- // Fixtures
38
- // =============================================================================
39
-
40
- class TestRetry extends Retry.makeRetryService("RetryTest/Retry") {}
41
- class OtherRetry extends Retry.makeRetryService("RetryTest/OtherRetry") {}
42
-
43
- class UnknownTestError extends Schema.TaggedError<UnknownTestError>()("UnknownTestError", {
44
- message: Schema.String,
45
- }) {}
46
-
47
- const TestProtocol = makeRestProtocol<{}>({
48
- credentials: Effect.succeed({}),
49
- baseUrl: () => "https://api.test",
50
- headers: () => ({}),
51
- unknownError: ({ message }) => new UnknownTestError({ message }),
52
- parseError: ({ body }) => new UnknownTestError({ message: String(body) }),
53
- });
54
-
55
- const Output = S.Struct({ ok: S.optional(S.Boolean) });
56
-
57
- type Call = (input: Record<string, unknown>) => Effect.Effect<any, any, any>;
58
-
59
- const operation = (method: "GET" | "POST", retry?: typeof TestRetry): Call =>
60
- API.make(() => ({
61
- input: S.Struct({}).pipe(T.Http({ method, uri: "/thing" })),
62
- output: Output,
63
- protocol: TestProtocol,
64
- ...(retry ? { retry } : {}),
65
- })) as unknown as Call;
66
-
67
- const getThing = operation("GET", TestRetry);
68
- const createThing = operation("POST", TestRetry);
69
- const getThingWithoutRetry = operation("GET");
70
-
71
- type Reply = Response | "transport";
72
-
73
- /**
74
- * Fake HttpClient replying from `script` by attempt number (0-based) and
75
- * recording the TestClock time of every attempt.
76
- */
77
- const scripted = (script: (attempt: number) => Reply) => {
78
- const attempts: Array<{ readonly at: number; readonly method: string }> = [];
79
- const layer = Layer.succeed(
80
- HttpClient.HttpClient,
81
- HttpClient.make((request) =>
82
- Effect.gen(function* () {
83
- const at = yield* Clock.currentTimeMillis;
84
- const reply = script(attempts.length);
85
- attempts.push({ at, method: request.method });
86
- if (reply === "transport") {
87
- return yield* Effect.fail(
88
- new HttpClientError.HttpClientError({
89
- reason: new HttpClientError.TransportError({ request, description: "reset" }),
90
- }),
91
- );
92
- }
93
- return HttpClientResponse.fromWeb(request, reply);
94
- }),
95
- ),
96
- );
97
- return { attempts, layer, times: () => attempts.map((a) => a.at) };
98
- };
99
-
100
- const status = (code: number, headers: Record<string, string> = {}) =>
101
- new Response(JSON.stringify({ message: `status ${code}` }), { status: code, headers });
102
- const ok = () => new Response(JSON.stringify({ ok: true }), { status: 200 });
103
-
104
- /** Fail `n` times with `reply`, then succeed. */
105
- const failTimes = (n: number, reply: () => Reply) => (attempt: number) =>
106
- attempt < n ? reply() : ok();
107
-
108
- /**
109
- * Run `effect` under a TestClock, advancing virtual time far enough for any
110
- * finite retry sequence in these tests to complete.
111
- */
112
- const runVirtual = <A, E>(effect: Effect.Effect<A, E, any>, layer: Layer.Layer<any>) =>
113
- Effect.runPromise(
114
- Effect.gen(function* () {
115
- const fiber = yield* Effect.forkChild(Effect.result(effect));
116
- yield* TestClock.adjust(Duration.hours(1));
117
- return yield* Fiber.join(fiber);
118
- }).pipe(Effect.provide(Layer.mergeAll(layer, TestClock.layer()))) as Effect.Effect<
119
- any,
120
- never,
121
- never
122
- >,
123
- );
124
-
125
- const delays = (times: ReadonlyArray<number>) => times.slice(1).map((t, i) => t - times[i]!);
126
-
127
- const withPolicy =
128
- (policy: Retry.Policy) =>
129
- <A, E, R>(effect: Effect.Effect<A, E, R>) =>
130
- effect.pipe(Effect.provideService(TestRetry, policy));
131
-
132
- const none: Retry.Options = { while: () => false };
133
-
134
- beforeEach(() => {
135
- // Pin jitter (Math.random * 50ms) to zero so delays are exact.
136
- vi.spyOn(Math, "random").mockReturnValue(0);
137
- });
138
-
139
- afterEach(() => {
140
- vi.restoreAllMocks();
141
- });
142
-
143
- // The default ConfigProvider snapshots process.env on first use, so env
144
- // overrides are supplied through an explicit provider.
145
- const env = (vars: Record<string, string>) =>
146
- ConfigProvider.layer(ConfigProvider.fromEnv({ env: vars }));
147
-
148
- // =============================================================================
149
- // Error categories
150
- // =============================================================================
151
-
152
- const request = HttpClientRequest.get("https://api.test/thing");
153
- const transportError = new HttpClientError.HttpClientError({
154
- reason: new HttpClientError.TransportError({ request }),
155
- });
156
- const decodeError = new HttpClientError.HttpClientError({
157
- reason: new HttpClientError.DecodeError({
158
- request,
159
- response: HttpClientResponse.fromWeb(request, new Response("x")),
160
- }),
161
- });
162
-
163
- class CategorizedNetworkError extends Schema.TaggedError<CategorizedNetworkError>()(
164
- "CategorizedNetworkError",
165
- { message: Schema.String },
166
- ).pipe(Category.withCategory(Category.NetworkError)) {}
167
-
168
- class CategorizedTimeoutError extends Schema.TaggedError<CategorizedTimeoutError>()(
169
- "CategorizedTimeoutError",
170
- { message: Schema.String },
171
- ).pipe(Category.withCategory(Category.TimeoutError)) {}
172
-
173
- class RetryableMarked extends Schema.TaggedError<RetryableMarked>()("RetryableMarked", {
174
- message: Schema.String,
175
- }).pipe(Category.withRetryable()) {}
176
-
177
- const transient = [
178
- new TooManyRequests({ message: "" }),
179
- new Locked({ message: "" }),
180
- new InternalServerError({ message: "" }),
181
- new BadGateway({ message: "" }),
182
- new ServiceUnavailable({ message: "" }),
183
- new GatewayTimeout({ message: "" }),
184
- new CategorizedNetworkError({ message: "" }),
185
- new CategorizedTimeoutError({ message: "" }),
186
- new RetryableMarked({ message: "" }),
187
- transportError,
188
- ];
189
-
190
- const permanent = [
191
- new BadRequest({ message: "" }),
192
- new Unauthorized({ message: "" }),
193
- new Forbidden({ message: "" }),
194
- new NotFound({ message: "" }),
195
- new Conflict({ message: "" }),
196
- new UnknownTestError({ message: "" }),
197
- decodeError,
198
- new Error("plain"),
199
- undefined,
200
- ];
201
-
202
- const nameOf = (e: unknown) =>
203
- (e as { _tag?: string } | undefined)?._tag === "HttpClientError"
204
- ? `HttpClientError(${(e as HttpClientError.HttpClientError).reason._tag})`
205
- : ((e as { _tag?: string } | undefined)?._tag ?? String(e));
206
-
207
- describe("retry predicates by error category", () => {
208
- const lastError = Effect.runSync(Ref.make<unknown>(undefined));
209
- const policies = {
210
- makeDefault: Retry.makeDefault(lastError),
211
- transientFactory: Retry.transientFactory(lastError),
212
- transientOptions: Retry.transientOptions,
213
- throttlingFactory: Retry.throttlingFactory(lastError),
214
- throttlingOptions: Retry.throttlingOptions,
215
- };
216
-
217
- for (const error of transient) {
218
- test(`default and transient policies retry ${nameOf(error)}`, () => {
219
- expect(policies.makeDefault.while!(error)).toBe(true);
220
- expect(policies.transientFactory.while!(error)).toBe(true);
221
- expect(policies.transientOptions.while!(error)).toBe(true);
222
- });
223
- }
224
-
225
- for (const error of permanent) {
226
- test(`no policy retries ${nameOf(error)}`, () => {
227
- for (const policy of Object.values(policies)) {
228
- expect(policy.while!(error)).toBe(false);
229
- }
230
- });
231
- }
232
-
233
- test("throttling policies retry only throttling errors", () => {
234
- for (const policy of [policies.throttlingFactory, policies.throttlingOptions]) {
235
- expect(policy.while!(new TooManyRequests({ message: "" }))).toBe(true);
236
- for (const error of transient.filter((e) => !(e instanceof TooManyRequests))) {
237
- expect(policy.while!(error)).toBe(false);
238
- }
239
- }
240
- });
241
- });
242
-
243
- // =============================================================================
244
- // Default policy
245
- // =============================================================================
246
-
247
- describe("makeDefault schedule", () => {
248
- test("backs off exponentially from 250ms, capped at 5s, and gives up after 8 retries", async () => {
249
- const http = scripted(() => status(503));
250
- const result = await runVirtual(getThing({}), http.layer);
251
- expect(result._tag).toBe("Failure");
252
- expect(result.failure).toBeInstanceOf(ServiceUnavailable);
253
- expect(http.attempts).toHaveLength(9);
254
- expect(delays(http.times())).toEqual([250, 500, 1000, 2000, 4000, 5000, 5000, 5000]);
255
- });
256
-
257
- test("succeeds as soon as a retry succeeds", async () => {
258
- const http = scripted(failTimes(2, () => status(500)));
259
- const result = await runVirtual(getThing({}), http.layer);
260
- expect(result).toMatchObject({ _tag: "Success", success: { ok: true } });
261
- expect(http.attempts).toHaveLength(3);
262
- });
263
-
264
- test("throttling errors wait at least 500ms", async () => {
265
- const http = scripted(failTimes(3, () => status(429)));
266
- await runVirtual(getThing({}), http.layer);
267
- expect(delays(http.times())).toEqual([500, 500, 1000]);
268
- });
269
-
270
- test("retries wire-level transport failures", async () => {
271
- const http = scripted(failTimes(2, () => "transport"));
272
- const result = await runVirtual(getThing({}), http.layer);
273
- expect(result._tag).toBe("Success");
274
- expect(delays(http.times())).toEqual([250, 500]);
275
- });
276
-
277
- test("does not retry non-transient failures", async () => {
278
- for (const code of [400, 401, 403, 404, 409, 422, 402]) {
279
- const http = scripted(() => status(code));
280
- const result = await runVirtual(getThing({}), http.layer);
281
- expect(result._tag).toBe("Failure");
282
- expect(http.attempts).toHaveLength(1);
283
- }
284
- });
285
-
286
- test("does not distinguish mutations from reads", async () => {
287
- const reads = scripted(failTimes(2, () => status(503)));
288
- const writes = scripted(failTimes(2, () => status(503)));
289
- await runVirtual(getThing({}), reads.layer);
290
- await runVirtual(createThing({}), writes.layer);
291
- expect(writes.attempts.map((a) => a.method)).toEqual(["POST", "POST", "POST"]);
292
- expect(writes.times()).toEqual(reads.times());
293
- });
294
-
295
- test("jitter adds up to 50ms to each delay", async () => {
296
- vi.spyOn(Math, "random").mockReturnValue(0.5);
297
- const http = scripted(failTimes(2, () => status(503)));
298
- await runVirtual(getThing({}), http.layer);
299
- expect(delays(http.times())).toEqual([275, 525]);
300
- });
301
- });
302
-
303
- describe("server retry hints", () => {
304
- test("the default policy honors retryAfter over its own backoff", async () => {
305
- const http = scripted(failTimes(2, () => status(503, { "retry-after": "2" })));
306
- await runVirtual(getThing({}), http.layer);
307
- expect(delays(http.times())).toEqual([2000, 2000]);
308
- });
309
-
310
- test("a hint longer than the 5s backoff cap is honored in full", async () => {
311
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "30" })));
312
- await runVirtual(getThing({}), http.layer);
313
- expect(delays(http.times())).toEqual([30_000]);
314
- });
315
-
316
- test("a hint shorter than the throttling floor still wins", async () => {
317
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "0" })));
318
- await runVirtual(getThing({}), http.layer);
319
- expect(delays(http.times())).toEqual([0]);
320
- });
321
-
322
- test("hints are capped at DEFAULT_SERVER_RETRY_HINT_CAP_MS", async () => {
323
- expect(Retry.DEFAULT_SERVER_RETRY_HINT_CAP_MS).toBe(60_000);
324
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
325
- await runVirtual(getThing({}), http.layer);
326
- expect(delays(http.times())).toEqual([60_000]);
327
- });
328
-
329
- test("serverRetryHintCapLayer overrides the cap", async () => {
330
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
331
- await runVirtual(
332
- getThing({}),
333
- Layer.mergeAll(http.layer, Retry.serverRetryHintCapLayer(1_500)),
334
- );
335
- expect(delays(http.times())).toEqual([1_500]);
336
- });
337
-
338
- test("DISTILLED_SERVER_RETRY_HINT_CAP_MS overrides the cap", async () => {
339
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
340
- await runVirtual(
341
- getThing({}),
342
- Layer.mergeAll(http.layer, env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: "2500" })),
343
- );
344
- expect(delays(http.times())).toEqual([2_500]);
345
- });
346
-
347
- test("an invalid DISTILLED_SERVER_RETRY_HINT_CAP_MS falls back to the default cap", async () => {
348
- for (const raw of ["-1", "soon", "Infinity", ""]) {
349
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
350
- await runVirtual(
351
- getThing({}),
352
- Layer.mergeAll(http.layer, env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: raw })),
353
- );
354
- expect(delays(http.times())).toEqual([60_000]);
355
- }
356
- });
357
-
358
- test("DISTILLED_SERVER_RETRY_HINT_CAP_MS is truncated to whole milliseconds", async () => {
359
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
360
- await runVirtual(
361
- getThing({}),
362
- Layer.mergeAll(http.layer, env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: "1234.9" })),
363
- );
364
- expect(delays(http.times())).toEqual([1_234]);
365
- });
366
-
367
- test("the layer cap wins over the environment cap", async () => {
368
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
369
- await runVirtual(
370
- getThing({}),
371
- Layer.mergeAll(
372
- http.layer,
373
- Retry.serverRetryHintCapLayer(1_000),
374
- env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: "2500" }),
375
- ),
376
- );
377
- expect(delays(http.times())).toEqual([1_000]);
378
- });
379
-
380
- test("an invalid layer cap falls back to the environment cap", async () => {
381
- const http = scripted(failTimes(1, () => status(429, { "retry-after": "600" })));
382
- await runVirtual(
383
- getThing({}),
384
- Layer.mergeAll(
385
- http.layer,
386
- Retry.serverRetryHintCapLayer(-1),
387
- env({ DISTILLED_SERVER_RETRY_HINT_CAP_MS: "2500" }),
388
- ),
389
- );
390
- expect(delays(http.times())).toEqual([2_500]);
391
- });
392
-
393
- test("factory policies honor hints; static Options policies cannot", async () => {
394
- const factory = scripted(failTimes(1, () => status(429, { "retry-after": "3" })));
395
- await runVirtual(getThing({}).pipe(withPolicy(Retry.throttlingFactory)), factory.layer);
396
- expect(delays(factory.times())).toEqual([3_000]);
397
-
398
- const options = scripted(failTimes(1, () => status(429, { "retry-after": "3" })));
399
- await runVirtual(getThing({}).pipe(withPolicy(Retry.throttlingOptions)), options.layer);
400
- expect(delays(options.times())).toEqual([1_000]);
401
- });
402
- });
403
-
404
- describe("readServerRetryHintCapMsFromEnv", () => {
405
- test("returns the default cap when the variable is unset", () => {
406
- expect(process.env.DISTILLED_SERVER_RETRY_HINT_CAP_MS).toBeUndefined();
407
- expect(Retry.readServerRetryHintCapMsFromEnv()).toBe(Retry.DEFAULT_SERVER_RETRY_HINT_CAP_MS);
408
- });
409
- });
410
-
411
- // =============================================================================
412
- // Other built-in policies
413
- // =============================================================================
414
-
415
- describe("throttling and transient policies", () => {
416
- test("transient policies back off from 1s, cap at 5s, and keep retrying past 8 attempts", async () => {
417
- for (const policy of [Retry.transientFactory, Retry.transientOptions]) {
418
- const http = scripted(failTimes(10, () => status(502)));
419
- const result = await runVirtual(getThing({}).pipe(withPolicy(policy)), http.layer);
420
- expect(result._tag).toBe("Success");
421
- expect(delays(http.times())).toEqual([
422
- 1000, 2000, 4000, 5000, 5000, 5000, 5000, 5000, 5000, 5000,
423
- ]);
424
- }
425
- });
426
-
427
- test("throttling policies do not retry server errors", async () => {
428
- const http = scripted(() => status(503));
429
- const result = await runVirtual(
430
- getThing({}).pipe(withPolicy(Retry.throttlingFactory)),
431
- http.layer,
432
- );
433
- expect(result.failure).toBeInstanceOf(ServiceUnavailable);
434
- expect(http.attempts).toHaveLength(1);
435
- });
436
- });
437
-
438
- describe("schedule helpers", () => {
439
- const delaysOf = async (schedule: Schedule.Schedule<unknown>, failures: number) => {
440
- const http = scripted(failTimes(failures, () => status(503)));
441
- await runVirtual(getThing({}).pipe(withPolicy({ while: () => true, schedule })), http.layer);
442
- return delays(http.times());
443
- };
444
-
445
- test("capped clamps each delay to the maximum", async () => {
446
- expect(
447
- await delaysOf(Schedule.exponential(1000, 3).pipe(Retry.capped(Duration.seconds(2))), 3),
448
- ).toEqual([1000, 2000, 2000]);
449
- });
450
-
451
- test("jittered adds Math.random() * 50ms", async () => {
452
- vi.spyOn(Math, "random").mockReturnValue(0.2);
453
- expect(await delaysOf(Schedule.spaced(100).pipe(Retry.jittered), 2)).toEqual([110, 110]);
454
- });
455
- });
456
-
457
- // =============================================================================
458
- // Policy resolution from context
459
- // =============================================================================
460
-
461
- describe("retry policy resolution", () => {
462
- test("operations without a retry tag never retry", async () => {
463
- const http = scripted(() => status(503));
464
- await runVirtual(getThingWithoutRetry({}).pipe(withPolicy(Retry.transientOptions)), http.layer);
465
- expect(http.attempts).toHaveLength(1);
466
- });
467
-
468
- test("with no policy in context the default policy applies", async () => {
469
- const http = scripted(failTimes(1, () => status(503)));
470
- const result = await runVirtual(getThing({}), http.layer);
471
- expect(result._tag).toBe("Success");
472
- expect(http.attempts).toHaveLength(2);
473
- });
474
-
475
- test("a policy that never retries disables the default", async () => {
476
- const http = scripted(() => status(503));
477
- await runVirtual(getThing({}).pipe(withPolicy(none)), http.layer);
478
- expect(http.attempts).toHaveLength(1);
479
- });
480
-
481
- test("a policy without `while` disables retrying", async () => {
482
- const http = scripted(() => status(503));
483
- await runVirtual(getThing({}).pipe(withPolicy({ schedule: Schedule.recurs(5) })), http.layer);
484
- expect(http.attempts).toHaveLength(1);
485
- });
486
-
487
- test("a policy without a schedule retries immediately while its predicate holds", async () => {
488
- const http = scripted(failTimes(3, () => status(400)));
489
- const result = await runVirtual(
490
- getThing({}).pipe(withPolicy({ while: (e) => e instanceof BadRequest })),
491
- http.layer,
492
- );
493
- expect(result._tag).toBe("Success");
494
- expect(http.times()).toEqual([0, 0, 0, 0]);
495
- });
496
-
497
- test("a custom schedule bounds the attempts", async () => {
498
- const http = scripted(() => status(503));
499
- await runVirtual(
500
- getThing({}).pipe(withPolicy({ while: () => true, schedule: Schedule.recurs(2) })),
501
- http.layer,
502
- );
503
- expect(http.attempts).toHaveLength(3);
504
- });
505
-
506
- test("factory policies get a ref holding the latest failure", async () => {
507
- const seen: Array<unknown> = [];
508
- const factory: Retry.Factory = (lastError) => ({
509
- while: () => true,
510
- schedule: Schedule.recurs(2).pipe(
511
- Schedule.tap(() =>
512
- Effect.flatMap(Ref.get(lastError), (e) => Effect.sync(() => seen.push(e))),
513
- ),
514
- ),
515
- });
516
- let n = 0;
517
- const http = scripted(() => status([500, 502, 503][n++]!));
518
- const result = await runVirtual(getThing({}).pipe(withPolicy(factory)), http.layer);
519
- // One schedule step per retry decision, each seeing the failure it retries.
520
- expect(seen.map((e) => (e as { _tag: string })._tag)).toEqual([
521
- "InternalServerError",
522
- "BadGateway",
523
- ]);
524
- expect(result.failure).toBeInstanceOf(ServiceUnavailable);
525
- });
526
-
527
- test("an outer policy applies to every call beneath it", async () => {
528
- const http = scripted(() => status(503));
529
- await runVirtual(
530
- Effect.all([Effect.result(getThing({})), Effect.result(createThing({}))]).pipe(
531
- withPolicy(none),
532
- ),
533
- http.layer,
534
- );
535
- expect(http.attempts.map((a) => a.method)).toEqual(["GET", "POST"]);
536
- });
537
-
538
- test("an inner policy overrides the outer one for its own subtree only", async () => {
539
- const http = scripted(() => status(503));
540
- const inner = getThing({}).pipe(withPolicy(none));
541
- const sibling = createThing({});
542
- await runVirtual(
543
- Effect.all([Effect.result(inner), Effect.result(sibling)]).pipe(
544
- withPolicy({ while: () => true, schedule: Schedule.recurs(2) }),
545
- ),
546
- http.layer,
547
- );
548
- expect(http.attempts.map((a) => a.method)).toEqual(["GET", "POST", "POST", "POST"]);
549
- });
550
-
551
- test("a policy under another SDK's tag does not apply", async () => {
552
- const http = scripted(failTimes(1, () => status(503)));
553
- const result = await runVirtual(
554
- getThing({}).pipe(Effect.provideService(OtherRetry, none)),
555
- http.layer,
556
- );
557
- expect(result._tag).toBe("Success");
558
- expect(http.attempts).toHaveLength(2);
559
- });
560
-
561
- test("a policy installed as a layer applies like provideService", async () => {
562
- const http = scripted(() => status(503));
563
- await runVirtual(getThing({}), Layer.mergeAll(http.layer, Layer.succeed(TestRetry, none)));
564
- expect(http.attempts).toHaveLength(1);
565
- });
566
- });
package/src/sdks.test.ts DELETED
@@ -1,87 +0,0 @@
1
- /**
2
- * Checks that run against every SDK package in the repo. Generated code is
3
- * tested once, here in core; these catch a package whose hand-written glue
4
- * drifted from what the generator and protocols expect.
5
- */
6
- import { existsSync, readdirSync, readFileSync } from "node:fs";
7
- import { join } from "node:path";
8
- import { describe, expect, test } from "vitest";
9
-
10
- const PACKAGES = join(import.meta.dirname, "..", "..");
11
-
12
- const sdks = readdirSync(PACKAGES)
13
- .filter((name) => name !== "core" && existsSync(join(PACKAGES, name, "package.json")))
14
- .sort();
15
-
16
- /** Packages without a `<Sdk>OpError` union over a REST/RPC protocol, and why. */
17
- const NO_OP_ERROR: Record<string, string> = {
18
- aws: "hand-written protocols; ParseError is part of CommonErrors in src/errors.ts",
19
- railway: "GraphQL Query SDK; errors are typed per root by core/graphql",
20
- };
21
-
22
- const read = (pkg: string, file: string): string | undefined => {
23
- const p = join(PACKAGES, pkg, file);
24
- return existsSync(p) ? readFileSync(p, "utf8") : undefined;
25
- };
26
-
27
- /** Hand-written sources: `src/*.ts` without tests (generated code lives in `src/services/`). */
28
- const handWritten = (pkg: string): string[] => {
29
- const dir = join(PACKAGES, pkg, "src");
30
- return readdirSync(dir)
31
- .filter((f) => f.endsWith(".ts") && !f.endsWith(".test.ts"))
32
- .map((f) => readFileSync(join(dir, f), "utf8"));
33
- };
34
-
35
- /** Every `export type <Name> = …` in the package's hand-written sources. */
36
- const typeAliases = (pkg: string): Map<string, string> => {
37
- const aliases = new Map<string, string>();
38
- for (const source of handWritten(pkg)) {
39
- for (const match of source.matchAll(/^export type (\w+)\s*=([\s\S]*?);[ \t]*$/gm)) {
40
- aliases.set(match[1]!, match[2]!);
41
- }
42
- }
43
- return aliases;
44
- };
45
-
46
- /** Names reachable from `root` through the package's own aliases (`DefaultErrors`, `ClientErrors`, …). */
47
- const reachable = (aliases: Map<string, string>, root: string): Set<string> => {
48
- const seen = new Set<string>();
49
- const stack = [root];
50
- while (stack.length > 0) {
51
- const name = stack.pop()!;
52
- if (seen.has(name)) continue;
53
- seen.add(name);
54
- for (const ref of aliases.get(name)?.match(/\b[A-Z]\w*\b/g) ?? []) stack.push(ref);
55
- }
56
- return seen;
57
- };
58
-
59
- describe("every SDK", () => {
60
- test("the exemption list names real packages", () => {
61
- for (const pkg of Object.keys(NO_OP_ERROR)) expect(sdks).toContain(pkg);
62
- });
63
-
64
- describe.each(sdks.filter((pkg) => !(pkg in NO_OP_ERROR)))("%s", (pkg) => {
65
- test("declares its ParseError in the operation error union and constructs it in the protocol", () => {
66
- const aliases = typeAliases(pkg);
67
- const opError = [...aliases.keys()].find((name) => name.endsWith("OpError"));
68
- expect(opError, `packages/${pkg}/src declares no \`export type <Sdk>OpError\``).toBeDefined();
69
- const parseError = [...reachable(aliases, opError!)].find((name) =>
70
- name.endsWith("ParseError"),
71
- );
72
- // Without it, `Effect.catchTag("<Sdk>ParseError")` on a strict call is a type error.
73
- expect(parseError, `${opError} has no ParseError member`).toBeDefined();
74
- expect(
75
- handWritten(pkg).some((source) => source.includes(`new ${parseError}(`)),
76
- `nothing outside src/services constructs ${parseError}`,
77
- ).toBe(true);
78
- expect(read(pkg, "src/errors.ts")).toMatch(new RegExp(`export class ${parseError}\\b`));
79
- });
80
- });
81
-
82
- test("aws keeps ParseError in CommonErrors", () => {
83
- const errors = read("aws", "src/errors.ts")!;
84
- expect(errors).toMatch(/export class ParseError\b/);
85
- expect(/export type CommonErrors =([^;]*);/.exec(errors)?.[1]).toMatch(/\bParseError\b/);
86
- });
87
- });