deno.land / x / domain_functions@v1.2.0 / src / domain-functions.ts

domain-functions.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { z } from 'https://deno.land/x/zod@v3.19.1/mod.ts'
import { EnvironmentError, InputError, InputErrors } from './errors.ts'import { schemaError, toErrorWithMessage } from './errors.ts'import { isListOfSuccess, formatSchemaErrors, mergeObjects } from './utils.ts'import type { DomainFunction, ErrorData, MergeObjs, Result } from './types.ts'import type { Last, List, ListToResultData } from './types.ts'import type { SuccessResult } from './types.ts'
type MakeDomainFunction = < Schema extends z.ZodTypeAny, EnvSchema extends z.ZodTypeAny,>( inputSchema: Schema, environmentSchema?: EnvSchema,) => <Output>( handler: ( inputSchema: z.infer<Schema>, environmentSchema: z.infer<EnvSchema>, ) => Promise<Output>,) => DomainFunction<Output>
const makeDomainFunction: MakeDomainFunction = ( inputSchema: z.ZodTypeAny = z.object({}), environmentSchema: z.ZodTypeAny = z.object({}), ) => (handler) => { const domainFunction = (async (input, environment = {}) => { const envResult = await environmentSchema.safeParseAsync(environment) const result = await inputSchema.safeParseAsync(input)
try { if (result.success === true && envResult.success === true) { return { success: true, data: await handler(result.data, envResult.data), errors: [], inputErrors: [], environmentErrors: [], } } } catch (error) { if (error instanceof InputError) { return { success: false, errors: [], environmentErrors: [], inputErrors: [schemaError(error.message, error.path)], } } if (error instanceof EnvironmentError) { return { success: false, errors: [], environmentErrors: [schemaError(error.message, error.path)], inputErrors: [], } } if (error instanceof InputErrors) { return { success: false, errors: [], environmentErrors: [], inputErrors: error.errors.map((e) => schemaError(e.message, e.path), ), } } return { success: false, errors: [toErrorWithMessage(error)], inputErrors: [], environmentErrors: [], } } return { success: false, errors: [], inputErrors: result.success ? [] : formatSchemaErrors(result.error.issues), environmentErrors: envResult.success ? [] : formatSchemaErrors(envResult.error.issues), } }) as DomainFunction<Awaited<ReturnType<typeof handler>>> return domainFunction }
type All = <Fns extends DomainFunction[]>( ...fns: Fns) => DomainFunction<List.Map<ListToResultData, Fns>>const all: All = (...fns) => { return async (input, environment) => { const results = await Promise.all( fns.map((fn) => (fn as DomainFunction)(input, environment)), )
if (!isListOfSuccess(results)) { return { success: false, errors: results.map(({ errors }) => errors).flat(), inputErrors: results.map(({ inputErrors }) => inputErrors).flat(), environmentErrors: results .map(({ environmentErrors }) => environmentErrors) .flat(), } }
return { success: true, data: results.map(({ data }) => data), inputErrors: [], environmentErrors: [], errors: [], } as SuccessResult<List.Map<ListToResultData, typeof fns>> }}
type Merge = <Fns extends DomainFunction[]>( ...fns: Fns) => DomainFunction<MergeObjs<List.Map<ListToResultData, Fns>>>const merge: Merge = (...fns) => { return async (input, environment) => { const results = await Promise.all( fns.map((fn) => (fn as DomainFunction)(input, environment)), )
if (!isListOfSuccess(results)) { return { success: false, errors: results.map(({ errors }) => errors).flat(), inputErrors: results.map(({ inputErrors }) => inputErrors).flat(), environmentErrors: results .map(({ environmentErrors }) => environmentErrors) .flat(), } }
const collectedResults = results.map(({ data }) => data)
const resultSchema = z.array(z.object({})) if (!resultSchema.safeParse(collectedResults).success) { return { success: false, errors: [ { message: 'Invalid data format returned from some domainFunction' }, ], inputErrors: [], environmentErrors: [], } }
return { success: true, data: mergeObjects(collectedResults), inputErrors: [], environmentErrors: [], errors: [], } as SuccessResult<MergeObjs<List.Map<ListToResultData, typeof fns>>> }}
type Pipe = <T extends DomainFunction[]>(...fns: T) => Last<T>const pipe: Pipe = (...fns) => { const [head, ...tail] = fns
return ((input: unknown, environment?: unknown) => { return tail.reduce(async (memo, fn) => { const resolved = await memo if (resolved.success) { return fn(resolved.data as unknown, environment) } else { return memo } }, head(input, environment)) }) as Last<typeof fns>}
type Sequence = <Fns extends DomainFunction[]>( ...fns: Fns) => DomainFunction<List.Map<ListToResultData, Fns>>const sequence: Sequence = (...fns) => { return async function (input: unknown, environment?: unknown) { const results = [] let currResult: undefined | Result<unknown> for await (const fn of fns as DomainFunction[]) { const result = await fn( currResult?.success ? currResult.data : input, environment, ) if (!result.success) return result currResult = result results.push(result.data) }
return { success: true, data: results, inputErrors: [], environmentErrors: [], errors: [], } as SuccessResult<List.Map<ListToResultData, typeof fns>> }}
type Map = <O, R>( dfn: DomainFunction<O>, mapper: (element: O) => R,) => DomainFunction<R>const map: Map = (dfn, mapper) => { return async (input, environment) => { const result = await dfn(input, environment) if (!result.success) return result
try { return { success: true, data: mapper(result.data), errors: [], inputErrors: [], environmentErrors: [], } } catch (error) { const errors = [toErrorWithMessage(error)] return { success: false, errors, inputErrors: [], environmentErrors: [], } } }}
type MapError = <O>( dfn: DomainFunction<O>, mapper: (element: ErrorData) => ErrorData,) => DomainFunction<O>const mapError: MapError = (dfn, mapper) => { return async (input, environment) => { const result = await dfn(input, environment) if (result.success) return result
try { return { ...mapper(result), success: false } } catch (error) { const errors = [toErrorWithMessage(error)] return { success: false, errors, inputErrors: [], environmentErrors: [], } } }}
export { makeDomainFunction, all, pipe, sequence, map, mapError, merge }
domain_functions

Version Info

Tagged at
a year ago