@distilled.cloud/core 1.0.0-rc.14 → 1.0.0-rc.16

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 (312) hide show
  1. package/lib/api.d.ts +118 -13
  2. package/lib/api.d.ts.map +1 -1
  3. package/lib/api.js +755 -147
  4. package/lib/api.js.map +1 -1
  5. package/lib/category.d.ts.map +1 -1
  6. package/lib/category.js +146 -146
  7. package/lib/category.js.map +1 -1
  8. package/lib/chunks.d.ts +3 -0
  9. package/lib/chunks.d.ts.map +1 -0
  10. package/lib/chunks.js +11 -0
  11. package/lib/chunks.js.map +1 -0
  12. package/lib/codegen/boolean-string-enums.js +36 -36
  13. package/lib/codegen/chunk.d.ts +7 -0
  14. package/lib/codegen/chunk.d.ts.map +1 -0
  15. package/lib/codegen/chunk.js +139 -0
  16. package/lib/codegen/chunk.js.map +1 -0
  17. package/lib/codegen/cli.d.ts.map +1 -1
  18. package/lib/codegen/cli.js +28 -14
  19. package/lib/codegen/cli.js.map +1 -1
  20. package/lib/codegen/datafy.d.ts +14 -0
  21. package/lib/codegen/datafy.d.ts.map +1 -0
  22. package/lib/codegen/datafy.js +853 -0
  23. package/lib/codegen/datafy.js.map +1 -0
  24. package/lib/codegen/dedupe.d.ts +5 -0
  25. package/lib/codegen/dedupe.d.ts.map +1 -0
  26. package/lib/codegen/dedupe.js +153 -0
  27. package/lib/codegen/dedupe.js.map +1 -0
  28. package/lib/codegen/emit.js +56 -56
  29. package/lib/codegen/format.js +18 -18
  30. package/lib/codegen/generated-at.js +29 -29
  31. package/lib/codegen/generator.d.ts.map +1 -1
  32. package/lib/codegen/generator.js +92 -84
  33. package/lib/codegen/generator.js.map +1 -1
  34. package/lib/codegen/graph.js +36 -36
  35. package/lib/codegen/graphql-client.d.ts +2 -0
  36. package/lib/codegen/graphql-client.d.ts.map +1 -1
  37. package/lib/codegen/graphql-client.js +55 -26
  38. package/lib/codegen/graphql-client.js.map +1 -1
  39. package/lib/codegen/graphql.js +113 -113
  40. package/lib/codegen/latin1.d.ts +3 -0
  41. package/lib/codegen/latin1.d.ts.map +1 -0
  42. package/lib/codegen/latin1.js +126 -0
  43. package/lib/codegen/latin1.js.map +1 -0
  44. package/lib/codegen/members.d.ts +7 -0
  45. package/lib/codegen/members.d.ts.map +1 -1
  46. package/lib/codegen/members.js +26 -19
  47. package/lib/codegen/members.js.map +1 -1
  48. package/lib/codegen/model-diff.js +6 -6
  49. package/lib/codegen/naming.js +19 -19
  50. package/lib/codegen/openapi-cli.js +15 -15
  51. package/lib/codegen/openapi.d.ts +17 -0
  52. package/lib/codegen/openapi.d.ts.map +1 -1
  53. package/lib/codegen/openapi.js +323 -169
  54. package/lib/codegen/openapi.js.map +1 -1
  55. package/lib/codegen/operations.js +14 -14
  56. package/lib/codegen/pagination.js +11 -11
  57. package/lib/codegen/patch-audit.js +57 -57
  58. package/lib/codegen/patches.js +79 -79
  59. package/lib/codegen/prelude.js +19 -19
  60. package/lib/codegen/privatize.d.ts +21 -0
  61. package/lib/codegen/privatize.d.ts.map +1 -0
  62. package/lib/codegen/privatize.js +176 -0
  63. package/lib/codegen/privatize.js.map +1 -0
  64. package/lib/codegen/proto.js +25 -25
  65. package/lib/codegen/pure.d.ts +6 -0
  66. package/lib/codegen/pure.d.ts.map +1 -0
  67. package/lib/codegen/pure.js +91 -0
  68. package/lib/codegen/pure.js.map +1 -0
  69. package/lib/codegen/rewrite-operation-ids.js +108 -108
  70. package/lib/codegen/spec-path.js +57 -57
  71. package/lib/codegen/table.d.ts +5 -0
  72. package/lib/codegen/table.d.ts.map +1 -0
  73. package/lib/codegen/table.js +249 -0
  74. package/lib/codegen/table.js.map +1 -0
  75. package/lib/config.d.ts +27 -0
  76. package/lib/config.d.ts.map +1 -0
  77. package/lib/config.js +161 -0
  78. package/lib/config.js.map +1 -0
  79. package/lib/crypto.d.ts +76 -0
  80. package/lib/crypto.d.ts.map +1 -0
  81. package/lib/crypto.js +130 -0
  82. package/lib/crypto.js.map +1 -0
  83. package/lib/debug.d.ts +2 -0
  84. package/lib/debug.d.ts.map +1 -0
  85. package/lib/debug.js +8 -0
  86. package/lib/debug.js.map +1 -0
  87. package/lib/error-category.d.ts +1 -1
  88. package/lib/error-category.d.ts.map +1 -1
  89. package/lib/error-category.js +7 -6
  90. package/lib/error-category.js.map +1 -1
  91. package/lib/errors.d.ts +62 -62
  92. package/lib/errors.d.ts.map +1 -1
  93. package/lib/errors.js +117 -114
  94. package/lib/errors.js.map +1 -1
  95. package/lib/graphql.d.ts +31 -20
  96. package/lib/graphql.d.ts.map +1 -1
  97. package/lib/graphql.js +150 -119
  98. package/lib/graphql.js.map +1 -1
  99. package/lib/graphql.types.js +4 -3
  100. package/lib/graphql.types.js.map +1 -1
  101. package/lib/index.d.ts +3 -2
  102. package/lib/index.d.ts.map +1 -1
  103. package/lib/index.js +15 -14
  104. package/lib/index.js.map +1 -1
  105. package/lib/json-patch.d.ts +2 -2
  106. package/lib/json-patch.js +28 -28
  107. package/lib/pagination.d.ts +9 -25
  108. package/lib/pagination.d.ts.map +1 -1
  109. package/lib/pagination.js +95 -90
  110. package/lib/pagination.js.map +1 -1
  111. package/lib/plain-values.d.ts +10 -0
  112. package/lib/plain-values.d.ts.map +1 -0
  113. package/lib/plain-values.js +40 -0
  114. package/lib/plain-values.js.map +1 -0
  115. package/lib/protocol-http.d.ts +50 -14
  116. package/lib/protocol-http.d.ts.map +1 -1
  117. package/lib/protocol-http.js +351 -178
  118. package/lib/protocol-http.js.map +1 -1
  119. package/lib/protocol-rest.d.ts +38 -25
  120. package/lib/protocol-rest.d.ts.map +1 -1
  121. package/lib/protocol-rest.js +238 -130
  122. package/lib/protocol-rest.js.map +1 -1
  123. package/lib/query.js +16 -16
  124. package/lib/response-validation-strict.d.ts +48 -0
  125. package/lib/response-validation-strict.d.ts.map +1 -0
  126. package/lib/response-validation-strict.js +284 -0
  127. package/lib/response-validation-strict.js.map +1 -0
  128. package/lib/response-validation.d.ts +43 -25
  129. package/lib/response-validation.d.ts.map +1 -1
  130. package/lib/response-validation.js +112 -70
  131. package/lib/response-validation.js.map +1 -1
  132. package/lib/retry-after.d.ts +3 -3
  133. package/lib/retry-after.js +49 -49
  134. package/lib/retry.d.ts +48 -29
  135. package/lib/retry.d.ts.map +1 -1
  136. package/lib/retry.js +142 -154
  137. package/lib/retry.js.map +1 -1
  138. package/lib/schema-lite-impl.d.ts +158 -0
  139. package/lib/schema-lite-impl.d.ts.map +1 -0
  140. package/lib/schema-lite-impl.js +341 -0
  141. package/lib/schema-lite-impl.js.map +1 -0
  142. package/lib/schema-lite-typed.d.ts +41 -0
  143. package/lib/schema-lite-typed.d.ts.map +1 -0
  144. package/lib/schema-lite-typed.js +27 -0
  145. package/lib/schema-lite-typed.js.map +1 -0
  146. package/lib/schema-lite.d.ts +40 -0
  147. package/lib/schema-lite.d.ts.map +1 -0
  148. package/lib/schema-lite.js +31 -0
  149. package/lib/schema-lite.js.map +1 -0
  150. package/lib/schema.d.ts +20 -6
  151. package/lib/schema.d.ts.map +1 -1
  152. package/lib/schema.js +202 -30
  153. package/lib/schema.js.map +1 -1
  154. package/lib/testing.d.ts +3 -2
  155. package/lib/testing.d.ts.map +1 -1
  156. package/lib/testing.js +22 -21
  157. package/lib/testing.js.map +1 -1
  158. package/lib/trait.d.ts +13 -13
  159. package/lib/trait.d.ts.map +1 -1
  160. package/lib/trait.js +80 -80
  161. package/lib/trait.js.map +1 -1
  162. package/lib/xml.d.ts +1 -1
  163. package/lib/xml.d.ts.map +1 -1
  164. package/lib/xml.js +38 -26
  165. package/lib/xml.js.map +1 -1
  166. package/package.json +9 -7
  167. package/src/api.ts +1079 -180
  168. package/src/category.ts +33 -33
  169. package/src/chunks.ts +30 -0
  170. package/src/codegen/chunk.ts +147 -0
  171. package/src/codegen/cli.ts +27 -1
  172. package/src/codegen/datafy.ts +874 -0
  173. package/src/codegen/dedupe.ts +173 -0
  174. package/src/codegen/generator.ts +10 -3
  175. package/src/codegen/graphql-client.ts +49 -15
  176. package/src/codegen/latin1.ts +131 -0
  177. package/src/codegen/members.ts +8 -0
  178. package/src/codegen/openapi.ts +194 -3
  179. package/src/codegen/privatize.ts +200 -0
  180. package/src/codegen/pure.ts +94 -0
  181. package/src/codegen/table.ts +254 -0
  182. package/src/config.ts +216 -0
  183. package/src/crypto.ts +202 -0
  184. package/src/debug.ts +8 -0
  185. package/src/error-category.ts +2 -1
  186. package/src/errors.ts +65 -56
  187. package/src/graphql.ts +109 -62
  188. package/src/graphql.types.ts +9 -7
  189. package/src/index.ts +3 -2
  190. package/src/json-patch.ts +2 -2
  191. package/src/pagination.ts +37 -18
  192. package/src/plain-values.ts +41 -0
  193. package/src/protocol-http.ts +378 -145
  194. package/src/protocol-rest.ts +337 -169
  195. package/src/response-validation-strict.ts +327 -0
  196. package/src/response-validation.ts +101 -50
  197. package/src/retry-after.ts +3 -3
  198. package/src/retry.ts +94 -93
  199. package/src/schema-lite-impl.ts +550 -0
  200. package/src/schema-lite-typed.ts +44 -0
  201. package/src/schema-lite.ts +44 -0
  202. package/src/schema.ts +258 -14
  203. package/src/testing.ts +4 -2
  204. package/src/trait.ts +36 -28
  205. package/src/xml.ts +31 -17
  206. package/lib/codegen/boolean-string-enums.test.d.ts +0 -2
  207. package/lib/codegen/boolean-string-enums.test.d.ts.map +0 -1
  208. package/lib/codegen/boolean-string-enums.test.js +0 -275
  209. package/lib/codegen/boolean-string-enums.test.js.map +0 -1
  210. package/lib/codegen/defer-naming.test.d.ts +0 -2
  211. package/lib/codegen/defer-naming.test.d.ts.map +0 -1
  212. package/lib/codegen/defer-naming.test.js +0 -105
  213. package/lib/codegen/defer-naming.test.js.map +0 -1
  214. package/lib/codegen/generated-at.test.d.ts +0 -2
  215. package/lib/codegen/generated-at.test.d.ts.map +0 -1
  216. package/lib/codegen/generated-at.test.js +0 -81
  217. package/lib/codegen/generated-at.test.js.map +0 -1
  218. package/lib/codegen/generator.test.d.ts +0 -2
  219. package/lib/codegen/generator.test.d.ts.map +0 -1
  220. package/lib/codegen/generator.test.js +0 -121
  221. package/lib/codegen/generator.test.js.map +0 -1
  222. package/lib/codegen/graphql-client.test.d.ts +0 -2
  223. package/lib/codegen/graphql-client.test.d.ts.map +0 -1
  224. package/lib/codegen/graphql-client.test.js +0 -171
  225. package/lib/codegen/graphql-client.test.js.map +0 -1
  226. package/lib/codegen/openapi-binary.test.d.ts +0 -2
  227. package/lib/codegen/openapi-binary.test.d.ts.map +0 -1
  228. package/lib/codegen/openapi-binary.test.js +0 -67
  229. package/lib/codegen/openapi-binary.test.js.map +0 -1
  230. package/lib/codegen/patch-audit.test.d.ts +0 -2
  231. package/lib/codegen/patch-audit.test.d.ts.map +0 -1
  232. package/lib/codegen/patch-audit.test.js +0 -141
  233. package/lib/codegen/patch-audit.test.js.map +0 -1
  234. package/lib/codegen/patches.test.d.ts +0 -2
  235. package/lib/codegen/patches.test.d.ts.map +0 -1
  236. package/lib/codegen/patches.test.js +0 -155
  237. package/lib/codegen/patches.test.js.map +0 -1
  238. package/lib/codegen/protocol-imports.test.d.ts +0 -2
  239. package/lib/codegen/protocol-imports.test.d.ts.map +0 -1
  240. package/lib/codegen/protocol-imports.test.js +0 -36
  241. package/lib/codegen/protocol-imports.test.js.map +0 -1
  242. package/lib/codegen/rewrite-operation-ids.test.d.ts +0 -2
  243. package/lib/codegen/rewrite-operation-ids.test.d.ts.map +0 -1
  244. package/lib/codegen/rewrite-operation-ids.test.js +0 -451
  245. package/lib/codegen/rewrite-operation-ids.test.js.map +0 -1
  246. package/lib/codegen/untagged-unions.test.d.ts +0 -2
  247. package/lib/codegen/untagged-unions.test.d.ts.map +0 -1
  248. package/lib/codegen/untagged-unions.test.js +0 -80
  249. package/lib/codegen/untagged-unions.test.js.map +0 -1
  250. package/lib/fixtures/xml/upstream-validator.json +0 -397
  251. package/lib/graphql.test.d.ts +0 -2
  252. package/lib/graphql.test.d.ts.map +0 -1
  253. package/lib/graphql.test.js +0 -151
  254. package/lib/graphql.test.js.map +0 -1
  255. package/lib/json-patch.test.d.ts +0 -2
  256. package/lib/json-patch.test.d.ts.map +0 -1
  257. package/lib/json-patch.test.js +0 -65
  258. package/lib/json-patch.test.js.map +0 -1
  259. package/lib/pagination.test.d.ts +0 -2
  260. package/lib/pagination.test.d.ts.map +0 -1
  261. package/lib/pagination.test.js +0 -131
  262. package/lib/pagination.test.js.map +0 -1
  263. package/lib/protocol-http.test.d.ts +0 -2
  264. package/lib/protocol-http.test.d.ts.map +0 -1
  265. package/lib/protocol-http.test.js +0 -228
  266. package/lib/protocol-http.test.js.map +0 -1
  267. package/lib/protocol-rest.test.d.ts +0 -2
  268. package/lib/protocol-rest.test.d.ts.map +0 -1
  269. package/lib/protocol-rest.test.js +0 -498
  270. package/lib/protocol-rest.test.js.map +0 -1
  271. package/lib/response-validation.test.d.ts +0 -2
  272. package/lib/response-validation.test.d.ts.map +0 -1
  273. package/lib/response-validation.test.js +0 -72
  274. package/lib/response-validation.test.js.map +0 -1
  275. package/lib/retry-after.test.d.ts +0 -2
  276. package/lib/retry-after.test.d.ts.map +0 -1
  277. package/lib/retry-after.test.js +0 -204
  278. package/lib/retry-after.test.js.map +0 -1
  279. package/lib/retry.test.d.ts +0 -2
  280. package/lib/retry.test.d.ts.map +0 -1
  281. package/lib/retry.test.js +0 -407
  282. package/lib/retry.test.js.map +0 -1
  283. package/lib/sdks.test.d.ts +0 -2
  284. package/lib/sdks.test.d.ts.map +0 -1
  285. package/lib/sdks.test.js +0 -76
  286. package/lib/sdks.test.js.map +0 -1
  287. package/lib/xml.test.d.ts +0 -2
  288. package/lib/xml.test.d.ts.map +0 -1
  289. package/lib/xml.test.js +0 -210
  290. package/lib/xml.test.js.map +0 -1
  291. package/src/codegen/boolean-string-enums.test.ts +0 -299
  292. package/src/codegen/defer-naming.test.ts +0 -113
  293. package/src/codegen/generated-at.test.ts +0 -89
  294. package/src/codegen/generator.test.ts +0 -137
  295. package/src/codegen/graphql-client.test.ts +0 -189
  296. package/src/codegen/openapi-binary.test.ts +0 -91
  297. package/src/codegen/patch-audit.test.ts +0 -160
  298. package/src/codegen/patches.test.ts +0 -188
  299. package/src/codegen/protocol-imports.test.ts +0 -39
  300. package/src/codegen/rewrite-operation-ids.test.ts +0 -472
  301. package/src/codegen/untagged-unions.test.ts +0 -85
  302. package/src/fixtures/xml/upstream-validator.json +0 -397
  303. package/src/graphql.test.ts +0 -198
  304. package/src/json-patch.test.ts +0 -77
  305. package/src/pagination.test.ts +0 -169
  306. package/src/protocol-http.test.ts +0 -276
  307. package/src/protocol-rest.test.ts +0 -703
  308. package/src/response-validation.test.ts +0 -89
  309. package/src/retry-after.test.ts +0 -267
  310. package/src/retry.test.ts +0 -566
  311. package/src/sdks.test.ts +0 -87
  312. package/src/xml.test.ts +0 -251
