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

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 (276) hide show
  1. package/lib/api.d.ts +4 -4
  2. package/lib/api.d.ts.map +1 -1
  3. package/lib/api.js +3 -5
  4. package/lib/api.js.map +1 -1
  5. package/lib/category.d.ts.map +1 -1
  6. package/lib/category.js +3 -5
  7. package/lib/category.js.map +1 -1
  8. package/lib/codegen/boolean-string-enums.d.ts +8 -8
  9. package/lib/codegen/boolean-string-enums.d.ts.map +1 -1
  10. package/lib/codegen/boolean-string-enums.js +83 -30
  11. package/lib/codegen/boolean-string-enums.js.map +1 -1
  12. package/lib/codegen/boolean-string-enums.test.js +130 -2
  13. package/lib/codegen/boolean-string-enums.test.js.map +1 -1
  14. package/lib/codegen/cli.d.ts +5 -4
  15. package/lib/codegen/cli.d.ts.map +1 -1
  16. package/lib/codegen/cli.js +12 -9
  17. package/lib/codegen/cli.js.map +1 -1
  18. package/lib/codegen/defer-naming.test.d.ts +2 -0
  19. package/lib/codegen/defer-naming.test.d.ts.map +1 -0
  20. package/lib/codegen/defer-naming.test.js +105 -0
  21. package/lib/codegen/defer-naming.test.js.map +1 -0
  22. package/lib/codegen/emit.d.ts.map +1 -1
  23. package/lib/codegen/emit.js +1 -3
  24. package/lib/codegen/emit.js.map +1 -1
  25. package/lib/codegen/format.d.ts +0 -12
  26. package/lib/codegen/format.d.ts.map +1 -1
  27. package/lib/codegen/format.js +10 -6
  28. package/lib/codegen/format.js.map +1 -1
  29. package/lib/codegen/generated-at.d.ts +29 -0
  30. package/lib/codegen/generated-at.d.ts.map +1 -0
  31. package/lib/codegen/generated-at.js +78 -0
  32. package/lib/codegen/generated-at.js.map +1 -0
  33. package/lib/codegen/generated-at.test.d.ts +2 -0
  34. package/lib/codegen/generated-at.test.d.ts.map +1 -0
  35. package/lib/codegen/generated-at.test.js +81 -0
  36. package/lib/codegen/generated-at.test.js.map +1 -0
  37. package/lib/codegen/generator.d.ts +24 -5
  38. package/lib/codegen/generator.d.ts.map +1 -1
  39. package/lib/codegen/generator.js +69 -80
  40. package/lib/codegen/generator.js.map +1 -1
  41. package/lib/codegen/generator.test.d.ts +2 -0
  42. package/lib/codegen/generator.test.d.ts.map +1 -0
  43. package/lib/codegen/generator.test.js +121 -0
  44. package/lib/codegen/generator.test.js.map +1 -0
  45. package/lib/codegen/graphql-client.d.ts +7 -1
  46. package/lib/codegen/graphql-client.d.ts.map +1 -1
  47. package/lib/codegen/graphql-client.js +264 -148
  48. package/lib/codegen/graphql-client.js.map +1 -1
  49. package/lib/codegen/graphql-client.test.js +98 -238
  50. package/lib/codegen/graphql-client.test.js.map +1 -1
  51. package/lib/codegen/graphql.d.ts.map +1 -1
  52. package/lib/codegen/graphql.js +7 -20
  53. package/lib/codegen/graphql.js.map +1 -1
  54. package/lib/codegen/model-diff.d.ts +10 -0
  55. package/lib/codegen/model-diff.d.ts.map +1 -0
  56. package/lib/codegen/model-diff.js +67 -0
  57. package/lib/codegen/model-diff.js.map +1 -0
  58. package/lib/codegen/naming.d.ts.map +1 -1
  59. package/lib/codegen/naming.js.map +1 -1
  60. package/lib/codegen/openapi-binary.test.d.ts +2 -0
  61. package/lib/codegen/openapi-binary.test.d.ts.map +1 -0
  62. package/lib/codegen/openapi-binary.test.js +67 -0
  63. package/lib/codegen/openapi-binary.test.js.map +1 -0
  64. package/lib/codegen/openapi-cli.d.ts +11 -11
  65. package/lib/codegen/openapi-cli.d.ts.map +1 -1
  66. package/lib/codegen/openapi-cli.js +26 -51
  67. package/lib/codegen/openapi-cli.js.map +1 -1
  68. package/lib/codegen/openapi.d.ts +16 -2
  69. package/lib/codegen/openapi.d.ts.map +1 -1
  70. package/lib/codegen/openapi.js +56 -39
  71. package/lib/codegen/openapi.js.map +1 -1
  72. package/lib/codegen/operations.d.ts.map +1 -1
  73. package/lib/codegen/operations.js.map +1 -1
  74. package/lib/codegen/pagination.d.ts.map +1 -1
  75. package/lib/codegen/pagination.js +2 -4
  76. package/lib/codegen/pagination.js.map +1 -1
  77. package/lib/codegen/patch-audit.d.ts +84 -0
  78. package/lib/codegen/patch-audit.d.ts.map +1 -0
  79. package/lib/codegen/patch-audit.js +366 -0
  80. package/lib/codegen/patch-audit.js.map +1 -0
  81. package/lib/codegen/patch-audit.test.d.ts +2 -0
  82. package/lib/codegen/patch-audit.test.d.ts.map +1 -0
  83. package/lib/codegen/patch-audit.test.js +141 -0
  84. package/lib/codegen/patch-audit.test.js.map +1 -0
  85. package/lib/codegen/patches.d.ts +75 -11
  86. package/lib/codegen/patches.d.ts.map +1 -1
  87. package/lib/codegen/patches.js +301 -65
  88. package/lib/codegen/patches.js.map +1 -1
  89. package/lib/codegen/patches.test.js +55 -5
  90. package/lib/codegen/patches.test.js.map +1 -1
  91. package/lib/codegen/prelude.js +1 -1
  92. package/lib/codegen/prelude.js.map +1 -1
  93. package/lib/codegen/proto.d.ts.map +1 -1
  94. package/lib/codegen/proto.js +2 -4
  95. package/lib/codegen/proto.js.map +1 -1
  96. package/lib/codegen/protocol-imports.test.d.ts +2 -0
  97. package/lib/codegen/protocol-imports.test.d.ts.map +1 -0
  98. package/lib/codegen/protocol-imports.test.js +36 -0
  99. package/lib/codegen/protocol-imports.test.js.map +1 -0
  100. package/lib/codegen/rewrite-operation-ids.d.ts +23 -0
  101. package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -1
  102. package/lib/codegen/rewrite-operation-ids.js +97 -59
  103. package/lib/codegen/rewrite-operation-ids.js.map +1 -1
  104. package/lib/codegen/rewrite-operation-ids.test.js +19 -101
  105. package/lib/codegen/rewrite-operation-ids.test.js.map +1 -1
  106. package/lib/codegen/spec-path.d.ts +1 -1
  107. package/lib/codegen/spec-path.d.ts.map +1 -1
  108. package/lib/codegen/spec-path.js +1 -1
  109. package/lib/codegen/spec-path.js.map +1 -1
  110. package/lib/codegen/untagged-unions.test.d.ts +2 -0
  111. package/lib/codegen/untagged-unions.test.d.ts.map +1 -0
  112. package/lib/codegen/untagged-unions.test.js +80 -0
  113. package/lib/codegen/untagged-unions.test.js.map +1 -0
  114. package/lib/error-category.d.ts.map +1 -1
  115. package/lib/error-category.js.map +1 -1
  116. package/lib/errors.d.ts +2 -1
  117. package/lib/errors.d.ts.map +1 -1
  118. package/lib/errors.js +9 -4
  119. package/lib/errors.js.map +1 -1
  120. package/lib/fixtures/xml/upstream-validator.json +397 -0
  121. package/lib/graphql.d.ts +292 -205
  122. package/lib/graphql.d.ts.map +1 -1
  123. package/lib/graphql.js +945 -636
  124. package/lib/graphql.js.map +1 -1
  125. package/lib/graphql.test.js +135 -764
  126. package/lib/graphql.test.js.map +1 -1
  127. package/lib/graphql.types.d.ts +10 -0
  128. package/lib/graphql.types.d.ts.map +1 -1
  129. package/lib/graphql.types.js +4 -44
  130. package/lib/graphql.types.js.map +1 -1
  131. package/lib/index.d.ts +27 -0
  132. package/lib/index.d.ts.map +1 -0
  133. package/lib/index.js +27 -0
  134. package/lib/index.js.map +1 -0
  135. package/lib/json-patch.d.ts +13 -6
  136. package/lib/json-patch.d.ts.map +1 -1
  137. package/lib/json-patch.js +34 -19
  138. package/lib/json-patch.js.map +1 -1
  139. package/lib/json-patch.test.d.ts +2 -0
  140. package/lib/json-patch.test.d.ts.map +1 -0
  141. package/lib/json-patch.test.js +65 -0
  142. package/lib/json-patch.test.js.map +1 -0
  143. package/lib/pagination.d.ts +2 -1
  144. package/lib/pagination.d.ts.map +1 -1
  145. package/lib/pagination.js +28 -30
  146. package/lib/pagination.js.map +1 -1
  147. package/lib/pagination.test.d.ts +2 -0
  148. package/lib/pagination.test.d.ts.map +1 -0
  149. package/lib/pagination.test.js +131 -0
  150. package/lib/pagination.test.js.map +1 -0
  151. package/lib/protocol-http.d.ts +30 -16
  152. package/lib/protocol-http.d.ts.map +1 -1
  153. package/lib/protocol-http.js +133 -84
  154. package/lib/protocol-http.js.map +1 -1
  155. package/lib/protocol-http.test.js +141 -1
  156. package/lib/protocol-http.test.js.map +1 -1
  157. package/lib/protocol-rest.d.ts +18 -1
  158. package/lib/protocol-rest.d.ts.map +1 -1
  159. package/lib/protocol-rest.js +32 -21
  160. package/lib/protocol-rest.js.map +1 -1
  161. package/lib/protocol-rest.test.d.ts +2 -0
  162. package/lib/protocol-rest.test.d.ts.map +1 -0
  163. package/lib/protocol-rest.test.js +498 -0
  164. package/lib/protocol-rest.test.js.map +1 -0
  165. package/lib/query.d.ts +29 -0
  166. package/lib/query.d.ts.map +1 -0
  167. package/lib/query.js +27 -0
  168. package/lib/query.js.map +1 -0
  169. package/lib/response-validation.d.ts +67 -0
  170. package/lib/response-validation.d.ts.map +1 -0
  171. package/lib/response-validation.js +76 -0
  172. package/lib/response-validation.js.map +1 -0
  173. package/lib/response-validation.test.d.ts +2 -0
  174. package/lib/response-validation.test.d.ts.map +1 -0
  175. package/lib/response-validation.test.js +72 -0
  176. package/lib/response-validation.test.js.map +1 -0
  177. package/lib/retry-after.d.ts.map +1 -1
  178. package/lib/retry-after.js +1 -1
  179. package/lib/retry-after.js.map +1 -1
  180. package/lib/retry-after.test.d.ts +2 -0
  181. package/lib/retry-after.test.d.ts.map +1 -0
  182. package/lib/retry-after.test.js +204 -0
  183. package/lib/retry-after.test.js.map +1 -0
  184. package/lib/retry.d.ts.map +1 -1
  185. package/lib/retry.js +1 -2
  186. package/lib/retry.js.map +1 -1
  187. package/lib/retry.test.d.ts +2 -0
  188. package/lib/retry.test.d.ts.map +1 -0
  189. package/lib/retry.test.js +407 -0
  190. package/lib/retry.test.js.map +1 -0
  191. package/lib/sdks.test.d.ts +2 -0
  192. package/lib/sdks.test.d.ts.map +1 -0
  193. package/lib/sdks.test.js +76 -0
  194. package/lib/sdks.test.js.map +1 -0
  195. package/lib/testing.d.ts +39 -0
  196. package/lib/testing.d.ts.map +1 -0
  197. package/lib/testing.js +41 -0
  198. package/lib/testing.js.map +1 -0
  199. package/lib/trait.d.ts +29 -9
  200. package/lib/trait.d.ts.map +1 -1
  201. package/lib/trait.js +8 -0
  202. package/lib/trait.js.map +1 -1
  203. package/lib/xml.d.ts +44 -0
  204. package/lib/xml.d.ts.map +1 -0
  205. package/lib/xml.js +311 -0
  206. package/lib/xml.js.map +1 -0
  207. package/lib/xml.test.d.ts +2 -0
  208. package/lib/xml.test.d.ts.map +1 -0
  209. package/lib/xml.test.js +210 -0
  210. package/lib/xml.test.js.map +1 -0
  211. package/package.json +8 -11
  212. package/src/api.ts +38 -55
  213. package/src/category.ts +21 -50
  214. package/src/codegen/boolean-string-enums.test.ts +148 -17
  215. package/src/codegen/boolean-string-enums.ts +81 -32
  216. package/src/codegen/cli.ts +29 -47
  217. package/src/codegen/defer-naming.test.ts +113 -0
  218. package/src/codegen/emit.ts +2 -5
  219. package/src/codegen/format.ts +12 -10
  220. package/src/codegen/generated-at.test.ts +89 -0
  221. package/src/codegen/generated-at.ts +88 -0
  222. package/src/codegen/generator.test.ts +137 -0
  223. package/src/codegen/generator.ts +132 -193
  224. package/src/codegen/graphql-client.test.ts +100 -297
  225. package/src/codegen/graphql-client.ts +302 -210
  226. package/src/codegen/graphql.ts +22 -94
  227. package/src/codegen/model-diff.ts +67 -0
  228. package/src/codegen/naming.ts +1 -2
  229. package/src/codegen/openapi-binary.test.ts +91 -0
  230. package/src/codegen/openapi-cli.ts +38 -79
  231. package/src/codegen/openapi.ts +98 -133
  232. package/src/codegen/operations.ts +2 -5
  233. package/src/codegen/pagination.ts +4 -11
  234. package/src/codegen/patch-audit.test.ts +160 -0
  235. package/src/codegen/patch-audit.ts +480 -0
  236. package/src/codegen/patches.test.ts +73 -15
  237. package/src/codegen/patches.ts +389 -99
  238. package/src/codegen/prelude.ts +1 -1
  239. package/src/codegen/proto.ts +18 -54
  240. package/src/codegen/protocol-imports.test.ts +39 -0
  241. package/src/codegen/rewrite-operation-ids.test.ts +23 -114
  242. package/src/codegen/rewrite-operation-ids.ts +111 -98
  243. package/src/codegen/spec-path.ts +3 -6
  244. package/src/codegen/untagged-unions.test.ts +85 -0
  245. package/src/error-category.ts +5 -17
  246. package/src/errors.ts +18 -30
  247. package/src/fixtures/xml/upstream-validator.json +397 -0
  248. package/src/graphql.test.ts +173 -949
  249. package/src/graphql.ts +1468 -1186
  250. package/src/graphql.types.ts +78 -164
  251. package/src/index.ts +26 -0
  252. package/src/json-patch.test.ts +77 -0
  253. package/src/json-patch.ts +54 -60
  254. package/src/pagination.test.ts +169 -0
  255. package/src/pagination.ts +46 -103
  256. package/src/protocol-http.test.ts +178 -9
  257. package/src/protocol-http.ts +174 -163
  258. package/src/protocol-rest.test.ts +703 -0
  259. package/src/protocol-rest.ts +76 -65
  260. package/src/query.ts +39 -0
  261. package/src/response-validation.test.ts +89 -0
  262. package/src/response-validation.ts +107 -0
  263. package/src/retry-after.test.ts +267 -0
  264. package/src/retry-after.ts +4 -10
  265. package/src/retry.test.ts +566 -0
  266. package/src/retry.ts +6 -21
  267. package/src/sdks.test.ts +87 -0
  268. package/src/testing.ts +72 -0
  269. package/src/trait.ts +48 -63
  270. package/src/xml.test.ts +251 -0
  271. package/src/xml.ts +323 -0
  272. package/lib/graphql.fixture.d.ts +0 -249
  273. package/lib/graphql.fixture.d.ts.map +0 -1
  274. package/lib/graphql.fixture.js +0 -240
  275. package/lib/graphql.fixture.js.map +0 -1
  276. package/src/graphql.fixture.ts +0 -371
