@treatwell/moleculer-essentials 1.0.0
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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/context-factory-BWO3xPWE.d.cts +520 -0
- package/dist/context-factory-BWO3xPWE.d.mts +520 -0
- package/dist/index-82e1CXJX.cjs +11 -0
- package/dist/index-BV1ZqQrU.mjs +351 -0
- package/dist/index-DNJWwcZu.mjs +8 -0
- package/dist/index-rZl77S1z.cjs +375 -0
- package/dist/index.cjs +1570 -0
- package/dist/index.d.cts +373 -0
- package/dist/index.d.mts +373 -0
- package/dist/index.mjs +1535 -0
- package/dist/mixins/database.mixin.cjs +1673 -0
- package/dist/mixins/database.mixin.d.cts +958 -0
- package/dist/mixins/database.mixin.d.mts +958 -0
- package/dist/mixins/database.mixin.mjs +1645 -0
- package/dist/mixins/encryptor.mixin.cjs +84 -0
- package/dist/mixins/encryptor.mixin.d.cts +31 -0
- package/dist/mixins/encryptor.mixin.d.mts +31 -0
- package/dist/mixins/encryptor.mixin.mjs +81 -0
- package/dist/mixins/global-store.mixin.cjs +56 -0
- package/dist/mixins/global-store.mixin.d.cts +39 -0
- package/dist/mixins/global-store.mixin.d.mts +39 -0
- package/dist/mixins/global-store.mixin.mjs +54 -0
- package/dist/mixins/jwt.mixin.cjs +118 -0
- package/dist/mixins/jwt.mixin.d.cts +43 -0
- package/dist/mixins/jwt.mixin.d.mts +43 -0
- package/dist/mixins/jwt.mixin.mjs +115 -0
- package/dist/mixins/queue.mixin.cjs +420 -0
- package/dist/mixins/queue.mixin.d.cts +150 -0
- package/dist/mixins/queue.mixin.d.mts +150 -0
- package/dist/mixins/queue.mixin.mjs +414 -0
- package/dist/mixins/redis.mixin.cjs +50 -0
- package/dist/mixins/redis.mixin.d.cts +27 -0
- package/dist/mixins/redis.mixin.d.mts +27 -0
- package/dist/mixins/redis.mixin.mjs +48 -0
- package/dist/mixins/redlock.mixin.cjs +76 -0
- package/dist/mixins/redlock.mixin.d.cts +30 -0
- package/dist/mixins/redlock.mixin.d.mts +30 -0
- package/dist/mixins/redlock.mixin.mjs +74 -0
- package/package.json +181 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lodash = require('lodash');
|
|
4
|
+
var v4 = require('zod/v4');
|
|
5
|
+
var bson = require('bson');
|
|
6
|
+
var dateFns = require('date-fns');
|
|
7
|
+
var zod = require('zod');
|
|
8
|
+
|
|
9
|
+
const COERCE_ARRAY_ATTRIBUTE = "x-coerce-array";
|
|
10
|
+
const SCHEMA_REF_NAME = "$id";
|
|
11
|
+
const EMPTY_OBJECT_SCHEMA = {
|
|
12
|
+
type: "object",
|
|
13
|
+
additionalProperties: true,
|
|
14
|
+
required: [],
|
|
15
|
+
properties: {}
|
|
16
|
+
};
|
|
17
|
+
const DATE_TYPE = {
|
|
18
|
+
type: "string",
|
|
19
|
+
format: "date-time"
|
|
20
|
+
};
|
|
21
|
+
const OBJECTID_TYPE = {
|
|
22
|
+
type: "string",
|
|
23
|
+
pattern: "^[0-9A-Fa-f]{24}$",
|
|
24
|
+
format: "object-id",
|
|
25
|
+
description: "ObjectId"
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function omitFields(schema, fields, refName) {
|
|
29
|
+
if (schema.anyOf || schema.allOf || schema.oneOf) {
|
|
30
|
+
return {
|
|
31
|
+
...schema,
|
|
32
|
+
[SCHEMA_REF_NAME]: refName,
|
|
33
|
+
anyOf: schema.anyOf?.map(
|
|
34
|
+
(s) => omitFields(s, fields)
|
|
35
|
+
),
|
|
36
|
+
allOf: schema.allOf?.map(
|
|
37
|
+
(s) => omitFields(s, fields)
|
|
38
|
+
),
|
|
39
|
+
oneOf: schema.oneOf?.map(
|
|
40
|
+
(s) => omitFields(s, fields)
|
|
41
|
+
)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
...schema,
|
|
46
|
+
[SCHEMA_REF_NAME]: refName,
|
|
47
|
+
required: schema.required.filter((f) => !fields.includes(f)),
|
|
48
|
+
properties: lodash.omit(schema.properties, fields)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function pickFields(schema, fields, refName) {
|
|
52
|
+
if (schema.anyOf || schema.allOf || schema.oneOf) {
|
|
53
|
+
return {
|
|
54
|
+
...schema,
|
|
55
|
+
[SCHEMA_REF_NAME]: refName,
|
|
56
|
+
anyOf: schema.anyOf?.map(
|
|
57
|
+
(s) => pickFields(s, fields)
|
|
58
|
+
),
|
|
59
|
+
allOf: schema.allOf?.map(
|
|
60
|
+
(s) => pickFields(s, fields)
|
|
61
|
+
),
|
|
62
|
+
oneOf: schema.oneOf?.map(
|
|
63
|
+
(s) => pickFields(s, fields)
|
|
64
|
+
)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
...schema,
|
|
69
|
+
[SCHEMA_REF_NAME]: refName,
|
|
70
|
+
required: schema.required.filter((f) => fields.includes(f)),
|
|
71
|
+
properties: lodash.pick(schema.properties, fields)
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function optionalExceptFields(schema, fields, refName) {
|
|
75
|
+
if (schema.anyOf || schema.allOf || schema.oneOf) {
|
|
76
|
+
return {
|
|
77
|
+
...schema,
|
|
78
|
+
[SCHEMA_REF_NAME]: refName,
|
|
79
|
+
anyOf: schema.anyOf?.map(
|
|
80
|
+
(s) => optionalExceptFields(s, fields)
|
|
81
|
+
),
|
|
82
|
+
allOf: schema.allOf?.map(
|
|
83
|
+
(s) => optionalExceptFields(s, fields)
|
|
84
|
+
),
|
|
85
|
+
oneOf: schema.oneOf?.map(
|
|
86
|
+
(s) => optionalExceptFields(s, fields)
|
|
87
|
+
)
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
...schema,
|
|
92
|
+
[SCHEMA_REF_NAME]: refName,
|
|
93
|
+
required: schema.required.filter((f) => fields.includes(f))
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function optionalFields(schema, fields, refName) {
|
|
97
|
+
if (schema.anyOf || schema.allOf || schema.oneOf) {
|
|
98
|
+
return {
|
|
99
|
+
...schema,
|
|
100
|
+
[SCHEMA_REF_NAME]: refName,
|
|
101
|
+
anyOf: schema.anyOf?.map(
|
|
102
|
+
(s) => optionalFields(s, fields)
|
|
103
|
+
),
|
|
104
|
+
allOf: schema.allOf?.map(
|
|
105
|
+
(s) => optionalFields(s, fields)
|
|
106
|
+
),
|
|
107
|
+
oneOf: schema.oneOf?.map(
|
|
108
|
+
(s) => optionalFields(s, fields)
|
|
109
|
+
)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
...schema,
|
|
114
|
+
[SCHEMA_REF_NAME]: refName,
|
|
115
|
+
required: schema.required.filter((f) => !fields.includes(f))
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function toPartialSchema(schema, refName) {
|
|
119
|
+
if (schema.anyOf || schema.allOf || schema.oneOf) {
|
|
120
|
+
return {
|
|
121
|
+
...schema,
|
|
122
|
+
[SCHEMA_REF_NAME]: refName,
|
|
123
|
+
anyOf: schema.anyOf?.map(
|
|
124
|
+
(s) => toPartialSchema(s)
|
|
125
|
+
),
|
|
126
|
+
allOf: schema.allOf?.map(
|
|
127
|
+
(s) => toPartialSchema(s)
|
|
128
|
+
),
|
|
129
|
+
oneOf: schema.oneOf?.map(
|
|
130
|
+
(s) => toPartialSchema(s)
|
|
131
|
+
)
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
...schema,
|
|
136
|
+
[SCHEMA_REF_NAME]: refName,
|
|
137
|
+
required: []
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function addFieldsToSchema(schema, newFields, refName) {
|
|
141
|
+
return {
|
|
142
|
+
...schema,
|
|
143
|
+
[SCHEMA_REF_NAME]: refName,
|
|
144
|
+
properties: { ...schema.properties, ...newFields }
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function composeSchemas(schemaRefName, ...schemas) {
|
|
148
|
+
return {
|
|
149
|
+
[SCHEMA_REF_NAME]: schemaRefName ?? void 0,
|
|
150
|
+
type: "object",
|
|
151
|
+
required: [],
|
|
152
|
+
unevaluatedProperties: false,
|
|
153
|
+
allOf: schemas
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function deepRefReplacer(obj, extractor, currentId = "") {
|
|
158
|
+
if (!obj || typeof obj !== "object") {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if ("$ref" in obj && typeof obj.$ref === "string") {
|
|
162
|
+
let id;
|
|
163
|
+
if (obj.$ref === "#") {
|
|
164
|
+
if (!currentId) {
|
|
165
|
+
throw new Error("Got a recursive ref (#) without a parent id.");
|
|
166
|
+
}
|
|
167
|
+
id = currentId;
|
|
168
|
+
} else if (obj.$ref.startsWith("#/$defs/")) {
|
|
169
|
+
id = obj.$ref.replace("#/$defs/", "");
|
|
170
|
+
}
|
|
171
|
+
if (id) {
|
|
172
|
+
obj.$ref = extractor.refReplacer(id);
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
for (const val of Object.values(obj)) {
|
|
176
|
+
deepRefReplacer(val, extractor, currentId);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function zodToOpenAPISchema(schema, extractor) {
|
|
181
|
+
const res = zod.z.toJSONSchema(schema, {
|
|
182
|
+
io: "input",
|
|
183
|
+
unrepresentable: "any",
|
|
184
|
+
override: ({ zodSchema, jsonSchema }) => {
|
|
185
|
+
const { def } = zodSchema._zod;
|
|
186
|
+
if (def.type === "date") {
|
|
187
|
+
jsonSchema.type = "string";
|
|
188
|
+
jsonSchema.format = "date-time";
|
|
189
|
+
}
|
|
190
|
+
if (def.type === "custom" && def.fn === isObjectId) {
|
|
191
|
+
jsonSchema.type = "string";
|
|
192
|
+
jsonSchema.format = "object-id";
|
|
193
|
+
jsonSchema.pattern = "^[0-9A-Fa-f]{24}$";
|
|
194
|
+
}
|
|
195
|
+
if (Object.keys(jsonSchema).length === 0) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
`Zod schema ${def.type} is not representable in OpenAPI.`
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
if (jsonSchema.id) {
|
|
201
|
+
jsonSchema.$id = jsonSchema.id;
|
|
202
|
+
delete jsonSchema.id;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
deepRefReplacer(res, extractor, res.$id);
|
|
207
|
+
if (res.$defs) {
|
|
208
|
+
for (const [key, val] of Object.entries(res.$defs)) {
|
|
209
|
+
deepRefReplacer(val, extractor, key);
|
|
210
|
+
extractor.onNewRef(key, val);
|
|
211
|
+
}
|
|
212
|
+
delete res.$defs;
|
|
213
|
+
}
|
|
214
|
+
delete res.$schema;
|
|
215
|
+
if (res.$id) {
|
|
216
|
+
extractor.onNewRef(res.$id, res);
|
|
217
|
+
return { $ref: extractor.refReplacer(res.$id) };
|
|
218
|
+
}
|
|
219
|
+
return res;
|
|
220
|
+
}
|
|
221
|
+
const zodDate = zod.z.transform((val) => {
|
|
222
|
+
if (typeof val === "string") {
|
|
223
|
+
const date = dateFns.parseISO(val);
|
|
224
|
+
if (!Number.isNaN(date.getTime())) {
|
|
225
|
+
return date;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return val;
|
|
229
|
+
}).pipe(zod.z.date());
|
|
230
|
+
const OBJECT_ID_PATTERN = /^[a-f\d]{24}$/i;
|
|
231
|
+
function isObjectId(val) {
|
|
232
|
+
return val instanceof bson.ObjectId;
|
|
233
|
+
}
|
|
234
|
+
const zodObjectId = zod.z.transform((val) => {
|
|
235
|
+
if (typeof val === "string" && OBJECT_ID_PATTERN.test(val)) {
|
|
236
|
+
return new bson.ObjectId(val);
|
|
237
|
+
}
|
|
238
|
+
return val;
|
|
239
|
+
}).pipe(zod.z.custom(isObjectId, { abort: true })).meta({ id: "ObjectId" });
|
|
240
|
+
function zodCoerceArray(element, params) {
|
|
241
|
+
return zod.z.transform((val) => !Array.isArray(val) && val !== void 0 ? [val] : val).pipe(zod.z.array(element, params));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const ServerErrorSchema = {
|
|
245
|
+
type: "object",
|
|
246
|
+
additionalProperties: false,
|
|
247
|
+
required: [],
|
|
248
|
+
properties: {
|
|
249
|
+
name: { type: "string" },
|
|
250
|
+
stack: { type: "string", nullable: true },
|
|
251
|
+
type: { type: "string" },
|
|
252
|
+
data: { type: "object", nullable: true },
|
|
253
|
+
message: { type: "string" },
|
|
254
|
+
code: { type: "integer" },
|
|
255
|
+
retryable: { type: "boolean" }
|
|
256
|
+
},
|
|
257
|
+
example: {
|
|
258
|
+
name: "MoleculerServerError",
|
|
259
|
+
message: "Internal Server Error",
|
|
260
|
+
code: 500
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
const UnauthorizedErrorSchema = {
|
|
264
|
+
type: "object",
|
|
265
|
+
additionalProperties: false,
|
|
266
|
+
required: [],
|
|
267
|
+
properties: {},
|
|
268
|
+
example: {
|
|
269
|
+
name: "UnAuthorizedError",
|
|
270
|
+
message: "Unauthorized",
|
|
271
|
+
code: 401,
|
|
272
|
+
type: "NO_TOKEN",
|
|
273
|
+
data: null
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const FileNotExistSchema = {
|
|
277
|
+
type: "object",
|
|
278
|
+
additionalProperties: false,
|
|
279
|
+
required: [],
|
|
280
|
+
properties: {},
|
|
281
|
+
example: {
|
|
282
|
+
name: "MoleculerClientError",
|
|
283
|
+
message: "File missing in the request",
|
|
284
|
+
code: 400
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
const FileTooBigSchema = {
|
|
288
|
+
type: "object",
|
|
289
|
+
additionalProperties: false,
|
|
290
|
+
required: [],
|
|
291
|
+
properties: {},
|
|
292
|
+
example: {
|
|
293
|
+
name: "PayloadTooLarge",
|
|
294
|
+
message: "Payload too large",
|
|
295
|
+
code: 413,
|
|
296
|
+
type: "PAYLOAD_TOO_LARGE",
|
|
297
|
+
data: {
|
|
298
|
+
fieldname: "file",
|
|
299
|
+
filename: "4b2005c0b8.png",
|
|
300
|
+
encoding: "7bit",
|
|
301
|
+
mimetype: "image/png"
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
const ValidationErrorSchema = {
|
|
306
|
+
type: "object",
|
|
307
|
+
additionalProperties: false,
|
|
308
|
+
required: [],
|
|
309
|
+
properties: ServerErrorSchema.properties,
|
|
310
|
+
example: {
|
|
311
|
+
code: 422,
|
|
312
|
+
type: "VALIDATION_ERROR",
|
|
313
|
+
data: [
|
|
314
|
+
{
|
|
315
|
+
keyword: "additionalProperties",
|
|
316
|
+
dataPath: "",
|
|
317
|
+
schemaPath: "#/additionalProperties",
|
|
318
|
+
params: {
|
|
319
|
+
additionalProperty: "originalDate"
|
|
320
|
+
},
|
|
321
|
+
message: "should NOT have additional properties"
|
|
322
|
+
}
|
|
323
|
+
]
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
const GetOpenApiParamsSchema = {
|
|
327
|
+
type: "object",
|
|
328
|
+
additionalProperties: false,
|
|
329
|
+
required: [],
|
|
330
|
+
properties: { kind: { type: "string" } }
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
function createOpenAPIResponses(model, description = "") {
|
|
334
|
+
const isZodType = model instanceof v4.z.ZodType;
|
|
335
|
+
return {
|
|
336
|
+
responses: {
|
|
337
|
+
"200": {
|
|
338
|
+
description,
|
|
339
|
+
content: {
|
|
340
|
+
"application/json": {
|
|
341
|
+
// @ts-expect-error Moleculer will clone this object using lodash,
|
|
342
|
+
// losing the zod class instance. By using a function, we ensure
|
|
343
|
+
// that clone doesn't break the zod instance
|
|
344
|
+
zodInstance: isZodType ? () => model : void 0,
|
|
345
|
+
schema: isZodType ? void 0 : model
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
exports.COERCE_ARRAY_ATTRIBUTE = COERCE_ARRAY_ATTRIBUTE;
|
|
354
|
+
exports.DATE_TYPE = DATE_TYPE;
|
|
355
|
+
exports.EMPTY_OBJECT_SCHEMA = EMPTY_OBJECT_SCHEMA;
|
|
356
|
+
exports.FileNotExistSchema = FileNotExistSchema;
|
|
357
|
+
exports.FileTooBigSchema = FileTooBigSchema;
|
|
358
|
+
exports.GetOpenApiParamsSchema = GetOpenApiParamsSchema;
|
|
359
|
+
exports.OBJECTID_TYPE = OBJECTID_TYPE;
|
|
360
|
+
exports.SCHEMA_REF_NAME = SCHEMA_REF_NAME;
|
|
361
|
+
exports.ServerErrorSchema = ServerErrorSchema;
|
|
362
|
+
exports.UnauthorizedErrorSchema = UnauthorizedErrorSchema;
|
|
363
|
+
exports.ValidationErrorSchema = ValidationErrorSchema;
|
|
364
|
+
exports.addFieldsToSchema = addFieldsToSchema;
|
|
365
|
+
exports.composeSchemas = composeSchemas;
|
|
366
|
+
exports.createOpenAPIResponses = createOpenAPIResponses;
|
|
367
|
+
exports.omitFields = omitFields;
|
|
368
|
+
exports.optionalExceptFields = optionalExceptFields;
|
|
369
|
+
exports.optionalFields = optionalFields;
|
|
370
|
+
exports.pickFields = pickFields;
|
|
371
|
+
exports.toPartialSchema = toPartialSchema;
|
|
372
|
+
exports.zodCoerceArray = zodCoerceArray;
|
|
373
|
+
exports.zodDate = zodDate;
|
|
374
|
+
exports.zodObjectId = zodObjectId;
|
|
375
|
+
exports.zodToOpenAPISchema = zodToOpenAPISchema;
|