effect-app 4.0.0-beta.7 → 4.0.0-beta.71

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 +313 -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 +61 -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 +39 -1
  24. package/dist/Schema/Class.d.ts.map +1 -1
  25. package/dist/Schema/Class.js +89 -12
  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 +81 -45
  38. package/dist/Schema/ext.d.ts.map +1 -1
  39. package/dist/Schema/ext.js +94 -49
  40. package/dist/Schema/moreStrings.d.ts +6 -6
  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 +3 -3
  54. package/dist/client/apiClientFactory.d.ts.map +1 -1
  55. package/dist/client/apiClientFactory.js +14 -15
  56. package/dist/client/errors.d.ts +17 -9
  57. package/dist/client/errors.d.ts.map +1 -1
  58. package/dist/client/errors.js +35 -10
  59. package/dist/client/makeClient.d.ts +13 -12
  60. package/dist/client/makeClient.d.ts.map +1 -1
  61. package/dist/client/makeClient.js +5 -2
  62. package/dist/http/Request.d.ts.map +1 -1
  63. package/dist/http/Request.js +5 -5
  64. package/dist/ids.d.ts +3 -3
  65. package/dist/ids.d.ts.map +1 -1
  66. package/dist/ids.js +3 -2
  67. package/dist/index.d.ts +3 -8
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +4 -9
  70. package/dist/middleware.d.ts +2 -2
  71. package/dist/middleware.d.ts.map +1 -1
  72. package/dist/middleware.js +3 -3
  73. package/dist/rpc/MiddlewareMaker.d.ts +4 -3
  74. package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
  75. package/dist/rpc/MiddlewareMaker.js +7 -6
  76. package/dist/rpc/RpcContextMap.d.ts +2 -2
  77. package/dist/rpc/RpcContextMap.d.ts.map +1 -1
  78. package/dist/rpc/RpcContextMap.js +4 -4
  79. package/dist/rpc/RpcMiddleware.d.ts +4 -3
  80. package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
  81. package/dist/rpc/RpcMiddleware.js +1 -1
  82. package/dist/utils/gen.d.ts +1 -1
  83. package/dist/utils/gen.d.ts.map +1 -1
  84. package/dist/utils/logger.d.ts +2 -2
  85. package/dist/utils/logger.d.ts.map +1 -1
  86. package/dist/utils/logger.js +3 -3
  87. package/dist/utils.d.ts +18 -0
  88. package/dist/utils.d.ts.map +1 -1
  89. package/dist/utils.js +24 -5
  90. package/package.json +29 -17
  91. package/src/Config/SecretURL.ts +1 -1
  92. package/src/Config.ts +14 -0
  93. package/src/ConfigProvider.ts +48 -0
  94. package/src/{ServiceMap.ts → Context.ts} +57 -64
  95. package/src/Effect.ts +11 -9
  96. package/src/Layer.ts +5 -4
  97. package/src/Pure.ts +17 -18
  98. package/src/Schema/Class.ts +114 -16
  99. package/src/Schema/SpecialJsonSchema.ts +69 -0
  100. package/src/Schema/SpecialOpenApi.ts +130 -0
  101. package/src/Schema/brand.ts +13 -7
  102. package/src/Schema/email.ts +10 -2
  103. package/src/Schema/ext.ts +182 -80
  104. package/src/Schema/moreStrings.ts +20 -10
  105. package/src/Schema/numbers.ts +9 -8
  106. package/src/Schema/phoneNumber.ts +8 -1
  107. package/src/Schema.ts +79 -103
  108. package/src/client/apiClientFactory.ts +18 -18
  109. package/src/client/errors.ts +46 -12
  110. package/src/client/makeClient.ts +32 -12
  111. package/src/http/Request.ts +7 -4
  112. package/src/ids.ts +3 -2
  113. package/src/index.ts +3 -11
  114. package/src/middleware.ts +2 -2
  115. package/src/rpc/MiddlewareMaker.ts +8 -7
  116. package/src/rpc/RpcContextMap.ts +6 -5
  117. package/src/rpc/RpcMiddleware.ts +5 -4
  118. package/src/utils/gen.ts +1 -1
  119. package/src/utils/logger.ts +2 -2
  120. package/src/utils.ts +26 -4
  121. package/test/dist/moreStrings.test.d.ts.map +1 -0
  122. package/test/dist/rpc.test.d.ts.map +1 -1
  123. package/test/dist/secretURL.test.d.ts.map +1 -0
  124. package/test/dist/special.test.d.ts.map +1 -0
  125. package/test/moreStrings.test.ts +17 -0
  126. package/test/rpc.test.ts +26 -5
  127. package/test/schema.test.ts +396 -3
  128. package/test/secretURL.test.ts +157 -0
  129. package/test/special.test.ts +732 -0
  130. package/test/utils.test.ts +1 -1
  131. package/tsconfig.base.json +0 -1
  132. package/tsconfig.json +0 -1
  133. package/dist/ServiceMap.d.ts +0 -44
  134. package/dist/ServiceMap.d.ts.map +0 -1
  135. package/dist/ServiceMap.js +0 -91
  136. package/dist/Struct.d.ts +0 -44
  137. package/dist/Struct.d.ts.map +0 -1
  138. package/dist/Struct.js +0 -29
  139. package/src/Struct.ts +0 -54
