effect-app 4.0.0-beta.13 → 4.0.0-beta.131

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