@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
@@ -1,89 +0,0 @@
1
- import * as Effect from "effect/Effect";
2
- import * as HttpClientRequest from "effect/http/HttpClientRequest";
3
- import * as HttpClientResponse from "effect/http/HttpClientResponse";
4
- import * as Layer from "effect/Layer";
5
- import * as Schema from "effect/Schema";
6
- import { describe, expect, test } from "vitest";
7
- import * as API from "./api.ts";
8
- import { makeRestProtocol } from "./protocol-rest.ts";
9
- import * as ResponseValidation from "./response-validation.ts";
10
-
11
- class TestParseError extends Schema.TaggedError<TestParseError>()("TestParseError", {
12
- body: Schema.Unknown,
13
- cause: Schema.Unknown,
14
- }) {}
15
-
16
- class TestUnknownError extends Schema.TaggedError<TestUnknownError>()("TestUnknownError", {
17
- message: Schema.String,
18
- }) {}
19
-
20
- const TestProtocol = makeRestProtocol<{}>({
21
- credentials: Effect.succeed({}),
22
- baseUrl: () => "https://api.test",
23
- headers: () => ({}),
24
- unknownError: ({ message }) => new TestUnknownError({ message }),
25
- parseError: ({ body, cause }) => new TestParseError({ body, cause }),
26
- });
27
-
28
- const Output = Schema.Struct({ id: Schema.String });
29
-
30
- const decode = (body: string) =>
31
- Effect.gen(function* () {
32
- const protocol = yield* API.Protocol;
33
- const request = HttpClientRequest.get("https://api.test/thing");
34
- return yield* protocol.decode({
35
- response: HttpClientResponse.fromWeb(request, new Response(body, { status: 200 })),
36
- outputAst: Output.ast,
37
- errors: [],
38
- config: {},
39
- });
40
- }).pipe(Effect.provide(TestProtocol));
41
-
42
- const run = <A, E>(effect: Effect.Effect<A, E>, layer?: Layer.Layer<never>) =>
43
- Effect.runPromise(Effect.result(layer ? effect.pipe(Effect.provide(layer)) : effect));
44
-
45
- describe("makeRestProtocol response validation", () => {
46
- test("lenient (default) returns a non-JSON 2xx body as text", async () => {
47
- const result = await run(decode("not json"));
48
- expect(result).toMatchObject({ _tag: "Success", success: "not json" });
49
- });
50
-
51
- test("lenient (default) returns {} for an output that requires id", async () => {
52
- const result = await run(decode("{}"));
53
- expect(result).toMatchObject({ _tag: "Success", success: {} });
54
- });
55
-
56
- test("strict fails a non-JSON 2xx body with the provider's parse error", async () => {
57
- const result = await run(decode("not json"), ResponseValidation.strict);
58
- expect(result._tag).toBe("Failure");
59
- const error = (result as any).failure;
60
- expect(error).toBeInstanceOf(TestParseError);
61
- expect(error.body).toBe("not json");
62
- });
63
-
64
- test("strict fails a body missing a required member", async () => {
65
- const result = await run(decode("{}"), ResponseValidation.strict);
66
- expect(result._tag).toBe("Failure");
67
- expect((result as any).failure).toBeInstanceOf(TestParseError);
68
- expect((result as any).failure.body).toEqual({});
69
- });
70
-
71
- test("strict passes a matching body through unchanged, extra members included", async () => {
72
- const result = await run(
73
- decode(JSON.stringify({ id: "a", extra: 1 })),
74
- ResponseValidation.strict,
75
- );
76
- expect(result).toMatchObject({
77
- _tag: "Success",
78
- success: { id: "a", extra: 1 },
79
- });
80
- });
81
-
82
- test("an inner lenient layer overrides an outer strict one", async () => {
83
- const result = await run(
84
- decode("{}").pipe(Effect.provide(ResponseValidation.lenient)),
85
- ResponseValidation.strict,
86
- );
87
- expect(result).toMatchObject({ _tag: "Success", success: {} });
88
- });
89
- });
@@ -1,267 +0,0 @@
1
- import * as Clock from "effect/Clock";
2
- import * as Duration from "effect/Duration";
3
- import * as Effect from "effect/Effect";
4
- import * as Fiber from "effect/Fiber";
5
- import * as HttpClient from "effect/http/HttpClient";
6
- import * as HttpClientResponse from "effect/http/HttpClientResponse";
7
- import * as Layer from "effect/Layer";
8
- import * as Schema from "effect/Schema";
9
- import * as TestClock from "effect/testing/TestClock";
10
- import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
11
- import * as API from "./api.ts";
12
- import { RETRYABLE_HTTP_STATUSES } from "./errors.ts";
13
- import { makeRestProtocol } from "./protocol-rest.ts";
14
- import {
15
- parseRatelimit,
16
- parseRetryAfter,
17
- parseRetryAfterForStatus,
18
- parseServerRetryHint,
19
- } from "./retry-after.ts";
20
- import * as Retry from "./retry.ts";
21
- import * as S from "./schema.ts";
22
- import * as T from "./trait.ts";
23
-
24
- const NOW = Date.parse("2026-10-05T12:00:00Z");
25
-
26
- const millis = (d: Duration.Duration | undefined) =>
27
- d === undefined ? undefined : Duration.toMillis(d);
28
-
29
- beforeEach(() => {
30
- vi.spyOn(Date, "now").mockReturnValue(NOW);
31
- // Pin retry jitter to zero so end-to-end delays are exact.
32
- vi.spyOn(Math, "random").mockReturnValue(0);
33
- });
34
-
35
- afterEach(() => {
36
- vi.restoreAllMocks();
37
- });
38
-
39
- describe("parseRetryAfter delay-seconds", () => {
40
- test("parses a non-negative integer number of seconds", () => {
41
- expect(millis(parseRetryAfter({ "retry-after": "120" }))).toBe(120_000);
42
- expect(millis(parseRetryAfter({ "retry-after": "1" }))).toBe(1_000);
43
- });
44
-
45
- test("zero seconds is a zero duration, not absent", () => {
46
- expect(millis(parseRetryAfter({ "retry-after": "0" }))).toBe(0);
47
- });
48
-
49
- test("surrounding whitespace is ignored", () => {
50
- expect(millis(parseRetryAfter({ "retry-after": " 5\t" }))).toBe(5_000);
51
- });
52
-
53
- test("leading zeros are accepted", () => {
54
- expect(millis(parseRetryAfter({ "retry-after": "007" }))).toBe(7_000);
55
- });
56
-
57
- test("signed or fractional numbers are rejected", () => {
58
- for (const raw of ["-5", "+5", "1.5", "-0", "0.0"]) {
59
- expect(parseRetryAfter({ "retry-after": raw })).toBeUndefined();
60
- }
61
- });
62
- });
63
-
64
- describe("parseRetryAfter HTTP-date", () => {
65
- test("parses an IMF-fixdate relative to now", () => {
66
- expect(millis(parseRetryAfter({ "retry-after": "Mon, 05 Oct 2026 12:00:30 GMT" }))).toBe(
67
- 30_000,
68
- );
69
- });
70
-
71
- test("parses the obsolete RFC 850 and asctime forms", () => {
72
- expect(millis(parseRetryAfter({ "retry-after": "Monday, 05-Oct-26 12:01:00 GMT" }))).toBe(
73
- 60_000,
74
- );
75
- expect(millis(parseRetryAfter({ "retry-after": "Mon Oct 5 12:00:10 2026 GMT" }))).toBe(10_000);
76
- });
77
-
78
- test("a date in the past is a zero duration", () => {
79
- expect(millis(parseRetryAfter({ "retry-after": "Mon, 05 Oct 2026 11:00:00 GMT" }))).toBe(0);
80
- });
81
-
82
- test("a date equal to now is a zero duration", () => {
83
- expect(millis(parseRetryAfter({ "retry-after": "Mon, 05 Oct 2026 12:00:00 GMT" }))).toBe(0);
84
- });
85
- });
86
-
87
- describe("parseRetryAfter absent or invalid headers", () => {
88
- test("no header bag, or no retry-after header, is absent", () => {
89
- expect(parseRetryAfter(undefined)).toBeUndefined();
90
- expect(parseRetryAfter({})).toBeUndefined();
91
- expect(parseRetryAfter({ "retry-after": undefined })).toBeUndefined();
92
- });
93
-
94
- test("empty and whitespace-only values are absent", () => {
95
- expect(parseRetryAfter({ "retry-after": "" })).toBeUndefined();
96
- expect(parseRetryAfter({ "retry-after": " " })).toBeUndefined();
97
- });
98
-
99
- test("unparseable values are absent", () => {
100
- for (const raw of ["soon", "5s", "PT5S", "tomorrow at noon"]) {
101
- expect(parseRetryAfter({ "retry-after": raw })).toBeUndefined();
102
- }
103
- });
104
-
105
- test("only the lowercase header key is read", () => {
106
- expect(parseRetryAfter({ "Retry-After": "5" })).toBeUndefined();
107
- });
108
- });
109
-
110
- describe("parseRatelimit", () => {
111
- test("returns the reset time when the remaining quota is zero", () => {
112
- expect(millis(parseRatelimit({ ratelimit: "r=0, t=30" }))).toBe(30_000);
113
- expect(millis(parseRatelimit({ ratelimit: "limit=100, remaining=0, reset=30" }))).toBe(30_000);
114
- expect(millis(parseRatelimit({ ratelimit: '"default";r=0;t=30' }))).toBe(30_000);
115
- });
116
-
117
- test("tolerates whitespace and key case", () => {
118
- expect(millis(parseRatelimit({ ratelimit: " R = 0 ; T = 12 " }))).toBe(12_000);
119
- });
120
-
121
- test("is absent while quota remains", () => {
122
- expect(parseRatelimit({ ratelimit: "r=5, t=30" })).toBeUndefined();
123
- expect(parseRatelimit({ ratelimit: "limit=100, remaining=1, reset=30" })).toBeUndefined();
124
- expect(parseRatelimit({ ratelimit: '"default";r=2;t=30' })).toBeUndefined();
125
- });
126
-
127
- test("is absent unless remaining is present and exactly zero", () => {
128
- expect(parseRatelimit({ ratelimit: "t=30" })).toBeUndefined();
129
- expect(parseRatelimit({ ratelimit: "limit=100, reset=30" })).toBeUndefined();
130
- expect(parseRatelimit({ ratelimit: '"default";t=30' })).toBeUndefined();
131
- expect(parseRatelimit({ ratelimit: "r=-1, t=30" })).toBeUndefined();
132
- expect(parseRatelimit({ ratelimit: "remaining=-1, reset=30" })).toBeUndefined();
133
- expect(parseRatelimit({ ratelimit: "r=0.5, t=30" })).toBeUndefined();
134
- expect(parseRatelimit({ ratelimit: "r=none, t=30" })).toBeUndefined();
135
- });
136
-
137
- test("is absent without a usable reset", () => {
138
- expect(parseRatelimit({ ratelimit: "r=0" })).toBeUndefined();
139
- expect(parseRatelimit({ ratelimit: "r=0, t=-1" })).toBeUndefined();
140
- expect(parseRatelimit({ ratelimit: "r=0, t=later" })).toBeUndefined();
141
- });
142
-
143
- test("is absent when the header is missing or empty", () => {
144
- expect(parseRatelimit(undefined)).toBeUndefined();
145
- expect(parseRatelimit({})).toBeUndefined();
146
- expect(parseRatelimit({ ratelimit: "" })).toBeUndefined();
147
- });
148
- });
149
-
150
- describe("parseServerRetryHint", () => {
151
- test("prefers Retry-After over RateLimit", () => {
152
- expect(millis(parseServerRetryHint({ "retry-after": "5", ratelimit: "r=0, t=30" }))).toBe(
153
- 5_000,
154
- );
155
- });
156
-
157
- test("falls back to RateLimit when Retry-After is absent or invalid", () => {
158
- expect(millis(parseServerRetryHint({ ratelimit: "r=0, t=30" }))).toBe(30_000);
159
- expect(millis(parseServerRetryHint({ "retry-after": "soon", ratelimit: "r=0, t=30" }))).toBe(
160
- 30_000,
161
- );
162
- });
163
-
164
- test("is absent when neither header yields a value", () => {
165
- expect(parseServerRetryHint({ ratelimit: "r=3, t=30" })).toBeUndefined();
166
- expect(parseServerRetryHint({ ratelimit: "t=30" })).toBeUndefined();
167
- expect(parseServerRetryHint(undefined)).toBeUndefined();
168
- });
169
- });
170
-
171
- describe("parseRetryAfterForStatus", () => {
172
- test("returns the hint for every retryable status", () => {
173
- for (const status of RETRYABLE_HTTP_STATUSES) {
174
- expect(millis(parseRetryAfterForStatus(status, { "retry-after": "4" }))).toBe(4_000);
175
- }
176
- });
177
-
178
- test("ignores hints on non-retryable statuses", () => {
179
- for (const status of [200, 400, 401, 403, 404, 409, 422, 501, 521]) {
180
- expect(parseRetryAfterForStatus(status, { "retry-after": "4" })).toBeUndefined();
181
- }
182
- });
183
- });
184
-
185
- // =============================================================================
186
- // End to end: header → error.retryAfter → retry delay
187
- // =============================================================================
188
-
189
- class TestRetry extends Retry.makeRetryService("RetryAfterTest/Retry") {}
190
-
191
- class UnknownTestError extends Schema.TaggedError<UnknownTestError>()("UnknownTestError", {
192
- message: Schema.String,
193
- }) {}
194
-
195
- const TestProtocol = makeRestProtocol<{}>({
196
- credentials: Effect.succeed({}),
197
- baseUrl: () => "https://api.test",
198
- headers: () => ({}),
199
- unknownError: ({ message }) => new UnknownTestError({ message }),
200
- parseError: ({ body }) => new UnknownTestError({ message: String(body) }),
201
- });
202
-
203
- const getThing = API.make(() => ({
204
- input: S.Struct({}).pipe(T.Http({ method: "GET", uri: "/thing" })),
205
- output: S.Struct({ ok: S.optional(S.Boolean) }),
206
- protocol: TestProtocol,
207
- retry: TestRetry,
208
- })) as unknown as (input: Record<string, unknown>) => Effect.Effect<any, any, any>;
209
-
210
- /** First response fails with `status`/`headers`, the second succeeds; returns attempt times. */
211
- const retryDelays = async (status: number, headers: Record<string, string>) => {
212
- const times: Array<number> = [];
213
- const client = Layer.succeed(
214
- HttpClient.HttpClient,
215
- HttpClient.make((request) =>
216
- Effect.gen(function* () {
217
- times.push(yield* Clock.currentTimeMillis);
218
- const response =
219
- times.length === 1
220
- ? new Response(JSON.stringify({ message: "nope" }), { status, headers })
221
- : new Response(JSON.stringify({ ok: true }), { status: 200 });
222
- return HttpClientResponse.fromWeb(request, response);
223
- }),
224
- ),
225
- );
226
- await Effect.runPromise(
227
- Effect.gen(function* () {
228
- const fiber = yield* Effect.forkChild(Effect.result(getThing({})));
229
- yield* TestClock.adjust(Duration.hours(1));
230
- return yield* Fiber.join(fiber);
231
- }).pipe(Effect.provide(Layer.mergeAll(client, TestClock.layer()))) as Effect.Effect<
232
- any,
233
- never,
234
- never
235
- >,
236
- );
237
- return times.slice(1).map((t, i) => t - times[i]!);
238
- };
239
-
240
- describe("Retry-After feeding the default retry delay", () => {
241
- test("delay-seconds replaces the default backoff", async () => {
242
- expect(await retryDelays(503, { "retry-after": "3" })).toEqual([3_000]);
243
- });
244
-
245
- test("an HTTP-date waits until that date", async () => {
246
- expect(await retryDelays(429, { "retry-after": "Mon, 05 Oct 2026 12:00:04 GMT" })).toEqual([
247
- 4_000,
248
- ]);
249
- });
250
-
251
- test("a RateLimit reset is used when Retry-After is absent", async () => {
252
- expect(await retryDelays(429, { ratelimit: "r=0, t=2" })).toEqual([2_000]);
253
- });
254
-
255
- test("an invalid header falls back to the default backoff", async () => {
256
- expect(await retryDelays(503, { "retry-after": "soon" })).toEqual([250]);
257
- expect(await retryDelays(429, { "retry-after": "soon" })).toEqual([500]);
258
- });
259
-
260
- test("a RateLimit header with quota left falls back to the default backoff", async () => {
261
- expect(await retryDelays(503, { ratelimit: "r=4, t=30" })).toEqual([250]);
262
- });
263
-
264
- test("a hint on a non-retryable status neither stamps nor triggers a retry", async () => {
265
- expect(await retryDelays(404, { "retry-after": "3" })).toEqual([]);
266
- });
267
- });