effect-app 4.0.0-beta.1 → 4.0.0-beta.100

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 (142) hide show
  1. package/CHANGELOG.md +444 -0
  2. package/dist/Config/SecretURL.js +2 -2
  3. package/dist/Config.d.ts +7 -0
  4. package/dist/Config.d.ts.map +1 -0
  5. package/dist/Config.js +6 -0
  6. package/dist/ConfigProvider.d.ts +39 -0
  7. package/dist/ConfigProvider.d.ts.map +1 -0
  8. package/dist/ConfigProvider.js +42 -0
  9. package/dist/Context.d.ts +40 -0
  10. package/dist/Context.d.ts.map +1 -0
  11. package/dist/Context.js +66 -0
  12. package/dist/Effect.d.ts +8 -7
  13. package/dist/Effect.d.ts.map +1 -1
  14. package/dist/Effect.js +3 -2
  15. package/dist/Layer.d.ts +5 -4
  16. package/dist/Layer.d.ts.map +1 -1
  17. package/dist/Layer.js +1 -1
  18. package/dist/Operations.d.ts +62 -25
  19. package/dist/Operations.d.ts.map +1 -1
  20. package/dist/Pure.d.ts +2 -2
  21. package/dist/Pure.d.ts.map +1 -1
  22. package/dist/Pure.js +13 -13
  23. package/dist/Schema/Class.d.ts +43 -10
  24. package/dist/Schema/Class.d.ts.map +1 -1
  25. package/dist/Schema/Class.js +89 -16
  26. package/dist/Schema/SpecialJsonSchema.d.ts +21 -0
  27. package/dist/Schema/SpecialJsonSchema.d.ts.map +1 -0
  28. package/dist/Schema/SpecialJsonSchema.js +59 -0
  29. package/dist/Schema/SpecialOpenApi.d.ts +32 -0
  30. package/dist/Schema/SpecialOpenApi.d.ts.map +1 -0
  31. package/dist/Schema/SpecialOpenApi.js +123 -0
  32. package/dist/Schema/brand.d.ts +8 -5
  33. package/dist/Schema/brand.d.ts.map +1 -1
  34. package/dist/Schema/brand.js +1 -1
  35. package/dist/Schema/email.d.ts.map +1 -1
  36. package/dist/Schema/email.js +9 -4
  37. package/dist/Schema/ext.d.ts +103 -46
  38. package/dist/Schema/ext.d.ts.map +1 -1
  39. package/dist/Schema/ext.js +110 -51
  40. package/dist/Schema/moreStrings.d.ts +25 -13
  41. package/dist/Schema/moreStrings.d.ts.map +1 -1
  42. package/dist/Schema/moreStrings.js +14 -9
  43. package/dist/Schema/numbers.d.ts +11 -11
  44. package/dist/Schema/numbers.d.ts.map +1 -1
  45. package/dist/Schema/numbers.js +10 -9
  46. package/dist/Schema/phoneNumber.d.ts.map +1 -1
  47. package/dist/Schema/phoneNumber.js +8 -3
  48. package/dist/Schema/strings.d.ts +4 -4
  49. package/dist/Schema/strings.d.ts.map +1 -1
  50. package/dist/Schema.d.ts +22 -55
  51. package/dist/Schema.d.ts.map +1 -1
  52. package/dist/Schema.js +43 -64
  53. package/dist/client/apiClientFactory.d.ts +11 -28
  54. package/dist/client/apiClientFactory.d.ts.map +1 -1
  55. package/dist/client/apiClientFactory.js +21 -23
  56. package/dist/client/clientFor.d.ts +8 -7
  57. package/dist/client/clientFor.d.ts.map +1 -1
  58. package/dist/client/errors.d.ts +18 -9
  59. package/dist/client/errors.d.ts.map +1 -1
  60. package/dist/client/errors.js +35 -10
  61. package/dist/client/makeClient.d.ts +21 -16
  62. package/dist/client/makeClient.d.ts.map +1 -1
  63. package/dist/client/makeClient.js +32 -34
  64. package/dist/http/Request.d.ts.map +1 -1
  65. package/dist/http/Request.js +5 -5
  66. package/dist/ids.d.ts +10 -10
  67. package/dist/ids.d.ts.map +1 -1
  68. package/dist/ids.js +3 -2
  69. package/dist/index.d.ts +3 -8
  70. package/dist/index.d.ts.map +1 -1
  71. package/dist/index.js +4 -9
  72. package/dist/middleware.d.ts +2 -2
  73. package/dist/middleware.d.ts.map +1 -1
  74. package/dist/middleware.js +3 -3
  75. package/dist/rpc/MiddlewareMaker.d.ts +6 -5
  76. package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
  77. package/dist/rpc/MiddlewareMaker.js +7 -6
  78. package/dist/rpc/RpcContextMap.d.ts +2 -2
  79. package/dist/rpc/RpcContextMap.d.ts.map +1 -1
  80. package/dist/rpc/RpcContextMap.js +4 -4
  81. package/dist/rpc/RpcMiddleware.d.ts +6 -5
  82. package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
  83. package/dist/rpc/RpcMiddleware.js +1 -1
  84. package/dist/utils/gen.d.ts +1 -1
  85. package/dist/utils/gen.d.ts.map +1 -1
  86. package/dist/utils/logger.d.ts +2 -2
  87. package/dist/utils/logger.d.ts.map +1 -1
  88. package/dist/utils/logger.js +3 -3
  89. package/dist/utils.d.ts +18 -0
  90. package/dist/utils.d.ts.map +1 -1
  91. package/dist/utils.js +24 -5
  92. package/package.json +29 -17
  93. package/src/Config/SecretURL.ts +1 -1
  94. package/src/Config.ts +14 -0
  95. package/src/ConfigProvider.ts +48 -0
  96. package/src/{ServiceMap.ts → Context.ts} +57 -64
  97. package/src/Effect.ts +11 -9
  98. package/src/Layer.ts +5 -4
  99. package/src/Pure.ts +17 -18
  100. package/src/Schema/Class.ts +118 -30
  101. package/src/Schema/SpecialJsonSchema.ts +69 -0
  102. package/src/Schema/SpecialOpenApi.ts +130 -0
  103. package/src/Schema/brand.ts +13 -7
  104. package/src/Schema/email.ts +10 -2
  105. package/src/Schema/ext.ts +185 -82
  106. package/src/Schema/moreStrings.ts +27 -18
  107. package/src/Schema/numbers.ts +9 -8
  108. package/src/Schema/phoneNumber.ts +8 -1
  109. package/src/Schema.ts +79 -103
  110. package/src/client/apiClientFactory.ts +35 -43
  111. package/src/client/clientFor.ts +8 -3
  112. package/src/client/errors.ts +46 -12
  113. package/src/client/makeClient.ts +122 -75
  114. package/src/http/Request.ts +7 -4
  115. package/src/ids.ts +4 -3
  116. package/src/index.ts +3 -11
  117. package/src/middleware.ts +2 -2
  118. package/src/rpc/MiddlewareMaker.ts +12 -10
  119. package/src/rpc/RpcContextMap.ts +6 -5
  120. package/src/rpc/RpcMiddleware.ts +7 -6
  121. package/src/utils/gen.ts +1 -1
  122. package/src/utils/logger.ts +2 -2
  123. package/src/utils.ts +26 -4
  124. package/test/dist/moreStrings.test.d.ts.map +1 -0
  125. package/test/dist/rpc.test.d.ts.map +1 -1
  126. package/test/dist/secretURL.test.d.ts.map +1 -0
  127. package/test/dist/special.test.d.ts.map +1 -0
  128. package/test/moreStrings.test.ts +17 -0
  129. package/test/rpc.test.ts +28 -6
  130. package/test/schema.test.ts +397 -4
  131. package/test/secretURL.test.ts +157 -0
  132. package/test/special.test.ts +729 -0
  133. package/test/utils.test.ts +2 -2
  134. package/tsconfig.base.json +0 -1
  135. package/tsconfig.json +0 -1
  136. package/dist/ServiceMap.d.ts +0 -44
  137. package/dist/ServiceMap.d.ts.map +0 -1
  138. package/dist/ServiceMap.js +0 -91
  139. package/dist/Struct.d.ts +0 -44
  140. package/dist/Struct.d.ts.map +0 -1
  141. package/dist/Struct.js +0 -29
  142. package/src/Struct.ts +0 -54
