@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
@@ -1,185 +1,99 @@
1
- /** Compile-only contract checks. The coordinator runs these with noCheck=false. */
2
- import type * as G from "./graphql.ts";
1
+ import type * as Effect from "effect/Effect";
3
2
  import type * as Stream from "effect/Stream";
3
+ import { mapQuery } from "./graphql.ts";
4
4
  import type {
5
- FixtureSchema,
6
- ProjectNotFound,
7
- ServicesUnavailable,
8
- Unauthorized,
9
- PageInfoUnavailable,
10
- } from "./graphql.fixture.ts";
5
+ itemsQuery,
6
+ GraphQLFailure,
7
+ GraphQLTransportError,
8
+ UnknownGraphQLError,
9
+ } from "./graphql.ts";
10
+ /** Compile-only: Query.fn unwraps Query/Effect fields on the returned plan. */
11
+ import type { PlanError, Query, QueryError, UnwrapPlan } from "./query.ts";
11
12
 
12
- type Assert<T extends true> = T;
13
- type Assignable<A, B> = [A] extends [B] ? true : false;
14
- type Equal<A, B> = Assignable<A, B> extends true ? Assignable<B, A> : false;
15
-
16
- type IdentitySelection = {
17
- project: { where: { id: "p1" }; select: { id: true } };
18
- };
19
- type DetailedSelection = {
20
- project: {
21
- where: { id: "p1" };
22
- select: {
23
- id: true;
24
- services: { select: { edges: { node: { id: true } } } };
25
- };
26
- };
13
+ type Plan = {
14
+ readonly email: Query<string>;
15
+ readonly projects: Query<ReadonlyArray<{ name: string; stars: number }>>;
16
+ readonly flag: Effect.Effect<boolean>;
27
17
  };
28
- type AliasSelection = {
29
- __alias: {
30
- production: { project: { where: { id: "p1" }; select: { id: true } } };
31
- };
32
- };
33
- type SearchSelection = {
34
- search: {
35
- where: { text: "prod" };
36
- select: {
37
- __typename: true;
38
- __on: { Project: { name: true }; Service: { status: true } };
39
- };
40
- };
41
- };
42
-
43
- type Identity = G.Result<FixtureSchema, "Query!", IdentitySelection>;
44
- type _Identity = Assert<Equal<Identity, { project: { id: string } | null }>>;
45
- type _UnselectedFieldsAbsent = Assert<
46
- Equal<keyof NonNullable<Identity["project"]>, "id">
47
- >;
48
18
 
49
- type Alias = G.Result<FixtureSchema, "Query!", AliasSelection>;
50
- type _Alias = Assert<Equal<Alias, { production: { id: string } | null }>>;
19
+ type Unwrapped = UnwrapPlan<Plan>;
20
+ type Assert<T extends true> = T;
21
+ type Equal<Left, Right> = [Left] extends [Right] ? ([Right] extends [Left] ? true : false) : false;
51
22
 
52
- type Search = G.Result<FixtureSchema, "Query!", SearchSelection>;
53
- type _Union = Assert<
54
- Equal<
55
- Search["search"][number],
56
- | { __typename: "Project"; name: string }
57
- | { __typename: "Service"; status: "ACTIVE" | "ARCHIVED" }
58
- >
23
+ type _Email = Assert<Equal<Unwrapped["email"], string>>;
24
+ type _Projects = Assert<
25
+ Equal<Unwrapped["projects"], ReadonlyArray<{ name: string; stars: number }>>
59
26
  >;
27
+ type _Flag = Assert<Equal<Unwrapped["flag"], boolean>>;
60
28
 
61
- type IdentityErrors = G.Errors<FixtureSchema, "Query", IdentitySelection>;
62
- type DetailedErrors = G.Errors<FixtureSchema, "Query", DetailedSelection>;
63
- type _RootErrorIncluded = Assert<Assignable<ProjectNotFound, IdentityErrors>>;
64
- type _GlobalErrorIncluded = Assert<Assignable<Unauthorized, IdentityErrors>>;
65
- type _NestedErrorIncluded = Assert<
66
- Assignable<ServicesUnavailable, DetailedErrors>
67
- >;
68
- type _UnselectedErrorExcluded = Assert<
69
- Equal<Extract<IdentityErrors, { _tag: "ServicesUnavailable" }>, never>
29
+ // ── Errors ──────────────────────────────────────────────────────────────────
30
+ declare class NotFound {
31
+ readonly _tag: "NotFound";
32
+ readonly message: string;
33
+ }
34
+ declare class RateLimited {
35
+ readonly _tag: "RateLimited";
36
+ readonly message: string;
37
+ }
38
+ declare const project: Query<
39
+ { name: string; services: ReadonlyArray<{ name: string }> },
40
+ NotFound | RateLimited
70
41
  >;
42
+ declare const me: Query<{ email: string }, RateLimited>;
43
+ declare const lookup: Effect.Effect<number, "lookup-failed">;
71
44
 
72
- type ConditionalRoot = G.Result<
73
- FixtureSchema,
74
- "Query!",
75
- {
76
- ping: { $include: boolean; select: true };
77
- }
78
- >;
79
- type _ConditionalRoot = Assert<Equal<ConditionalRoot, { ping?: boolean }>>;
80
- type ConditionalNested = G.Result<
81
- FixtureSchema,
82
- "Query!",
83
- {
84
- project: {
85
- where: { id: string };
86
- select: {
87
- id: true;
88
- name: { $skip: boolean; select: true };
89
- };
90
- };
91
- }
45
+ type ErrorPlan = {
46
+ readonly name: typeof project.name;
47
+ readonly services: Query<ReadonlyArray<string>, NotFound>;
48
+ readonly email: typeof me.email;
49
+ readonly lookup: typeof lookup;
50
+ };
51
+
52
+ type _FieldsKeepRootErrors = Assert<
53
+ Equal<(typeof project.name)["errorType"], NotFound | RateLimited>
92
54
  >;
93
- type _ConditionalNested = Assert<
55
+ type _PlanError = Assert<Equal<PlanError<ErrorPlan>, NotFound | RateLimited | "lookup-failed">>;
56
+ type _NoErrors = Assert<Equal<PlanError<{ literal: 1 }>, never>>;
57
+ type _QueryError = Assert<
94
58
  Equal<
95
- ConditionalNested,
96
- {
97
- project: { id: string; name?: string } | null;
98
- }
59
+ Exclude<
60
+ QueryError<NotFound>,
61
+ GraphQLFailure<NotFound | UnknownGraphQLError> | UnknownGraphQLError | GraphQLTransportError
62
+ >,
63
+ NotFound
99
64
  >
100
65
  >;
101
- type ConditionalAlias = G.Result<
102
- FixtureSchema,
103
- "Query!",
104
- {
105
- __alias: { healthy: { ping: { $include: boolean; select: true } } };
106
- }
107
- >;
108
- type _ConditionalAlias = Assert<Equal<ConditionalAlias, { healthy?: boolean }>>;
109
66
 
110
- type MergedInterface = G.Result<
111
- FixtureSchema,
112
- "Node!",
113
- {
114
- __typename: true;
115
- parent: { id: true };
116
- __on: { Project: { parent: { name: true } } };
117
- }
118
- >;
119
- type _MergedInterface = Assert<
67
+ // ── Pagination ──────────────────────────────────────────────────────────────
68
+ declare const services: Query<ReadonlyArray<{ name: string }>, NotFound>;
69
+ type PagedItems = ReturnType<typeof itemsQuery<{ name: string }, NotFound>>;
70
+ type _ItemsValue = Assert<Equal<Stream.Success<PagedItems>, { name: string }>>;
71
+ type _ItemsError = Assert<
120
72
  Equal<
121
- Extract<MergedInterface, { __typename: "Project" }>["parent"],
122
- { id: string; name: string } | null
73
+ Extract<Stream.Error<PagedItems>, { _tag: "NotFound" | "GraphQLPaginationError" }>["_tag"],
74
+ "NotFound" | "GraphQLPaginationError"
123
75
  >
124
76
  >;
77
+ export type _Services = typeof services;
125
78
 
126
- // These assignments exercise user-facing argument and nested selection validation.
127
- const validSelection: G.Selection<FixtureSchema, "Query"> = {
128
- project: {
129
- where: { id: "p1" },
130
- select: {
131
- services: {
132
- where: { first: 10 },
133
- select: { edges: { node: { id: true } } },
134
- },
135
- },
136
- },
137
- };
138
- const missingArgument: G.Selection<FixtureSchema, "Query"> = {
139
- // @ts-expect-error Required GraphQL field arguments cannot be omitted.
140
- project: { select: { id: true } },
141
- };
142
- const wrongArgument: G.Selection<FixtureSchema, "Query"> = {
143
- // @ts-expect-error GraphQL argument types follow the schema.
144
- project: { where: { id: 42 }, select: { id: true } },
145
- };
146
- const unknownField: G.Selection<FixtureSchema, "Query"> = {
147
- // @ts-expect-error Unknown nested fields cannot be selected.
148
- project: { where: { id: "p1" }, select: { invented: true } },
149
- };
150
- // @ts-expect-error Fields returning objects require a projection.
151
- const noProjection: G.Selection<FixtureSchema, "Query"> = { project: true };
152
-
153
- void [
154
- validSelection,
155
- missingArgument,
156
- wrongArgument,
157
- unknownField,
158
- noProjection,
159
- ];
160
-
161
- // Named operations retain projection and pagination constraints during inference.
162
- declare const inferredClient: ReturnType<
163
- typeof G.makeClient<FixtureSchema, never>
164
- >;
165
-
166
- // @ts-expect-error An object-returning operation always requires a projection.
167
- inferredClient.operation("query", "project")({ id: "p1" });
168
- // @ts-expect-error Non-connection object queries do not expose pages.
169
- inferredClient.operation("query", "project").pages;
170
- // @ts-expect-error Scalar queries do not expose connection pagination.
171
- inferredClient.operation("query", "ping").items;
172
- // @ts-expect-error Mutations cannot be paginated.
173
- inferredClient.operation("mutation", "editProject").pages;
79
+ // ── Nullability ─────────────────────────────────────────────────────────────
80
+ declare const nullable: Query<{
81
+ owner: { name: string; tags: ReadonlyArray<string> | null } | null;
82
+ }>;
83
+ type _NullParent = Assert<Equal<UnwrapPlan<typeof nullable.owner.name>, string | null>>;
174
84
 
175
- const selectivePages = inferredClient
176
- .operation("query", "services")
177
- .pages({}, { edges: { node: { id: true } } });
178
- type PageErrors = Stream.Error<typeof selectivePages>;
179
- type PageIssues =
180
- Extract<PageErrors, G.GraphQLFailure> extends G.GraphQLFailure<infer E>
181
- ? E
182
- : never;
183
- type _HiddenPaginationErrorIncluded = Assert<
184
- Assignable<PageInfoUnavailable, PageIssues>
85
+ // ── Query.map on objects ────────────────────────────────────────────────────
86
+ declare const service: Query<{
87
+ name: string;
88
+ stars: number;
89
+ latestDeployment: { id: string; status: string } | null;
90
+ tags: ReadonlyArray<{ label: string }>;
91
+ }>;
92
+ const deployment = service.latestDeployment.pipe(mapQuery((d) => ({ id: d.id, status: d.status })));
93
+ type _ObjectMap = Assert<
94
+ Equal<UnwrapPlan<typeof deployment>, { id: string; status: string } | null>
185
95
  >;
96
+ const popular = service.stars.pipe(mapQuery((stars) => stars > 5));
97
+ type _ScalarMap = Assert<Equal<UnwrapPlan<typeof popular>, boolean>>;
98
+ const labels = service.tags.pipe(mapQuery((tag) => tag.label));
99
+ type _ListMap = Assert<Equal<UnwrapPlan<typeof labels>, ReadonlyArray<string>>>;
package/src/index.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * `@distilled.cloud/core` — the runtime shared by every distilled SDK.
3
+ *
4
+ * ```ts
5
+ * import { ResponseValidation, Retry } from "@distilled.cloud/core";
6
+ *
7
+ * program.pipe(Effect.provide(ResponseValidation.strict));
8
+ * ```
9
+ *
10
+ * Each namespace is also importable on its own subpath
11
+ * (`@distilled.cloud/core/response-validation`, …); generated SDKs use the
12
+ * subpaths. Code generation (`codegen/*`) and test helpers (`testing`) stay
13
+ * subpath-only.
14
+ */
15
+ export * as API from "./api.ts";
16
+ export * as Category from "./category.ts";
17
+ export * as Errors from "./errors.ts";
18
+ export * as GraphQL from "./graphql.ts";
19
+ export * as Pagination from "./pagination.ts";
20
+ export * as ProtocolHttp from "./protocol-http.ts";
21
+ export * as ProtocolRest from "./protocol-rest.ts";
22
+ export { Query } from "./query.ts";
23
+ export * as ResponseValidation from "./response-validation.ts";
24
+ export * as Retry from "./retry.ts";
25
+ export * as RetryAfter from "./retry-after.ts";
26
+ export * as Trait from "./trait.ts";
@@ -0,0 +1,77 @@
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
+ });
package/src/json-patch.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  /**
2
2
  * JSON Patch (RFC 6902) Implementation
3
3
  *
4
- * Provides a unified spec patching system for all SDKs. Patches apply in
5
- * `convert` so `.generated-specs` is the patched Smithy model. generate does
6
- * not patch. Prefer `operationNaming: "verbNoun"` for operationId renames —
7
- * JSON pointers at `/paths/~1foo/operationId` go stale when upstream prefixes
8
- * paths.
4
+ * The patch engine for every SDK. Patches are Smithy ops applied in
5
+ * `convert` (`finalizeConvert`), so `.generated-specs` is the patched Smithy
6
+ * model; generate does not patch. Operation names are convert policy
7
+ * (`operationNames`), never a patch.
9
8
  *
10
9
  * Pure functions only — callers load patch files themselves (the generators
11
10
  * use Effect's FileSystem) and hand the parsed operations to `applyPatch`.
@@ -20,6 +19,14 @@ export interface JsonPatchOperation {
20
19
  path: string;
21
20
  value?: unknown;
22
21
  from?: string;
22
+ /**
23
+ * Extension for `add` and `move`: when the op creates a new object key,
24
+ * insert it just before this sibling key instead of at the end, so the
25
+ * patched model keeps the order the converter would have written. A `move`
26
+ * whose `from` equals `path` reorders a key. Ignored when the key already
27
+ * exists, the parent is an array, or the sibling is absent.
28
+ */
29
+ before?: string;
23
30
  }
