@alevnyacow/nzmt 0.7.3 → 0.7.5
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/README.md +1 -0
- package/dist/index.cjs +1 -554
- package/dist/index.js +1 -502
- package/dist/rslib-runtime.js +1 -37
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://badge.fury.io/js/@alevnyacow%2Fnzmt)
|
|
4
4
|

|
|
5
|
+

|
|
5
6
|
|
|
6
7
|
# About
|
|
7
8
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,554 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
(()=>{
|
|
4
|
-
__webpack_require__.n = (module)=>{
|
|
5
|
-
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
6
|
-
__webpack_require__.d(getter, {
|
|
7
|
-
a: getter
|
|
8
|
-
});
|
|
9
|
-
return getter;
|
|
10
|
-
};
|
|
11
|
-
})();
|
|
12
|
-
(()=>{
|
|
13
|
-
__webpack_require__.d = (exports1, definition)=>{
|
|
14
|
-
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
get: definition[key]
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
})();
|
|
20
|
-
(()=>{
|
|
21
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
22
|
-
})();
|
|
23
|
-
(()=>{
|
|
24
|
-
__webpack_require__.r = (exports1)=>{
|
|
25
|
-
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
26
|
-
value: 'Module'
|
|
27
|
-
});
|
|
28
|
-
Object.defineProperty(exports1, '__esModule', {
|
|
29
|
-
value: true
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
})();
|
|
33
|
-
var __webpack_exports__ = {};
|
|
34
|
-
__webpack_require__.r(__webpack_exports__);
|
|
35
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
36
|
-
Controller: ()=>zod_controller_utils_namespaceObject,
|
|
37
|
-
Store: ()=>store_namespaceObject,
|
|
38
|
-
Module: ()=>zod_module_utils_namespaceObject,
|
|
39
|
-
ValueObjects: ()=>entities_namespaceObject
|
|
40
|
-
});
|
|
41
|
-
var zod_module_utils_namespaceObject = {};
|
|
42
|
-
__webpack_require__.r(zod_module_utils_namespaceObject);
|
|
43
|
-
__webpack_require__.d(zod_module_utils_namespaceObject, {
|
|
44
|
-
methods: ()=>methods
|
|
45
|
-
});
|
|
46
|
-
var zod_controller_utils_namespaceObject = {};
|
|
47
|
-
__webpack_require__.r(zod_controller_utils_namespaceObject);
|
|
48
|
-
__webpack_require__.d(zod_controller_utils_namespaceObject, {
|
|
49
|
-
DefaultErrorCodes: ()=>zod_controller_utils_DefaultErrorCodes,
|
|
50
|
-
endpoints: ()=>endpoints
|
|
51
|
-
});
|
|
52
|
-
var store_namespaceObject = {};
|
|
53
|
-
__webpack_require__.r(store_namespaceObject);
|
|
54
|
-
__webpack_require__.d(store_namespaceObject, {
|
|
55
|
-
InRAM: ()=>InRAM,
|
|
56
|
-
methods: ()=>store_zod_utils_methods
|
|
57
|
-
});
|
|
58
|
-
var entities_namespaceObject = {};
|
|
59
|
-
__webpack_require__.r(entities_namespaceObject);
|
|
60
|
-
__webpack_require__.d(entities_namespaceObject, {
|
|
61
|
-
Identifier: ()=>Identifier,
|
|
62
|
-
Pagination: ()=>Pagination
|
|
63
|
-
});
|
|
64
|
-
class ErrorFactory {
|
|
65
|
-
static isControllerError = (error)=>{
|
|
66
|
-
if (!error || 'object' != typeof error) return false;
|
|
67
|
-
const argKeys = Object.keys(error);
|
|
68
|
-
const requiredKeys = [
|
|
69
|
-
'name',
|
|
70
|
-
'message',
|
|
71
|
-
'code',
|
|
72
|
-
'module',
|
|
73
|
-
'method',
|
|
74
|
-
'timestamp',
|
|
75
|
-
'statusCode'
|
|
76
|
-
];
|
|
77
|
-
return requiredKeys.every((x)=>argKeys.includes(x));
|
|
78
|
-
};
|
|
79
|
-
static isModuleError = (arg)=>{
|
|
80
|
-
if ('object' != typeof arg || !arg) return false;
|
|
81
|
-
const argKeys = Object.keys(arg);
|
|
82
|
-
const requiredKeys = [
|
|
83
|
-
'name',
|
|
84
|
-
'message',
|
|
85
|
-
'code',
|
|
86
|
-
'module',
|
|
87
|
-
'method',
|
|
88
|
-
'timestamp'
|
|
89
|
-
];
|
|
90
|
-
if (!requiredKeys.every((x)=>argKeys.includes(x))) return false;
|
|
91
|
-
return !argKeys.includes('statusCode');
|
|
92
|
-
};
|
|
93
|
-
static base = ({ error, code, details })=>{
|
|
94
|
-
let err = error instanceof Error ? error : new Error('unknown error');
|
|
95
|
-
if ('string' == typeof error || 'number' == typeof error) err = new Error(error?.toString());
|
|
96
|
-
if ('object' == typeof error && null !== error) if (error instanceof Error) err = error;
|
|
97
|
-
else {
|
|
98
|
-
const jsonRepresentation = JSON.stringify(error);
|
|
99
|
-
err = new Error(jsonRepresentation);
|
|
100
|
-
}
|
|
101
|
-
return {
|
|
102
|
-
message: err.message,
|
|
103
|
-
name: err.name,
|
|
104
|
-
timestamp: Date.now(),
|
|
105
|
-
code: code || err.name,
|
|
106
|
-
details: details || null
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
static forModule = (serviceName)=>({
|
|
110
|
-
inMethod: (methodName)=>({
|
|
111
|
-
newError: (payload, cause)=>{
|
|
112
|
-
const errorBase = ErrorFactory.base(payload);
|
|
113
|
-
return {
|
|
114
|
-
...errorBase,
|
|
115
|
-
cause: cause ? ErrorFactory.fromUnknownError(cause) : null,
|
|
116
|
-
module: serviceName,
|
|
117
|
-
method: methodName
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
});
|
|
122
|
-
static forController = (controllerName)=>({
|
|
123
|
-
inMethod: (methodName)=>({
|
|
124
|
-
newError: (payload, cause)=>{
|
|
125
|
-
const errorBase = ErrorFactory.base(payload);
|
|
126
|
-
const formattedCause = cause ? ErrorFactory.fromUnknownError(cause) : null;
|
|
127
|
-
if (!payload.code && formattedCause?.code) errorBase.code = formattedCause.code;
|
|
128
|
-
return {
|
|
129
|
-
...errorBase,
|
|
130
|
-
cause: formattedCause,
|
|
131
|
-
module: controllerName,
|
|
132
|
-
method: methodName,
|
|
133
|
-
statusCode: payload.statusCode
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
})
|
|
137
|
-
});
|
|
138
|
-
static fromUnknownError = (error)=>{
|
|
139
|
-
const isErrorBase = (arg)=>{
|
|
140
|
-
if ('object' != typeof arg || !arg) return false;
|
|
141
|
-
const argKeys = Object.keys(arg);
|
|
142
|
-
const requiredKeys = [
|
|
143
|
-
'name',
|
|
144
|
-
'message',
|
|
145
|
-
'code',
|
|
146
|
-
'timestamp'
|
|
147
|
-
];
|
|
148
|
-
return requiredKeys.every((x)=>argKeys.includes(x));
|
|
149
|
-
};
|
|
150
|
-
if (ErrorFactory.isModuleError(error)) return error;
|
|
151
|
-
if (isErrorBase(error)) return error;
|
|
152
|
-
return ErrorFactory.base({
|
|
153
|
-
error
|
|
154
|
-
});
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
const methods = (metadata, sharedConfig = {})=>(methodName, handler, config = {})=>{
|
|
158
|
-
const { name, schemas } = metadata;
|
|
159
|
-
return async (payload)=>{
|
|
160
|
-
try {
|
|
161
|
-
const parsedPayload = schemas[methodName].payload.parse(payload);
|
|
162
|
-
const response = await handler(parsedPayload, {
|
|
163
|
-
methodError: (payload, cause)=>ErrorFactory.forModule(name).inMethod(methodName).newError('string' == typeof payload ? {
|
|
164
|
-
error: payload,
|
|
165
|
-
code: payload,
|
|
166
|
-
details: parsedPayload
|
|
167
|
-
} : payload, cause)
|
|
168
|
-
});
|
|
169
|
-
const parsedResponse = schemas[methodName].response.parse(response);
|
|
170
|
-
return parsedResponse;
|
|
171
|
-
} catch (error) {
|
|
172
|
-
if (ErrorFactory.isModuleError(error)) {
|
|
173
|
-
if (sharedConfig.onError) await sharedConfig.onError(error);
|
|
174
|
-
if (config.onError) await config.onError(error);
|
|
175
|
-
throw error;
|
|
176
|
-
}
|
|
177
|
-
const serviceErrorGenerator = ErrorFactory.forModule(name).inMethod(methodName);
|
|
178
|
-
const serviceError = serviceErrorGenerator.newError({
|
|
179
|
-
error: 'Caught unhandled module error (see `cause` field for details)'
|
|
180
|
-
}, error);
|
|
181
|
-
if (config.onError) await config.onError(serviceError);
|
|
182
|
-
throw serviceError;
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
};
|
|
186
|
-
const server_namespaceObject = require("next/server");
|
|
187
|
-
const external_zod_namespaceObject = require("zod");
|
|
188
|
-
var external_zod_default = /*#__PURE__*/ __webpack_require__.n(external_zod_namespaceObject);
|
|
189
|
-
var zod_controller_utils_DefaultErrorCodes = /*#__PURE__*/ function(DefaultErrorCodes) {
|
|
190
|
-
DefaultErrorCodes["REQUEST_PARSING"] = "ZOD-CONTROLLER___REQUEST-PARSING";
|
|
191
|
-
DefaultErrorCodes["RESPONSE_PARSING"] = "ZOD-CONTROLLER___RESPONSE-PARSING";
|
|
192
|
-
return DefaultErrorCodes;
|
|
193
|
-
}({});
|
|
194
|
-
const endpoints = (metadata, sharedConfig = {})=>{
|
|
195
|
-
const endpointLogic = (method, handler, configuration = {})=>async (request)=>{
|
|
196
|
-
let requestPayload = {};
|
|
197
|
-
const errorFactory = ErrorFactory.forController(metadata.name).inMethod(method);
|
|
198
|
-
try {
|
|
199
|
-
const endpointError = (payload, errorStatus, cause)=>{
|
|
200
|
-
if ('string' == typeof payload) return errorFactory.newError({
|
|
201
|
-
statusCode: errorStatus ?? 500,
|
|
202
|
-
code: payload,
|
|
203
|
-
error: payload,
|
|
204
|
-
details: requestPayload
|
|
205
|
-
}, cause);
|
|
206
|
-
return errorFactory.newError({
|
|
207
|
-
...payload,
|
|
208
|
-
statusCode: errorStatus ?? 500
|
|
209
|
-
}, cause);
|
|
210
|
-
};
|
|
211
|
-
if (sharedConfig.guards) for (const sharedGuard of sharedConfig.guards){
|
|
212
|
-
const error = await sharedGuard({
|
|
213
|
-
request,
|
|
214
|
-
endpointError
|
|
215
|
-
});
|
|
216
|
-
if (error) throw error;
|
|
217
|
-
}
|
|
218
|
-
if (configuration.guards) for (const endpointGuard of configuration.guards){
|
|
219
|
-
const error = await endpointGuard({
|
|
220
|
-
request,
|
|
221
|
-
endpointError
|
|
222
|
-
});
|
|
223
|
-
if (error) throw error;
|
|
224
|
-
}
|
|
225
|
-
const endpointSchemas = metadata.schemas[method];
|
|
226
|
-
if (!endpointSchemas) throw errorFactory.newError({
|
|
227
|
-
error: 'No schemas were found for the endpoint',
|
|
228
|
-
statusCode: 500
|
|
229
|
-
});
|
|
230
|
-
if (endpointSchemas.query) {
|
|
231
|
-
const queryParamsAsObject = Object.fromEntries(request.nextUrl.searchParams.entries());
|
|
232
|
-
const queryParamsParsed = endpointSchemas.query.safeParse(queryParamsAsObject);
|
|
233
|
-
if (!queryParamsParsed.success) throw errorFactory.newError({
|
|
234
|
-
error: queryParamsParsed.error.message,
|
|
235
|
-
statusCode: 400,
|
|
236
|
-
code: "ZOD-CONTROLLER___REQUEST-PARSING",
|
|
237
|
-
details: external_zod_default().treeifyError(queryParamsParsed.error)
|
|
238
|
-
});
|
|
239
|
-
requestPayload = {
|
|
240
|
-
...requestPayload,
|
|
241
|
-
...queryParamsParsed.data
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
if (endpointSchemas.body) {
|
|
245
|
-
const body = await request.json();
|
|
246
|
-
const bodyParsed = endpointSchemas.body.safeParse(body);
|
|
247
|
-
if (!bodyParsed.success) throw errorFactory.newError({
|
|
248
|
-
error: bodyParsed.error.message,
|
|
249
|
-
statusCode: 400,
|
|
250
|
-
code: "ZOD-CONTROLLER___REQUEST-PARSING",
|
|
251
|
-
details: external_zod_default().treeifyError(bodyParsed.error)
|
|
252
|
-
});
|
|
253
|
-
requestPayload = {
|
|
254
|
-
...requestPayload,
|
|
255
|
-
...bodyParsed.data
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
const flags = {};
|
|
259
|
-
const result = await handler(requestPayload, {
|
|
260
|
-
request,
|
|
261
|
-
flags,
|
|
262
|
-
endpointError: (payload, errorStatus, cause)=>{
|
|
263
|
-
if ('string' == typeof payload) return errorFactory.newError({
|
|
264
|
-
statusCode: errorStatus ?? 500,
|
|
265
|
-
code: payload,
|
|
266
|
-
error: payload,
|
|
267
|
-
details: requestPayload
|
|
268
|
-
}, cause);
|
|
269
|
-
return errorFactory.newError({
|
|
270
|
-
...payload,
|
|
271
|
-
statusCode: errorStatus ?? 500
|
|
272
|
-
}, cause);
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
if (endpointSchemas.response) {
|
|
276
|
-
const resultParsed = endpointSchemas.response.safeParse(result);
|
|
277
|
-
if (!resultParsed.success) throw errorFactory.newError({
|
|
278
|
-
error: resultParsed.error.message,
|
|
279
|
-
statusCode: 500,
|
|
280
|
-
code: "ZOD-CONTROLLER___RESPONSE-PARSING",
|
|
281
|
-
details: external_zod_default().treeifyError(resultParsed.error)
|
|
282
|
-
});
|
|
283
|
-
if (configuration.eventHandlers?.onSuccess) for (const onSuccessHandler of configuration.eventHandlers.onSuccess)onSuccessHandler({
|
|
284
|
-
request,
|
|
285
|
-
result: resultParsed.data,
|
|
286
|
-
requestPayload: requestPayload,
|
|
287
|
-
flags
|
|
288
|
-
});
|
|
289
|
-
if (configuration.customResponseLogic?.onSuccess) return await configuration.customResponseLogic.onSuccess({
|
|
290
|
-
req: request,
|
|
291
|
-
response: resultParsed.data
|
|
292
|
-
});
|
|
293
|
-
return server_namespaceObject.NextResponse.json(resultParsed.data, {
|
|
294
|
-
status: 200
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
if (configuration.eventHandlers?.onSuccess) for (const onSuccessHanlder of configuration.eventHandlers.onSuccess)await onSuccessHanlder({
|
|
298
|
-
request,
|
|
299
|
-
result: void 0,
|
|
300
|
-
requestPayload: requestPayload,
|
|
301
|
-
flags
|
|
302
|
-
});
|
|
303
|
-
if (configuration.customResponseLogic?.onSuccess) return await configuration.customResponseLogic.onSuccess({
|
|
304
|
-
req: request,
|
|
305
|
-
response: {}
|
|
306
|
-
});
|
|
307
|
-
return server_namespaceObject.NextResponse.json({}, {
|
|
308
|
-
status: 200
|
|
309
|
-
});
|
|
310
|
-
} catch (e) {
|
|
311
|
-
let controllerError = e;
|
|
312
|
-
if (!ErrorFactory.isControllerError(e)) controllerError = errorFactory.newError({
|
|
313
|
-
error: 'Internal error',
|
|
314
|
-
statusCode: 500
|
|
315
|
-
}, e);
|
|
316
|
-
if (!controllerError.details || 'object' != typeof controllerError.details) controllerError.details = {};
|
|
317
|
-
if (sharedConfig.onErrorHandlers) try {
|
|
318
|
-
for (const sharedOnError of sharedConfig.onErrorHandlers)await sharedOnError({
|
|
319
|
-
error: controllerError,
|
|
320
|
-
req: request
|
|
321
|
-
});
|
|
322
|
-
} catch (errorFromOnErrorHandler) {
|
|
323
|
-
controllerError = errorFactory.newError({
|
|
324
|
-
error: 'onError handler error',
|
|
325
|
-
statusCode: 500
|
|
326
|
-
}, errorFromOnErrorHandler);
|
|
327
|
-
}
|
|
328
|
-
if (configuration.eventHandlers?.onError) try {
|
|
329
|
-
for (const onErrorHandler of configuration.eventHandlers.onError)await onErrorHandler({
|
|
330
|
-
error: controllerError,
|
|
331
|
-
req: request
|
|
332
|
-
});
|
|
333
|
-
} catch (errorFromOnErrorHandler) {
|
|
334
|
-
controllerError = errorFactory.newError({
|
|
335
|
-
error: 'onError handler error',
|
|
336
|
-
statusCode: 500
|
|
337
|
-
}, errorFromOnErrorHandler);
|
|
338
|
-
}
|
|
339
|
-
if (configuration.customResponseLogic?.onError) return await configuration.customResponseLogic.onError({
|
|
340
|
-
req: request,
|
|
341
|
-
error: controllerError
|
|
342
|
-
});
|
|
343
|
-
return server_namespaceObject.NextResponse.json({
|
|
344
|
-
message: controllerError.message,
|
|
345
|
-
details: controllerError.statusCode.toString().startsWith('4') ? controllerError.details : null,
|
|
346
|
-
code: controllerError.code
|
|
347
|
-
}, {
|
|
348
|
-
status: controllerError.statusCode
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
return endpointLogic;
|
|
353
|
-
};
|
|
354
|
-
class Pagination {
|
|
355
|
-
data;
|
|
356
|
-
static schema = external_zod_default().object({
|
|
357
|
-
zeroBasedIndex: external_zod_default().coerce.number().int().nonnegative(),
|
|
358
|
-
pageSize: external_zod_default().coerce.number().int().positive()
|
|
359
|
-
});
|
|
360
|
-
constructor(data){
|
|
361
|
-
this.data = data;
|
|
362
|
-
}
|
|
363
|
-
static create = (data)=>{
|
|
364
|
-
const parsedModel = Pagination.schema.parse(data);
|
|
365
|
-
return new Pagination(parsedModel);
|
|
366
|
-
};
|
|
367
|
-
get model() {
|
|
368
|
-
return this.data;
|
|
369
|
-
}
|
|
370
|
-
same = (pagination)=>pagination.model.pageSize === this.model.pageSize && pagination.model.zeroBasedIndex === this.model.zeroBasedIndex;
|
|
371
|
-
get nextPage() {
|
|
372
|
-
return Pagination.create({
|
|
373
|
-
pageSize: this.data.pageSize,
|
|
374
|
-
zeroBasedIndex: this.data.zeroBasedIndex + 1
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
get previousPage() {
|
|
378
|
-
if (0 === this.model.zeroBasedIndex) return Pagination.create(this.model);
|
|
379
|
-
return Pagination.create({
|
|
380
|
-
pageSize: this.model.pageSize,
|
|
381
|
-
zeroBasedIndex: this.model.zeroBasedIndex - 1
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
get previousPage_UNSAFE() {
|
|
385
|
-
return Pagination.create({
|
|
386
|
-
pageSize: this.model.pageSize,
|
|
387
|
-
zeroBasedIndex: this.model.zeroBasedIndex - 1
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
const mapStoreSchemasToModuleMetadata = (schemas, name)=>({
|
|
392
|
-
name,
|
|
393
|
-
schemas: {
|
|
394
|
-
...schemas.customOperations ?? {},
|
|
395
|
-
list: {
|
|
396
|
-
payload: external_zod_default().object({
|
|
397
|
-
filter: schemas.searchPayload.list,
|
|
398
|
-
pagination: Pagination.schema.optional()
|
|
399
|
-
}),
|
|
400
|
-
response: external_zod_default().array(schemas.models.list)
|
|
401
|
-
},
|
|
402
|
-
details: {
|
|
403
|
-
payload: external_zod_default().object({
|
|
404
|
-
filter: schemas.searchPayload.specific
|
|
405
|
-
}),
|
|
406
|
-
response: schemas.models.details.nullable()
|
|
407
|
-
},
|
|
408
|
-
create: {
|
|
409
|
-
payload: external_zod_default().object({
|
|
410
|
-
payload: schemas.actionsPayload.create
|
|
411
|
-
}),
|
|
412
|
-
response: external_zod_default().object({
|
|
413
|
-
id: external_zod_default().string()
|
|
414
|
-
})
|
|
415
|
-
},
|
|
416
|
-
updateOne: {
|
|
417
|
-
payload: external_zod_default().object({
|
|
418
|
-
filter: schemas.searchPayload.specific,
|
|
419
|
-
payload: schemas.actionsPayload.update
|
|
420
|
-
}),
|
|
421
|
-
response: external_zod_default().object({
|
|
422
|
-
success: external_zod_default().boolean()
|
|
423
|
-
})
|
|
424
|
-
},
|
|
425
|
-
deleteOne: {
|
|
426
|
-
payload: external_zod_default().object({
|
|
427
|
-
filter: schemas.searchPayload.specific
|
|
428
|
-
}),
|
|
429
|
-
response: external_zod_default().object({
|
|
430
|
-
success: external_zod_default().boolean()
|
|
431
|
-
})
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
});
|
|
435
|
-
const store_zod_utils_methods = (schemas)=>{
|
|
436
|
-
const data = mapStoreSchemasToModuleMetadata(schemas, schemas.name);
|
|
437
|
-
return methods(data);
|
|
438
|
-
};
|
|
439
|
-
const InRAM = (schemas, options)=>{
|
|
440
|
-
class RAMStore {
|
|
441
|
-
___data = [];
|
|
442
|
-
method = store_zod_utils_methods(schemas);
|
|
443
|
-
___listSearchLogic = (entity, pattern)=>{
|
|
444
|
-
const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
|
|
445
|
-
const entityAsObject = entity;
|
|
446
|
-
return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
|
|
447
|
-
};
|
|
448
|
-
___specificSearchLogic = (entity, pattern)=>{
|
|
449
|
-
const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
|
|
450
|
-
const entityAsObject = entity;
|
|
451
|
-
return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
|
|
452
|
-
};
|
|
453
|
-
___mapDetailToList = (entity)=>entity;
|
|
454
|
-
___mapCreatePayloadToDetail = (payload)=>({
|
|
455
|
-
...payload,
|
|
456
|
-
id: Math.random().toString()
|
|
457
|
-
});
|
|
458
|
-
___mapUpdatePayloadToDetail = (prevValue, rawUpdate)=>{
|
|
459
|
-
const update = Object.fromEntries(Object.entries(rawUpdate).filter(([, value])=>void 0 !== value));
|
|
460
|
-
return {
|
|
461
|
-
...prevValue,
|
|
462
|
-
...update
|
|
463
|
-
};
|
|
464
|
-
};
|
|
465
|
-
constructor(){
|
|
466
|
-
if (!options) return;
|
|
467
|
-
if (options.searchLogic) {
|
|
468
|
-
if (options.searchLogic.list) this.___listSearchLogic = options.searchLogic.list;
|
|
469
|
-
if (options.searchLogic.specific) this.___specificSearchLogic = options.searchLogic.specific;
|
|
470
|
-
}
|
|
471
|
-
if (options.mappers) {
|
|
472
|
-
if (options.mappers.detailToList) this.___mapDetailToList = options.mappers.detailToList;
|
|
473
|
-
if (options.mappers.createPayloadToDetail) this.___mapCreatePayloadToDetail = options.mappers.createPayloadToDetail;
|
|
474
|
-
if (options.mappers.updatePayloadToDetail) this.___mapUpdatePayloadToDetail = options.mappers.updatePayloadToDetail;
|
|
475
|
-
}
|
|
476
|
-
if (options.initialData) this.___data = [
|
|
477
|
-
...options.initialData
|
|
478
|
-
];
|
|
479
|
-
}
|
|
480
|
-
list = this.method('list', async ({ filter, pagination = {
|
|
481
|
-
pageSize: 1000,
|
|
482
|
-
zeroBasedIndex: 0
|
|
483
|
-
} })=>{
|
|
484
|
-
schemas.searchPayload.list.parse(filter);
|
|
485
|
-
Pagination.schema.parse(pagination);
|
|
486
|
-
const afterFiltration = this.___data.filter((x)=>this.___listSearchLogic(x, filter));
|
|
487
|
-
const afterPagination = afterFiltration.filter((_, i)=>i >= pagination.pageSize * pagination.zeroBasedIndex && i < (pagination.zeroBasedIndex + 1) * pagination.pageSize);
|
|
488
|
-
const result = afterPagination.map((x)=>this.___mapDetailToList(x));
|
|
489
|
-
external_zod_default().array(schemas.models.list).parse(result);
|
|
490
|
-
return result;
|
|
491
|
-
});
|
|
492
|
-
details = this.method('details', async ({ filter })=>{
|
|
493
|
-
const details = this.___data.find((x)=>this.___specificSearchLogic(x, filter));
|
|
494
|
-
if (!details) return null;
|
|
495
|
-
return details;
|
|
496
|
-
});
|
|
497
|
-
create = this.method('create', async ({ payload })=>{
|
|
498
|
-
const newItem = this.___mapCreatePayloadToDetail(payload);
|
|
499
|
-
this.___data = this.___data.concat(newItem);
|
|
500
|
-
return {
|
|
501
|
-
id: newItem.id
|
|
502
|
-
};
|
|
503
|
-
});
|
|
504
|
-
updateOne = this.method('updateOne', async ({ filter, payload })=>{
|
|
505
|
-
const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
|
|
506
|
-
this.___data = this.___data.map((x, i)=>{
|
|
507
|
-
if (i !== index) return x;
|
|
508
|
-
return this.___mapUpdatePayloadToDetail(x, payload);
|
|
509
|
-
});
|
|
510
|
-
return {
|
|
511
|
-
success: index > -1
|
|
512
|
-
};
|
|
513
|
-
});
|
|
514
|
-
deleteOne = this.method('deleteOne', async ({ filter })=>{
|
|
515
|
-
const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
|
|
516
|
-
this.___data = this.___data.filter((_x, i)=>i !== index);
|
|
517
|
-
return {
|
|
518
|
-
success: index > -1
|
|
519
|
-
};
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
return RAMStore;
|
|
523
|
-
};
|
|
524
|
-
const external_node_crypto_namespaceObject = require("node:crypto");
|
|
525
|
-
class Identifier {
|
|
526
|
-
data;
|
|
527
|
-
static schema = external_zod_default().string().nonempty();
|
|
528
|
-
constructor(data){
|
|
529
|
-
this.data = data;
|
|
530
|
-
}
|
|
531
|
-
static create = (data)=>new Identifier(Identifier.schema.parse(data));
|
|
532
|
-
static get randomUUID() {
|
|
533
|
-
return Identifier.create((0, external_node_crypto_namespaceObject.randomUUID)());
|
|
534
|
-
}
|
|
535
|
-
get model() {
|
|
536
|
-
return this.data;
|
|
537
|
-
}
|
|
538
|
-
get isUUID() {
|
|
539
|
-
return external_zod_default().uuidv4().safeParse(this.data).success;
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
exports.Controller = __webpack_exports__.Controller;
|
|
543
|
-
exports.Module = __webpack_exports__.Module;
|
|
544
|
-
exports.Store = __webpack_exports__.Store;
|
|
545
|
-
exports.ValueObjects = __webpack_exports__.ValueObjects;
|
|
546
|
-
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
547
|
-
"Controller",
|
|
548
|
-
"Module",
|
|
549
|
-
"Store",
|
|
550
|
-
"ValueObjects"
|
|
551
|
-
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
552
|
-
Object.defineProperty(exports, '__esModule', {
|
|
553
|
-
value: true
|
|
554
|
-
});
|
|
1
|
+
"use strict";const __rslib_import_meta_url__="u"<typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_require__={};__webpack_require__.n=e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return __webpack_require__.d(r,{a:r}),r},__webpack_require__.d=(e,r)=>{for(var t in r)__webpack_require__.o(r,t)&&!__webpack_require__.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:r[t]})},__webpack_require__.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),__webpack_require__.r=e=>{"u">typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var __webpack_exports__={};__webpack_require__.r(__webpack_exports__),__webpack_require__.d(__webpack_exports__,{Controller:()=>zod_controller_utils_namespaceObject,Store:()=>store_namespaceObject,Module:()=>zod_module_utils_namespaceObject,ValueObjects:()=>entities_namespaceObject});var zod_module_utils_namespaceObject={};__webpack_require__.r(zod_module_utils_namespaceObject),__webpack_require__.d(zod_module_utils_namespaceObject,{methods:()=>methods});var zod_controller_utils_namespaceObject={};__webpack_require__.r(zod_controller_utils_namespaceObject),__webpack_require__.d(zod_controller_utils_namespaceObject,{DefaultErrorCodes:()=>zod_controller_utils_DefaultErrorCodes,endpoints:()=>endpoints});var store_namespaceObject={};__webpack_require__.r(store_namespaceObject),__webpack_require__.d(store_namespaceObject,{InRAM:()=>InRAM,methods:()=>store_zod_utils_methods});var entities_namespaceObject={};__webpack_require__.r(entities_namespaceObject),__webpack_require__.d(entities_namespaceObject,{Identifier:()=>Identifier,Pagination:()=>Pagination});class ErrorFactory{static isControllerError=e=>{if(!e||"object"!=typeof e)return!1;let r=Object.keys(e);return["name","message","code","module","method","timestamp","statusCode"].every(e=>r.includes(e))};static isModuleError=e=>{if("object"!=typeof e||!e)return!1;let r=Object.keys(e);return!!["name","message","code","module","method","timestamp"].every(e=>r.includes(e))&&!r.includes("statusCode")};static base=({error:e,code:r,details:t})=>{let a=e instanceof Error?e:Error("unknown error");return("string"==typeof e||"number"==typeof e)&&(a=Error(e?.toString())),"object"==typeof e&&null!==e&&(a=e instanceof Error?e:Error(JSON.stringify(e))),{message:a.message,name:a.name,timestamp:Date.now(),code:r||a.name,details:t||null}};static forModule=e=>({inMethod:r=>({newError:(t,a)=>({...ErrorFactory.base(t),cause:a?ErrorFactory.fromUnknownError(a):null,module:e,method:r})})});static forController=e=>({inMethod:r=>({newError:(t,a)=>{let o=ErrorFactory.base(t),s=a?ErrorFactory.fromUnknownError(a):null;return!t.code&&s?.code&&(o.code=s.code),{...o,cause:s,module:e,method:r,statusCode:t.statusCode}}})});static fromUnknownError=e=>ErrorFactory.isModuleError(e)||(e=>{if("object"!=typeof e||!e)return!1;let r=Object.keys(e);return["name","message","code","timestamp"].every(e=>r.includes(e))})(e)?e:ErrorFactory.base({error:e})}const methods=(e,r={})=>(t,a,o={})=>{let{name:s,schemas:n}=e;return async e=>{try{let r=n[t].payload.parse(e),o=await a(r,{methodError:(e,a)=>ErrorFactory.forModule(s).inMethod(t).newError("string"==typeof e?{error:e,code:e,details:r}:e,a)});return n[t].response.parse(o)}catch(a){if(ErrorFactory.isModuleError(a))throw r.onError&&await r.onError(a),o.onError&&await o.onError(a),a;let e=ErrorFactory.forModule(s).inMethod(t).newError({error:"Caught unhandled module error (see `cause` field for details)"},a);throw o.onError&&await o.onError(e),e}}},server_namespaceObject=require("next/server"),external_zod_namespaceObject=require("zod");var external_zod_default=__webpack_require__.n(external_zod_namespaceObject),zod_controller_utils_DefaultErrorCodes=function(e){return e.REQUEST_PARSING="ZOD-CONTROLLER___REQUEST-PARSING",e.RESPONSE_PARSING="ZOD-CONTROLLER___RESPONSE-PARSING",e}({});const endpoints=(e,r={})=>(t,a,o={})=>async s=>{let n={},i=ErrorFactory.forController(e.name).inMethod(t);try{let _=(e,r,t)=>"string"==typeof e?i.newError({statusCode:r??500,code:e,error:e,details:n},t):i.newError({...e,statusCode:r??500},t);if(r.guards)for(let e of r.guards){let r=await e({request:s,endpointError:_});if(r)throw r}if(o.guards)for(let e of o.guards){let r=await e({request:s,endpointError:_});if(r)throw r}let d=e.schemas[t];if(!d)throw i.newError({error:"No schemas were found for the endpoint",statusCode:500});if(d.query){let e=Object.fromEntries(s.nextUrl.searchParams.entries()),r=d.query.safeParse(e);if(!r.success)throw i.newError({error:r.error.message,statusCode:400,code:"ZOD-CONTROLLER___REQUEST-PARSING",details:external_zod_default().treeifyError(r.error)});n={...n,...r.data}}if(d.body){let e=await s.json(),r=d.body.safeParse(e);if(!r.success)throw i.newError({error:r.error.message,statusCode:400,code:"ZOD-CONTROLLER___REQUEST-PARSING",details:external_zod_default().treeifyError(r.error)});n={...n,...r.data}}let c={},l=await a(n,{request:s,flags:c,endpointError:(e,r,t)=>"string"==typeof e?i.newError({statusCode:r??500,code:e,error:e,details:n},t):i.newError({...e,statusCode:r??500},t)});if(d.response){let e=d.response.safeParse(l);if(!e.success)throw i.newError({error:e.error.message,statusCode:500,code:"ZOD-CONTROLLER___RESPONSE-PARSING",details:external_zod_default().treeifyError(e.error)});if(o.eventHandlers?.onSuccess)for(let r of o.eventHandlers.onSuccess)r({request:s,result:e.data,requestPayload:n,flags:c});if(o.customResponseLogic?.onSuccess)return await o.customResponseLogic.onSuccess({req:s,response:e.data});return server_namespaceObject.NextResponse.json(e.data,{status:200})}if(o.eventHandlers?.onSuccess)for(let e of o.eventHandlers.onSuccess)await e({request:s,result:void 0,requestPayload:n,flags:c});if(o.customResponseLogic?.onSuccess)return await o.customResponseLogic.onSuccess({req:s,response:{}});return server_namespaceObject.NextResponse.json({},{status:200})}catch(t){let e=t;if(ErrorFactory.isControllerError(t)||(e=i.newError({error:"Internal error",statusCode:500},t)),e.details&&"object"==typeof e.details||(e.details={}),r.onErrorHandlers)try{for(let t of r.onErrorHandlers)await t({error:e,req:s})}catch(r){e=i.newError({error:"onError handler error",statusCode:500},r)}if(o.eventHandlers?.onError)try{for(let r of o.eventHandlers.onError)await r({error:e,req:s})}catch(r){e=i.newError({error:"onError handler error",statusCode:500},r)}if(o.customResponseLogic?.onError)return await o.customResponseLogic.onError({req:s,error:e});return server_namespaceObject.NextResponse.json({message:e.message,details:e.statusCode.toString().startsWith("4")?e.details:null,code:e.code},{status:e.statusCode})}};class Pagination{data;static schema=external_zod_default().object({zeroBasedIndex:external_zod_default().coerce.number().int().nonnegative(),pageSize:external_zod_default().coerce.number().int().positive()});constructor(e){this.data=e}static create=e=>{let r=Pagination.schema.parse(e);return new Pagination(r)};get model(){return this.data}same=e=>e.model.pageSize===this.model.pageSize&&e.model.zeroBasedIndex===this.model.zeroBasedIndex;get nextPage(){return Pagination.create({pageSize:this.data.pageSize,zeroBasedIndex:this.data.zeroBasedIndex+1})}get previousPage(){return 0===this.model.zeroBasedIndex?Pagination.create(this.model):Pagination.create({pageSize:this.model.pageSize,zeroBasedIndex:this.model.zeroBasedIndex-1})}get previousPage_UNSAFE(){return Pagination.create({pageSize:this.model.pageSize,zeroBasedIndex:this.model.zeroBasedIndex-1})}}const mapStoreSchemasToModuleMetadata=(e,r)=>({name:r,schemas:{...e.customOperations??{},list:{payload:external_zod_default().object({filter:e.searchPayload.list,pagination:Pagination.schema.optional()}),response:external_zod_default().array(e.models.list)},details:{payload:external_zod_default().object({filter:e.searchPayload.specific}),response:e.models.details.nullable()},create:{payload:external_zod_default().object({payload:e.actionsPayload.create}),response:external_zod_default().object({id:external_zod_default().string()})},updateOne:{payload:external_zod_default().object({filter:e.searchPayload.specific,payload:e.actionsPayload.update}),response:external_zod_default().object({success:external_zod_default().boolean()})},deleteOne:{payload:external_zod_default().object({filter:e.searchPayload.specific}),response:external_zod_default().object({success:external_zod_default().boolean()})}}}),store_zod_utils_methods=e=>methods(mapStoreSchemasToModuleMetadata(e,e.name)),InRAM=(e,r)=>{class t{___data=[];method=store_zod_utils_methods(e);___listSearchLogic=(e,r)=>{let t=Object.entries(r).filter(([e,r])=>!!r).map(e=>e[0]);return Object.entries(e).filter(([e])=>t.includes(e)).every(([e,t])=>t===r[e]||t?.toString()?.includes(r[e]?.toString()))};___specificSearchLogic=(e,r)=>{let t=Object.entries(r).filter(([e,r])=>!!r).map(e=>e[0]);return Object.entries(e).filter(([e])=>t.includes(e)).every(([e,t])=>t===r[e]||t?.toString()?.includes(r[e]?.toString()))};___mapDetailToList=e=>e;___mapCreatePayloadToDetail=e=>({...e,id:Math.random().toString()});___mapUpdatePayloadToDetail=(e,r)=>{let t=Object.fromEntries(Object.entries(r).filter(([,e])=>void 0!==e));return{...e,...t}};constructor(){if(!r)return;r.searchLogic&&(r.searchLogic.list&&(this.___listSearchLogic=r.searchLogic.list),r.searchLogic.specific&&(this.___specificSearchLogic=r.searchLogic.specific)),r.mappers&&(r.mappers.detailToList&&(this.___mapDetailToList=r.mappers.detailToList),r.mappers.createPayloadToDetail&&(this.___mapCreatePayloadToDetail=r.mappers.createPayloadToDetail),r.mappers.updatePayloadToDetail&&(this.___mapUpdatePayloadToDetail=r.mappers.updatePayloadToDetail)),r.initialData&&(this.___data=[...r.initialData])}list=this.method("list",async({filter:r,pagination:t={pageSize:1e3,zeroBasedIndex:0}})=>{e.searchPayload.list.parse(r),Pagination.schema.parse(t);let a=this.___data.filter(e=>this.___listSearchLogic(e,r)).filter((e,r)=>r>=t.pageSize*t.zeroBasedIndex&&r<(t.zeroBasedIndex+1)*t.pageSize).map(e=>this.___mapDetailToList(e));return external_zod_default().array(e.models.list).parse(a),a});details=this.method("details",async({filter:e})=>{let r=this.___data.find(r=>this.___specificSearchLogic(r,e));return r||null});create=this.method("create",async({payload:e})=>{let r=this.___mapCreatePayloadToDetail(e);return this.___data=this.___data.concat(r),{id:r.id}});updateOne=this.method("updateOne",async({filter:e,payload:r})=>{let t=this.___data.findIndex(r=>this.___specificSearchLogic(r,e));return this.___data=this.___data.map((e,a)=>a!==t?e:this.___mapUpdatePayloadToDetail(e,r)),{success:t>-1}});deleteOne=this.method("deleteOne",async({filter:e})=>{let r=this.___data.findIndex(r=>this.___specificSearchLogic(r,e));return this.___data=this.___data.filter((e,t)=>t!==r),{success:r>-1}})}return t},external_node_crypto_namespaceObject=require("node:crypto");class Identifier{data;static schema=external_zod_default().string().nonempty();constructor(e){this.data=e}static create=e=>new Identifier(Identifier.schema.parse(e));static get randomUUID(){return Identifier.create((0,external_node_crypto_namespaceObject.randomUUID)())}get model(){return this.data}get isUUID(){return external_zod_default().uuidv4().safeParse(this.data).success}}for(var __rspack_i in exports.Controller=__webpack_exports__.Controller,exports.Module=__webpack_exports__.Module,exports.Store=__webpack_exports__.Store,exports.ValueObjects=__webpack_exports__.ValueObjects,__webpack_exports__)-1===["Controller","Module","Store","ValueObjects"].indexOf(__rspack_i)&&(exports[__rspack_i]=__webpack_exports__[__rspack_i]);Object.defineProperty(exports,"__esModule",{value:!0});
|
package/dist/index.js
CHANGED
|
@@ -1,502 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { NextResponse } from "next/server";
|
|
3
|
-
import zod from "zod";
|
|
4
|
-
import { randomUUID } from "node:crypto";
|
|
5
|
-
var entities_namespaceObject = {};
|
|
6
|
-
__webpack_require__.r(entities_namespaceObject);
|
|
7
|
-
__webpack_require__.d(entities_namespaceObject, {
|
|
8
|
-
Identifier: ()=>Identifier,
|
|
9
|
-
Pagination: ()=>Pagination
|
|
10
|
-
});
|
|
11
|
-
var store_namespaceObject = {};
|
|
12
|
-
__webpack_require__.r(store_namespaceObject);
|
|
13
|
-
__webpack_require__.d(store_namespaceObject, {
|
|
14
|
-
InRAM: ()=>InRAM,
|
|
15
|
-
methods: ()=>store_zod_utils_methods
|
|
16
|
-
});
|
|
17
|
-
var zod_controller_utils_namespaceObject = {};
|
|
18
|
-
__webpack_require__.r(zod_controller_utils_namespaceObject);
|
|
19
|
-
__webpack_require__.d(zod_controller_utils_namespaceObject, {
|
|
20
|
-
DefaultErrorCodes: ()=>zod_controller_utils_DefaultErrorCodes,
|
|
21
|
-
endpoints: ()=>endpoints
|
|
22
|
-
});
|
|
23
|
-
var zod_module_utils_namespaceObject = {};
|
|
24
|
-
__webpack_require__.r(zod_module_utils_namespaceObject);
|
|
25
|
-
__webpack_require__.d(zod_module_utils_namespaceObject, {
|
|
26
|
-
methods: ()=>methods
|
|
27
|
-
});
|
|
28
|
-
class ErrorFactory {
|
|
29
|
-
static isControllerError = (error)=>{
|
|
30
|
-
if (!error || 'object' != typeof error) return false;
|
|
31
|
-
const argKeys = Object.keys(error);
|
|
32
|
-
const requiredKeys = [
|
|
33
|
-
'name',
|
|
34
|
-
'message',
|
|
35
|
-
'code',
|
|
36
|
-
'module',
|
|
37
|
-
'method',
|
|
38
|
-
'timestamp',
|
|
39
|
-
'statusCode'
|
|
40
|
-
];
|
|
41
|
-
return requiredKeys.every((x)=>argKeys.includes(x));
|
|
42
|
-
};
|
|
43
|
-
static isModuleError = (arg)=>{
|
|
44
|
-
if ('object' != typeof arg || !arg) return false;
|
|
45
|
-
const argKeys = Object.keys(arg);
|
|
46
|
-
const requiredKeys = [
|
|
47
|
-
'name',
|
|
48
|
-
'message',
|
|
49
|
-
'code',
|
|
50
|
-
'module',
|
|
51
|
-
'method',
|
|
52
|
-
'timestamp'
|
|
53
|
-
];
|
|
54
|
-
if (!requiredKeys.every((x)=>argKeys.includes(x))) return false;
|
|
55
|
-
return !argKeys.includes('statusCode');
|
|
56
|
-
};
|
|
57
|
-
static base = ({ error, code, details })=>{
|
|
58
|
-
let err = error instanceof Error ? error : new Error('unknown error');
|
|
59
|
-
if ('string' == typeof error || 'number' == typeof error) err = new Error(error?.toString());
|
|
60
|
-
if ('object' == typeof error && null !== error) if (error instanceof Error) err = error;
|
|
61
|
-
else {
|
|
62
|
-
const jsonRepresentation = JSON.stringify(error);
|
|
63
|
-
err = new Error(jsonRepresentation);
|
|
64
|
-
}
|
|
65
|
-
return {
|
|
66
|
-
message: err.message,
|
|
67
|
-
name: err.name,
|
|
68
|
-
timestamp: Date.now(),
|
|
69
|
-
code: code || err.name,
|
|
70
|
-
details: details || null
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
static forModule = (serviceName)=>({
|
|
74
|
-
inMethod: (methodName)=>({
|
|
75
|
-
newError: (payload, cause)=>{
|
|
76
|
-
const errorBase = ErrorFactory.base(payload);
|
|
77
|
-
return {
|
|
78
|
-
...errorBase,
|
|
79
|
-
cause: cause ? ErrorFactory.fromUnknownError(cause) : null,
|
|
80
|
-
module: serviceName,
|
|
81
|
-
method: methodName
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
})
|
|
85
|
-
});
|
|
86
|
-
static forController = (controllerName)=>({
|
|
87
|
-
inMethod: (methodName)=>({
|
|
88
|
-
newError: (payload, cause)=>{
|
|
89
|
-
const errorBase = ErrorFactory.base(payload);
|
|
90
|
-
const formattedCause = cause ? ErrorFactory.fromUnknownError(cause) : null;
|
|
91
|
-
if (!payload.code && formattedCause?.code) errorBase.code = formattedCause.code;
|
|
92
|
-
return {
|
|
93
|
-
...errorBase,
|
|
94
|
-
cause: formattedCause,
|
|
95
|
-
module: controllerName,
|
|
96
|
-
method: methodName,
|
|
97
|
-
statusCode: payload.statusCode
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
});
|
|
102
|
-
static fromUnknownError = (error)=>{
|
|
103
|
-
const isErrorBase = (arg)=>{
|
|
104
|
-
if ('object' != typeof arg || !arg) return false;
|
|
105
|
-
const argKeys = Object.keys(arg);
|
|
106
|
-
const requiredKeys = [
|
|
107
|
-
'name',
|
|
108
|
-
'message',
|
|
109
|
-
'code',
|
|
110
|
-
'timestamp'
|
|
111
|
-
];
|
|
112
|
-
return requiredKeys.every((x)=>argKeys.includes(x));
|
|
113
|
-
};
|
|
114
|
-
if (ErrorFactory.isModuleError(error)) return error;
|
|
115
|
-
if (isErrorBase(error)) return error;
|
|
116
|
-
return ErrorFactory.base({
|
|
117
|
-
error
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
const methods = (metadata, sharedConfig = {})=>(methodName, handler, config = {})=>{
|
|
122
|
-
const { name, schemas } = metadata;
|
|
123
|
-
return async (payload)=>{
|
|
124
|
-
try {
|
|
125
|
-
const parsedPayload = schemas[methodName].payload.parse(payload);
|
|
126
|
-
const response = await handler(parsedPayload, {
|
|
127
|
-
methodError: (payload, cause)=>ErrorFactory.forModule(name).inMethod(methodName).newError('string' == typeof payload ? {
|
|
128
|
-
error: payload,
|
|
129
|
-
code: payload,
|
|
130
|
-
details: parsedPayload
|
|
131
|
-
} : payload, cause)
|
|
132
|
-
});
|
|
133
|
-
const parsedResponse = schemas[methodName].response.parse(response);
|
|
134
|
-
return parsedResponse;
|
|
135
|
-
} catch (error) {
|
|
136
|
-
if (ErrorFactory.isModuleError(error)) {
|
|
137
|
-
if (sharedConfig.onError) await sharedConfig.onError(error);
|
|
138
|
-
if (config.onError) await config.onError(error);
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
const serviceErrorGenerator = ErrorFactory.forModule(name).inMethod(methodName);
|
|
142
|
-
const serviceError = serviceErrorGenerator.newError({
|
|
143
|
-
error: 'Caught unhandled module error (see `cause` field for details)'
|
|
144
|
-
}, error);
|
|
145
|
-
if (config.onError) await config.onError(serviceError);
|
|
146
|
-
throw serviceError;
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
};
|
|
150
|
-
var zod_controller_utils_DefaultErrorCodes = /*#__PURE__*/ function(DefaultErrorCodes) {
|
|
151
|
-
DefaultErrorCodes["REQUEST_PARSING"] = "ZOD-CONTROLLER___REQUEST-PARSING";
|
|
152
|
-
DefaultErrorCodes["RESPONSE_PARSING"] = "ZOD-CONTROLLER___RESPONSE-PARSING";
|
|
153
|
-
return DefaultErrorCodes;
|
|
154
|
-
}({});
|
|
155
|
-
const endpoints = (metadata, sharedConfig = {})=>{
|
|
156
|
-
const endpointLogic = (method, handler, configuration = {})=>async (request)=>{
|
|
157
|
-
let requestPayload = {};
|
|
158
|
-
const errorFactory = ErrorFactory.forController(metadata.name).inMethod(method);
|
|
159
|
-
try {
|
|
160
|
-
const endpointError = (payload, errorStatus, cause)=>{
|
|
161
|
-
if ('string' == typeof payload) return errorFactory.newError({
|
|
162
|
-
statusCode: errorStatus ?? 500,
|
|
163
|
-
code: payload,
|
|
164
|
-
error: payload,
|
|
165
|
-
details: requestPayload
|
|
166
|
-
}, cause);
|
|
167
|
-
return errorFactory.newError({
|
|
168
|
-
...payload,
|
|
169
|
-
statusCode: errorStatus ?? 500
|
|
170
|
-
}, cause);
|
|
171
|
-
};
|
|
172
|
-
if (sharedConfig.guards) for (const sharedGuard of sharedConfig.guards){
|
|
173
|
-
const error = await sharedGuard({
|
|
174
|
-
request,
|
|
175
|
-
endpointError
|
|
176
|
-
});
|
|
177
|
-
if (error) throw error;
|
|
178
|
-
}
|
|
179
|
-
if (configuration.guards) for (const endpointGuard of configuration.guards){
|
|
180
|
-
const error = await endpointGuard({
|
|
181
|
-
request,
|
|
182
|
-
endpointError
|
|
183
|
-
});
|
|
184
|
-
if (error) throw error;
|
|
185
|
-
}
|
|
186
|
-
const endpointSchemas = metadata.schemas[method];
|
|
187
|
-
if (!endpointSchemas) throw errorFactory.newError({
|
|
188
|
-
error: 'No schemas were found for the endpoint',
|
|
189
|
-
statusCode: 500
|
|
190
|
-
});
|
|
191
|
-
if (endpointSchemas.query) {
|
|
192
|
-
const queryParamsAsObject = Object.fromEntries(request.nextUrl.searchParams.entries());
|
|
193
|
-
const queryParamsParsed = endpointSchemas.query.safeParse(queryParamsAsObject);
|
|
194
|
-
if (!queryParamsParsed.success) throw errorFactory.newError({
|
|
195
|
-
error: queryParamsParsed.error.message,
|
|
196
|
-
statusCode: 400,
|
|
197
|
-
code: "ZOD-CONTROLLER___REQUEST-PARSING",
|
|
198
|
-
details: zod.treeifyError(queryParamsParsed.error)
|
|
199
|
-
});
|
|
200
|
-
requestPayload = {
|
|
201
|
-
...requestPayload,
|
|
202
|
-
...queryParamsParsed.data
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
if (endpointSchemas.body) {
|
|
206
|
-
const body = await request.json();
|
|
207
|
-
const bodyParsed = endpointSchemas.body.safeParse(body);
|
|
208
|
-
if (!bodyParsed.success) throw errorFactory.newError({
|
|
209
|
-
error: bodyParsed.error.message,
|
|
210
|
-
statusCode: 400,
|
|
211
|
-
code: "ZOD-CONTROLLER___REQUEST-PARSING",
|
|
212
|
-
details: zod.treeifyError(bodyParsed.error)
|
|
213
|
-
});
|
|
214
|
-
requestPayload = {
|
|
215
|
-
...requestPayload,
|
|
216
|
-
...bodyParsed.data
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
const flags = {};
|
|
220
|
-
const result = await handler(requestPayload, {
|
|
221
|
-
request,
|
|
222
|
-
flags,
|
|
223
|
-
endpointError: (payload, errorStatus, cause)=>{
|
|
224
|
-
if ('string' == typeof payload) return errorFactory.newError({
|
|
225
|
-
statusCode: errorStatus ?? 500,
|
|
226
|
-
code: payload,
|
|
227
|
-
error: payload,
|
|
228
|
-
details: requestPayload
|
|
229
|
-
}, cause);
|
|
230
|
-
return errorFactory.newError({
|
|
231
|
-
...payload,
|
|
232
|
-
statusCode: errorStatus ?? 500
|
|
233
|
-
}, cause);
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
if (endpointSchemas.response) {
|
|
237
|
-
const resultParsed = endpointSchemas.response.safeParse(result);
|
|
238
|
-
if (!resultParsed.success) throw errorFactory.newError({
|
|
239
|
-
error: resultParsed.error.message,
|
|
240
|
-
statusCode: 500,
|
|
241
|
-
code: "ZOD-CONTROLLER___RESPONSE-PARSING",
|
|
242
|
-
details: zod.treeifyError(resultParsed.error)
|
|
243
|
-
});
|
|
244
|
-
if (configuration.eventHandlers?.onSuccess) for (const onSuccessHandler of configuration.eventHandlers.onSuccess)onSuccessHandler({
|
|
245
|
-
request,
|
|
246
|
-
result: resultParsed.data,
|
|
247
|
-
requestPayload: requestPayload,
|
|
248
|
-
flags
|
|
249
|
-
});
|
|
250
|
-
if (configuration.customResponseLogic?.onSuccess) return await configuration.customResponseLogic.onSuccess({
|
|
251
|
-
req: request,
|
|
252
|
-
response: resultParsed.data
|
|
253
|
-
});
|
|
254
|
-
return NextResponse.json(resultParsed.data, {
|
|
255
|
-
status: 200
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
if (configuration.eventHandlers?.onSuccess) for (const onSuccessHanlder of configuration.eventHandlers.onSuccess)await onSuccessHanlder({
|
|
259
|
-
request,
|
|
260
|
-
result: void 0,
|
|
261
|
-
requestPayload: requestPayload,
|
|
262
|
-
flags
|
|
263
|
-
});
|
|
264
|
-
if (configuration.customResponseLogic?.onSuccess) return await configuration.customResponseLogic.onSuccess({
|
|
265
|
-
req: request,
|
|
266
|
-
response: {}
|
|
267
|
-
});
|
|
268
|
-
return NextResponse.json({}, {
|
|
269
|
-
status: 200
|
|
270
|
-
});
|
|
271
|
-
} catch (e) {
|
|
272
|
-
let controllerError = e;
|
|
273
|
-
if (!ErrorFactory.isControllerError(e)) controllerError = errorFactory.newError({
|
|
274
|
-
error: 'Internal error',
|
|
275
|
-
statusCode: 500
|
|
276
|
-
}, e);
|
|
277
|
-
if (!controllerError.details || 'object' != typeof controllerError.details) controllerError.details = {};
|
|
278
|
-
if (sharedConfig.onErrorHandlers) try {
|
|
279
|
-
for (const sharedOnError of sharedConfig.onErrorHandlers)await sharedOnError({
|
|
280
|
-
error: controllerError,
|
|
281
|
-
req: request
|
|
282
|
-
});
|
|
283
|
-
} catch (errorFromOnErrorHandler) {
|
|
284
|
-
controllerError = errorFactory.newError({
|
|
285
|
-
error: 'onError handler error',
|
|
286
|
-
statusCode: 500
|
|
287
|
-
}, errorFromOnErrorHandler);
|
|
288
|
-
}
|
|
289
|
-
if (configuration.eventHandlers?.onError) try {
|
|
290
|
-
for (const onErrorHandler of configuration.eventHandlers.onError)await onErrorHandler({
|
|
291
|
-
error: controllerError,
|
|
292
|
-
req: request
|
|
293
|
-
});
|
|
294
|
-
} catch (errorFromOnErrorHandler) {
|
|
295
|
-
controllerError = errorFactory.newError({
|
|
296
|
-
error: 'onError handler error',
|
|
297
|
-
statusCode: 500
|
|
298
|
-
}, errorFromOnErrorHandler);
|
|
299
|
-
}
|
|
300
|
-
if (configuration.customResponseLogic?.onError) return await configuration.customResponseLogic.onError({
|
|
301
|
-
req: request,
|
|
302
|
-
error: controllerError
|
|
303
|
-
});
|
|
304
|
-
return NextResponse.json({
|
|
305
|
-
message: controllerError.message,
|
|
306
|
-
details: controllerError.statusCode.toString().startsWith('4') ? controllerError.details : null,
|
|
307
|
-
code: controllerError.code
|
|
308
|
-
}, {
|
|
309
|
-
status: controllerError.statusCode
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
};
|
|
313
|
-
return endpointLogic;
|
|
314
|
-
};
|
|
315
|
-
class Pagination {
|
|
316
|
-
data;
|
|
317
|
-
static schema = zod.object({
|
|
318
|
-
zeroBasedIndex: zod.coerce.number().int().nonnegative(),
|
|
319
|
-
pageSize: zod.coerce.number().int().positive()
|
|
320
|
-
});
|
|
321
|
-
constructor(data){
|
|
322
|
-
this.data = data;
|
|
323
|
-
}
|
|
324
|
-
static create = (data)=>{
|
|
325
|
-
const parsedModel = Pagination.schema.parse(data);
|
|
326
|
-
return new Pagination(parsedModel);
|
|
327
|
-
};
|
|
328
|
-
get model() {
|
|
329
|
-
return this.data;
|
|
330
|
-
}
|
|
331
|
-
same = (pagination)=>pagination.model.pageSize === this.model.pageSize && pagination.model.zeroBasedIndex === this.model.zeroBasedIndex;
|
|
332
|
-
get nextPage() {
|
|
333
|
-
return Pagination.create({
|
|
334
|
-
pageSize: this.data.pageSize,
|
|
335
|
-
zeroBasedIndex: this.data.zeroBasedIndex + 1
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
get previousPage() {
|
|
339
|
-
if (0 === this.model.zeroBasedIndex) return Pagination.create(this.model);
|
|
340
|
-
return Pagination.create({
|
|
341
|
-
pageSize: this.model.pageSize,
|
|
342
|
-
zeroBasedIndex: this.model.zeroBasedIndex - 1
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
get previousPage_UNSAFE() {
|
|
346
|
-
return Pagination.create({
|
|
347
|
-
pageSize: this.model.pageSize,
|
|
348
|
-
zeroBasedIndex: this.model.zeroBasedIndex - 1
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
const mapStoreSchemasToModuleMetadata = (schemas, name)=>({
|
|
353
|
-
name,
|
|
354
|
-
schemas: {
|
|
355
|
-
...schemas.customOperations ?? {},
|
|
356
|
-
list: {
|
|
357
|
-
payload: zod.object({
|
|
358
|
-
filter: schemas.searchPayload.list,
|
|
359
|
-
pagination: Pagination.schema.optional()
|
|
360
|
-
}),
|
|
361
|
-
response: zod.array(schemas.models.list)
|
|
362
|
-
},
|
|
363
|
-
details: {
|
|
364
|
-
payload: zod.object({
|
|
365
|
-
filter: schemas.searchPayload.specific
|
|
366
|
-
}),
|
|
367
|
-
response: schemas.models.details.nullable()
|
|
368
|
-
},
|
|
369
|
-
create: {
|
|
370
|
-
payload: zod.object({
|
|
371
|
-
payload: schemas.actionsPayload.create
|
|
372
|
-
}),
|
|
373
|
-
response: zod.object({
|
|
374
|
-
id: zod.string()
|
|
375
|
-
})
|
|
376
|
-
},
|
|
377
|
-
updateOne: {
|
|
378
|
-
payload: zod.object({
|
|
379
|
-
filter: schemas.searchPayload.specific,
|
|
380
|
-
payload: schemas.actionsPayload.update
|
|
381
|
-
}),
|
|
382
|
-
response: zod.object({
|
|
383
|
-
success: zod.boolean()
|
|
384
|
-
})
|
|
385
|
-
},
|
|
386
|
-
deleteOne: {
|
|
387
|
-
payload: zod.object({
|
|
388
|
-
filter: schemas.searchPayload.specific
|
|
389
|
-
}),
|
|
390
|
-
response: zod.object({
|
|
391
|
-
success: zod.boolean()
|
|
392
|
-
})
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
});
|
|
396
|
-
const store_zod_utils_methods = (schemas)=>{
|
|
397
|
-
const data = mapStoreSchemasToModuleMetadata(schemas, schemas.name);
|
|
398
|
-
return methods(data);
|
|
399
|
-
};
|
|
400
|
-
const InRAM = (schemas, options)=>{
|
|
401
|
-
class RAMStore {
|
|
402
|
-
___data = [];
|
|
403
|
-
method = store_zod_utils_methods(schemas);
|
|
404
|
-
___listSearchLogic = (entity, pattern)=>{
|
|
405
|
-
const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
|
|
406
|
-
const entityAsObject = entity;
|
|
407
|
-
return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
|
|
408
|
-
};
|
|
409
|
-
___specificSearchLogic = (entity, pattern)=>{
|
|
410
|
-
const patternKeys = Object.entries(pattern).filter(([_, value])=>!!value).map((x)=>x[0]);
|
|
411
|
-
const entityAsObject = entity;
|
|
412
|
-
return Object.entries(entityAsObject).filter(([key])=>patternKeys.includes(key)).every(([key, value])=>value === pattern[key] || value?.toString()?.includes(pattern[key]?.toString()));
|
|
413
|
-
};
|
|
414
|
-
___mapDetailToList = (entity)=>entity;
|
|
415
|
-
___mapCreatePayloadToDetail = (payload)=>({
|
|
416
|
-
...payload,
|
|
417
|
-
id: Math.random().toString()
|
|
418
|
-
});
|
|
419
|
-
___mapUpdatePayloadToDetail = (prevValue, rawUpdate)=>{
|
|
420
|
-
const update = Object.fromEntries(Object.entries(rawUpdate).filter(([, value])=>void 0 !== value));
|
|
421
|
-
return {
|
|
422
|
-
...prevValue,
|
|
423
|
-
...update
|
|
424
|
-
};
|
|
425
|
-
};
|
|
426
|
-
constructor(){
|
|
427
|
-
if (!options) return;
|
|
428
|
-
if (options.searchLogic) {
|
|
429
|
-
if (options.searchLogic.list) this.___listSearchLogic = options.searchLogic.list;
|
|
430
|
-
if (options.searchLogic.specific) this.___specificSearchLogic = options.searchLogic.specific;
|
|
431
|
-
}
|
|
432
|
-
if (options.mappers) {
|
|
433
|
-
if (options.mappers.detailToList) this.___mapDetailToList = options.mappers.detailToList;
|
|
434
|
-
if (options.mappers.createPayloadToDetail) this.___mapCreatePayloadToDetail = options.mappers.createPayloadToDetail;
|
|
435
|
-
if (options.mappers.updatePayloadToDetail) this.___mapUpdatePayloadToDetail = options.mappers.updatePayloadToDetail;
|
|
436
|
-
}
|
|
437
|
-
if (options.initialData) this.___data = [
|
|
438
|
-
...options.initialData
|
|
439
|
-
];
|
|
440
|
-
}
|
|
441
|
-
list = this.method('list', async ({ filter, pagination = {
|
|
442
|
-
pageSize: 1000,
|
|
443
|
-
zeroBasedIndex: 0
|
|
444
|
-
} })=>{
|
|
445
|
-
schemas.searchPayload.list.parse(filter);
|
|
446
|
-
Pagination.schema.parse(pagination);
|
|
447
|
-
const afterFiltration = this.___data.filter((x)=>this.___listSearchLogic(x, filter));
|
|
448
|
-
const afterPagination = afterFiltration.filter((_, i)=>i >= pagination.pageSize * pagination.zeroBasedIndex && i < (pagination.zeroBasedIndex + 1) * pagination.pageSize);
|
|
449
|
-
const result = afterPagination.map((x)=>this.___mapDetailToList(x));
|
|
450
|
-
zod.array(schemas.models.list).parse(result);
|
|
451
|
-
return result;
|
|
452
|
-
});
|
|
453
|
-
details = this.method('details', async ({ filter })=>{
|
|
454
|
-
const details = this.___data.find((x)=>this.___specificSearchLogic(x, filter));
|
|
455
|
-
if (!details) return null;
|
|
456
|
-
return details;
|
|
457
|
-
});
|
|
458
|
-
create = this.method('create', async ({ payload })=>{
|
|
459
|
-
const newItem = this.___mapCreatePayloadToDetail(payload);
|
|
460
|
-
this.___data = this.___data.concat(newItem);
|
|
461
|
-
return {
|
|
462
|
-
id: newItem.id
|
|
463
|
-
};
|
|
464
|
-
});
|
|
465
|
-
updateOne = this.method('updateOne', async ({ filter, payload })=>{
|
|
466
|
-
const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
|
|
467
|
-
this.___data = this.___data.map((x, i)=>{
|
|
468
|
-
if (i !== index) return x;
|
|
469
|
-
return this.___mapUpdatePayloadToDetail(x, payload);
|
|
470
|
-
});
|
|
471
|
-
return {
|
|
472
|
-
success: index > -1
|
|
473
|
-
};
|
|
474
|
-
});
|
|
475
|
-
deleteOne = this.method('deleteOne', async ({ filter })=>{
|
|
476
|
-
const index = this.___data.findIndex((x)=>this.___specificSearchLogic(x, filter));
|
|
477
|
-
this.___data = this.___data.filter((_x, i)=>i !== index);
|
|
478
|
-
return {
|
|
479
|
-
success: index > -1
|
|
480
|
-
};
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
return RAMStore;
|
|
484
|
-
};
|
|
485
|
-
class Identifier {
|
|
486
|
-
data;
|
|
487
|
-
static schema = zod.string().nonempty();
|
|
488
|
-
constructor(data){
|
|
489
|
-
this.data = data;
|
|
490
|
-
}
|
|
491
|
-
static create = (data)=>new Identifier(Identifier.schema.parse(data));
|
|
492
|
-
static get randomUUID() {
|
|
493
|
-
return Identifier.create(randomUUID());
|
|
494
|
-
}
|
|
495
|
-
get model() {
|
|
496
|
-
return this.data;
|
|
497
|
-
}
|
|
498
|
-
get isUUID() {
|
|
499
|
-
return zod.uuidv4().safeParse(this.data).success;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
export { entities_namespaceObject as ValueObjects, store_namespaceObject as Store, zod_controller_utils_namespaceObject as Controller, zod_module_utils_namespaceObject as Module };
|
|
1
|
+
import{__webpack_require__ as e}from"./rslib-runtime.js";import{NextResponse as r}from"next/server";import t from"zod";import{randomUUID as a}from"node:crypto";var o,s={};e.r(s),e.d(s,{Identifier:()=>f,Pagination:()=>p});var i={};e.r(i),e.d(i,{InRAM:()=>h,methods:()=>_});var n={};e.r(n),e.d(n,{DefaultErrorCodes:()=>u,endpoints:()=>m});var d={};e.r(d),e.d(d,{methods:()=>l});class c{static isControllerError=e=>{if(!e||"object"!=typeof e)return!1;let r=Object.keys(e);return["name","message","code","module","method","timestamp","statusCode"].every(e=>r.includes(e))};static isModuleError=e=>{if("object"!=typeof e||!e)return!1;let r=Object.keys(e);return!!["name","message","code","module","method","timestamp"].every(e=>r.includes(e))&&!r.includes("statusCode")};static base=({error:e,code:r,details:t})=>{let a=e instanceof Error?e:Error("unknown error");return("string"==typeof e||"number"==typeof e)&&(a=Error(e?.toString())),"object"==typeof e&&null!==e&&(a=e instanceof Error?e:Error(JSON.stringify(e))),{message:a.message,name:a.name,timestamp:Date.now(),code:r||a.name,details:t||null}};static forModule=e=>({inMethod:r=>({newError:(t,a)=>({...c.base(t),cause:a?c.fromUnknownError(a):null,module:e,method:r})})});static forController=e=>({inMethod:r=>({newError:(t,a)=>{let o=c.base(t),s=a?c.fromUnknownError(a):null;return!t.code&&s?.code&&(o.code=s.code),{...o,cause:s,module:e,method:r,statusCode:t.statusCode}}})});static fromUnknownError=e=>c.isModuleError(e)||(e=>{if("object"!=typeof e||!e)return!1;let r=Object.keys(e);return["name","message","code","timestamp"].every(e=>r.includes(e))})(e)?e:c.base({error:e})}let l=(e,r={})=>(t,a,o={})=>{let{name:s,schemas:i}=e;return async e=>{try{let r=i[t].payload.parse(e),o=await a(r,{methodError:(e,a)=>c.forModule(s).inMethod(t).newError("string"==typeof e?{error:e,code:e,details:r}:e,a)});return i[t].response.parse(o)}catch(a){if(c.isModuleError(a))throw r.onError&&await r.onError(a),o.onError&&await o.onError(a),a;let e=c.forModule(s).inMethod(t).newError({error:"Caught unhandled module error (see `cause` field for details)"},a);throw o.onError&&await o.onError(e),e}}};var u=((o={}).REQUEST_PARSING="ZOD-CONTROLLER___REQUEST-PARSING",o.RESPONSE_PARSING="ZOD-CONTROLLER___RESPONSE-PARSING",o);let m=(e,a={})=>(o,s,i={})=>async n=>{let d={},l=c.forController(e.name).inMethod(o);try{let c=(e,r,t)=>"string"==typeof e?l.newError({statusCode:r??500,code:e,error:e,details:d},t):l.newError({...e,statusCode:r??500},t);if(a.guards)for(let e of a.guards){let r=await e({request:n,endpointError:c});if(r)throw r}if(i.guards)for(let e of i.guards){let r=await e({request:n,endpointError:c});if(r)throw r}let u=e.schemas[o];if(!u)throw l.newError({error:"No schemas were found for the endpoint",statusCode:500});if(u.query){let e=Object.fromEntries(n.nextUrl.searchParams.entries()),r=u.query.safeParse(e);if(!r.success)throw l.newError({error:r.error.message,statusCode:400,code:"ZOD-CONTROLLER___REQUEST-PARSING",details:t.treeifyError(r.error)});d={...d,...r.data}}if(u.body){let e=await n.json(),r=u.body.safeParse(e);if(!r.success)throw l.newError({error:r.error.message,statusCode:400,code:"ZOD-CONTROLLER___REQUEST-PARSING",details:t.treeifyError(r.error)});d={...d,...r.data}}let m={},p=await s(d,{request:n,flags:m,endpointError:(e,r,t)=>"string"==typeof e?l.newError({statusCode:r??500,code:e,error:e,details:d},t):l.newError({...e,statusCode:r??500},t)});if(u.response){let e=u.response.safeParse(p);if(!e.success)throw l.newError({error:e.error.message,statusCode:500,code:"ZOD-CONTROLLER___RESPONSE-PARSING",details:t.treeifyError(e.error)});if(i.eventHandlers?.onSuccess)for(let r of i.eventHandlers.onSuccess)r({request:n,result:e.data,requestPayload:d,flags:m});if(i.customResponseLogic?.onSuccess)return await i.customResponseLogic.onSuccess({req:n,response:e.data});return r.json(e.data,{status:200})}if(i.eventHandlers?.onSuccess)for(let e of i.eventHandlers.onSuccess)await e({request:n,result:void 0,requestPayload:d,flags:m});if(i.customResponseLogic?.onSuccess)return await i.customResponseLogic.onSuccess({req:n,response:{}});return r.json({},{status:200})}catch(t){let e=t;if(c.isControllerError(t)||(e=l.newError({error:"Internal error",statusCode:500},t)),e.details&&"object"==typeof e.details||(e.details={}),a.onErrorHandlers)try{for(let r of a.onErrorHandlers)await r({error:e,req:n})}catch(r){e=l.newError({error:"onError handler error",statusCode:500},r)}if(i.eventHandlers?.onError)try{for(let r of i.eventHandlers.onError)await r({error:e,req:n})}catch(r){e=l.newError({error:"onError handler error",statusCode:500},r)}if(i.customResponseLogic?.onError)return await i.customResponseLogic.onError({req:n,error:e});return r.json({message:e.message,details:e.statusCode.toString().startsWith("4")?e.details:null,code:e.code},{status:e.statusCode})}};class p{data;static schema=t.object({zeroBasedIndex:t.coerce.number().int().nonnegative(),pageSize:t.coerce.number().int().positive()});constructor(e){this.data=e}static create=e=>{let r=p.schema.parse(e);return new p(r)};get model(){return this.data}same=e=>e.model.pageSize===this.model.pageSize&&e.model.zeroBasedIndex===this.model.zeroBasedIndex;get nextPage(){return p.create({pageSize:this.data.pageSize,zeroBasedIndex:this.data.zeroBasedIndex+1})}get previousPage(){return 0===this.model.zeroBasedIndex?p.create(this.model):p.create({pageSize:this.model.pageSize,zeroBasedIndex:this.model.zeroBasedIndex-1})}get previousPage_UNSAFE(){return p.create({pageSize:this.model.pageSize,zeroBasedIndex:this.model.zeroBasedIndex-1})}}let _=e=>l({name:e.name,schemas:{...e.customOperations??{},list:{payload:t.object({filter:e.searchPayload.list,pagination:p.schema.optional()}),response:t.array(e.models.list)},details:{payload:t.object({filter:e.searchPayload.specific}),response:e.models.details.nullable()},create:{payload:t.object({payload:e.actionsPayload.create}),response:t.object({id:t.string()})},updateOne:{payload:t.object({filter:e.searchPayload.specific,payload:e.actionsPayload.update}),response:t.object({success:t.boolean()})},deleteOne:{payload:t.object({filter:e.searchPayload.specific}),response:t.object({success:t.boolean()})}}}),h=(e,r)=>{class a{___data=[];method=_(e);___listSearchLogic=(e,r)=>{let t=Object.entries(r).filter(([e,r])=>!!r).map(e=>e[0]);return Object.entries(e).filter(([e])=>t.includes(e)).every(([e,t])=>t===r[e]||t?.toString()?.includes(r[e]?.toString()))};___specificSearchLogic=(e,r)=>{let t=Object.entries(r).filter(([e,r])=>!!r).map(e=>e[0]);return Object.entries(e).filter(([e])=>t.includes(e)).every(([e,t])=>t===r[e]||t?.toString()?.includes(r[e]?.toString()))};___mapDetailToList=e=>e;___mapCreatePayloadToDetail=e=>({...e,id:Math.random().toString()});___mapUpdatePayloadToDetail=(e,r)=>{let t=Object.fromEntries(Object.entries(r).filter(([,e])=>void 0!==e));return{...e,...t}};constructor(){if(!r)return;r.searchLogic&&(r.searchLogic.list&&(this.___listSearchLogic=r.searchLogic.list),r.searchLogic.specific&&(this.___specificSearchLogic=r.searchLogic.specific)),r.mappers&&(r.mappers.detailToList&&(this.___mapDetailToList=r.mappers.detailToList),r.mappers.createPayloadToDetail&&(this.___mapCreatePayloadToDetail=r.mappers.createPayloadToDetail),r.mappers.updatePayloadToDetail&&(this.___mapUpdatePayloadToDetail=r.mappers.updatePayloadToDetail)),r.initialData&&(this.___data=[...r.initialData])}list=this.method("list",async({filter:r,pagination:a={pageSize:1e3,zeroBasedIndex:0}})=>{e.searchPayload.list.parse(r),p.schema.parse(a);let o=this.___data.filter(e=>this.___listSearchLogic(e,r)).filter((e,r)=>r>=a.pageSize*a.zeroBasedIndex&&r<(a.zeroBasedIndex+1)*a.pageSize).map(e=>this.___mapDetailToList(e));return t.array(e.models.list).parse(o),o});details=this.method("details",async({filter:e})=>{let r=this.___data.find(r=>this.___specificSearchLogic(r,e));return r||null});create=this.method("create",async({payload:e})=>{let r=this.___mapCreatePayloadToDetail(e);return this.___data=this.___data.concat(r),{id:r.id}});updateOne=this.method("updateOne",async({filter:e,payload:r})=>{let t=this.___data.findIndex(r=>this.___specificSearchLogic(r,e));return this.___data=this.___data.map((e,a)=>a!==t?e:this.___mapUpdatePayloadToDetail(e,r)),{success:t>-1}});deleteOne=this.method("deleteOne",async({filter:e})=>{let r=this.___data.findIndex(r=>this.___specificSearchLogic(r,e));return this.___data=this.___data.filter((e,t)=>t!==r),{success:r>-1}})}return a};class f{data;static schema=t.string().nonempty();constructor(e){this.data=e}static create=e=>new f(f.schema.parse(e));static get randomUUID(){return f.create(a())}get model(){return this.data}get isUUID(){return t.uuidv4().safeParse(this.data).success}}export{s as ValueObjects,i as Store,n as Controller,d as Module};
|
package/dist/rslib-runtime.js
CHANGED
|
@@ -1,37 +1 @@
|
|
|
1
|
-
var
|
|
2
|
-
function __webpack_require__(moduleId) {
|
|
3
|
-
var cachedModule = __webpack_module_cache__[moduleId];
|
|
4
|
-
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
5
|
-
var module = __webpack_module_cache__[moduleId] = {
|
|
6
|
-
exports: {}
|
|
7
|
-
};
|
|
8
|
-
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
9
|
-
return module.exports;
|
|
10
|
-
}
|
|
11
|
-
(()=>{
|
|
12
|
-
__webpack_require__.d = (exports, definition)=>{
|
|
13
|
-
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) Object.defineProperty(exports, key, {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
get: definition[key]
|
|
16
|
-
});
|
|
17
|
-
};
|
|
18
|
-
})();
|
|
19
|
-
(()=>{
|
|
20
|
-
__webpack_require__.add = function(modules) {
|
|
21
|
-
Object.assign(__webpack_require__.m, modules);
|
|
22
|
-
};
|
|
23
|
-
})();
|
|
24
|
-
(()=>{
|
|
25
|
-
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
26
|
-
})();
|
|
27
|
-
(()=>{
|
|
28
|
-
__webpack_require__.r = (exports)=>{
|
|
29
|
-
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports, Symbol.toStringTag, {
|
|
30
|
-
value: 'Module'
|
|
31
|
-
});
|
|
32
|
-
Object.defineProperty(exports, '__esModule', {
|
|
33
|
-
value: true
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
})();
|
|
37
|
-
export { __webpack_require__ };
|
|
1
|
+
var e={};function r(o){var t=e[o];if(void 0!==t)return t.exports;var a=e[o]={exports:{}};return __webpack_modules__[o](a,a.exports,r),a.exports}r.d=(e,o)=>{for(var t in o)r.o(o,t)&&!r.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:o[t]})},r.add=function(e){Object.assign(r.m,e)},r.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),r.r=e=>{"u">typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};export{r as __webpack_require__};
|