@@ -0,0 +1,729 @@
1
+ import { Option, Predicate, Schema, SchemaGetter } from "effect"
2
+ import { InvalidStateError, LoginError, NotFoundError, NotLoggedInError, OptimisticConcurrencyException, ServiceUnavailableError, UnauthorizedError, ValidationError } from "effect-app/client/errors"
3
+ import * as AppSchema from "effect-app/Schema"
4
+ import { Class, TaggedClass } from "effect-app/Schema/Class"
5
+ import { flattenSimpleAllOf, specialJsonSchemaDocument } from "effect-app/Schema/SpecialJsonSchema"
6
+ import { deduplicateOpenApiSchemas } from "effect-app/Schema/SpecialOpenApi"
7
+ import * as S from "effect/Schema"
8
+ import { describe, expect, it } from "vitest"
9
+
10
+ describe("Class", () => {
11
+ it("encoding accepts plain objects matching the struct (Fields argument)", () => {
12
+ class A extends Class<A>("A")({ a: S.String }) {}
13
+
14
+ // Encoding a class instance still works
15
+ expect(S.encodeUnknownSync(A)(new A({ a: "hello" }))).toStrictEqual({ a: "hello" })
16
+
17
+ // Encoding a plain object matching the struct now succeeds
18
+ expect(S.encodeUnknownSync(A)({ a: "world" })).toStrictEqual({ a: "world" })
19
+
20
+ // Encoding null still fails
21
+ expect(() => S.encodeUnknownSync(A)(null)).toThrow()
22
+ })
23
+
24
+ it("encoding accepts plain objects matching the struct (Struct argument)", () => {
25
+ class A extends Class<A>("A")(S.Struct({ a: S.String })) {}
26
+
27
+ expect(S.encodeUnknownSync(A)(new A({ a: "hello" }))).toStrictEqual({ a: "hello" })
28
+ expect(S.encodeUnknownSync(A)({ a: "world" })).toStrictEqual({ a: "world" })
29
+ expect(() => S.encodeUnknownSync(A)(null)).toThrow()
30
+ })
31
+
32
+ it("decoding still works normally", () => {
33
+ class A extends Class<A>("A")({ a: S.String }) {}
34
+
35
+ const decoded = S.decodeUnknownSync(A)({ a: "hello" })
36
+ expect(decoded).toBeInstanceOf(A)
37
+ expect((decoded as A).a).toBe("hello")
38
+
39
+ expect(() => S.decodeUnknownSync(A)(null)).toThrow()
40
+ expect(() => S.decodeUnknownSync(A)({ a: 1 })).toThrow()
41
+ })
42
+
43
+ it("rejects values that don't match the struct", () => {
44
+ class A extends Class<A>("A")({ a: S.String }) {}
45
+
46
+ expect(() => S.encodeUnknownSync(A)({ a: 123 })).toThrow()
47
+ expect(() => S.encodeUnknownSync(A)("not an object")).toThrow()
48
+ })
49
+
50
+ it("returns a class constructor — new and instanceof work", () => {
51
+ class A extends Class<A>("A")({ a: S.String }) {}
52
+
53
+ const instance = new A({ a: "hello" })
54
+ expect(instance).toBeInstanceOf(A)
55
+ expect(instance.a).toBe("hello")
56
+ })
57
+
58
+ it("preserves fields and identifier", () => {
59
+ class A extends Class<A>("A")({ a: S.String, b: S.Number }) {}
60
+
61
+ expect(A.identifier).toBe("A")
62
+ expect(Object.keys(A.fields)).toStrictEqual(["a", "b"])
63
+ })
64
+ })
65
+
66
+ describe("Class constructor", () => {
67
+ it("works as a base class — new, instanceof, encoding plain objects", () => {
68
+ class A extends Class<A>("A")({ a: S.String }) {}
69
+
70
+ // Construction
71
+ const instance = new A({ a: "hello" })
72
+ expect(instance).toBeInstanceOf(A)
73
+ expect(instance.a).toBe("hello")
74
+
75
+ // Encoding a class instance
76
+ expect(S.encodeUnknownSync(A)(instance)).toStrictEqual({ a: "hello" })
77
+
78
+ // Encoding a plain object
79
+ expect(S.encodeUnknownSync(A)({ a: "world" })).toStrictEqual({ a: "world" })
80
+
81
+ // Encoding invalid input fails
82
+ expect(() => S.encodeUnknownSync(A)(null)).toThrow()
83
+ expect(() => S.encodeUnknownSync(A)({ a: 123 })).toThrow()
84
+ })
85
+
86
+ it("decoding works normally", () => {
87
+ class A extends Class<A>("A")({ a: S.String }) {}
88
+
89
+ const decoded = S.decodeUnknownSync(A)({ a: "hello" })
90
+ expect(decoded).toBeInstanceOf(A)
91
+ expect((decoded as A).a).toBe("hello")
92
+
93
+ expect(() => S.decodeUnknownSync(A)({ a: 1 })).toThrow()
94
+ })
95
+
96
+ it("exposes fields, identifier", () => {
97
+ class A extends Class<A>("A")({ a: S.String, b: S.Number }) {}
98
+
99
+ expect(A.identifier).toBe("A")
100
+ expect(Object.keys(A.fields)).toStrictEqual(["a", "b"])
101
+ })
102
+ })
103
+
104
+ describe("TaggedClass constructor", () => {
105
+ it("works as a base class with _tag — new, instanceof, encoding plain objects", () => {
106
+ class Circle extends TaggedClass<Circle>()("Circle", { radius: S.Number }) {}
107
+
108
+ // Construction
109
+ const instance = new Circle({ radius: 5 })
110
+ expect(instance).toBeInstanceOf(Circle)
111
+ expect(instance._tag).toBe("Circle")
112
+ expect(instance.radius).toBe(5)
113
+
114
+ // Encoding a class instance
115
+ expect(S.encodeUnknownSync(Circle)(instance)).toStrictEqual({ _tag: "Circle", radius: 5 })
116
+
117
+ // Encoding a plain object
118
+ expect(S.encodeUnknownSync(Circle)({ _tag: "Circle", radius: 10 })).toStrictEqual({ _tag: "Circle", radius: 10 })
119
+
120
+ // Encoding invalid input fails
121
+ expect(() => S.encodeUnknownSync(Circle)(null)).toThrow()
122
+ expect(() => S.encodeUnknownSync(Circle)({ _tag: "Circle", radius: "nope" })).toThrow()
123
+ })
124
+
125
+ it("decoding works normally", () => {
126
+ class Circle extends TaggedClass<Circle>()("Circle", { radius: S.Number }) {}
127
+
128
+ const decoded = S.decodeUnknownSync(Circle)({ _tag: "Circle", radius: 5 })
129
+ expect(decoded).toBeInstanceOf(Circle)
130
+ expect((decoded as Circle).radius).toBe(5)
131
+ expect((decoded as Circle)._tag).toBe("Circle")
132
+ })
133
+
134
+ it("exposes fields, identifier", () => {
135
+ class Circle extends TaggedClass<Circle>()("Circle", { radius: S.Number }) {}
136
+
137
+ expect(Circle.identifier).toBe("Circle")
138
+ expect(Object.keys(Circle.fields)).toContain("_tag")
139
+ expect(Object.keys(Circle.fields)).toContain("radius")
140
+ })
141
+ })
142
+
143
+ describe("TaggedError", () => {
144
+ it("InvalidStateError toString includes the message", () => {
145
+ const error = new InvalidStateError("something went wrong")
146
+ expect(error.toString()).toContain("something went wrong")
147
+ })
148
+
149
+ it("NotFoundError toString includes the message", () => {
150
+ const error = new NotFoundError({ type: "User", id: "123" })
151
+ expect(error.toString()).toContain("Didn't find User")
152
+ expect(error.toString()).toContain("123")
153
+ })
154
+
155
+ it("ServiceUnavailableError toString includes the message", () => {
156
+ const error = new ServiceUnavailableError("service down")
157
+ expect(error.toString()).toContain("service down")
158
+ })
159
+
160
+ it("ValidationError toString includes the message", () => {
161
+ const error = new ValidationError({ errors: ["field required"] })
162
+ expect(error.toString()).toContain("Validation failed")
163
+ expect(error.toString()).toContain("field required")
164
+ })
165
+
166
+ it("NotLoggedInError toString includes the message", () => {
167
+ const error = new NotLoggedInError("not logged in")
168
+ expect(error.toString()).toContain("not logged in")
169
+ })
170
+
171
+ it("LoginError toString includes the message", () => {
172
+ const error = new LoginError("login failed")
173
+ expect(error.toString()).toContain("login failed")
174
+ })
175
+
176
+ it("UnauthorizedError toString includes the message", () => {
177
+ const error = new UnauthorizedError("forbidden")
178
+ expect(error.toString()).toContain("forbidden")
179
+ })
180
+
181
+ it("OptimisticConcurrencyException toString includes the message", () => {
182
+ const error = new OptimisticConcurrencyException({ message: "conflict" })
183
+ expect(error.toString()).toContain("conflict")
184
+ })
185
+
186
+ it("OptimisticConcurrencyException from details toString includes the message", () => {
187
+ const error = new OptimisticConcurrencyException({ type: "User", id: "123", code: 409 })
188
+ expect(error.toString()).toContain("Existing User 123 record changed")
189
+ })
190
+ })
191
+
192
+ describe("SpecialJsonSchema", () => {
193
+ it("nullable to optional — from NullOr", () => {
194
+ const nullableDecodedUndefinedEncoded = (schema: Schema.Top) => {
195
+ const isNullableSchema = "members" in schema
196
+ && globalThis.Array.isArray((schema as any).members)
197
+ && (schema as any).members.length === 2
198
+ && (schema as any).members.some((member: any) => member.ast._tag === "Null")
199
+
200
+ const nullableMembers = isNullableSchema ? (schema as any).members as ReadonlyArray<Schema.Top> : undefined
201
+ const innerSchema = nullableMembers
202
+ ? nullableMembers.find((member: any) => member.ast._tag !== "Null")!
203
+ : schema
204
+
205
+ const nullableSchema = isNullableSchema ? schema : Schema.NullOr(schema)
206
+
207
+ return nullableSchema.pipe(
208
+ Schema.encodeTo(Schema.optionalKey(innerSchema), {
209
+ decode: SchemaGetter.transformOptional(Option.orElseSome(() => null)),
210
+ encode: SchemaGetter.transformOptional(Option.filter(Predicate.isNotNull))
211
+ })
212
+ )
213
+ }
214
+
215
+ const fromNullOr = nullableDecodedUndefinedEncoded(Schema.NullOr(Schema.String))
216
+ const structFromNullOr = Schema.Struct({ status: fromNullOr })
217
+
218
+ const encode = Schema.encodeUnknownSync(structFromNullOr as any)
219
+ const encodedNull = encode({ status: null }) as any
220
+ expect("status" in encodedNull).toBe(false)
221
+ expect(encode({ status: "test" })).toStrictEqual({ status: "test" })
222
+
223
+ const decode = Schema.decodeUnknownSync(structFromNullOr as any)
224
+ expect(decode({})).toStrictEqual({ status: null })
225
+ expect(decode({ status: "test" })).toStrictEqual({ status: "test" })
226
+
227
+ const doc = specialJsonSchemaDocument(structFromNullOr)
228
+ expect(doc).toStrictEqual({
229
+ dialect: "draft-2020-12",
230
+ schema: {
231
+ "type": "object",
232
+ "properties": {
233
+ "status": { "type": "string" }
234
+ },
235
+ "additionalProperties": false
236
+ },
237
+ definitions: {}
238
+ })
239
+ })
240
+
241
+ it("identifies X universally — deduplicates same-fingerprint references", () => {
242
+ const X = Schema.String.annotate({ title: "X", identifier: "X" })
243
+
244
+ const s = Schema.Struct({
245
+ a: Schema.NullOr(X).pipe(
246
+ Schema.encodeTo(Schema.optionalKey(X), {
247
+ decode: SchemaGetter.transformOptional(Option.orElseSome(() => null)),
248
+ encode: SchemaGetter.transformOptional(Option.filter(Predicate.isNotNull))
249
+ })
250
+ ),
251
+ b: Schema.NullOr(X).pipe(
252
+ Schema.encodeTo(Schema.optionalKey(X), {
253
+ decode: SchemaGetter.transformOptional(Option.orElseSome(() => null)),
254
+ encode: SchemaGetter.transformOptional(Option.filter(Predicate.isNotNull))
255
+ })
256
+ ),
257
+ c: Schema.NullOr(X),
258
+ d: X,
259
+ e: X.pipe(Schema.optionalKey)
260
+ })
261
+
262
+ const doc = specialJsonSchemaDocument(s)
263
+ expect(doc).toStrictEqual({
264
+ dialect: "draft-2020-12",
265
+ schema: {
266
+ "type": "object",
267
+ "properties": {
268
+ "a": { "$ref": "#/$defs/X" },
269
+ "b": { "$ref": "#/$defs/X" },
270
+ "c": {
271
+ "anyOf": [
272
+ { "$ref": "#/$defs/X" },
273
+ { "type": "null" }
274
+ ]
275
+ },
276
+ "d": { "$ref": "#/$defs/X" },
277
+ "e": { "$ref": "#/$defs/X" }
278
+ },
279
+ "required": ["c", "d"],
280
+ "additionalProperties": false
281
+ },
282
+ definitions: {
283
+ X: {
284
+ "type": "string",
285
+ "title": "X"
286
+ }
287
+ }
288
+ })
289
+ })
290
+
291
+ it("shared annotated schema via helper — deduplicates", () => {
292
+ const X = Schema.String.annotate({ title: "X", identifier: "X" })
293
+
294
+ const cache = new WeakMap()
295
+ const nullableDecodedUndefinedEncoded = (schema: Schema.Top) => {
296
+ const isNullableSchema = "members" in schema
297
+ && globalThis.Array.isArray((schema as any).members)
298
+ && (schema as any).members.length === 2
299
+ && (schema as any).members.some((member: any) => member.ast._tag === "Null")
300
+
301
+ const nullableMembers = isNullableSchema ? (schema as any).members as ReadonlyArray<Schema.Top> : undefined
302
+ const innerSchema = nullableMembers
303
+ ? nullableMembers.find((member: any) => member.ast._tag !== "Null")!
304
+ : schema
305
+
306
+ const cached = cache.get(innerSchema.ast)
307
+ if (cached !== undefined) return cached
308
+
309
+ const nullableSchema = isNullableSchema ? schema : Schema.NullOr(schema)
310
+ const out = nullableSchema.pipe(
311
+ Schema.encodeTo(Schema.optionalKey(innerSchema), {
312
+ decode: SchemaGetter.transformOptional(Option.orElseSome(() => null)),
313
+ encode: SchemaGetter.transformOptional(Option.filter(Predicate.isNotNull))
314
+ })
315
+ )
316
+
317
+ cache.set(innerSchema.ast, out)
318
+ return out
319
+ }
320
+
321
+ const structWithShared = Schema.Struct({
322
+ a: nullableDecodedUndefinedEncoded(X),
323
+ b: nullableDecodedUndefinedEncoded(Schema.NullOr(X)),
324
+ c: Schema.NullOr(X),
325
+ d: X,
326
+ e: X.pipe(Schema.optionalKey)
327
+ })
328
+
329
+ const doc = specialJsonSchemaDocument(structWithShared)
330
+ expect(doc).toStrictEqual({
331
+ dialect: "draft-2020-12",
332
+ schema: {
333
+ "type": "object",
334
+ "properties": {
335
+ "a": { "$ref": "#/$defs/X" },
336
+ "b": { "$ref": "#/$defs/X" },
337
+ "c": {
338
+ "anyOf": [
339
+ { "$ref": "#/$defs/X" },
340
+ { "type": "null" }
341
+ ]
342
+ },
343
+ "d": { "$ref": "#/$defs/X" },
344
+ "e": { "$ref": "#/$defs/X" }
345
+ },
346
+ "required": ["c", "d"],
347
+ "additionalProperties": false
348
+ },
349
+ definitions: {
350
+ X: {
351
+ "type": "string",
352
+ "title": "X"
353
+ }
354
+ }
355
+ })
356
+ })
357
+ })
358
+
359
+ describe("SpecialOpenApi", () => {
360
+ it("deduplicates identical components.schemas entries with same base identifier", () => {
361
+ const spec = {
362
+ openapi: "3.1.0",
363
+ info: { title: "Test", version: "1.0" },
364
+ paths: {
365
+ "/foo": {
366
+ get: {
367
+ responses: {
368
+ 200: {
369
+ content: {
370
+ "application/json": {
371
+ schema: { $ref: "#/components/schemas/X" }
372
+ }
373
+ }
374
+ }
375
+ }
376
+ }
377
+ },
378
+ "/bar": {
379
+ get: {
380
+ responses: {
381
+ 200: {
382
+ content: {
383
+ "application/json": {
384
+ schema: { $ref: "#/components/schemas/X1" }
385
+ }
386
+ }
387
+ }
388
+ }
389
+ }
390
+ }
391
+ },
392
+ components: {
393
+ schemas: {
394
+ X: { type: "string", title: "X" },
395
+ X1: { type: "string", title: "X" }
396
+ }
397
+ }
398
+ }
399
+
400
+ const result = deduplicateOpenApiSchemas(spec) as any
401
+
402
+ // X1 should be removed, and $ref to X1 rewritten to X
403
+ expect(result.components.schemas).toStrictEqual({
404
+ X: { type: "string", title: "X" }
405
+ })
406
+ expect(
407
+ result.paths["/bar"].get.responses[200].content["application/json"].schema
408
+ )
409
+ .toStrictEqual({ $ref: "#/components/schemas/X" })
410
+ })
411
+
412
+ it("does not deduplicate entries with different representations", () => {
413
+ const spec = {
414
+ openapi: "3.1.0",
415
+ info: { title: "Test", version: "1.0" },
416
+ paths: {},
417
+ components: {
418
+ schemas: {
419
+ X: { type: "string", title: "X" },
420
+ X1: { type: "number", title: "X" }
421
+ }
422
+ }
423
+ }
424
+
425
+ const result = deduplicateOpenApiSchemas(spec) as any
426
+
427
+ // Both should remain since they have different representations
428
+ expect(result.components.schemas).toStrictEqual({
429
+ X: { type: "string", title: "X" },
430
+ X1: { type: "number", title: "X" }
431
+ })
432
+ })
433
+
434
+ it("returns spec unchanged when no duplicates exist", () => {
435
+ const spec = {
436
+ openapi: "3.1.0",
437
+ info: { title: "Test", version: "1.0" },
438
+ paths: {},
439
+ components: {
440
+ schemas: {
441
+ Foo: { type: "string" },
442
+ Bar: { type: "number" }
443
+ }
444
+ }
445
+ }
446
+
447
+ const result = deduplicateOpenApiSchemas(spec)
448
+ expect(result).toStrictEqual(spec)
449
+ })
450
+
451
+ it("rewrites nested $ref pointers in allOf/anyOf/oneOf", () => {
452
+ const spec = {
453
+ openapi: "3.1.0",
454
+ info: { title: "Test", version: "1.0" },
455
+ paths: {
456
+ "/baz": {
457
+ post: {
458
+ requestBody: {
459
+ content: {
460
+ "application/json": {
461
+ schema: {
462
+ anyOf: [
463
+ { $ref: "#/components/schemas/Y1" },
464
+ { type: "null" }
465
+ ]
466
+ }
467
+ }
468
+ }
469
+ }
470
+ }
471
+ }
472
+ },
473
+ components: {
474
+ schemas: {
475
+ Y: { type: "object", properties: { name: { type: "string" } } },
476
+ Y1: { type: "object", properties: { name: { type: "string" } } }
477
+ }
478
+ }
479
+ }
480
+
481
+ const result = deduplicateOpenApiSchemas(spec) as any
482
+
483
+ expect(result.components.schemas).toStrictEqual({
484
+ Y: { type: "object", properties: { name: { type: "string" } } }
485
+ })
486
+ expect(
487
+ result.paths["/baz"].post.requestBody.content["application/json"].schema.anyOf[0]
488
+ )
489
+ .toStrictEqual({ $ref: "#/components/schemas/Y" })
490
+ })
491
+
492
+ it("rewrites $ref pointers inside definitions themselves", () => {
493
+ const spec = {
494
+ openapi: "3.1.0",
495
+ info: { title: "Test", version: "1.0" },
496
+ paths: {},
497
+ components: {
498
+ schemas: {
499
+ Inner: { type: "string" },
500
+ Inner1: { type: "string" },
501
+ Outer: {
502
+ type: "object",
503
+ properties: {
504
+ field: { $ref: "#/components/schemas/Inner1" }
505
+ }
506
+ }
507
+ }
508
+ }
509
+ }
510
+
511
+ const result = deduplicateOpenApiSchemas(spec) as any
512
+
513
+ expect(Object.keys(result.components.schemas)).toStrictEqual(["Inner", "Outer"])
514
+ expect(result.components.schemas.Outer.properties.field).toStrictEqual({
515
+ $ref: "#/components/schemas/Inner"
516
+ })
517
+ })
518
+
519
+ it("handles spec without components gracefully", () => {
520
+ const spec = { openapi: "3.1.0", info: { title: "Test", version: "1.0" }, paths: {} }
521
+ const result = deduplicateOpenApiSchemas(spec)
522
+ expect(result).toStrictEqual(spec)
523
+ })
524
+
525
+ it("flattens allOf in components.schemas", () => {
526
+ const spec = {
527
+ openapi: "3.1.0",
528
+ info: { title: "Test", version: "1.0" },
529
+ paths: {},
530
+ components: {
531
+ schemas: {
532
+ PositiveInt: {
533
+ type: "integer",
534
+ allOf: [{ exclusiveMinimum: 0, title: "PositiveInt" }]
535
+ }
536
+ }
537
+ }
538
+ }
539
+
540
+ const result = deduplicateOpenApiSchemas(spec) as any
541
+
542
+ expect(result.components.schemas.PositiveInt).toStrictEqual({
543
+ type: "integer",
544
+ exclusiveMinimum: 0,
545
+ title: "PositiveInt"
546
+ })
547
+ })
548
+
549
+ it("does not flatten allOf containing $ref entries", () => {
550
+ const spec = {
551
+ openapi: "3.1.0",
552
+ info: { title: "Test", version: "1.0" },
553
+ paths: {},
554
+ components: {
555
+ schemas: {
556
+ Composed: {
557
+ type: "object",
558
+ allOf: [{ $ref: "#/components/schemas/Base" }]
559
+ },
560
+ Base: { type: "object", properties: { id: { type: "string" } } }
561
+ }
562
+ }
563
+ }
564
+
565
+ const result = deduplicateOpenApiSchemas(spec) as any
566
+
567
+ expect(result.components.schemas.Composed).toStrictEqual({
568
+ type: "object",
569
+ allOf: [{ $ref: "#/components/schemas/Base" }]
570
+ })
571
+ })
572
+
573
+ it("does not flatten allOf entries that define their own type", () => {
574
+ const spec = {
575
+ openapi: "3.1.0",
576
+ info: { title: "Test", version: "1.0" },
577
+ paths: {},
578
+ components: {
579
+ schemas: {
580
+ Mixed: {
581
+ type: "object",
582
+ allOf: [{ type: "string", minLength: 1 }]
583
+ }
584
+ }
585
+ }
586
+ }
587
+
588
+ const result = deduplicateOpenApiSchemas(spec) as any
589
+
590
+ expect(result.components.schemas.Mixed).toStrictEqual({
591
+ type: "object",
592
+ allOf: [{ type: "string", minLength: 1 }]
593
+ })
594
+ })
595
+ })
596
+
597
+ describe("flattenSimpleAllOf", () => {
598
+ it("flattens constraint-only allOf into parent with type", () => {
599
+ const input = {
600
+ type: "integer",
601
+ allOf: [{ exclusiveMinimum: 0, title: "PositiveInt" }]
602
+ }
603
+ expect(flattenSimpleAllOf(input)).toStrictEqual({
604
+ type: "integer",
605
+ exclusiveMinimum: 0,
606
+ title: "PositiveInt"
607
+ })
608
+ })
609
+
610
+ it("flattens string type with multiple constraints", () => {
611
+ const input = {
612
+ type: "string",
613
+ allOf: [
614
+ { minLength: 1, maxLength: 255 },
615
+ { title: "NonEmptyString255" }
616
+ ]
617
+ }
618
+ expect(flattenSimpleAllOf(input)).toStrictEqual({
619
+ type: "string",
620
+ minLength: 1,
621
+ maxLength: 255,
622
+ title: "NonEmptyString255"
623
+ })
624
+ })
625
+
626
+ it("does not flatten allOf with $ref", () => {
627
+ const input = {
628
+ type: "object",
629
+ allOf: [{ $ref: "#/components/schemas/Base" }]
630
+ }
631
+ expect(flattenSimpleAllOf(input)).toStrictEqual(input)
632
+ })
633
+
634
+ it("does not flatten allOf entries with their own type", () => {
635
+ const input = {
636
+ type: "object",
637
+ allOf: [{ type: "string", minLength: 1 }]
638
+ }
639
+ expect(flattenSimpleAllOf(input)).toStrictEqual(input)
640
+ })
641
+
642
+ it("allOf entry wins on property conflict", () => {
643
+ const input = {
644
+ type: "integer",
645
+ title: "OldTitle",
646
+ allOf: [{ title: "NewTitle", minimum: 0 }]
647
+ }
648
+ expect(flattenSimpleAllOf(input)).toStrictEqual({
649
+ type: "integer",
650
+ title: "NewTitle",
651
+ minimum: 0
652
+ })
653
+ })
654
+ })
655
+
656
+ describe("Post-processing integration — real Effect Schema types", () => {
657
+ it("PositiveInt — allOf flattened, no wrapping", () => {
658
+ const doc = specialJsonSchemaDocument(AppSchema.PositiveInt)
659
+ expect(doc.definitions["PositiveInt"]).toStrictEqual({
660
+ type: "integer",
661
+ exclusiveMinimum: 0,
662
+ title: "PositiveInt"
663
+ })
664
+ })
665
+
666
+ it("NonEmptyString255 — multiple allOf constraints merged", () => {
667
+ const doc = specialJsonSchemaDocument(AppSchema.NonEmptyString255)
668
+ expect(doc.definitions["NonEmptyString255"]).toStrictEqual({
669
+ type: "string",
670
+ minLength: 1,
671
+ maxLength: 255,
672
+ title: "NonEmptyString255"
673
+ })
674
+ })
675
+
676
+ it("NullOr(NonEmptyString64k) — null preserved in anyOf, allOf flattened in definition", () => {
677
+ const schema = S.Struct({ note: S.NullOr(AppSchema.NonEmptyString64k) })
678
+ const doc = specialJsonSchemaDocument(schema)
679
+
680
+ // null variant preserved (correct JSON Schema for NullOr)
681
+ expect(doc.schema).toStrictEqual({
682
+ type: "object",
683
+ properties: {
684
+ note: {
685
+ anyOf: [
686
+ { $ref: "#/$defs/NonEmptyString64k" },
687
+ { type: "null" }
688
+ ]
689
+ }
690
+ },
691
+ required: ["note"],
692
+ additionalProperties: false
693
+ })
694
+
695
+ // allOf flattened in the referenced definition
696
+ expect(doc.definitions["NonEmptyString64k"]).toStrictEqual({
697
+ type: "string",
698
+ minLength: 1,
699
+ maxLength: 65536,
700
+ title: "NonEmptyString64k"
701
+ })
702
+ })
703
+
704
+ it("NonNegativeInt — allOf flattened", () => {
705
+ const doc = specialJsonSchemaDocument(AppSchema.NonNegativeInt)
706
+ expect(doc.definitions["NonNegativeInt"]).toStrictEqual({
707
+ type: "integer",
708
+ minimum: 0,
709
+ title: "NonNegativeInt"
710
+ })
711
+ })
712
+
713
+ it("NullOr union preserves null and non-null members in anyOf", () => {
714
+ const A = S.String.annotate({ identifier: "A", title: "A" })
715
+ const B = S.Boolean.annotate({ identifier: "B", title: "B" })
716
+ const schema = S.Struct({
717
+ value: S.NullOr(S.Union([A, B]))
718
+ })
719
+ const doc = specialJsonSchemaDocument(schema)
720
+ const valueProp = (doc.schema as Record<string, any>)["properties"]["value"]
721
+
722
+ expect(valueProp).toStrictEqual({
723
+ anyOf: [
724
+ { anyOf: [{ $ref: "#/$defs/A" }, { $ref: "#/$defs/B" }] },
725
+ { type: "null" }
726
+ ]
727
+ })
728
+ })
729
+ })