24
31
 
25
32
  export type JsonPatch = JsonPatchOperation[];
@@ -59,11 +66,7 @@ export function getValueAtPath(obj: unknown, pointer: string): unknown {
59
66
  let current: unknown = obj;
60
67
 
61
68
  for (const segment of segments) {
62
- if (
63
- current === null ||
64
- current === undefined ||
65
- typeof current !== "object"
66
- ) {
69
+ if (current === null || current === undefined || typeof current !== "object") {
67
70
  throw new StaleTargetError(
68
71
  `JSON pointer ${pointer} missing (at '${segment}'): not an object`,
69
72
  );
@@ -71,17 +74,13 @@ export function getValueAtPath(obj: unknown, pointer: string): unknown {
71
74
  if (Array.isArray(current)) {
72
75
  const index = segment === "-" ? current.length : parseInt(segment, 10);
73
76
  if (index < 0 || index >= current.length) {
74
- throw new StaleTargetError(
75
- `JSON pointer ${pointer} missing index '${segment}'`,
76
- );
77
+ throw new StaleTargetError(`JSON pointer ${pointer} missing index '${segment}'`);
77
78
  }
78
79
  current = current[index];
79
80
  } else {
80
81
  const record = current as Record<string, unknown>;
81
82
  if (!Object.prototype.hasOwnProperty.call(record, segment)) {
82
- throw new StaleTargetError(
83
- `JSON pointer ${pointer} missing key '${segment}'`,
84
- );
83
+ throw new StaleTargetError(`JSON pointer ${pointer} missing key '${segment}'`);
85
84
  }
86
85
  current = record[segment];
87
86
  }
@@ -95,6 +94,8 @@ export function setValueAtPath(
95
94
  obj: unknown,
96
95
  pointer: string,
97
96
  value: unknown,
97
+ mode: "add" | "replace" = "add",
98
+ before?: string,
98
99
  ): void {
99
100
  const segments = parseJsonPointer(pointer);
100
101
  if (segments.length === 0) {
@@ -106,9 +107,7 @@ export function setValueAtPath(
106
107
  for (let i = 0; i < segments.length - 1; i++) {
107
108
  const segment = segments[i]!;
108
109
  if (current === null || typeof current !== "object") {
109
- throw new StaleTargetError(
110
- `Cannot traverse path ${pointer}: not an object`,
111
- );
110
+ throw new StaleTargetError(`Cannot traverse path ${pointer}: not an object`);
112
111
  }
113
112
  if (Array.isArray(current)) {
114
113
  current = current[parseInt(segment, 10)];
@@ -119,19 +118,38 @@ export function setValueAtPath(
119
118
 
120
119
  const lastSegment = segments[segments.length - 1]!;
121
120
  if (current === null || typeof current !== "object") {
122
- throw new StaleTargetError(
123
- `Cannot set value at path ${pointer}: parent is not an object`,
124
- );
121
+ throw new StaleTargetError(`Cannot set value at path ${pointer}: parent is not an object`);
125
122
  }
126
123
 
127
124
  if (Array.isArray(current)) {
128
125
  if (lastSegment === "-") {
129
126
  current.push(value);
130
- } else {
127
+ } else if (mode === "replace") {
131
128
  current[parseInt(lastSegment, 10)] = value;
129
+ } else {
130
+ // RFC 6902 §4.1: `add` at an array index inserts before it.
131
+ const index = parseInt(lastSegment, 10);
132
+ if (Number.isNaN(index) || index < 0 || index > current.length) {
133
+ throw new StaleTargetError(
134
+ `JSON pointer ${pointer} index '${lastSegment}' is out of bounds`,
135
+ );
136
+ }
137
+ current.splice(index, 0, value);
132
138
  }
133
139
  } else {
134
- (current as Record<string, unknown>)[lastSegment] = value;
140
+ const record = current as Record<string, unknown>;
141
+ if (before === undefined || lastSegment in record || !(before in record)) {
142
+ record[lastSegment] = value;
143
+ return;
144
+ }
145
+ // Rebuild the keys in place: the parent object stays the same object,
146
+ // since callers and later ops may hold a reference to it.
147
+ const entries = Object.entries(record);
148
+ for (const [key] of entries) delete record[key];
149
+ for (const [key, v] of entries) {
150
+ if (key === before) record[lastSegment] = value;
151
+ record[key] = v;
152
+ }
135
153
  }
136
154
  }
137
155
 
@@ -147,9 +165,7 @@ export function removeValueAtPath(obj: unknown, pointer: string): void {
147
165
  for (let i = 0; i < segments.length - 1; i++) {
148
166
  const segment = segments[i]!;
149
167
  if (current === null || typeof current !== "object") {
150
- throw new StaleTargetError(
151
- `Cannot traverse path ${pointer}: not an object`,
152
- );
168
+ throw new StaleTargetError(`Cannot traverse path ${pointer}: not an object`);
153
169
  }
154
170
  if (Array.isArray(current)) {
155
171
  current = current[parseInt(segment, 10)];
@@ -160,26 +176,20 @@ export function removeValueAtPath(obj: unknown, pointer: string): void {
160
176
 
161
177
  const lastSegment = segments[segments.length - 1]!;
162
178
  if (current === null || typeof current !== "object") {
163
- throw new StaleTargetError(
164
- `Cannot remove at path ${pointer}: parent is not an object`,
165
- );
179
+ throw new StaleTargetError(`Cannot remove at path ${pointer}: parent is not an object`);
166
180
  }
167
181
 
168
182
  // RFC 6902 §4.2: the target location MUST exist.
169
183
  if (Array.isArray(current)) {
170
184
  const index = parseInt(lastSegment, 10);
171
185
  if (Number.isNaN(index) || index < 0 || index >= current.length) {
172
- throw new StaleTargetError(
173
- `JSON pointer ${pointer} missing index '${lastSegment}'`,
174
- );
186
+ throw new StaleTargetError(`JSON pointer ${pointer} missing index '${lastSegment}'`);
175
187
  }
176
188
  current.splice(index, 1);
177
189
  } else {
178
190
  const record = current as Record<string, unknown>;
179
191
  if (!Object.prototype.hasOwnProperty.call(record, lastSegment)) {
180
- throw new StaleTargetError(
181
- `JSON pointer ${pointer} missing key '${lastSegment}'`,
182
- );
192
+ throw new StaleTargetError(`JSON pointer ${pointer} missing key '${lastSegment}'`);
183
193
  }
184
194
  delete record[lastSegment];
185
195
  }
@@ -190,13 +200,10 @@ export function removeValueAtPath(obj: unknown, pointer: string): void {
190
200
  // ============================================================================
191
201
 
192
202
  /** Apply a single JSON Patch operation (mutates `obj` in place). */
193
- export function applyOperation(
194
- obj: unknown,
195
- operation: JsonPatchOperation,
196
- ): void {
203
+ export function applyOperation(obj: unknown, operation: JsonPatchOperation): void {
197
204
  switch (operation.op) {
198
205
  case "add":
199
- setValueAtPath(obj, operation.path, operation.value);
206
+ setValueAtPath(obj, operation.path, operation.value, "add", operation.before);
200
207
  break;
201
208
  case "remove":
202
209
  removeValueAtPath(obj, operation.path);
@@ -204,33 +211,25 @@ export function applyOperation(
204
211
  case "replace": {
205
212
  const existing = getValueAtPath(obj, operation.path);
206
213
  if (existing === undefined) {
207
- throw new StaleTargetError(
208
- `JSON pointer ${operation.path} does not exist`,
209
- );
214
+ throw new StaleTargetError(`JSON pointer ${operation.path} does not exist`);
210
215
  }
211
- setValueAtPath(obj, operation.path, operation.value);
216
+ setValueAtPath(obj, operation.path, operation.value, "replace");
212
217
  break;
213
218
  }
214
219
  case "move": {
215
220
  if (!operation.from) throw new Error("move operation requires 'from'");
216
221
  const moveValue = getValueAtPath(obj, operation.from);
217
222
  if (moveValue === undefined) {
218
- throw new StaleTargetError(
219
- `Cannot move from path ${operation.from}: not an object`,
220
- );
223
+ throw new StaleTargetError(`Cannot move from path ${operation.from}: not an object`);
221
224
  }
222
225
  removeValueAtPath(obj, operation.from);
223
- setValueAtPath(obj, operation.path, moveValue);
226
+ setValueAtPath(obj, operation.path, moveValue, "add", operation.before);
224
227
  break;
225
228
  }
226
229
  case "copy": {
227
230
  if (!operation.from) throw new Error("copy operation requires 'from'");
228
231
  const copyValue = getValueAtPath(obj, operation.from);
229
- setValueAtPath(
230
- obj,
231
- operation.path,
232
- JSON.parse(JSON.stringify(copyValue)),
233
- );
232
+ setValueAtPath(obj, operation.path, JSON.parse(JSON.stringify(copyValue)));
234
233
  break;
235
234
  }
236
235
  case "test": {
@@ -263,12 +262,7 @@ export function applyPatch(obj: unknown, patch: JsonPatch): void {
263
262
  */
264
263
  export function isStaleTargetError(error: unknown): boolean {
265
264
  if (error instanceof StaleTargetError) return true;
266
- const message =
267
- typeof error === "string"
268
- ? error
269
- : error instanceof Error
270
- ? error.message
271
- : "";
265
+ const message = typeof error === "string" ? error : error instanceof Error ? error.message : "";
272
266
  return (
273
267
  message.includes("not an object") ||
274
268
  message.includes("parent is not an object") ||