@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
@@ -0,0 +1,327 @@
1
+ /**
2
+ * The strict response checker: validates a decoded response against its
3
+ * output AST without Effect Schema.
4
+ *
5
+ * Generated output schemas are lite nodes (`schema-lite-impl.ts`); the
6
+ * checker accepts what Effect Schema's decoder accepted for the schema
7
+ * `toEffect` built from them (`response-validation-strict.test.ts` compares
8
+ * the two on random schemas and values). Each AST node compiles once to a
9
+ * closure, so a check walks the value only.
10
+ *
11
+ * Two modes: `additionalProperties` lets keys an object does not model
12
+ * through (Effect's default), `strict` (`exact`) rejects them (Effect's
13
+ * `onExcessProperty: "error"`). In both, an index signature covers only the
14
+ * keys the object does not declare (OpenAPI `additionalProperties`), and an
15
+ * empty struct accepts any object (generated code's opaque output).
16
+ *
17
+ * Hand-written protocols may pass real Effect ASTs. Their structure is
18
+ * checked the same way; refinement `checks` run, but a real `Declaration`
19
+ * with type parameters and `TemplateLiteral` contents are not checked.
20
+ */
21
+ import type * as AST from "effect/SchemaAST";
22
+ import type { Node } from "./schema-lite-impl.ts";
23
+
24
+ /** A response that does not match its output schema (the `cause` of a strict `<Sdk>ParseError`). */
25
+ export class SchemaMismatch extends Error {
26
+ override readonly name = "SchemaError";
27
+ /** Where in the response the mismatch is, outermost key first. */
28
+ readonly path: ReadonlyArray<PropertyKey>;
29
+ /** What the schema expected there, e.g. `string`. */
30
+ readonly expected: string;
31
+ readonly actual: unknown;
32
+ constructor(path: ReadonlyArray<PropertyKey>, expected: string, actual: unknown) {
33
+ super(
34
+ `Expected ${expected}, got ${show(actual)}${path.length ? ` at ${JSON.stringify(path.map((k) => (typeof k === "symbol" ? String(k) : k)))}` : ""}`,
35
+ );
36
+ this.path = path;
37
+ this.expected = expected;
38
+ this.actual = actual;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * `mismatch` with its offending value replaced by `"[REDACTED]"`, for a parse
44
+ * error that withholds the body (the value may be a secret).
45
+ */
46
+ export const withholdActual = (mismatch: SchemaMismatch): SchemaMismatch =>
47
+ new SchemaMismatch(mismatch.path, mismatch.expected, "[REDACTED]");
48
+
49
+ /**
50
+ * Validate `input` synchronously: `undefined` when it matches, else the
51
+ * mismatch.
52
+ */
53
+ export type Decode = (input: unknown) => SchemaMismatch | undefined;
54
+
55
+ const show = (u: unknown): string => {
56
+ if (u === MISSING) return "nothing (missing key)";
57
+ if (u === undefined) return "undefined";
58
+ if (typeof u === "string") return JSON.stringify(u.length > 40 ? `${u.slice(0, 40)}...` : u);
59
+ if (Array.isArray(u)) return "an array";
60
+ if (u !== null && typeof u === "object") return "an object";
61
+ return String(u);
62
+ };
63
+
64
+ /** A failed check: the path is filled in on the way out, innermost key first. */
65
+ interface Fail {
66
+ readonly path: PropertyKey[];
67
+ readonly expected: string;
68
+ readonly actual: unknown;
69
+ }
70
+ type Check = (u: unknown) => Fail | undefined;
71
+
72
+ const MISSING: unique symbol = Symbol("missing");
73
+ /**
74
+ * The node compiler, built on the first strict check: importing this module
75
+ * (every REST program does, through the protocol) allocates almost nothing.
76
+ */
77
+ const makeCompile = (exact: boolean): ((n: Node) => Check) => {
78
+ const fail = (expected: string, actual: unknown): Fail => ({ path: [], expected, actual });
79
+ const ok: Check = () => undefined;
80
+ const typeOf =
81
+ (t: string): Check =>
82
+ (u) =>
83
+ typeof u === t ? undefined : fail(t, u);
84
+
85
+ const compiled = new WeakMap<object, Check>();
86
+
87
+ const compile = (n: Node): Check => {
88
+ let c = compiled.get(n);
89
+ if (c === undefined) {
90
+ // A node reached again while it compiles (only through Suspend) calls
91
+ // through this slot.
92
+ let self: Check | undefined;
93
+ compiled.set(n, (u) => self!(u));
94
+ self = c = withChecks(n, build(n));
95
+ compiled.set(n, c);
96
+ }
97
+ return c;
98
+ };
99
+
100
+ /** Effect refinements (`checks`) on real nodes run after the structural check. */
101
+ const withChecks = (n: Node, base: Check): Check => {
102
+ const checks = (n as { checks?: ReadonlyArray<any> }).checks;
103
+ if (!checks?.length || isLite(n)) return base;
104
+ const flat: any[] = [];
105
+ const add = (c: any) => (c._tag === "FilterGroup" ? c.checks.forEach(add) : flat.push(c));
106
+ checks.forEach(add);
107
+ return (u) => {
108
+ const f = base(u);
109
+ if (f) return f;
110
+ for (const c of flat) {
111
+ const r = c.run(u, n, {});
112
+ if (r !== undefined && r !== true)
113
+ return fail(String(c.annotations?.expected ?? "a valid value"), u);
114
+ }
115
+ return undefined;
116
+ };
117
+ };
118
+
119
+ const isLite = (n: Node): boolean => (n as any)["~lite"] === true;
120
+
121
+ const isOptional = (n: Node): boolean => n.context?.isOptional === true;
122
+
123
+ const build = (n: Node): Check => {
124
+ // A real node that decodes from another form is checked in that form (the
125
+ // body as read); lite encodings are type-side only.
126
+ const encoding = (n as { encoding?: ReadonlyArray<{ to: Node }> }).encoding;
127
+ if (encoding?.length && !isLite(n)) return compile(encoding[encoding.length - 1]!.to);
128
+ switch (n._tag) {
129
+ case "String":
130
+ case "TemplateLiteral":
131
+ return typeOf("string");
132
+ case "Number":
133
+ return typeOf("number");
134
+ case "Boolean":
135
+ return typeOf("boolean");
136
+ case "BigInt":
137
+ return typeOf("bigint");
138
+ case "Symbol":
139
+ return typeOf("symbol");
140
+ case "Null":
141
+ return (u) => (u === null ? undefined : fail("null", u));
142
+ case "Undefined":
143
+ return (u) => (u === undefined ? undefined : fail("undefined", u));
144
+ case "Never":
145
+ return (u) => fail("never", u);
146
+ case "Literal":
147
+ case "UniqueSymbol": {
148
+ const lit = n._tag === "Literal" ? n.literal : (n as any).symbol;
149
+ return (u) => (u === lit ? undefined : fail(show(lit), u));
150
+ }
151
+ case "Enum": {
152
+ const values = ((n as any).enums as ReadonlyArray<readonly [string, unknown]>).map(
153
+ (e) => e[1],
154
+ );
155
+ return (u) => (values.includes(u) ? undefined : fail("an enum value", u));
156
+ }
157
+ case "ObjectKeyword":
158
+ return (u) =>
159
+ (typeof u === "object" && u !== null) || typeof u === "function"
160
+ ? undefined
161
+ : fail("object | function", u);
162
+ case "Union":
163
+ return union(n);
164
+ case "Arrays":
165
+ return arrays(n);
166
+ case "Objects":
167
+ return objects(n);
168
+ case "Suspend": {
169
+ let inner: Check | undefined;
170
+ return (u) => (inner ??= compile(n.thunk!()))(u);
171
+ }
172
+ case "Declaration": {
173
+ if (isLite(n)) {
174
+ const check = n.check;
175
+ const name = String(n.annotations?.identifier ?? "a valid value");
176
+ return check ? (u) => (check(u) ? undefined : fail(name, u)) : ok;
177
+ }
178
+ return declaration(n);
179
+ }
180
+ default:
181
+ // Unknown, Any, Void (Effect's `void` accepts any value) and anything
182
+ // without structure.
183
+ return ok;
184
+ }
185
+ };
186
+
187
+ /** A real Effect declaration without type parameters runs its own parser (synchronous ones only). */
188
+ const declaration = (n: Node): Check => {
189
+ const run = (n as any).run as ((tps: ReadonlyArray<unknown>) => any) | undefined;
190
+ if (!run || n.typeParameters?.length) return ok;
191
+ let parse: any;
192
+ return (u) => {
193
+ const exit = (parse ??= run([]))(u, n, {});
194
+ return exit?._tag === "Failure"
195
+ ? fail(String(n.annotations?.identifier ?? n.annotations?.expected ?? "a valid value"), u)
196
+ : undefined;
197
+ };
198
+ };
199
+
200
+ const union = (n: Node): Check => {
201
+ const members = n.types!.map(compile);
202
+ return (u) => {
203
+ let first: Fail | undefined;
204
+ for (let i = 0; i < members.length; i++) {
205
+ const f = members[i]!(u);
206
+ if (f === undefined) return undefined;
207
+ first ??= f;
208
+ }
209
+ return members.length === 1 ? first : fail("a union member", u);
210
+ };
211
+ };
212
+
213
+ const arrays = (n: Node): Check => {
214
+ const elements = (n.elements ?? []).map((e) => [compile(e), isOptional(e)] as const);
215
+ const rest = (n.rest ?? []).map(compile);
216
+ const head = rest[0];
217
+ const tail = rest.slice(1);
218
+ return (u) => {
219
+ if (!Array.isArray(u)) return fail("array", u);
220
+ const len = u.length;
221
+ for (let i = 0; i < elements.length; i++) {
222
+ const [check, optional] = elements[i]!;
223
+ if (i >= len) {
224
+ if (optional) continue;
225
+ return at(fail("an element", MISSING), i);
226
+ }
227
+ const f = check(u[i]);
228
+ if (f) return at(f, i);
229
+ }
230
+ if (head === undefined) {
231
+ return len > elements.length
232
+ ? at(fail("never", u[elements.length]), elements.length)
233
+ : undefined;
234
+ }
235
+ const end = len - tail.length;
236
+ if (end < elements.length) return at(fail("an element", MISSING), len);
237
+ for (let i = elements.length; i < end; i++) {
238
+ const f = head(u[i]);
239
+ if (f) return at(f, i);
240
+ }
241
+ for (let j = 0; j < tail.length; j++) {
242
+ const f = tail[j]!(u[end + j]);
243
+ if (f) return at(f, end + j);
244
+ }
245
+ return undefined;
246
+ };
247
+ };
248
+
249
+ const at = (f: Fail, key: PropertyKey): Fail => {
250
+ f.path.push(key);
251
+ return f;
252
+ };
253
+
254
+ const objects = (n: Node): Check => {
255
+ const props = (n.propertySignatures ?? []).map(
256
+ (p) => [p.name, compile(p.type), isOptional(p.type)] as const,
257
+ );
258
+ const index = (n.indexSignatures ?? []).map(
259
+ (s) => [compile(s.parameter), compile(s.type)] as const,
260
+ );
261
+ const known = new Set(props.map((p) => p[0]));
262
+ // Effect's empty struct is TypeScript's `{}`: any value but null and undefined.
263
+ // Exact mode leaves it open too: generated code emits `{}` for outputs
264
+ // it does not model.
265
+ if (props.length === 0 && index.length === 0) {
266
+ return (u) => (u === null || u === undefined ? fail("{}", u) : undefined);
267
+ }
268
+ return (u) => {
269
+ if (typeof u !== "object" || u === null || Array.isArray(u)) return fail("object", u);
270
+ const o = u as Record<PropertyKey, unknown>;
271
+ for (let i = 0; i < props.length; i++) {
272
+ const [name, check, optional] = props[i]!;
273
+ if (!(name === "__proto__" ? Object.hasOwn(o, name) : name in o)) {
274
+ if (optional) continue;
275
+ return at(fail("a value", MISSING), name);
276
+ }
277
+ const f = check(o[name]);
278
+ if (f) return at(f, name);
279
+ }
280
+ if (index.length || exact) {
281
+ for (const key of Object.keys(o)) {
282
+ if (known.has(key)) continue;
283
+ let covered = false;
284
+ for (const [param, check] of index) {
285
+ if (param(key) !== undefined) continue;
286
+ covered = true;
287
+ const f = check(o[key]);
288
+ if (f) return at(f, key);
289
+ }
290
+ if (exact && !covered) {
291
+ return at(fail("no value (the schema does not model this key)", o[key]), key);
292
+ }
293
+ }
294
+ }
295
+ return undefined;
296
+ };
297
+ };
298
+
299
+ return compile;
300
+ };
301
+ let compileNode: ((n: Node) => Check) | undefined;
302
+ let compileExact: ((n: Node) => Check) | undefined;
303
+
304
+ /**
305
+ * The checker for an output AST: compiled once per node and mode, then
306
+ * reused. `exact` also rejects keys an object does not model (the `strict`
307
+ * mode); without it they pass (`additionalProperties`).
308
+ */
309
+ export const decoderFor = (ast: AST.AST, exact = false): Decode => {
310
+ const decoders = exact ? exactDecoders : openDecoders;
311
+ let decode = decoders.get(ast);
312
+ if (decode === undefined) {
313
+ const check = exact
314
+ ? (compileExact ??= makeCompile(true))(ast as unknown as Node)
315
+ : (compileNode ??= makeCompile(false))(ast as unknown as Node);
316
+ decode = (input) => {
317
+ const f = check(input);
318
+ return f === undefined
319
+ ? undefined
320
+ : new SchemaMismatch(f.path.reverse(), f.expected, f.actual);
321
+ };
322
+ decoders.set(ast, decode);
323
+ }
324
+ return decode;
325
+ };
326
+ const openDecoders = /*@__PURE__*/ new WeakMap<AST.AST, Decode>();
327
+ const exactDecoders = /*@__PURE__*/ new WeakMap<AST.AST, Decode>();
@@ -7,14 +7,22 @@
7
7
  * lenient (default): the response is returned as the protocol read it.
8
8
  * A body that does not match the declared output type
9
9
  * (a missing member, a wrong primitive, a non-JSON
10
- * body) still succeeds.
11
- * strict: the response is decoded against the output schema,
12
- * and a mismatch fails the call with the SDK's
13
- * `<Sdk>ParseError` (AWS: `ParseError`).
10
+ * body) still succeeds, except that core's REST
11
+ * protocol, Stripe and Fly.io fail a non-JSON body for
12
+ * an output with sensitive members in every mode.
13
+ * additionalProperties: the response is checked against the output
14
+ * schema, and a mismatch fails the call with the SDK's
15
+ * `<Sdk>ParseError` (AWS: `ParseError`). Keys the
16
+ * schema does not model are allowed and kept.
17
+ * strict: as `additionalProperties`, and a key the schema does
18
+ * not model also fails the call, at any depth. Open
19
+ * objects (an index signature, e.g. OpenAPI
20
+ * `additionalProperties`) and empty structs (opaque
21
+ * outputs) still accept any key.
14
22
  *
15
23
  * The mode is one {@link ResponseValidation} reference keyed by a string, so
16
- * a single `Layer` switches every distilled SDK in a program — AWS,
17
- * Cloudflare, Neon, … — at once, and nests like any other Effect service:
24
+ * a single `Layer` switches every distilled SDK in a program -- AWS,
25
+ * Cloudflare, Neon, ... -- at once, and nests like any other Effect service:
18
26
  *
19
27
  * ```ts
20
28
  * import { ResponseValidation } from "@distilled.cloud/core";
@@ -31,77 +39,120 @@
31
39
  import * as Context from "effect/Context";
32
40
  import * as Effect from "effect/Effect";
33
41
  import * as Layer from "effect/Layer";
34
- import * as Schema from "effect/Schema";
35
42
  import type * as AST from "effect/SchemaAST";
43
+ import { type Decode, decoderFor, type SchemaMismatch } from "./response-validation-strict.ts";
36
44
 
37
- export type Mode = "lenient" | "strict";
45
+ export { SchemaMismatch, withholdActual } from "./response-validation-strict.ts";
46
+
47
+ export type Mode = "lenient" | "additionalProperties" | "strict";
38
48
 
39
49
  /**
40
50
  * The active validation mode. Read by protocols at call time on the calling
41
- * fiber; `lenient` unless a {@link strict} layer is provided.
51
+ * fiber; `lenient` unless an {@link additionalProperties} or {@link strict}
52
+ * layer is provided.
42
53
  */
43
- export const ResponseValidation = Context.Reference<Mode>(
54
+ export const ResponseValidation = /*@__PURE__*/ Context.Reference<Mode>(
44
55
  "@distilled.cloud/core/ResponseValidation",
45
56
  { defaultValue: () => "lenient" },
46
57
  );
47
58
 
48
- /** Decode every 2xx response against its output schema; fail on mismatch. */
49
- export const strict: Layer.Layer<never> = Layer.succeed(ResponseValidation, "strict");
59
+ /**
60
+ * The checker the validating modes use (`exact`: reject unmodeled keys). The
61
+ * `additionalProperties` and `strict` layers provide it, so the checker is
62
+ * bundled only with programs that validate.
63
+ */
64
+ export const StrictDecoder = /*@__PURE__*/ Context.Reference<
65
+ ((ast: AST.AST, exact: boolean) => Decode) | undefined
66
+ >("@distilled.cloud/core/ResponseValidation/StrictDecoder", { defaultValue: () => undefined });
67
+
68
+ /**
69
+ * A layer that is also the `Context` it provides. `Effect.provide` treats a
70
+ * `Context` as a plain `provideContext`, so `Effect.provide(strict)` around
71
+ * a single call costs a context merge instead of a layer build (scope, memo
72
+ * map and a few fibers steps per call); every other use sees an ordinary
73
+ * `Layer`. The context state is read through the prototype.
74
+ */
75
+ const layerOfContext = (context: Context.Context<never>): Layer.Layer<never> => {
76
+ const layer = Layer.succeedContext(context);
77
+ const both = Object.create(context);
78
+ const layerProto = Object.getPrototypeOf(layer);
79
+ for (const key of Reflect.ownKeys(layerProto)) {
80
+ if (key !== "pipe") both[key] = layerProto[key];
81
+ }
82
+ both.build = (layer as unknown as { build: unknown }).build;
83
+ return both;
84
+ };
85
+
86
+ /**
87
+ * Check every 2xx response against its output schema and fail on a
88
+ * mismatch, including any key the schema does not model.
89
+ */
90
+ export const strict: Layer.Layer<never> = /*@__PURE__*/ layerOfContext(
91
+ /*@__PURE__*/ Context.add(
92
+ /*@__PURE__*/ Context.make(ResponseValidation, "strict"),
93
+ StrictDecoder,
94
+ decoderFor,
95
+ ),
96
+ );
97
+
98
+ /**
99
+ * Check every 2xx response against its output schema and fail on a
100
+ * mismatch; keys the schema does not model are allowed.
101
+ */
102
+ export const additionalProperties: Layer.Layer<never> = /*@__PURE__*/ layerOfContext(
103
+ /*@__PURE__*/ Context.add(
104
+ /*@__PURE__*/ Context.make(ResponseValidation, "additionalProperties"),
105
+ StrictDecoder,
106
+ decoderFor,
107
+ ),
108
+ );
50
109
 
51
110
  /** Return 2xx responses as read, without checking them against the schema. */
52
- export const lenient: Layer.Layer<never> = Layer.succeed(ResponseValidation, "lenient");
111
+ export const lenient: Layer.Layer<never> = /*@__PURE__*/ layerOfContext(
112
+ /*@__PURE__*/ Context.make(ResponseValidation, "lenient"),
113
+ );
53
114
 
54
- /** Whether the calling fiber is in strict mode. */
55
- export const isStrict: Effect.Effect<boolean> = Effect.map(
115
+ /** Whether the calling fiber validates responses (any mode but lenient). */
116
+ export const isValidating: Effect.Effect<boolean> = /*@__PURE__*/ Effect.map(
56
117
  ResponseValidation,
57
- (mode) => mode === "strict",
118
+ (mode) => mode !== "lenient",
58
119
  );
59
120
 
60
121
  /**
61
122
  * For a 2xx body the protocol could not read into the shape it transforms
62
- * (e.g. invalid JSON): fail with `error` in strict mode, succeed with
123
+ * (e.g. invalid JSON): fail with `error` when validating, succeed with
63
124
  * `asRead` (usually the body text) in lenient mode.
64
125
  */
65
- export const failIfStrict = <A, E>(error: E, asRead: A): Effect.Effect<A, E> =>
126
+ export const failUnlessLenient = <A, E>(error: E, asRead: A): Effect.Effect<A, E> =>
66
127
  Effect.flatMap(ResponseValidation, (mode) =>
67
- mode === "strict" ? Effect.fail(error) : Effect.succeed(asRead),
128
+ mode === "lenient" ? Effect.succeed(asRead) : Effect.fail(error),
68
129
  );
69
130
 
70
- const decoders = new WeakMap<
71
- AST.AST,
72
- (input: unknown) => Effect.Effect<unknown, Schema.SchemaError>
73
- >();
74
-
75
- const decoderFor = (ast: AST.AST) => {
76
- let decode = decoders.get(ast);
77
- if (!decode) {
78
- decode = Schema.decodeUnknownEffect(Schema.make<Schema.Top>(ast)) as (
79
- input: unknown,
80
- ) => Effect.Effect<unknown, Schema.SchemaError>;
81
- decoders.set(ast, decode);
131
+ /**
132
+ * The schema error for `value` against `outputAst` when validating,
133
+ * `undefined` when it matches or the mode is lenient. Synchronous, for
134
+ * protocols that already hold the calling fiber's context.
135
+ */
136
+ export const checkResponse = (
137
+ context: Context.Context<any>,
138
+ outputAst: AST.AST,
139
+ value: unknown,
140
+ ): SchemaMismatch | undefined => {
141
+ const mode = Context.get(context, ResponseValidation);
142
+ if (mode === "lenient") return undefined;
143
+ const decoderOf = Context.get(context, StrictDecoder);
144
+ if (!decoderOf) {
145
+ throw new Error(`${mode} response validation needs the ResponseValidation.${mode} layer`);
82
146
  }
83
- return decode;
147
+ return decoderOf(outputAst, mode === "strict")(value);
84
148
  };
85
149
 
86
- /**
87
- * Check a 2xx response value against the operation's output schema.
88
- *
89
- * Lenient mode returns `value` untouched. Strict mode decodes it and, on a
90
- * mismatch, fails with `onError(schemaError)` — protocols pass their SDK's
91
- * `<Sdk>ParseError` constructor. On success the ORIGINAL value is returned
92
- * (members the schema does not model are kept), so switching modes never
93
- * changes what a successful call returns.
94
- *
95
- * `value` must already be in the schema's shape (TS member names), i.e.
96
- * after any wire→TS key mapping and before `Redacted` wrapping.
97
- */
98
150
  export const validateResponse = <E>(
99
151
  outputAst: AST.AST,
100
152
  value: unknown,
101
- onError: (cause: Schema.SchemaError) => E,
153
+ onError: (cause: SchemaMismatch) => E,
102
154
  ): Effect.Effect<unknown, E> =>
103
- Effect.flatMap(ResponseValidation, (mode) =>
104
- mode === "lenient"
105
- ? Effect.succeed(value)
106
- : decoderFor(outputAst)(value).pipe(Effect.mapError(onError), Effect.as(value)),
107
- );
155
+ Effect.withFiber((fiber) => {
156
+ const error = checkResponse(fiber.context as Context.Context<any>, outputAst, value);
157
+ return error === undefined ? Effect.succeed(value) : Effect.fail(onError(error));
158
+ });
@@ -18,7 +18,7 @@ import * as Duration from "effect/Duration";
18
18
  import { RETRYABLE_HTTP_STATUSES } from "./errors.ts";
19
19
 
20
20
  /**
21
- * Header bag — case-insensitive lookup is the caller's responsibility.
21
+ * Header bag -- case-insensitive lookup is the caller's responsibility.
22
22
  *
23
23
  * The helpers in this module read lowercase keys (`retry-after`, `ratelimit`)
24
24
  * because most HTTP clients normalize headers to lowercase. If your client
@@ -72,7 +72,7 @@ export const parseRetryAfter = (headers: Headers): Duration.Duration | undefined
72
72
  * `RateLimit: r=0, t=30` (compact form)
73
73
  *
74
74
  * Returns the `t` (seconds until reset) value as a Duration, but only when
75
- * `r` (remaining quota) is present and equals `0` — i.e., we're actually
75
+ * `r` (remaining quota) is present and equals `0` -- i.e., we're actually
76
76
  * rate-limited right now. Returns `undefined` otherwise.
77
77
  */
78
78
  export const parseRatelimit = (headers: Headers): Duration.Duration | undefined => {
@@ -112,7 +112,7 @@ export const parseServerRetryHint = (headers: Headers): Duration.Duration | unde
112
112
  * unless `status` is one whose error class actually declares a `retryAfter`
113
113
  * field (see `RETRYABLE_HTTP_STATUSES`). Use this in `matchError` when you
114
114
  * dispatch off a generic `HTTP_STATUS_MAP` lookup that includes both
115
- * retryable (429/5xx/423) and non-retryable (4xx) classes — it prevents
115
+ * retryable (429/5xx/423) and non-retryable (4xx) classes -- it prevents
116
116
  * stale `retryAfter` properties from being attached to errors like
117
117
  * `BadRequest` or `NotFound` (which would otherwise leak into JSON output).
118
118
  */