effect-app 4.0.0-beta.10 → 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.
- package/CHANGELOG.md +390 -0
- package/dist/Config/SecretURL.js +2 -2
- package/dist/Config.d.ts +7 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +6 -0
- package/dist/ConfigProvider.d.ts +39 -0
- package/dist/ConfigProvider.d.ts.map +1 -0
- package/dist/ConfigProvider.js +42 -0
- package/dist/{ServiceMap.d.ts → Context.d.ts} +14 -18
- package/dist/Context.d.ts.map +1 -0
- package/dist/Context.js +66 -0
- package/dist/Effect.d.ts +8 -7
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js +3 -2
- package/dist/Layer.d.ts +5 -4
- package/dist/Layer.d.ts.map +1 -1
- package/dist/Layer.js +1 -1
- package/dist/Operations.d.ts +62 -25
- package/dist/Operations.d.ts.map +1 -1
- package/dist/Pure.d.ts +2 -2
- package/dist/Pure.d.ts.map +1 -1
- package/dist/Pure.js +13 -13
- package/dist/Schema/Class.d.ts +43 -10
- package/dist/Schema/Class.d.ts.map +1 -1
- package/dist/Schema/Class.js +89 -16
- package/dist/Schema/SpecialJsonSchema.d.ts +21 -0
- package/dist/Schema/SpecialJsonSchema.d.ts.map +1 -0
- package/dist/Schema/SpecialJsonSchema.js +59 -0
- package/dist/Schema/SpecialOpenApi.d.ts +32 -0
- package/dist/Schema/SpecialOpenApi.d.ts.map +1 -0
- package/dist/Schema/SpecialOpenApi.js +123 -0
- package/dist/Schema/brand.d.ts +5 -1
- package/dist/Schema/brand.d.ts.map +1 -1
- package/dist/Schema/brand.js +1 -1
- package/dist/Schema/email.d.ts.map +1 -1
- package/dist/Schema/email.js +9 -4
- package/dist/Schema/ext.d.ts +103 -46
- package/dist/Schema/ext.d.ts.map +1 -1
- package/dist/Schema/ext.js +110 -51
- package/dist/Schema/moreStrings.d.ts +19 -7
- package/dist/Schema/moreStrings.d.ts.map +1 -1
- package/dist/Schema/moreStrings.js +14 -9
- package/dist/Schema/numbers.d.ts +11 -11
- package/dist/Schema/numbers.d.ts.map +1 -1
- package/dist/Schema/numbers.js +10 -9
- package/dist/Schema/phoneNumber.d.ts.map +1 -1
- package/dist/Schema/phoneNumber.js +8 -3
- package/dist/Schema/strings.d.ts +4 -4
- package/dist/Schema/strings.d.ts.map +1 -1
- package/dist/Schema.d.ts +22 -55
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +43 -63
- package/dist/client/apiClientFactory.d.ts +11 -28
- package/dist/client/apiClientFactory.d.ts.map +1 -1
- package/dist/client/apiClientFactory.js +14 -15
- package/dist/client/clientFor.d.ts +6 -5
- package/dist/client/clientFor.d.ts.map +1 -1
- package/dist/client/errors.d.ts +18 -9
- package/dist/client/errors.d.ts.map +1 -1
- package/dist/client/errors.js +35 -10
- package/dist/client/makeClient.d.ts +20 -15
- package/dist/client/makeClient.d.ts.map +1 -1
- package/dist/client/makeClient.js +32 -23
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +5 -5
- package/dist/ids.d.ts +2 -2
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js +3 -2
- package/dist/index.d.ts +3 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -9
- package/dist/middleware.d.ts +2 -2
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +3 -3
- package/dist/rpc/MiddlewareMaker.d.ts +4 -3
- package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
- package/dist/rpc/MiddlewareMaker.js +7 -6
- package/dist/rpc/RpcContextMap.d.ts +2 -2
- package/dist/rpc/RpcContextMap.d.ts.map +1 -1
- package/dist/rpc/RpcContextMap.js +4 -4
- package/dist/rpc/RpcMiddleware.d.ts +4 -3
- package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
- package/dist/rpc/RpcMiddleware.js +1 -1
- package/dist/utils/gen.d.ts +1 -1
- package/dist/utils/gen.d.ts.map +1 -1
- package/dist/utils/logger.d.ts +2 -2
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +3 -3
- package/dist/utils.d.ts +18 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +22 -2
- package/package.json +29 -17
- package/src/Config/SecretURL.ts +1 -1
- package/src/Config.ts +14 -0
- package/src/ConfigProvider.ts +48 -0
- package/src/{ServiceMap.ts → Context.ts} +51 -60
- package/src/Effect.ts +11 -9
- package/src/Layer.ts +5 -4
- package/src/Pure.ts +17 -18
- package/src/Schema/Class.ts +118 -30
- package/src/Schema/SpecialJsonSchema.ts +69 -0
- package/src/Schema/SpecialOpenApi.ts +130 -0
- package/src/Schema/brand.ts +10 -3
- package/src/Schema/email.ts +10 -2
- package/src/Schema/ext.ts +185 -82
- package/src/Schema/moreStrings.ts +21 -11
- package/src/Schema/numbers.ts +9 -8
- package/src/Schema/phoneNumber.ts +8 -1
- package/src/Schema.ts +79 -103
- package/src/client/apiClientFactory.ts +24 -29
- package/src/client/clientFor.ts +6 -1
- package/src/client/errors.ts +34 -9
- package/src/client/makeClient.ts +121 -61
- package/src/http/Request.ts +7 -4
- package/src/ids.ts +2 -1
- package/src/index.ts +3 -11
- package/src/middleware.ts +2 -2
- package/src/rpc/MiddlewareMaker.ts +8 -7
- package/src/rpc/RpcContextMap.ts +6 -5
- package/src/rpc/RpcMiddleware.ts +5 -4
- package/src/utils/gen.ts +1 -1
- package/src/utils/logger.ts +2 -2
- package/src/utils.ts +24 -1
- package/test/dist/moreStrings.test.d.ts.map +1 -0
- package/test/dist/rpc.test.d.ts.map +1 -1
- package/test/dist/secretURL.test.d.ts.map +1 -0
- package/test/dist/special.test.d.ts.map +1 -0
- package/test/moreStrings.test.ts +17 -0
- package/test/rpc.test.ts +28 -6
- package/test/schema.test.ts +397 -4
- package/test/secretURL.test.ts +157 -0
- package/test/special.test.ts +729 -0
- package/test/utils.test.ts +2 -2
- package/tsconfig.base.json +0 -1
- package/tsconfig.json +0 -1
- package/dist/ServiceMap.d.ts.map +0 -1
- package/dist/ServiceMap.js +0 -91
- package/dist/Struct.d.ts +0 -44
- package/dist/Struct.d.ts.map +0 -1
- package/dist/Struct.js +0 -29
- package/src/Struct.ts +0 -54
package/src/Schema/ext.ts
CHANGED
|
@@ -1,56 +1,94 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
3
|
-
import { Effect, Option, pipe,
|
|
3
|
+
import { Effect, Option, pipe, type SchemaAST, SchemaIssue, SchemaTransformation } from "effect"
|
|
4
4
|
import * as S from "effect/Schema"
|
|
5
|
+
import { isDateValid } from "effect/Schema"
|
|
5
6
|
import { type NonEmptyReadonlyArray } from "../Array.js"
|
|
7
|
+
import * as Context from "../Context.js"
|
|
6
8
|
import { extendM, typedKeysOf } from "../utils.js"
|
|
7
9
|
import { type AST } from "./schema.js"
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
) =>
|
|
16
|
-
<Self extends S.Top>(self: Self): S.withConstructorDefault<Self & S.WithoutConstructorDefault> => {
|
|
17
|
-
type Narrowed = Self & S.WithoutConstructorDefault
|
|
18
|
-
return S.withConstructorDefault<Narrowed>(
|
|
19
|
-
() => Option.some(makeDefault() as Narrowed["~type.make.in"])
|
|
20
|
-
)(self as Narrowed)
|
|
21
|
-
}
|
|
11
|
+
type ProvidedCodec<Self extends S.Top, R> = S.Codec<
|
|
12
|
+
Self["Type"],
|
|
13
|
+
Self["Encoded"],
|
|
14
|
+
Exclude<Self["DecodingServices"], R>,
|
|
15
|
+
Exclude<Self["EncodingServices"], R>
|
|
16
|
+
>
|
|
22
17
|
|
|
23
18
|
// TODO: v4 migration - Date is no longer by default encoded to string.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
)
|
|
19
|
+
|
|
20
|
+
const DateString = S.String.annotate({
|
|
21
|
+
identifier: "Date",
|
|
22
|
+
description: "a string in ISO 8601 format that will be decoded as a Date",
|
|
23
|
+
format: "date-time"
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Schema type for {@link DateFromString}.
|
|
28
|
+
*
|
|
29
|
+
* @category Schemas
|
|
30
|
+
* @since 4.0.0
|
|
31
|
+
*/
|
|
32
|
+
export interface DateFromString extends S.decodeTo<S.Date, S.String> {}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A transformation schema that parses an ISO 8601 string into a `Date`.
|
|
36
|
+
*
|
|
37
|
+
* Decoding:
|
|
38
|
+
* - A `string` is decoded as a `Date`.
|
|
39
|
+
*
|
|
40
|
+
* Encoding:
|
|
41
|
+
* - A `Date` is encoded as a `string`.
|
|
42
|
+
*
|
|
43
|
+
* @since 4.0.0
|
|
44
|
+
*/
|
|
45
|
+
export const DateFromString: DateFromString = DateString.pipe(S.decodeTo(S.Date, SchemaTransformation.dateFromString))
|
|
30
46
|
|
|
31
47
|
/**
|
|
32
48
|
* Like the default Schema `Date` but from String with `withDefault` => now
|
|
33
49
|
*/
|
|
34
50
|
export const Date = Object.assign(DateFromString, {
|
|
35
|
-
withDefault: DateFromString.pipe(
|
|
51
|
+
withDefault: DateFromString.pipe(S.withConstructorDefault(Effect.sync(() => new global.Date()))),
|
|
52
|
+
withDecodingDefaultType: DateFromString.pipe(S.withDecodingDefaultType(Effect.sync(() => new global.Date())))
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Like the default Schema `DateValid` but from String with `withDefault` => now
|
|
57
|
+
*/
|
|
58
|
+
export const DateValid = Object.assign(Date.check(isDateValid()), {
|
|
59
|
+
withDefault: DateFromString.pipe(S.withConstructorDefault(Effect.sync(() => new global.Date()))),
|
|
60
|
+
withDecodingDefaultType: DateFromString.pipe(S.withDecodingDefaultType(Effect.sync(() => new global.Date())))
|
|
36
61
|
})
|
|
37
62
|
|
|
38
63
|
/**
|
|
39
64
|
* Like the default Schema `Boolean` but with `withDefault` => false
|
|
40
65
|
*/
|
|
41
66
|
export const Boolean = Object.assign(S.Boolean, {
|
|
42
|
-
withDefault: S.Boolean.pipe(
|
|
67
|
+
withDefault: S.Boolean.pipe(S.withConstructorDefault(Effect.succeed(false))),
|
|
68
|
+
withDecodingDefaultType: S.Boolean.pipe(S.withDecodingDefaultType(Effect.succeed(false)))
|
|
43
69
|
})
|
|
44
70
|
|
|
45
71
|
/**
|
|
72
|
+
* You probably want to use `Finite` instead of this.
|
|
46
73
|
* Like the default Schema `Number` but with `withDefault` => 0
|
|
47
74
|
*/
|
|
48
|
-
export const Number = Object.assign(S.Number, {
|
|
75
|
+
export const Number = Object.assign(S.Number, {
|
|
76
|
+
withDefault: S.Number.pipe(S.withConstructorDefault(Effect.succeed(0))),
|
|
77
|
+
withDecodingDefaultType: S.Number.pipe(S.withDecodingDefaultType(Effect.succeed(0)))
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Like the default Schema `Finite` but with `withDefault` => 0
|
|
82
|
+
*/
|
|
83
|
+
export const Finite = Object.assign(S.Finite, {
|
|
84
|
+
withDefault: S.Finite.pipe(S.withConstructorDefault(Effect.succeed(0))),
|
|
85
|
+
withDecodingDefaultType: S.Finite.pipe(S.withDecodingDefaultType(Effect.succeed(0)))
|
|
86
|
+
})
|
|
49
87
|
|
|
50
88
|
/**
|
|
51
|
-
* Like the default Schema `
|
|
89
|
+
* Like the default Schema `Literals` but with `withDefault` => literals[0]
|
|
52
90
|
*/
|
|
53
|
-
export const
|
|
91
|
+
export const Literals = <const Literals extends NonEmptyReadonlyArray<AST.LiteralValue>>(literals: Literals) =>
|
|
54
92
|
pipe(
|
|
55
93
|
S.Literals(literals),
|
|
56
94
|
(s) =>
|
|
@@ -58,79 +96,140 @@ export const Literal = <Literals extends NonEmptyReadonlyArray<AST.LiteralValue>
|
|
|
58
96
|
changeDefault: <A extends Literals[number]>(a: A) => {
|
|
59
97
|
return Object.assign(S.Literals(literals), {
|
|
60
98
|
Default: a,
|
|
61
|
-
withDefault: s.pipe(
|
|
99
|
+
withDefault: s.pipe(S.withConstructorDefault(Effect.succeed(a))),
|
|
100
|
+
withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.succeed(a)))
|
|
62
101
|
}) // todo: copy annotations from original?
|
|
63
102
|
},
|
|
64
103
|
Default: literals[0] as typeof literals[0],
|
|
65
|
-
withDefault: s.pipe(
|
|
104
|
+
withDefault: s.pipe(S.withConstructorDefault(Effect.succeed(literals[0]))),
|
|
105
|
+
withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.succeed(literals[0])))
|
|
66
106
|
})
|
|
67
107
|
)
|
|
68
108
|
|
|
69
109
|
/**
|
|
70
110
|
* Like the default Schema `Array` but with `withDefault` => []
|
|
71
111
|
*/
|
|
72
|
-
export function Array<
|
|
112
|
+
export function Array<ValueSchema extends S.Top>(value: ValueSchema) {
|
|
73
113
|
return pipe(
|
|
74
114
|
S.Array(value),
|
|
75
|
-
(s) =>
|
|
115
|
+
(s) =>
|
|
116
|
+
Object.assign(s, {
|
|
117
|
+
withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => []))),
|
|
118
|
+
withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.sync(() => [])))
|
|
119
|
+
})
|
|
76
120
|
)
|
|
77
121
|
}
|
|
78
122
|
|
|
79
123
|
/**
|
|
80
|
-
*
|
|
124
|
+
* An annotated `S.Array` of unique items that decodes to a `ReadonlySet`.
|
|
81
125
|
*/
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
126
|
+
export const ReadonlySetFromArray = <ValueSchema extends S.Top>(value: ValueSchema) => {
|
|
127
|
+
const from = S
|
|
128
|
+
.Array(value)
|
|
129
|
+
.annotate({ expected: "an array of unique items that will be decoded as a ReadonlySet" })
|
|
130
|
+
const to = S.instanceOf(Set) as S.instanceOf<ReadonlySet<S.Schema.Type<ValueSchema>>>
|
|
131
|
+
const schema = from.pipe(
|
|
132
|
+
S.decodeTo(
|
|
133
|
+
to,
|
|
134
|
+
SchemaTransformation.transform({
|
|
135
|
+
decode: (arr) => new Set(arr) as ReadonlySet<S.Schema.Type<ValueSchema>>,
|
|
136
|
+
encode: (set) => [...set]
|
|
137
|
+
})
|
|
138
|
+
)
|
|
86
139
|
)
|
|
140
|
+
return schema
|
|
87
141
|
}
|
|
88
142
|
|
|
89
|
-
|
|
143
|
+
/**
|
|
144
|
+
* An annotated `S.Array` of key-value tuples that decodes to a `ReadonlyMap`.
|
|
145
|
+
*/
|
|
146
|
+
export const ReadonlyMapFromArray = <KeySchema extends S.Top, ValueSchema extends S.Top>(pair: {
|
|
147
|
+
readonly key: KeySchema
|
|
148
|
+
readonly value: ValueSchema
|
|
149
|
+
}) => {
|
|
150
|
+
const from = S
|
|
151
|
+
.Array(S.Tuple([pair.key, pair.value]))
|
|
152
|
+
.annotate({ expected: "an array of key-value tuples that will be decoded as a ReadonlyMap" })
|
|
153
|
+
const to = S.instanceOf(Map) as S.instanceOf<
|
|
154
|
+
ReadonlyMap<S.Schema.Type<KeySchema>, S.Schema.Type<ValueSchema>>
|
|
155
|
+
>
|
|
156
|
+
const schema = from.pipe(
|
|
157
|
+
S.decodeTo(
|
|
158
|
+
to,
|
|
159
|
+
SchemaTransformation.transform({
|
|
160
|
+
decode: (
|
|
161
|
+
arr
|
|
162
|
+
) => new Map(arr) as ReadonlyMap<S.Schema.Type<KeySchema>, S.Schema.Type<ValueSchema>>,
|
|
163
|
+
encode: (
|
|
164
|
+
map
|
|
165
|
+
) => [...map.entries()] as any // fu
|
|
166
|
+
})
|
|
167
|
+
)
|
|
168
|
+
)
|
|
169
|
+
return schema
|
|
170
|
+
}
|
|
90
171
|
|
|
91
172
|
/**
|
|
92
|
-
* Like the default Schema `ReadonlySet` but with `withDefault` => new Set()
|
|
173
|
+
* Like the default Schema `ReadonlySet` but from Array with `withDefault` => new Set()
|
|
93
174
|
*/
|
|
94
|
-
export const ReadonlySet = <
|
|
175
|
+
export const ReadonlySet = <ValueSchema extends S.Top>(value: ValueSchema) =>
|
|
95
176
|
pipe(
|
|
96
|
-
|
|
97
|
-
(s) =>
|
|
177
|
+
ReadonlySetFromArray(value),
|
|
178
|
+
(s) =>
|
|
179
|
+
Object.assign(s, {
|
|
180
|
+
withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => new Set<S.Schema.Type<ValueSchema>>()))),
|
|
181
|
+
withDecodingDefaultType: s.pipe(
|
|
182
|
+
S.withDecodingDefaultType(Effect.sync(() => new Set<S.Schema.Type<ValueSchema>>()))
|
|
183
|
+
)
|
|
184
|
+
})
|
|
98
185
|
)
|
|
99
186
|
|
|
100
187
|
/**
|
|
101
|
-
* Like the default Schema `ReadonlyMap` but with `withDefault` => new Map()
|
|
188
|
+
* Like the default Schema `ReadonlyMap` but from Array with `withDefault` => new Map()
|
|
102
189
|
*/
|
|
103
|
-
export const ReadonlyMap = <
|
|
104
|
-
readonly key:
|
|
105
|
-
readonly value:
|
|
190
|
+
export const ReadonlyMap = <KeySchema extends S.Top, ValueSchema extends S.Top>(pair: {
|
|
191
|
+
readonly key: KeySchema
|
|
192
|
+
readonly value: ValueSchema
|
|
106
193
|
}) =>
|
|
107
194
|
pipe(
|
|
108
|
-
|
|
109
|
-
(s) =>
|
|
195
|
+
ReadonlyMapFromArray(pair),
|
|
196
|
+
(s) =>
|
|
197
|
+
Object.assign(s, {
|
|
198
|
+
withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => new Map()))),
|
|
199
|
+
withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.sync(() => new Map())))
|
|
200
|
+
})
|
|
110
201
|
)
|
|
111
202
|
|
|
112
203
|
/**
|
|
113
204
|
* Like the default Schema `NullOr` but with `withDefault` => null
|
|
114
205
|
*/
|
|
115
|
-
export const NullOr = <
|
|
206
|
+
export const NullOr = <Schema extends S.Top>(self: Schema) =>
|
|
116
207
|
pipe(
|
|
117
208
|
S.NullOr(self),
|
|
118
|
-
(s) =>
|
|
209
|
+
(s) =>
|
|
210
|
+
Object.assign(s, {
|
|
211
|
+
withDefault: s.pipe(S.withConstructorDefault(Effect.succeed(null))),
|
|
212
|
+
withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.succeed(null)))
|
|
213
|
+
})
|
|
119
214
|
)
|
|
120
215
|
|
|
121
|
-
export const defaultDate =
|
|
216
|
+
export const defaultDate = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
|
|
217
|
+
schema.pipe(S.withConstructorDefault(Effect.sync(() => new global.Date())))
|
|
122
218
|
|
|
123
|
-
export const defaultBool =
|
|
219
|
+
export const defaultBool = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
|
|
220
|
+
schema.pipe(S.withConstructorDefault(Effect.succeed(false)))
|
|
124
221
|
|
|
125
|
-
export const defaultNullable = (
|
|
126
|
-
|
|
127
|
-
) => s.pipe(withDefaultConstructor(() => null))
|
|
222
|
+
export const defaultNullable = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
|
|
223
|
+
schema.pipe(S.withConstructorDefault(Effect.succeed(null)))
|
|
128
224
|
|
|
129
|
-
export const defaultArray =
|
|
225
|
+
export const defaultArray = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
|
|
226
|
+
schema.pipe(S.withConstructorDefault(Effect.sync(() => [])))
|
|
130
227
|
|
|
131
|
-
export const defaultMap =
|
|
228
|
+
export const defaultMap = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
|
|
229
|
+
schema.pipe(S.withConstructorDefault(Effect.sync(() => new Map())))
|
|
132
230
|
|
|
133
|
-
export const defaultSet =
|
|
231
|
+
export const defaultSet = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
|
|
232
|
+
schema.pipe(S.withConstructorDefault(Effect.sync(() => new Set())))
|
|
134
233
|
|
|
135
234
|
export const withDefaultMake = <Self extends S.Top>(s: Self) => {
|
|
136
235
|
const a = Object.assign(S.decodeSync(s as any) as WithDefaults<Self>, s)
|
|
@@ -160,8 +259,11 @@ export type WithDefaults<Self extends S.Top> = (
|
|
|
160
259
|
// : never
|
|
161
260
|
|
|
162
261
|
export const inputDate = extendM(
|
|
163
|
-
S.Union([S.DateValid,
|
|
164
|
-
(s) => ({
|
|
262
|
+
S.Union([S.DateValid, Date]),
|
|
263
|
+
(s) => ({
|
|
264
|
+
withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => new globalThis.Date()))),
|
|
265
|
+
withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.sync(() => new globalThis.Date())))
|
|
266
|
+
})
|
|
165
267
|
)
|
|
166
268
|
|
|
167
269
|
export interface UnionBrand {}
|
|
@@ -211,7 +313,7 @@ export const transformTo = <To extends S.Top, From extends S.Top>(
|
|
|
211
313
|
{ message: "One way schema transformation, encoding is not allowed" }
|
|
212
314
|
)
|
|
213
315
|
)
|
|
214
|
-
})
|
|
316
|
+
})
|
|
215
317
|
)
|
|
216
318
|
)
|
|
217
319
|
|
|
@@ -228,7 +330,7 @@ export const transformToOrFail = <To extends S.Top, From extends S.Top, RD>(
|
|
|
228
330
|
S.decodeTo(
|
|
229
331
|
to,
|
|
230
332
|
SchemaTransformation.transformOrFail({
|
|
231
|
-
decode
|
|
333
|
+
decode,
|
|
232
334
|
encode: (i: any) =>
|
|
233
335
|
Effect.fail(
|
|
234
336
|
new SchemaIssue.Forbidden(
|
|
@@ -236,33 +338,34 @@ export const transformToOrFail = <To extends S.Top, From extends S.Top, RD>(
|
|
|
236
338
|
{ message: "One way schema transformation, encoding is not allowed" }
|
|
237
339
|
)
|
|
238
340
|
)
|
|
239
|
-
})
|
|
341
|
+
})
|
|
240
342
|
)
|
|
241
343
|
)
|
|
242
344
|
|
|
243
|
-
// TODO: v4 migration — S.declare API changed (no [self] + decode/encode pattern)
|
|
244
|
-
// Need to find v4 equivalent for contextual schema wrapping
|
|
245
345
|
export const provide = <Self extends S.Top, R>(
|
|
246
346
|
self: Self,
|
|
247
|
-
context:
|
|
248
|
-
):
|
|
347
|
+
context: Context.Context<R>
|
|
348
|
+
): ProvidedCodec<Self, R> => {
|
|
249
349
|
const prov = Effect.provide(context)
|
|
250
|
-
return
|
|
251
|
-
.
|
|
252
|
-
.
|
|
253
|
-
|
|
254
|
-
self,
|
|
255
|
-
SchemaTransformation.transformOrFail({
|
|
256
|
-
decode: (n: any) => prov(SchemaParser.decodeUnknownEffect(self)(n)),
|
|
257
|
-
encode: (n: any) => prov(SchemaParser.encodeUnknownEffect(self)(n))
|
|
258
|
-
}) as any
|
|
259
|
-
) as any
|
|
260
|
-
)
|
|
261
|
-
}
|
|
262
|
-
// TODO: v4 migration — ServiceMap.pick and S.declare pattern removed
|
|
263
|
-
export const contextFromServices = <Self extends S.Top, Tags extends readonly any[]>(
|
|
264
|
-
_self: Self,
|
|
265
|
-
..._services: Tags
|
|
266
|
-
): any => {
|
|
267
|
-
throw new Error("contextFromServices: not yet migrated to v4")
|
|
350
|
+
return self.pipe(
|
|
351
|
+
S.middlewareDecoding((effect) => prov(effect)),
|
|
352
|
+
S.middlewareEncoding((effect) => prov(effect))
|
|
353
|
+
) as ProvidedCodec<Self, R>
|
|
268
354
|
}
|
|
355
|
+
export const contextFromServices = <
|
|
356
|
+
Self extends S.Top,
|
|
357
|
+
Tags extends ReadonlyArray<Context.Key<any, any>>
|
|
358
|
+
>(
|
|
359
|
+
self: Self,
|
|
360
|
+
...services: Tags
|
|
361
|
+
): Effect.Effect<
|
|
362
|
+
ProvidedCodec<Self, Context.Service.Identifier<Tags[number]>>,
|
|
363
|
+
never,
|
|
364
|
+
Context.Service.Identifier<Tags[number]>
|
|
365
|
+
> =>
|
|
366
|
+
Effect.gen(function*() {
|
|
367
|
+
const context: Context.Context<Context.Service.Identifier<Tags[number]>> = Context.pick(...services)(
|
|
368
|
+
yield* Effect.context<Context.Service.Identifier<Tags[number]>>()
|
|
369
|
+
)
|
|
370
|
+
return provide(self, context)
|
|
371
|
+
})
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { pipe } from "effect"
|
|
1
|
+
import { Effect, pipe } from "effect"
|
|
2
2
|
import type { Refinement } from "effect-app/Function"
|
|
3
3
|
import { extendM } from "effect-app/utils"
|
|
4
4
|
import * as S from "effect/Schema"
|
|
@@ -6,7 +6,7 @@ import type { Simplify } from "effect/Types"
|
|
|
6
6
|
import { customRandom, nanoid, urlAlphabet } from "nanoid"
|
|
7
7
|
import validator from "validator"
|
|
8
8
|
import { fromBrand, nominal } from "./brand.js"
|
|
9
|
-
import {
|
|
9
|
+
import { withDefaultMake, type WithDefaults } from "./ext.js"
|
|
10
10
|
import { type B } from "./schema.js"
|
|
11
11
|
import type { NonEmptyString255Brand, NonEmptyStringBrand } from "./strings.js"
|
|
12
12
|
|
|
@@ -140,10 +140,10 @@ const minLength = 6
|
|
|
140
140
|
const maxLength = 50
|
|
141
141
|
const size = 21
|
|
142
142
|
const length = 10 * size
|
|
143
|
-
const StringIdArb = (): S.LazyArbitrary<
|
|
143
|
+
const StringIdArb = (): S.LazyArbitrary<StringId> => (fc) =>
|
|
144
144
|
fc
|
|
145
145
|
.uint8Array({ minLength: length, maxLength: length })
|
|
146
|
-
.map((_) => customRandom(urlAlphabet, size, (size) => _.subarray(0, size))())
|
|
146
|
+
.map((_) => customRandom(urlAlphabet, size, (size) => _.subarray(0, size))() as StringId)
|
|
147
147
|
/**
|
|
148
148
|
* A string that is at least 6 characters long and a maximum of 50.
|
|
149
149
|
*/
|
|
@@ -154,13 +154,13 @@ export const StringId = extendM(
|
|
|
154
154
|
fromBrand(nominal<StringId>(), {
|
|
155
155
|
identifier: "StringId",
|
|
156
156
|
title: "StringId",
|
|
157
|
-
|
|
157
|
+
toArbitrary: () => (fc) => StringIdArb()(fc),
|
|
158
158
|
jsonSchema: {}
|
|
159
159
|
})
|
|
160
160
|
),
|
|
161
161
|
(s) => ({
|
|
162
162
|
make: makeStringId,
|
|
163
|
-
withDefault: s.pipe(
|
|
163
|
+
withDefault: s.pipe(S.withConstructorDefault(Effect.sync(makeStringId)))
|
|
164
164
|
})
|
|
165
165
|
)
|
|
166
166
|
.pipe(withDefaultMake)
|
|
@@ -182,14 +182,16 @@ export function prefixedStringId<Brand extends StringId>() {
|
|
|
182
182
|
(x) => (pref + x.substring(0, 50 - pref.length)) as Brand
|
|
183
183
|
)
|
|
184
184
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
185
|
-
const s
|
|
185
|
+
const s = StringId
|
|
186
186
|
.pipe(
|
|
187
187
|
S.refine((x: string): x is string & Brand => x.startsWith(pref), {
|
|
188
|
-
arbitrary: arb,
|
|
189
188
|
identifier: name,
|
|
190
189
|
title: name
|
|
190
|
+
}),
|
|
191
|
+
S.annotate({
|
|
192
|
+
toArbitrary: () => (fc) => arb()(fc)
|
|
191
193
|
})
|
|
192
|
-
)
|
|
194
|
+
)
|
|
193
195
|
const schema = s.pipe(withDefaultMake)
|
|
194
196
|
const make = () => (pref + StringId.make().substring(0, 50 - pref.length)) as Brand
|
|
195
197
|
|
|
@@ -206,7 +208,9 @@ export function prefixedStringId<Brand extends StringId>() {
|
|
|
206
208
|
*/
|
|
207
209
|
prefixSafe: <REST extends string>(str: `${Prefix}${Separator}${REST}`) => ex(str),
|
|
208
210
|
prefix,
|
|
209
|
-
withDefault: schema.pipe(
|
|
211
|
+
withDefault: schema.pipe(S.withConstructorDefault<S.Codec<Brand, string> & S.WithoutConstructorDefault>(
|
|
212
|
+
Effect.sync(make)
|
|
213
|
+
))
|
|
210
214
|
})
|
|
211
215
|
)
|
|
212
216
|
}
|
|
@@ -245,11 +249,17 @@ const isUrl: Refinement<string, Url> = (s: string): s is Url => {
|
|
|
245
249
|
export const Url = S
|
|
246
250
|
.String
|
|
247
251
|
.pipe(
|
|
252
|
+
S.annotate({
|
|
253
|
+
title: "Url",
|
|
254
|
+
format: "uri"
|
|
255
|
+
}),
|
|
248
256
|
S.refine(isUrl, {
|
|
249
|
-
arbitrary: (): S.LazyArbitrary<Url> => (fc) => fc.webUrl().map((_) => _ as Url),
|
|
250
257
|
identifier: "Url",
|
|
251
258
|
title: "Url",
|
|
252
259
|
jsonSchema: { format: "uri" }
|
|
253
260
|
}),
|
|
261
|
+
S.annotate({
|
|
262
|
+
toArbitrary: () => (fc) => fc.webUrl().map((_) => _ as Url)
|
|
263
|
+
}),
|
|
254
264
|
withDefaultMake
|
|
255
265
|
)
|
package/src/Schema/numbers.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { Effect } from "effect"
|
|
1
2
|
import { extendM } from "effect-app/utils"
|
|
2
3
|
import * as S from "effect/Schema"
|
|
3
4
|
import type { Simplify } from "effect/Types"
|
|
4
5
|
import { fromBrand, nominal } from "./brand.js"
|
|
5
|
-
import {
|
|
6
|
+
import { withDefaultMake } from "./ext.js"
|
|
6
7
|
import { type B } from "./schema.js"
|
|
7
8
|
|
|
8
9
|
export interface PositiveIntBrand
|
|
@@ -14,7 +15,7 @@ export const PositiveInt = extendM(
|
|
|
14
15
|
fromBrand(nominal<PositiveInt>(), { identifier: "PositiveInt", title: "PositiveInt", jsonSchema: {} }),
|
|
15
16
|
withDefaultMake
|
|
16
17
|
),
|
|
17
|
-
(s) => ({ withDefault: s.pipe(
|
|
18
|
+
(s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(1)))) })
|
|
18
19
|
)
|
|
19
20
|
export type PositiveInt = number & PositiveIntBrand
|
|
20
21
|
|
|
@@ -29,20 +30,20 @@ export const NonNegativeInt = extendM(
|
|
|
29
30
|
}),
|
|
30
31
|
withDefaultMake
|
|
31
32
|
),
|
|
32
|
-
(s) => ({ withDefault: s.pipe(
|
|
33
|
+
(s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(0)))) })
|
|
33
34
|
)
|
|
34
35
|
export type NonNegativeInt = number & NonNegativeIntBrand
|
|
35
36
|
|
|
36
37
|
export interface IntBrand extends Simplify<B.Brand<"Int">> {}
|
|
37
38
|
export const Int = extendM(
|
|
38
39
|
S.Int.pipe(fromBrand(nominal<Int>(), { identifier: "Int", title: "Int", jsonSchema: {} }), withDefaultMake),
|
|
39
|
-
(s) => ({ withDefault: s.pipe(
|
|
40
|
+
(s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(0)))) })
|
|
40
41
|
)
|
|
41
42
|
export type Int = number & IntBrand
|
|
42
43
|
|
|
43
44
|
export interface PositiveNumberBrand extends Simplify<B.Brand<"PositiveNumber"> & NonNegativeNumberBrand> {}
|
|
44
45
|
export const PositiveNumber = extendM(
|
|
45
|
-
S.
|
|
46
|
+
S.Finite.pipe(
|
|
46
47
|
S.check(S.isGreaterThan(0)),
|
|
47
48
|
fromBrand(nominal<PositiveNumber>(), {
|
|
48
49
|
identifier: "PositiveNumber",
|
|
@@ -51,14 +52,14 @@ export const PositiveNumber = extendM(
|
|
|
51
52
|
}),
|
|
52
53
|
withDefaultMake
|
|
53
54
|
),
|
|
54
|
-
(s) => ({ withDefault: s.pipe(
|
|
55
|
+
(s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(1)))) })
|
|
55
56
|
)
|
|
56
57
|
export type PositiveNumber = number & PositiveNumberBrand
|
|
57
58
|
|
|
58
59
|
export interface NonNegativeNumberBrand extends Simplify<B.Brand<"NonNegativeNumber">> {}
|
|
59
60
|
export const NonNegativeNumber = extendM(
|
|
60
61
|
S
|
|
61
|
-
.
|
|
62
|
+
.Finite
|
|
62
63
|
.pipe(
|
|
63
64
|
S.check(S.isGreaterThanOrEqualTo(0)),
|
|
64
65
|
fromBrand(nominal<NonNegativeNumber>(), {
|
|
@@ -68,7 +69,7 @@ export const NonNegativeNumber = extendM(
|
|
|
68
69
|
}),
|
|
69
70
|
withDefaultMake
|
|
70
71
|
),
|
|
71
|
-
(s) => ({ withDefault: s.pipe(
|
|
72
|
+
(s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(0)))) })
|
|
72
73
|
)
|
|
73
74
|
export type NonNegativeNumber = number & NonNegativeNumberBrand
|
|
74
75
|
|
|
@@ -13,12 +13,19 @@ export type PhoneNumber = string & PhoneNumberBrand
|
|
|
13
13
|
export const PhoneNumber = S
|
|
14
14
|
.String
|
|
15
15
|
.pipe(
|
|
16
|
+
S.annotate({
|
|
17
|
+
title: "PhoneNumber",
|
|
18
|
+
description: "a phone number with at least 7 digits",
|
|
19
|
+
format: "phone"
|
|
20
|
+
}),
|
|
16
21
|
S.refine(isValidPhone as Refinement<string, PhoneNumber>, {
|
|
17
22
|
identifier: "PhoneNumber",
|
|
18
23
|
title: "PhoneNumber",
|
|
19
24
|
description: "a phone number with at least 7 digits",
|
|
20
|
-
arbitrary: () => (fc: any) => Numbers(7, 10)(fc).map((_: any) => _ as PhoneNumber),
|
|
21
25
|
jsonSchema: { format: "phone" }
|
|
22
26
|
}),
|
|
27
|
+
S.annotate({
|
|
28
|
+
toArbitrary: () => (fc) => Numbers(7, 10)(fc).map((_) => _ as PhoneNumber)
|
|
29
|
+
}),
|
|
23
30
|
withDefaultMake
|
|
24
31
|
)
|