@@ -0,0 +1,169 @@
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
+ });
package/src/pagination.ts CHANGED
@@ -89,12 +89,7 @@ export interface PaginatedTrait {
89
89
  readonly hasNextPage?: string;
90
90
  }
91
91
 
92
- export type PaginationStrategy = <
93
- Input extends Record<string, unknown>,
94
- Output,
95
- E,
96
- R,
97
- >(
92
+ export type PaginationStrategy = <Input extends Record<string, unknown>, Output, E, R>(
98
93
  operation: (input: Input) => Effect.Effect<Output, E, R>,
99
94
  input: Input,
100
95
  pagination: PaginatedTrait,
@@ -120,9 +115,7 @@ export const isTerminalToken = (token: unknown): boolean =>
120
115
  * surface — emits exactly one page.
121
116
  */
122
117
  export const paginateSingle: PaginationStrategy = (operation, input) =>
123
- Stream.make(input).pipe(
124
- Stream.mapEffect((requestPayload) => operation(requestPayload)),
125
- );
118
+ Stream.make(input).pipe(Stream.mapEffect((requestPayload) => operation(requestPayload)));
126
119
 
127
120
  // ============================================================================
128
121
  // Page-based Pagination
@@ -133,12 +126,7 @@ export const paginateSingle: PaginationStrategy = (operation, input) =>
133
126
  * `outputToken` when it advances; otherwise the page number is incremented,
134
127
  * terminating when a page comes back with no items (or no token).
135
128
  */
136
- export const paginatePageNumber = <
137
- Input extends Record<string, unknown>,
138
- Output,
139
- E,
140
- R,
141
- >(
129
+ export const paginatePageNumber = <Input extends Record<string, unknown>, Output, E, R>(
142
130
  operation: (input: Input) => Effect.Effect<Output, E, R>,
143
131
  input: Input,
144
132
  pagination: PaginatedTrait,
@@ -146,13 +134,10 @@ export const paginatePageNumber = <
146
134
  const inputToken = pagination.inputToken;
147
135
  const outputToken = pagination.outputToken;
148
136
  if (!inputToken || !outputToken) {
149
- return missingPaginationConfig(
150
- "Page-number pagination requires inputToken and outputToken",
151
- );
137
+ return missingPaginationConfig("Page-number pagination requires inputToken and outputToken");
152
138
  }
153
139
  type State = { page: number; done: boolean };
154
- const startPage =
155
- typeof input[inputToken] === "number" ? (input[inputToken] as number) : 1;
140
+ const startPage = typeof input[inputToken] === "number" ? (input[inputToken] as number) : 1;
156
141
 
157
142
  return Stream.unfold({ page: startPage, done: false } as State, (state) =>
158
143
  Effect.gen(function* () {
@@ -161,10 +146,7 @@ export const paginatePageNumber = <
161
146
  const requestPayload = { ...input, [inputToken]: state.page } as Input;
162
147
  const response = yield* operation(requestPayload);
163
148
 
164
- const nextPage = getPath(response, outputToken) as
165
- | number
166
- | null
167
- | undefined;
149
+ const nextPage = getPath(response, outputToken) as number | null | undefined;
168
150
 
169
151
  // Some APIs report the CURRENT page at `outputToken` rather than the
170
152
  // next one (e.g. Cloudflare's `result_info.page`). Taking that value as
@@ -172,16 +154,11 @@ export const paginatePageNumber = <
172
154
  // *advancing* page number; otherwise advance by one and terminate when
173
155
  // a page comes back with no items (or the token is absent).
174
156
  const items = pagination.items
175
- ? (getPath(response, pagination.items) as
176
- | readonly unknown[]
177
- | undefined)
157
+ ? (getPath(response, pagination.items) as readonly unknown[] | undefined)
178
158
  : undefined;
179
159
 
180
160
  const nextState: State = {
181
- page:
182
- typeof nextPage === "number" && nextPage > state.page
183
- ? nextPage
184
- : state.page + 1,
161
+ page: typeof nextPage === "number" && nextPage > state.page ? nextPage : state.page + 1,
185
162
  done:
186
163
  nextPage === null ||
187
164
  nextPage === undefined ||
@@ -199,14 +176,10 @@ export const paginatePageNumber = <
199
176
 
200
177
  /**
201
178
  * Stream of pages using cursor-based pagination — follow `outputToken`
202
- * cursors until one comes back absent.
179
+ * cursors until one comes back absent or repeats a previously requested cursor.
180
+ * Empty pages still advance when they return a new cursor.
203
181
  */
204
- export const paginateCursor = <
205
- Input extends Record<string, unknown>,
206
- Output,
207
- E,
208
- R,
209
- >(
182
+ export const paginateCursor = <Input extends Record<string, unknown>, Output, E, R>(
210
183
  operation: (input: Input) => Effect.Effect<Output, E, R>,
211
184
  input: Input,
212
185
  pagination: PaginatedTrait,
@@ -214,40 +187,39 @@ export const paginateCursor = <
214
187
  const inputToken = pagination.inputToken;
215
188
  const outputToken = pagination.outputToken;
216
189
  if (!inputToken || !outputToken) {
217
- return missingPaginationConfig(
218
- "Cursor pagination requires inputToken and outputToken",
219
- );
190
+ return missingPaginationConfig("Cursor pagination requires inputToken and outputToken");
220
191
  }
221
192
  type State = { cursor: string | undefined; done: boolean };
222
193
  const startCursor =
223
- typeof input[inputToken] === "string"
224
- ? (input[inputToken] as string)
225
- : undefined;
194
+ typeof input[inputToken] === "string" ? (input[inputToken] as string) : undefined;
226
195
 
227
- return Stream.unfold({ cursor: startCursor, done: false } as State, (state) =>
228
- Effect.gen(function* () {
229
- if (state.done) return undefined;
196
+ return Stream.suspend(() => {
197
+ // Cursor history belongs to this traversal, not to the reusable stream.
198
+ const requestedCursors = new Set<string>();
199
+ return Stream.unfold({ cursor: startCursor, done: false } as State, (state) =>
200
+ Effect.gen(function* () {
201
+ if (state.done) return undefined;
202
+ if (state.cursor !== undefined) requestedCursors.add(state.cursor);
230
203
 
231
- const requestPayload = {
232
- ...input,
233
- ...(state.cursor ? { [inputToken]: state.cursor } : {}),
234
- } as Input;
204
+ const requestPayload = {
205
+ ...input,
206
+ ...(state.cursor ? { [inputToken]: state.cursor } : {}),
207
+ } as Input;
235
208
 
236
- const response = yield* operation(requestPayload);
209
+ const response = yield* operation(requestPayload);
237
210
 
238
- const nextCursor = getPath(response, outputToken) as
239
- | string
240
- | null
241
- | undefined;
211
+ const nextCursor = getPath(response, outputToken) as string | null | undefined;
242
212
 
243
- const nextState: State = {
244
- cursor: nextCursor ?? undefined,
245
- done: isTerminalToken(nextCursor),
246
- };
213
+ const nextState: State = {
214
+ cursor: nextCursor ?? undefined,
215
+ done:
216
+ isTerminalToken(nextCursor) || (nextCursor != null && requestedCursors.has(nextCursor)),
217
+ };
247
218
 
248
- return [response, nextState] as const;
249
- }),
250
- );
219
+ return [response, nextState] as const;
220
+ }),
221
+ );
222
+ });
251
223
  };
252
224
 
253
225
  // ============================================================================
@@ -258,12 +230,7 @@ export const paginateCursor = <
258
230
  * Stream of pages using token-based pagination — pass `outputToken` back as
259
231
  * `inputToken` until it comes back absent.
260
232
  */
261
- export const paginateToken = <
262
- Input extends Record<string, unknown>,
263
- Output,
264
- E,
265
- R,
266
- >(
233
+ export const paginateToken = <Input extends Record<string, unknown>, Output, E, R>(
267
234
  operation: (input: Input) => Effect.Effect<Output, E, R>,
268
235
  input: Input,
269
236
  pagination: PaginatedTrait,
@@ -271,9 +238,7 @@ export const paginateToken = <
271
238
  const inputToken = pagination.inputToken;
272
239
  const outputToken = pagination.outputToken;
273
240
  if (!inputToken || !outputToken) {
274
- return missingPaginationConfig(
275
- "Token pagination requires inputToken and outputToken",
276
- );
241
+ return missingPaginationConfig("Token pagination requires inputToken and outputToken");
277
242
  }
278
243
  type State = { token: unknown; done: boolean };
279
244
  const startToken = input[inputToken];
@@ -283,9 +248,7 @@ export const paginateToken = <
283
248
  if (state.done) return undefined;
284
249
 
285
250
  const requestPayload =
286
- state.token !== undefined
287
- ? ({ ...input, [inputToken]: state.token } as Input)
288
- : input;
251
+ state.token !== undefined ? ({ ...input, [inputToken]: state.token } as Input) : input;
289
252
 
290
253
  const response = yield* operation(requestPayload);
291
254
 
@@ -317,12 +280,7 @@ export const paginateToken = <
317
280
  * connection that omits `pageInfo` entirely (or returns an empty page) also
318
281
  * terminates, so a malformed response can't spin forever.
319
282
  */
320
- export const paginateRelay = <
321
- Input extends Record<string, unknown>,
322
- Output,
323
- E,
324
- R,
325
- >(
283
+ export const paginateRelay = <Input extends Record<string, unknown>, Output, E, R>(
326
284
  operation: (input: Input) => Effect.Effect<Output, E, R>,
327
285
  input: Input,
328
286
  pagination: PaginatedTrait,
@@ -330,9 +288,7 @@ export const paginateRelay = <
330
288
  const inputToken = pagination.inputToken;
331
289
  const outputToken = pagination.outputToken;
332
290
  if (!inputToken || !outputToken) {
333
- return missingPaginationConfig(
334
- "Relay pagination requires inputToken and outputToken",
335
- );
291
+ return missingPaginationConfig("Relay pagination requires inputToken and outputToken");
336
292
  }
337
293
  // `pageInfo.endCursor` → `pageInfo.hasNextPage` when the trait doesn't say.
338
294
  const hasNextPath =
@@ -341,9 +297,7 @@ export const paginateRelay = <
341
297
 
342
298
  type State = { cursor: string | undefined; done: boolean };
343
299
  const startCursor =
344
- typeof input[inputToken] === "string"
345
- ? (input[inputToken] as string)
346
- : undefined;
300
+ typeof input[inputToken] === "string" ? (input[inputToken] as string) : undefined;
347
301
 
348
302
  return Stream.unfold({ cursor: startCursor, done: false } as State, (state) =>
349
303
  Effect.gen(function* () {
@@ -356,16 +310,12 @@ export const paginateRelay = <
356
310
 
357
311
  const response = yield* operation(requestPayload);
358
312
 
359
- const nextCursor = getPath(response, outputToken) as
360
- | string
361
- | null
362
- | undefined;
313
+ const nextCursor = getPath(response, outputToken) as string | null | undefined;
363
314
  const hasNext = getPath(response, hasNextPath) === true;
364
315
  // An empty page means the connection is exhausted regardless of what
365
316
  // `hasNextPage` claims — re-requesting the same cursor would loop.
366
317
  const emptyPage =
367
- pagination.items !== undefined &&
368
- getItems(response, pagination.items).length === 0;
318
+ pagination.items !== undefined && getItems(response, pagination.items).length === 0;
369
319
  // A connection that keeps returning the same `endCursor` with
370
320
  // `hasNextPage: true` (Railway `projects` has done this) would
371
321
  // otherwise paginate forever.
@@ -377,8 +327,7 @@ export const paginateRelay = <
377
327
 
378
328
  const nextState: State = {
379
329
  cursor: nextCursor ?? undefined,
380
- done:
381
- !hasNext || isTerminalToken(nextCursor) || emptyPage || stuckCursor,
330
+ done: !hasNext || isTerminalToken(nextCursor) || emptyPage || stuckCursor,
382
331
  };
383
332
 
384
333
  return [response, nextState] as const;
@@ -390,11 +339,7 @@ export const paginateRelay = <
390
339
  * Shared default pagination dispatcher for SDKs that use generic
391
340
  * token/cursor/page traversal.
392
341
  */
393
- export const paginateWithDefaults: PaginationStrategy = (
394
- operation,
395
- input,
396
- pagination,
397
- ) => {
342
+ export const paginateWithDefaults: PaginationStrategy = (operation, input, pagination) => {
398
343
  const mode = pagination.mode ?? "token";
399
344
 
400
345
  switch (mode) {
@@ -429,7 +374,5 @@ export const extractItems = <Output, Item, E, R>(
429
374
  itemsPath: string,
430
375
  ): Stream.Stream<Item, E, R> =>
431
376
  pages.pipe(
432
- Stream.flatMap((page) =>
433
- Stream.fromIterable(getItems(page, itemsPath) as readonly Item[]),
434
- ),
377
+ Stream.flatMap((page) => Stream.fromIterable(getItems(page, itemsPath) as readonly Item[])),
435
378
  );
@@ -1,5 +1,7 @@
1
- import { describe, expect, test } from "bun:test";
1
+ import * as Redacted from "effect/Redacted";
2
+ import { describe, expect, test } from "vitest";
2
3
  import { buildRequest, mapKeys } from "./protocol-http.ts";
4
+ import { SensitiveValue, wrapSensitive } from "./protocol-rest.ts";
3
5
  import * as S from "./schema.ts";
4
6
  import * as T from "./trait.ts";
5
7
 
@@ -11,11 +13,13 @@ import * as T from "./trait.ts";
11
13
  const JsonInput = S.Struct({
12
14
  flag: S.optional(S.Boolean.pipe(T.Body("flag"), T.StringEncoded())),
13
15
  plain: S.optional(S.Boolean.pipe(T.Body("plain"))),
14
- nullable: S.optional(
15
- S.NullOr(S.Boolean).pipe(T.Body("nullable"), T.StringEncoded()),
16
- ),
17
- flags: S.optional(
18
- S.Array(S.Boolean).pipe(T.Body("flags"), T.StringEncoded()),
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")),
19
23
  ),
20
24
  }).pipe(T.Http({ method: "POST", uri: "/things" }));
21
25
 
@@ -47,12 +51,179 @@ describe("StringEncoded members", () => {
47
51
  });
48
52
  });
49
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
+
50
60
  test("null stays null and an omitted member stays omitted", () => {
51
61
  expect(jsonBodyOf({ nullable: null })).toEqual({ nullable: null });
52
62
  expect(jsonBodyOf({})).toEqual({});
53
63
  });
54
64
  });
55
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
+
56
227
  describe("UnionCases decoding", () => {
57
228
  const cases = [
58
229
  ["id", "type", "zoneName"],
@@ -87,9 +258,7 @@ describe("UnionCases decoding", () => {
87
258
  const schema = S.Unknown.pipe(
88
259
  T.UnionCases(cases, { key: "type", values: ["zone", "account"] }),
89
260
  );
90
- expect(
91
- decode(schema, { ...merged, type: "other", accountName: null }),
92
- ).toEqual({
261
+ expect(decode(schema, { ...merged, type: "other", accountName: null })).toEqual({
93
262
  id: "1",
94
263
  type: "other",
95
264
  zoneName: "zone-a",