package/src/Pure.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { Chunk, Effect, Layer, Result } from "effect"
3
+ import * as Context from "./Context.js"
3
4
  import { tuple } from "./Function.js"
4
- import * as ServiceMap from "./ServiceMap.js"
5
5
 
6
6
  const S1 = Symbol()
7
7
  const S2 = Symbol()
@@ -86,9 +86,9 @@ export function GMU<W, S, S2, GA, MR, ME>(modify: (i: GA) => Pure<W, S, S2, MR,
86
86
  ) => GMU_(get, modify, update)
87
87
  }
88
88
 
89
- const tagg = ServiceMap.Service<{ env: PureEnv<never, unknown, never> }>("PureEnv")
89
+ const tagg = Context.Service<{ env: PureEnv<never, unknown, never> }>("PureEnv")
90
90
  function castTag<W, S, S2>() {
91
- return tagg as any as ServiceMap.Service<PureEnvEnv<W, S, S2>, PureEnvEnv<W, S, S2>>
91
+ return tagg as any as Context.Service<PureEnvEnv<W, S, S2>, PureEnvEnv<W, S, S2>>
92
92
  }
93
93
 
94
94
  export const ServiceTag = Symbol()
@@ -100,9 +100,9 @@ export abstract class PhantomTypeParameter<Identifier extends keyof any, Instant
100
100
  }
101
101
  }
102
102
 
103
- export type ServiceShape<T extends ServiceMap.ServiceClass.Shape<any, any>> = Omit<
103
+ export type ServiceShape<T extends Context.ServiceClass.Shape<any, any>> = Omit<
104
104
  T,
105
- keyof ServiceMap.ServiceClass.Shape<any, any>
105
+ keyof Context.ServiceClass.Shape<any, any>
106
106
  >
107
107
 
108
108
  export abstract class ServiceTagged<ServiceKey> extends PhantomTypeParameter<string, ServiceKey> {}
@@ -117,11 +117,11 @@ export interface PureEnvEnv<W, S, S2> extends ServiceTagged<typeof PureEnvEnv> {
117
117
  }
118
118
 
119
119
  export function get<S>(): Pure<never, S, S, never, never, S> {
120
- return (castTag<never, S, S>() as any).use((_: any) => _.env.state)
120
+ return (castTag<never, S, S>()).useSync((_) => _.env.state)
121
121
  }
122
122
 