@@ -1,77 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { applyOperation, StaleTargetError } from "./json-patch.ts";
3
-
4
- describe("RFC 6902 arrays", () => {
5
- test("add at an index inserts before it", () => {
6
- const doc = { errors: ["a", "c"] };
7
- applyOperation(doc, { op: "add", path: "/errors/1", value: "b" });
8
- expect(doc.errors).toEqual(["a", "b", "c"]);
9
- });
10
-
11
- test("add at the length appends, like `-`", () => {
12
- const doc = { errors: ["a"] };
13
- applyOperation(doc, { op: "add", path: "/errors/1", value: "b" });
14
- applyOperation(doc, { op: "add", path: "/errors/-", value: "c" });
15
- expect(doc.errors).toEqual(["a", "b", "c"]);
16
- });
17
-
18
- test("add past the end is a stale target", () => {
19
- expect(() =>
20
- applyOperation({ errors: [] }, { op: "add", path: "/errors/2", value: "x" }),
21
- ).toThrow(StaleTargetError);
22
- });
23
-
24
- test("replace at an index overwrites", () => {
25
- const doc = { errors: ["a", "x", "c"] };
26
- applyOperation(doc, { op: "replace", path: "/errors/1", value: "b" });
27
- expect(doc.errors).toEqual(["a", "b", "c"]);
28
- });
29
-
30
- test("replace keeps an object key in place", () => {
31
- const doc: Record<string, unknown> = { a: 1, b: 2, c: 3 };
32
- applyOperation(doc, { op: "replace", path: "/b", value: 4 });
33
- expect(Object.keys(doc)).toEqual(["a", "b", "c"]);
34
- });
35
-
36
- test("move into an array inserts", () => {
37
- const doc = { from: "b", list: ["a", "c"] };
38
- applyOperation(doc, { op: "move", from: "/from", path: "/list/1" });
39
- expect(doc).toEqual({ list: ["a", "b", "c"] });
40
- });
41
- });
42
-
43
- describe("`before` on object keys", () => {
44
- test("add inserts a new key before the named sibling", () => {
45
- const doc: Record<string, unknown> = { input: 1, output: 2, traits: 3 };
46
- applyOperation(doc, { op: "add", path: "/errors", value: [], before: "traits" });
47
- expect(Object.keys(doc)).toEqual(["input", "output", "errors", "traits"]);
48
- });
49
-
50
- test("add keeps an existing key where it is", () => {
51
- const doc: Record<string, unknown> = { a: 1, b: 2, c: 3 };
52
- applyOperation(doc, { op: "add", path: "/c", value: 4, before: "a" });
53
- expect(doc).toEqual({ a: 1, b: 2, c: 4 });
54
- expect(Object.keys(doc)).toEqual(["a", "b", "c"]);
55
- });
56
-
57
- test("add appends when the sibling is absent", () => {
58
- const doc: Record<string, unknown> = { a: 1 };
59
- applyOperation(doc, { op: "add", path: "/b", value: 2, before: "missing" });
60
- expect(Object.keys(doc)).toEqual(["a", "b"]);
61
- });
62
-
63
- test("add keeps the parent object's identity", () => {
64
- const shapes: Record<string, unknown> = { a: 1, c: 3 };
65
- const doc = { shapes };
66
- applyOperation(doc, { op: "add", path: "/shapes/b", value: 2, before: "c" });
67
- expect(doc.shapes).toBe(shapes);
68
- expect(Object.keys(shapes)).toEqual(["a", "b", "c"]);
69
- });
70
-
71
- test("move onto itself reorders a key", () => {
72
- const doc: Record<string, unknown> = { a: 1, b: 2, c: 3 };
73
- applyOperation(doc, { op: "move", from: "/c", path: "/c", before: "a" });
74
- expect(Object.keys(doc)).toEqual(["c", "a", "b"]);
75
- expect(doc).toEqual({ a: 1, b: 2, c: 3 });
76
- });
77
- });
@@ -1,169 +0,0 @@
1
- import * as Effect from "effect/Effect";
2
- import * as Stream from "effect/Stream";
3
- import { describe, expect, test } from "vitest";
4
- import { extractItems, paginateCursor, type PaginatedTrait } from "./pagination.ts";
5
-
6
- const pagination: PaginatedTrait = {
7
- mode: "cursor",
8
- inputToken: "cursor",
9
- outputToken: "pagination.cursor",
10
- items: "items",
11
- pageSize: "limit",
12
- };
13
-
14
- type Page = {
15
- items?: number[];
16
- pagination?: { cursor?: string | null };
17
- };
18
-
19
- type Scenario = {
20
- name: string;
21
- start?: string;
22
- pages: Page[];
23
- cursors: Array<string | undefined>;
24
- };
25
-
26
- const scenarios: Scenario[] = [
27
- {
28
- name: "advances normally until the cursor is omitted",
29
- pages: [
30
- { items: [1], pagination: { cursor: "a" } },
31
- { items: [2], pagination: { cursor: "b" } },
32
- { items: [3] },
33
- ],
34
- cursors: [undefined, "a", "b"],
35
- },
36
- ...([null, ""] as const).map((cursor) => ({
37
- name: `terminates on ${JSON.stringify(cursor)}`,
38
- pages: [
39
- { items: [1], pagination: { cursor: "a" } },
40
- { items: [2], pagination: { cursor } },
41
- ],
42
- cursors: [undefined, "a"],
43
- })),
44
- {
45
- name: "terminates on an empty page echoing the requested cursor",
46
- pages: [
47
- { items: [1], pagination: { cursor: "a" } },
48
- { items: [], pagination: { cursor: "a" } },
49
- ],
50
- cursors: [undefined, "a"],
51
- },
52
- {
53
- name: "emits a nonempty page before stopping on a repeated cursor",
54
- pages: [
55
- { items: [1], pagination: { cursor: "a" } },
56
- { items: [2], pagination: { cursor: "a" } },
57
- ],
58
- cursors: [undefined, "a"],
59
- },
60
- {
61
- name: "terminates when the first response echoes the initial cursor",
62
- start: "initial",
63
- pages: [{ items: [1], pagination: { cursor: "initial" } }],
64
- cursors: ["initial"],
65
- },
66
- {
67
- name: "terminates a multi-cursor cycle",
68
- pages: [
69
- { items: [1], pagination: { cursor: "a" } },
70
- { items: [2], pagination: { cursor: "b" } },
71
- { items: [3], pagination: { cursor: "a" } },
72
- ],
73
- cursors: [undefined, "a", "b"],
74
- },
75
- {
76
- name: "terminates a cycle returning to the initial cursor",
77
- start: "initial",
78
- pages: [
79
- { items: [1], pagination: { cursor: "a" } },
80
- { items: [2], pagination: { cursor: "initial" } },
81
- ],
82
- cursors: ["initial", "a"],
83
- },
84
- {
85
- name: "preserves empty and sparse pages whose cursors advance",
86
- pages: [
87
- { items: [], pagination: { cursor: "a" } },
88
- { pagination: { cursor: "b" } },
89
- { items: [1], pagination: { cursor: "c" } },
90
- { items: [], pagination: { cursor: "d" } },
91
- { items: [2] },
92
- ],
93
- cursors: [undefined, "a", "b", "c", "d"],
94
- },
95
- ];
96
-
97
- const fixture = ({ pages, start }: Scenario) => {
98
- const requests: Array<{ cursor?: string; limit: number }> = [];
99
- const input = { ...(start === undefined ? {} : { cursor: start }), limit: 1 };
100
- const stream = paginateCursor(
101
- (request) =>
102
- Effect.sync(() => {
103
- const page = pages[requests.length];
104
- requests.push(request);
105
- if (!page) throw new Error("Paginator made an unexpected extra request");
106
- return page;
107
- }),
108
- input,
109
- pagination,
110
- );
111
- return { stream, requests, input };
112
- };
113
-
114
- const collect = <A>(stream: Stream.Stream<A>) =>
115
- Stream.runCollect(stream).pipe(Effect.timeout("1 second"));
116
-
117
- describe("cursor pagination", () => {
118
- test.each(scenarios)("$name", (scenario) =>
119
- Effect.runPromise(
120
- Effect.gen(function* () {
121
- const { stream, requests, input } = fixture(scenario);
122
- const pages = yield* collect(stream);
123
- expect(Array.from(pages)).toEqual(scenario.pages);
124
- expect(requests.map((request) => request.cursor)).toEqual(scenario.cursors);
125
- expect(requests.every((request) => request.limit === 1)).toBe(true);
126
- expect(input.cursor).toBe(scenario.start);
127
- }),
128
- ),
129
- );
130
-
131
- test.each(scenarios)("item extraction: $name", (scenario) =>
132
- Effect.runPromise(
133
- Effect.gen(function* () {
134
- const { stream, requests } = fixture(scenario);
135
- const items = yield* collect(extractItems<Page, number, never, never>(stream, "items"));
136
- expect(Array.from(items)).toEqual(scenario.pages.flatMap((page) => page.items ?? []));
137
- expect(requests.map((request) => request.cursor)).toEqual(scenario.cursors);
138
- }),
139
- ),
140
- );
141
-
142
- test("cursor history is isolated across repeated and concurrent stream runs", () =>
143
- Effect.runPromise(
144
- Effect.gen(function* () {
145
- let calls = 0;
146
- const pages: Page[] = [
147
- { items: [1], pagination: { cursor: "a" } },
148
- { items: [2], pagination: { cursor: "b" } },
149
- { items: [], pagination: { cursor: "b" } },
150
- ];
151
- const stream = paginateCursor(
152
- (input: { cursor?: string }) =>
153
- Effect.sync(() => {
154
- if (++calls > 12) throw new Error("Paginator exceeded the request bound");
155
- return pages[input.cursor === "a" ? 1 : input.cursor === "b" ? 2 : 0]!;
156
- }),
157
- {},
158
- pagination,
159
- );
160
- expect(Array.from(yield* collect(stream))).toEqual(pages);
161
- expect(Array.from(yield* collect(stream))).toEqual(pages);
162
- const concurrent = yield* Effect.all([collect(stream), collect(stream)], {
163
- concurrency: "unbounded",
164
- });
165
- expect(concurrent.map((result) => Array.from(result))).toEqual([pages, pages]);
166
- expect(calls).toBe(12);
167
- }),
168
- ));
169
- });
@@ -1,276 +0,0 @@
1
- import * as Redacted from "effect/Redacted";
2
- import { describe, expect, test } from "vitest";
3
- import { buildRequest, mapKeys } from "./protocol-http.ts";
4
- import { SensitiveValue, wrapSensitive } from "./protocol-rest.ts";
5
- import * as S from "./schema.ts";
6
- import * as T from "./trait.ts";
7
-
8
- /**
9
- * `T.StringEncoded()`: the member's TS type stays natural (boolean) but the
10
- * wire carries its string spelling, for APIs that document the field as the
11
- * enum `"true" | "false"` rather than a JSON boolean.
12
- */
13
- const JsonInput = S.Struct({
14
- flag: S.optional(S.Boolean.pipe(T.Body("flag"), T.StringEncoded())),
15
- plain: S.optional(S.Boolean.pipe(T.Body("plain"))),
16
- nullable: S.optional(S.NullOr(S.Boolean).pipe(T.Body("nullable"), T.StringEncoded())),
17
- flags: S.optional(S.Array(S.Boolean).pipe(T.Body("flags"), T.StringEncoded())),
18
- profile: S.optional(
19
- S.Struct({
20
- nested: S.optional(S.Boolean.pipe(T.Body("nested_flag"), T.StringEncoded())),
21
- plain: S.optional(S.Boolean.pipe(T.Body("plain_flag"))),
22
- }).pipe(T.Body("profile")),
23
- ),
24
- }).pipe(T.Http({ method: "POST", uri: "/things" }));
25
-
26
- const jsonBodyOf = (input: unknown): unknown => {
27
- const request = buildRequest({
28
- input,
29
- inputAst: JsonInput.ast,
30
- baseUrl: "https://example.test",
31
- });
32
- const body = request.body as { readonly body?: string };
33
- return JSON.parse(body.body ?? "{}");
34
- };
35
-
36
- describe("StringEncoded members", () => {
37
- test("booleans serialize as their string spelling", () => {
38
- expect(jsonBodyOf({ flag: true, plain: true })).toEqual({
39
- flag: "true",
40
- plain: true,
41
- });
42
- expect(jsonBodyOf({ flag: false, plain: false })).toEqual({
43
- flag: "false",
44
- plain: false,
45
- });
46
- });
47
-
48
- test("a list stringifies element-wise", () => {
49
- expect(jsonBodyOf({ flags: [true, false] })).toEqual({
50
- flags: ["true", "false"],
51
- });
52
- });
53
-
54
- test("a member nested in a body struct stringifies too", () => {
55
- expect(jsonBodyOf({ profile: { nested: true, plain: true } })).toEqual({
56
- profile: { nested_flag: "true", plain_flag: true },
57
- });
58
- });
59
-
60
- test("null stays null and an omitted member stays omitted", () => {
61
- expect(jsonBodyOf({ nullable: null })).toEqual({ nullable: null });
62
- expect(jsonBodyOf({})).toEqual({});
63
- });
64
- });
65
-
66
- describe("URI label encoding", () => {
67
- const input = (preserve?: string) =>
68
- S.Struct({
69
- scope:
70
- preserve === undefined
71
- ? S.String.pipe(T.Label())
72
- : S.String.pipe(T.Label(), T.LabelEncoding({ preserve })),
73
- }).pipe(T.Http({ method: "POST", uri: "/runtime/{scope}" }));
74
- const url = (scope: string, preserve?: string) =>
75
- buildRequest({
76
- input: { scope },
77
- inputAst: input(preserve).ast,
78
- baseUrl: "https://example.test",
79
- }).url;
80
-
81
- test("keeps the existing escaping unless a model opts in", () => {
82
- expect(url("__KV:abc")).toBe("https://example.test/runtime/__KV%3Aabc");
83
- expect(url("__KV:abc", ":")).toBe("https://example.test/runtime/__KV:abc");
84
- });
85
-
86
- test("preserves only modeled pchar delimiters, not separators or escapes", () => {
87
- expect(url("__KV:a/b?c#d%3Aé", ":")).toBe(
88
- "https://example.test/runtime/__KV:a%2Fb%3Fc%23d%253A%C3%A9",
89
- );
90
- expect(url("$&:@", "$&:")).toBe("https://example.test/runtime/$&:%40");
91
- });
92
-
93
- test("refuses unsafe preservation rules", () => {
94
- for (const preserve of ["/", "?", "#", "%", "\\r", "[", "]"]) {
95
- expect(() => T.LabelEncoding({ preserve })).toThrow(TypeError);
96
- }
97
- });
98
- });
99
-
100
- describe("greedy labels", () => {
101
- const ScopedInput = S.Struct({
102
- scope: S.String.pipe(T.Label()),
103
- name: S.String.pipe(T.Label()),
104
- }).pipe(T.Http({ method: "GET", uri: "/{scope+}/providers/Things/{name}" }));
105
-
106
- const urlOf = (input: unknown) =>
107
- buildRequest({
108
- input,
109
- inputAst: ScopedInput.ast,
110
- baseUrl: "https://example.test",
111
- }).url;
112
-
113
- test("keeps slashes and drops the leading slash of the value", () => {
114
- expect(urlOf({ scope: "/subscriptions/abc/resourceGroups/my rg", name: "a/b" })).toBe(
115
- "https://example.test/subscriptions/abc/resourceGroups/my%20rg/providers/Things/a%2Fb",
116
- );
117
- });
118
-
119
- test("accepts a value without a leading slash", () => {
120
- expect(urlOf({ scope: "subscriptions/abc", name: "x" })).toBe(
121
- "https://example.test/subscriptions/abc/providers/Things/x",
122
- );
123
- });
124
- });
125
-
126
- describe("multipart binary parts", () => {
127
- const schema = S.Struct({ zip: S.Unknown, environment: S.String }).pipe(
128
- T.Http({ method: "POST", uri: "/deployments", contentType: "multipart" }),
129
- );
130
-
131
- test("preserves typed-array slices and an already JSON-encoded environment", async () => {
132
- const input = new Uint8Array([99, 80, 75, 0, 255, 99]).subarray(1, 5);
133
- const environment = JSON.stringify({ SECRET: "value", REMOVED: "" });
134
- const request = buildRequest({
135
- input: { zip: input, environment },
136
- inputAst: schema.ast,
137
- baseUrl: "https://example.test",
138
- });
139
- if (request.body._tag !== "FormData") throw new Error("Expected multipart");
140
- const part = request.body.formData.get("zip");
141
- expect(part).toBeInstanceOf(File);
142
- expect(new Uint8Array(await (part as File).arrayBuffer())).toEqual(input);
143
- expect(request.body.formData.getAll("environment")).toEqual([environment]);
144
- const wire = new Request(request.url, {
145
- method: "POST",
146
- body: request.body.formData,
147
- });
148
- expect(wire.headers.get("content-type")).toContain("multipart/form-data; boundary=");
149
- });
150
-
151
- test("preserves File names and ArrayBuffer bytes", async () => {
152
- for (const zip of [
153
- new File([new Uint8Array([80, 75, 255])], "bundle.zip"),
154
- new Uint8Array([80, 75, 255]).buffer,
155
- ]) {
156
- const request = buildRequest({
157
- input: { zip, environment: "{}" },
158
- inputAst: schema.ast,
159
- baseUrl: "https://example.test",
160
- });
161
- if (request.body._tag !== "FormData") throw new Error("Expected multipart");
162
- const part = request.body.formData.get("zip") as File;
163
- expect(part.name).toBe(zip instanceof File ? "bundle.zip" : "zip");
164
- expect(new Uint8Array(await part.arrayBuffer())).toEqual(new Uint8Array([80, 75, 255]));
165
- }
166
- });
167
- });
168
-
169
- describe("sensitive union responses", () => {
170
- const schema = S.suspend(() =>
171
- S.Union([
172
- S.Struct({
173
- type: S.Literal("standard"),
174
- password: S.String.pipe(SensitiveValue()),
175
- }),
176
- S.Struct({
177
- type: S.Literal("token"),
178
- token: S.String.pipe(SensitiveValue()),
179
- }),
180
- S.Struct({ type: S.Literal("shared") }),
181
- ]),
182
- );
183
-
184
- test("all arms redact their secrets, even in partial or ambiguous responses", () => {
185
- for (const value of [
186
- { type: "standard", password: "fixture-password" },
187
- { type: "token", token: "fixture-token" },
188
- { password: "fixture-password", token: "fixture-token" },
189
- ]) {
190
- const wrapped = wrapSensitive(schema.ast, value);
191
- expect(wrapped).toEqual(
192
- Object.fromEntries(
193
- Object.entries(value).map(([key, value]) => [
194
- key,
195
- key === "type" ? value : Redacted.make(value),
196
- ]),
197
- ),
198
- );
199
- expect(JSON.stringify(wrapped)).not.toContain("fixture-password");
200
- expect(JSON.stringify(wrapped)).not.toContain("fixture-token");
201
- expect(wrapSensitive(schema.ast, wrapped)).toEqual(wrapped);
202
- }
203
- });
204
-
205
- test("nested arrays and optional nullable unions preserve nonsecret values", () => {
206
- const nested = S.Struct({
207
- provider: S.optional(S.NullOr(schema)),
208
- providers: S.Array(schema),
209
- });
210
- for (const value of [{}, { provider: null }, { provider: { type: "shared" } }]) {
211
- expect(
212
- wrapSensitive(nested.ast, {
213
- ...value,
214
- providers: [{ type: "standard", password: "fixture-password" }, { type: "shared" }],
215
- }),
216
- ).toEqual({
217
- ...value,
218
- providers: [
219
- { type: "standard", password: Redacted.make("fixture-password") },
220
- { type: "shared" },
221
- ],
222
- });
223
- }
224
- });
225
- });
226
-
227
- describe("UnionCases decoding", () => {
228
- const cases = [
229
- ["id", "type", "zoneName"],
230
- ["id", "type", "accountName"],
231
- ];
232
- const merged = {
233
- id: "1",
234
- type: "account",
235
- zoneName: "zone-a",
236
- accountName: "acct-a",
237
- };
238
- const decode = (schema: S.Schema<unknown>, value: unknown) =>
239
- mapKeys(schema.ast, value, "decode");
240
-
241
- test("the discriminator picks the case key sets cannot tell apart", () => {
242
- const schema = S.Unknown.pipe(
243
- T.UnionCases(cases, { key: "type", values: ["zone", "account"] }),
244
- );
245
- expect(decode(schema, merged)).toEqual({
246
- id: "1",
247
- type: "account",
248
- accountName: "acct-a",
249
- });
250
- expect(decode(schema, { ...merged, type: "zone" })).toEqual({
251
- id: "1",
252
- type: "zone",
253
- zoneName: "zone-a",
254
- });
255
- });
256
-
257
- test("an unknown tag falls back to key-set scoring", () => {
258
- const schema = S.Unknown.pipe(
259
- T.UnionCases(cases, { key: "type", values: ["zone", "account"] }),
260
- );
261
- expect(decode(schema, { ...merged, type: "other", accountName: null })).toEqual({
262
- id: "1",
263
- type: "other",
264
- zoneName: "zone-a",
265
- });
266
- });
267
-
268
- test("without a discriminator the best-explaining case wins", () => {
269
- const schema = S.Unknown.pipe(T.UnionCases(cases));
270
- expect(decode(schema, { ...merged, zoneName: null })).toEqual({
271
- id: "1",
272
- type: "account",
273
- accountName: "acct-a",
274
- });
275
- });
276
- });