123
123
  export function set<S>(s: S): Pure<never, S, S, never, never, void> {
124
- return (castTag<never, S, S>() as any).use((_: any) => {
124
+ return (castTag<never, S, S>()).useSync((_) => {
125
125
  _.env.state = s
126
126
  })
127
127
  }
@@ -129,13 +129,13 @@ export function set<S>(s: S): Pure<never, S, S, never, never, void> {
129
129
  export type PureLogT<W> = Pure<W, unknown, never, never, never, void>
130
130
 
131
131
  export function log<W>(w: W): PureLogT<W> {
132
- return (castTag<W, unknown, never>() as any).use((_: any) => {
132
+ return (castTag<W, unknown, never>()).useSync((_) => {
133
133
  _.env.log = Chunk.append(_.env.log, w)
134
134
  })
135
135
  }
136
136
 
137
137
  export function logMany<W>(w: Iterable<W>): PureLogT<W> {
138
- return (castTag<W, unknown, never>() as any).use((_: any) => {
138
+ return (castTag<W, unknown, never>()).useSync((_) => {
139
139
  _.env.log = Chunk.appendAll(_.env.log, Chunk.fromIterable(w))
140
140
  })
141
141
  }
@@ -150,17 +150,16 @@ export function runAll<R, E, A, W3, S1, S3, S4 extends S1>(
150
150
  > {
151
151
  const a = Effect
152
152
  .flatMap(self, (x) =>
153
- (castTag<W3, S1, S3>() as any)
154
- .use(
155
- ({ env: _ }: any) => Effect.sync(() => ({ log: _.log, state: _.state }))
153
+ (castTag<W3, S1, S3>())
154
+ .useSync(
155
+ ({ env: _ }) => ({ log: _.log, state: _.state })
156
156
  )
157
157
  .pipe(
158
- Effect.flatMap((_: any) => Effect.succeed(_)),
159
158
  Effect.map(
160
- ({ log, state }: any) => tuple(log, Result.succeed(tuple(state, x)))
159
+ ({ log, state }) => tuple(log, Result.succeed(tuple(state, x)))
161
160
  )
162
161
  ))
163
- .pipe(Effect.catch((err: any) => (tagg as any).use((env: any) => tuple(env.env.log, Result.fail(err)))))
162
+ .pipe(Effect.catch((err: any) => tagg.useSync((env) => tuple(env.env.log, Result.fail(err)))))
164
163
  return Effect.provide(a, Layer.succeed(tagg, { env: makePureEnv<W3, S3, S4>(s) as any }) as any) as any
165
164
  }
166
165
 
@@ -198,7 +197,7 @@ export function runA<R, E, A, W3, S1, S3, S4 extends S1>(
198
197
  export function modify<S2, A, S3>(
199
198
  mod: (s: S2) => readonly [S3, A]
200
199
  ): Effect.Effect<A, never, { env: PureEnv<never, S2, S3> }> {
201
- return (castTag<never, S3, S2>() as any).use((_: any) => {
200
+ return (castTag<never, S3, S2>()).useSync((_) => {
202
201
  const [s, a] = mod(_.env.state)
203
202
  _.env.state = s as any
204
203
  return a
@@ -210,10 +209,10 @@ export function modifyM<W, R, E, A, S2, S3>(
210
209
  ): Effect.Effect<A, E, FixEnv<R, W, S2, S3>> {
211
210
  // return serviceWithEffect(_ => Ref.modifyM_(_.state, mod))
212
211
  return Effect.flatMap(
213
- (castTag<W, S3, S2>() as any).use((_: any) => _),
212
+ (castTag<W, S3, S2>()).useSync((_) => _),
214
213
  (_: any) =>
215
214
  Effect.map(mod(_.env.state), ([s, a]: any) => {
216
- _.env.state = s as any
215
+ _.env.state = s
217
216
  return a
218
217
  })
219
218
  ) as any
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { pipe, Struct as Struct2 } from "effect"
2
+ import { Effect, Option, pipe, Schema, SchemaAST, SchemaIssue, Struct as Struct2 } from "effect"
3
3
  import type { Struct } from "effect/Schema"
4
4
  import * as S from "effect/Schema"
5
5
 
@@ -13,9 +13,6 @@ type MissingSelfGeneric<Usage extends string, Params extends string = ""> =
13
13
  `Missing \`Self\` generic - use \`class Self extends ${Usage}<Self>()(${Params}{ ... })\``
14
14
 
15
15
  export interface PropsExtensions<Fields> {
16
- // include: <NewProps extends S.Struct.Fields>(
17
- // fnc: (fields: Fields) => NewProps
18
- // ) => NewProps
19
16
  pick: <P extends keyof Fields>(...keys: readonly P[]) => Pick<Fields, P>
20
17
  omit: <P extends keyof Fields>(...keys: readonly P[]) => Omit<Fields, P>
21
18
  }
@@ -26,6 +23,57 @@ type HasFields<Fields extends Struct.Fields> = {
26
23
  readonly from: HasFields<Fields>
27
24
  }
28
25
 
26
+ export type Class<Self, S extends S.Top & { readonly fields: Struct.Fields }, Inherited> = S.Class<Self, S, Inherited>
27
+
28
+ /**
29
+ * Build a modified Declaration that accepts struct-matching values during
30
+ * encoding, given the original Declaration and the class's fields.
31
+ */
32
+ function makeRelaxedDeclaration(
33
+ ast: SchemaAST.Declaration,
34
+ fields: Schema.Struct.Fields,
35
+ cls: any
36
+ ): SchemaAST.Declaration {
37
+ const structSchema = Schema.Struct(fields)
38
+ const isStructValue = Schema.is(structSchema)
39
+ return new SchemaAST.Declaration(
40
+ ast.typeParameters,
41
+ () => (input: unknown, self: SchemaAST.Declaration) => {
42
+ if (input instanceof cls || isStructValue(input)) {
43
+ return Effect.succeed(input)
44
+ }
45
+ return Effect.fail(new SchemaIssue.InvalidType(self, Option.some(input)))
46
+ },
47
+ ast.annotations,
48
+ ast.checks,
49
+ ast.encoding,
50
+ ast.context
51
+ )
52
+ }
53
+
54
+ // ---------------------------------------------------------------------------
55
+ // Class — like Schema.Class but with relaxed encoding
56
+ // ---------------------------------------------------------------------------
57
+
58
+ /**
59
+ * Like `Schema.Class`, but the resulting class accepts plain objects matching
60
+ * the struct schema during encoding — not only `instanceof` or type-id
61
+ * checks.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * import { Schema } from "effect"
66
+ * import { Class } from "./Class.js"
67
+ *
68
+ * class A extends Class<A>("A")({ a: Schema.String }) {}
69
+ *
70
+ * // Construction works as normal:
71
+ * new A({ a: "hello" })
72
+ *
73
+ * // Encoding accepts plain objects:
74
+ * Schema.encodeUnknownSync(A)({ a: "hello" }) // { a: "hello" }
75
+ * ```
76
+ */
29
77
  export const Class: <Self = never>(identifier: string) => <Fields extends S.Struct.Fields>(
30
78
  fieldsOr: Fields | HasFields<Fields>,
31
79
  annotations?: ClassAnnotations<Self>
@@ -35,38 +83,84 @@ export const Class: <Self = never>(identifier: string) => <Fields extends S.Stru
35
83
  S.Struct<Fields>,
36
84
  {}
37
85
  > = (identifier) => (fields, annotations) => {
38
- const cls = S.Class as any
39
- return class extends cls(identifier)(fields, annotations) {
40
- constructor(a: any, b = true) {
41
- super(a, b)
86
+ // Build the original Schema.Class
87
+ const Base = (S.Class as any)(identifier)(fields, annotations)
88
+ // Get the original ast getter from the base class
89
+ const originalAstDescriptor = Object.getOwnPropertyDescriptor(Base, "ast")!
90
+
91
+ // Cache per-class to avoid recomputing
92
+ const astCache = new WeakMap<any, SchemaAST.Declaration>()
93
+
94
+ return class extends Base {
95
+ static get ast(): SchemaAST.Declaration {
96
+ let cached = astCache.get(this)
97
+ if (cached !== undefined) return cached
98
+ // Call the original getter with `this` bound to the actual user class,
99
+ // so getClassSchema(this) creates a schema that uses `new this(...)`.
100
+ const originalAst = originalAstDescriptor.get!.call(this) as SchemaAST.Declaration
101
+ cached = makeRelaxedDeclaration(originalAst, Base.fields, this)
102
+ astCache.set(this, cached)
103
+ return cached
42
104
  }
43
- // static readonly include = include(fields)
44
105
  static readonly pick = (...selection: any[]) => pipe(this["fields"], Struct2.pick(selection))
45
106
  static readonly omit = (...selection: any[]) => pipe(this["fields"], Struct2.omit(selection))
46
107
  } as any
47
108
  }
48
109
 
49
- export const TaggedClass: <Self = never>(identifier?: string) => <Tag extends string, Fields extends S.Struct.Fields>(
110
+ // ---------------------------------------------------------------------------
111
+ // TaggedClass — like Schema.TaggedClass but with relaxed encoding
112
+ // ---------------------------------------------------------------------------
113
+
114
+ /**
115
+ * Like `Schema.TaggedClass`, but the resulting class accepts plain objects
116
+ * matching the struct schema during encoding.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * import { Schema } from "effect"
121
+ * import { TaggedClass } from "./Class.js"
122
+ *
123
+ * class Circle extends TaggedClass<Circle>()("Circle", {
124
+ * radius: Schema.Number
125
+ * }) {}
126
+ *
127
+ * Schema.encodeUnknownSync(Circle)({ _tag: "Circle", radius: 5 })
128
+ * ```
129
+ */
130
+ export const TaggedClass: <Self = never>(
131
+ identifier?: string
132
+ ) => <Tag extends string, Fields extends S.Struct.Fields>(
50
133
  tag: Tag,
51
134
  fieldsOr: Fields | HasFields<Fields>,
52
135
  annotations?: ClassAnnotations<Self>
53
- ) => [Self] extends [never] ? MissingSelfGeneric<"Class">
136
+ ) => [Self] extends [never] ? MissingSelfGeneric<"TaggedClass">
54
137
  : EnhancedClass<
55
138
  Self,
56
139
  S.Struct<{ readonly _tag: S.tag<Tag> } & Fields>,
57
140
  {}
58
141
  > = (identifier) => (tag, fields, annotations) => {
59
- const cls = S.TaggedClass as any
60
- return class extends cls(identifier)(tag, fields, annotations) {
61
- constructor(a: any, b = true) {
62
- super(a, b)
142
+ const Base = (S.TaggedClass as any)(identifier)(tag, fields, annotations)
143
+ const originalAstDescriptor = Object.getOwnPropertyDescriptor(Base, "ast")!
144
+ const astCache = new WeakMap<any, SchemaAST.Declaration>()
145
+
146
+ return class extends Base {
147
+ static get ast(): SchemaAST.Declaration {
148
+ let cached = astCache.get(this)
149
+ if (cached !== undefined) return cached
150
+ const originalAst = originalAstDescriptor.get!.call(this) as SchemaAST.Declaration
151
+ cached = makeRelaxedDeclaration(originalAst, Base.fields, this)
152
+ astCache.set(this, cached)
153
+ return cached
63
154
  }
64
- // static readonly include = include(fields)
65
155
  static readonly pick = (...selection: any[]) => pipe(this["fields"], Struct2.pick(selection))
66
156
  static readonly omit = (...selection: any[]) => pipe(this["fields"], Struct2.omit(selection))
67
157
  } as any
68
158
  }
69
159
 
160
+ // ---------------------------------------------------------------------------
161
+ // ExtendedClass — like Class but with extra type parameter for hierarchies
162
+ // ---------------------------------------------------------------------------
163
+
70
164
  export const ExtendedClass: <Self, _SelfFrom>(identifier: string) => <Fields extends S.Struct.Fields>(
71
165
  fieldsOr: Fields | HasFields<Fields>,
72
166
  annotations?: ClassAnnotations<Self>
@@ -76,6 +170,10 @@ export const ExtendedClass: <Self, _SelfFrom>(identifier: string) => <Fields ext
76
170
  {}
77
171
  > = Class as any
78
172
 
173
+ // ---------------------------------------------------------------------------
174
+ // ExtendedTaggedClass — like TaggedClass but with extra type parameter for hierarchies
175
+ // ---------------------------------------------------------------------------
176
+
79
177
  export interface EnhancedTaggedClass<Self, Tag extends string, Fields extends Struct.Fields, SelfFrom>
80
178
  extends
81
179
  EnhancedClass<
@@ -0,0 +1,69 @@
1
+ /**
2
+ * SpecialJsonSchema — A variant of Schema.toJsonSchemaDocument that
3
+ * post-processes the output (e.g. flattens simple allOf).
4
+ */
5
+ import { type JsonSchema, type Schema, SchemaRepresentation } from "effect"
6
+
7
+ /**
8
+ * Converts a schema to a JSON Schema Document (draft-2020-12), with
9
+ * post-processing that flattens simple allOf entries.
10
+ */
11
+ export function specialJsonSchemaDocument(
12
+ schema: Schema.Top,
13
+ options?: Schema.ToJsonSchemaOptions
14
+ ): JsonSchema.Document<"draft-2020-12"> {
15
+ const doc = SchemaRepresentation.fromAST(schema.ast)
16
+ const jd = SchemaRepresentation.toJsonSchemaDocument(doc, options)
17
+ const processedDefs: JsonSchema.Definitions = {}
18
+ for (const [key, def] of Object.entries(jd.definitions)) {
19
+ processedDefs[key] = postProcessJsonSchema(def)
20
+ }
21
+ return {
22
+ dialect: "draft-2020-12",
23
+ schema: postProcessJsonSchema(jd.schema),
24
+ definitions: processedDefs
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Flattens `allOf` entries into the parent when the parent already has a
30
+ * `type` and every `allOf` entry is a plain constraint object (no `$ref`,
31
+ * no `type`). Merged properties from `allOf` entries win on conflict.
32
+ */
33
+ export function flattenSimpleAllOf(obj: unknown): unknown {
34
+ if (obj === null || typeof obj !== "object") return obj
35
+
36
+ if (globalThis.Array.isArray(obj)) {
37
+ return obj.map(flattenSimpleAllOf)
38
+ }
39
+
40
+ const record = obj as Record<string, unknown>
41
+ const result: Record<string, unknown> = {}
42
+ for (const [key, value] of Object.entries(record)) {
43
+ result[key] = flattenSimpleAllOf(value)
44
+ }
45
+
46
+ if (result["type"] && globalThis.Array.isArray(result["allOf"])) {
47
+ const allOf = result["allOf"] as Array<Record<string, unknown>>
48
+ const canFlatten = allOf.every((entry) =>
49
+ typeof entry === "object" && entry !== null && !("$ref" in entry) && !("type" in entry)
50
+ )
51
+ if (canFlatten) {
52
+ const { allOf: _, ...rest } = result
53
+ let merged: Record<string, unknown> = { ...rest }
54
+ for (const entry of allOf) {
55
+ merged = { ...merged, ...entry }
56
+ }
57
+ return merged
58
+ }
59
+ }
60
+
61
+ return result
62
+ }
63
+
64
+ /**
65
+ * Applies JSON Schema post-processing: flattens simple allOf.
66
+ */
67
+ export function postProcessJsonSchema(obj: JsonSchema.JsonSchema): JsonSchema.JsonSchema {
68
+ return flattenSimpleAllOf(obj) as JsonSchema.JsonSchema
69
+ }
@@ -0,0 +1,130 @@
1
+ /**
2
+ * SpecialOpenApi — Deduplicates `components/schemas` entries in an OpenAPI spec
3
+ * and applies JSON Schema post-processing (null removal, allOf flattening).
4
+ *
5
+ * When `OpenApi.fromApi` generates the spec, different AST nodes sharing the
6
+ * same identifier can produce duplicate entries (e.g. "X" and "X1") in
7
+ * `components.schemas`. This module provides a transform function that
8
+ * collapses those duplicates, rewrites all `$ref` pointers accordingly,
9
+ * and post-processes schemas for better codegen compatibility.
10
+ *
11
+ * Usage with the OpenApi `Transform` annotation:
12
+ *
13
+ * ```ts
14
+ * import { OpenApi } from "effect/unstable"
15
+ * import { deduplicateOpenApiSchemas } from "./SpecialOpenApi.js"
16
+ *
17
+ * const api = HttpApi.make("myApi")
18
+ * .pipe(HttpApi.annotateContext(OpenApi.annotations({ transform: deduplicateOpenApiSchemas })))
19
+ * ```
20
+ */
21
+
22
+ import { postProcessJsonSchema } from "./SpecialJsonSchema.js"
23
+
24
+ /**
25
+ * Deduplicates `components.schemas` entries in an OpenAPI spec.
26
+ *
27
+ * Entries sharing the same base identifier (e.g. "X" and "X1") whose JSON
28
+ * representations are identical are collapsed into a single canonical entry,
29
+ * and all `$ref` pointers throughout the spec are rewritten to point to
30
+ * the canonical key.
31
+ *
32
+ * Designed to be used as the `transform` option in `OpenApi.annotations`.
33
+ */
34
+ export function deduplicateOpenApiSchemas(
35
+ spec: Record<string, any>
36
+ ): Record<string, any> {
37
+ const components = spec["components"] as Record<string, any> | undefined
38
+ if (!components) return spec
39
+ const schemas = components["schemas"] as Record<string, any> | undefined
40
+ if (!schemas) return spec
41
+
42
+ const keys = Object.keys(schemas)
43
+ if (keys.length === 0) return spec
44
+
45
+ // Group keys by base identifier (strip trailing digits)
46
+ const groups = new Map<string, Array<{ key: string; fingerprint: string }>>()
47
+ for (const key of keys) {
48
+ const base = getBaseIdentifier(key)
49
+ const fingerprint = JSON.stringify(schemas[key])
50
+ const group = groups.get(base)
51
+ if (group === undefined) {
52
+ groups.set(base, [{ key, fingerprint }])
53
+ } else {
54
+ group.push({ key, fingerprint })
55
+ }
56
+ }
57
+
58
+ // Build remapping from duplicate keys to canonical keys
59
+ const remapping = new Map<string, string>()
60
+ for (const [, group] of groups) {
61
+ if (group.length <= 1) continue
62
+ const seen = new Map<string, string>() // fingerprint -> canonical key
63
+ for (const entry of group) {
64
+ const canonical = seen.get(entry.fingerprint)
65
+ if (canonical !== undefined) {
66
+ remapping.set(entry.key, canonical)
67
+ } else {
68
+ seen.set(entry.fingerprint, entry.key)
69
+ }
70
+ }
71
+ }
72
+
73
+ if (remapping.size === 0) return postProcessJsonSchema(spec) as Record<string, any>
74
+
75
+ // Build new schemas object without duplicates
76
+ const newSchemas: Record<string, any> = {}
77
+ for (const key of keys) {
78
+ if (!remapping.has(key)) {
79
+ newSchemas[key] = schemas[key]
80
+ }
81
+ }
82
+
83
+ // Deep clone the spec, replace schemas, and rewrite all $ref pointers
84
+ const newSpec = structuredClone(spec)
85
+ newSpec["components"]["schemas"] = newSchemas
86
+ rewriteRefs(newSpec, remapping)
87
+
88
+ return postProcessJsonSchema(newSpec) as Record<string, any>
89
+ }
90
+
91
+ /**
92
+ * Extracts the base identifier from a schema key by stripping trailing
93
+ * digits appended by the gen() function.
94
+ * E.g. "X1" -> "X", "X" -> "X", "MyType2" -> "MyType"
95
+ */
96
+ function getBaseIdentifier(key: string): string {
97
+ const match = key.match(/^(.+?)(\d+)$/)
98
+ return match ? match[1]! : key
99
+ }
100
+
101
+ /**
102
+ * Recursively rewrites `$ref` values in a JSON object tree.
103
+ * Mutates the object in-place (caller should pass a deep clone).
104
+ */
105
+ function rewriteRefs(obj: any, remapping: Map<string, string>): void {
106
+ if (obj === null || typeof obj !== "object") return
107
+
108
+ if (Array.isArray(obj)) {
109
+ for (const item of obj) {
110
+ rewriteRefs(item, remapping)
111
+ }
112
+ return
113
+ }
114
+
115
+ if (typeof obj.$ref === "string") {
116
+ // OpenAPI refs look like "#/components/schemas/X1"
117
+ const prefix = "#/components/schemas/"
118
+ if (obj.$ref.startsWith(prefix)) {
119
+ const refKey = obj.$ref.slice(prefix.length)
120
+ const canonical = remapping.get(refKey)
121
+ if (canonical !== undefined) {
122
+ obj.$ref = prefix + canonical
123
+ }
124
+ }
125
+ }
126
+
127
+ for (const value of Object.values(obj)) {
128
+ rewriteRefs(value, remapping)
129
+ }
130
+ }
@@ -2,7 +2,6 @@
2
2
  /* eslint-disable @typescript-eslint/no-unsafe-return */
3
3
  import type { Option } from "effect"
4
4
  import * as B from "effect/Brand"
5
- import type * as Brand from "effect/Brand"
6
5
  import type * as Result from "effect/Result"
7
6
  import * as S from "effect/Schema"
8
7
 
@@ -21,7 +20,7 @@ export interface Constructor<in out A extends B.Brand<any>> {
21
20
  * Constructs a branded type from a value of type `A`, returning `Result.succeed`
22
21
  * if the provided `A` is valid, `Result.fail` otherwise.
23
22
  */
24
- result(args: Unbranded<A>): Result.Result<A, Brand.BrandError>
23
+ result(args: Unbranded<A>): Result.Result<A, B.BrandError>
25
24
  /**
26
25
  * Attempts to refine the provided value of type `A`, returning `true` if
27
26
  * the provided `A` is valid, `false` otherwise.
@@ -29,19 +28,26 @@ export interface Constructor<in out A extends B.Brand<any>> {
29
28
  is(a: Unbranded<A>): a is Unbranded<A> & A
30
29
  }
31
30
 
32
- export const fromBrand = <C extends Brand.Brand<string>>(
31
+ type BrandAnnotations<C extends B.Brand<any>> =
32
+ & S.Annotations.Filter
33
+ & (
34
+ C extends string ? { readonly toArbitrary?: S.Annotations.ToArbitrary.Declaration<C, readonly []> }
35
+ : {}
36
+ )
37
+
38
+ export const fromBrand = <C extends B.Brand<any>>(
33
39
  constructor: Constructor<C>,
34
- options?: S.Annotations.Filter
40
+ options?: BrandAnnotations<C>
35
41
  ) =>
36
- <Self extends S.Top>(self: Self): S.brand<Self["~rebuild.out"], Brand.Brand.Keys<C>> => {
42
+ <Self extends S.Top>(self: Self): S.brand<Self["~rebuild.out"], B.Brand.Keys<C>> => {
37
43
  const branded = S.fromBrand(options?.identifier ?? "Brand", constructor as any)(self as any)
38
44
  return options ? (branded as any).pipe(S.annotate(options)) : branded as any
39
45
  }
40
46
 
41
- export type Brands<P> = P extends B.Brand<any> ? Brand.Brand.Brands<P>
47
+ export type Brands<P> = P extends B.Brand<any> ? B.Brand.Brands<P>
42
48
  : never
43
49
 
44
- export type Unbranded<P> = P extends B.Brand<any> ? Brand.Brand.Unbranded<P> : P
50
+ export type Unbranded<P> = P extends B.Brand<any> ? B.Brand.Unbranded<P> : P
45
51
 
46
52
  export const nominal: <A extends B.Brand<any>>() => Constructor<A> = <
47
53
  A extends B.Brand<any>
@@ -12,11 +12,19 @@ export type Email = string & EmailBrand
12
12
  export const Email = S
13
13
  .String
14
14
  .pipe(
15
+ S.annotate({
16
+ title: "Email",
17
+ description: "an email according to RFC 5322",
18
+ format: "email"
19
+ }),
20
+ S.check(S.isMinLength(3), /* a@b */ S.isMaxLength(998)),
15
21
  S.refine(isValidEmail as Refinement<string, Email>, {
16
22
  identifier: "Email",
17
23
  title: "Email",
18
24
  description: "an email according to RFC 5322",
19
- jsonSchema: { format: "email", minLength: 3, /* a@b */ maxLength: 998 },
20
- arbitrary: () => (fc: any) => fc.emailAddress().map((_: any) => _ as Email)
25
+ jsonSchema: { format: "email", minLength: 3, maxLength: 998 }
26
+ }),
27
+ S.annotate({
28
+ toArbitrary: () => (fc) => fc.emailAddress().map((_) => _ as Email)
21
29
  })
22
30
  )