@sqrzro/server 2.0.0-bz.10 → 2.0.0-bz.11
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/dist/auth.d.ts +100 -0
- package/dist/auth.js +891 -0
- package/dist/cache.d.ts +4 -0
- package/dist/cache.js +46 -0
- package/dist/forms.d.ts +46 -0
- package/dist/forms.js +327 -0
- package/dist/{lists/ListService.d.ts → lists.d.ts} +5 -3
- package/dist/lists.js +61 -0
- package/dist/mail.d.ts +12 -0
- package/dist/mail.js +97 -0
- package/dist/middleware.d.ts +5 -3
- package/dist/middleware.js +58 -22
- package/dist/{database/schema.d.ts → schema.d.ts} +38 -34
- package/dist/schema.js +77 -0
- package/dist/{url/URLService.d.ts → url.d.ts} +4 -2
- package/dist/url.js +56 -0
- package/package.json +10 -11
- package/database.d.ts +0 -1
- package/database.js +0 -1
- package/dist/auth/AuthService.d.ts +0 -14
- package/dist/auth/AuthService.js +0 -135
- package/dist/auth/ClientService.d.ts +0 -11
- package/dist/auth/ClientService.js +0 -47
- package/dist/auth/LoginRequest.d.ts +0 -4
- package/dist/auth/LoginRequest.js +0 -8
- package/dist/auth/MFARequest.d.ts +0 -4
- package/dist/auth/MFARequest.js +0 -9
- package/dist/auth/MFAService.d.ts +0 -6
- package/dist/auth/MFAService.js +0 -105
- package/dist/auth/PasswordRequest.d.ts +0 -4
- package/dist/auth/PasswordRequest.js +0 -12
- package/dist/auth/PasswordResetRequest.d.ts +0 -4
- package/dist/auth/PasswordResetRequest.js +0 -13
- package/dist/auth/PasswordService.d.ts +0 -8
- package/dist/auth/PasswordService.js +0 -54
- package/dist/auth/SessionService.d.ts +0 -42
- package/dist/auth/SessionService.js +0 -127
- package/dist/auth/index.d.ts +0 -6
- package/dist/auth/index.js +0 -6
- package/dist/auth/interfaces.d.ts +0 -20
- package/dist/auth/interfaces.js +0 -1
- package/dist/cache/CacheService.d.ts +0 -2
- package/dist/cache/CacheService.js +0 -14
- package/dist/cache/index.d.ts +0 -1
- package/dist/cache/index.js +0 -1
- package/dist/database/DatabaseService.d.ts +0 -7
- package/dist/database/DatabaseService.js +0 -12
- package/dist/database/schema.js +0 -42
- package/dist/forms/FormService.d.ts +0 -16
- package/dist/forms/FormService.js +0 -78
- package/dist/forms/ImageService.d.ts +0 -7
- package/dist/forms/ImageService.js +0 -19
- package/dist/forms/ValidationError.d.ts +0 -4
- package/dist/forms/ValidationError.js +0 -7
- package/dist/forms/ValidationService.d.ts +0 -20
- package/dist/forms/ValidationService.js +0 -59
- package/dist/forms/index.d.ts +0 -3
- package/dist/forms/index.js +0 -3
- package/dist/forms/lang.d.ts +0 -2
- package/dist/forms/lang.js +0 -115
- package/dist/lists/ListService.js +0 -28
- package/dist/lists/index.d.ts +0 -1
- package/dist/lists/index.js +0 -1
- package/dist/mail/MailService.d.ts +0 -12
- package/dist/mail/MailService.js +0 -55
- package/dist/mail/index.d.ts +0 -1
- package/dist/mail/index.js +0 -1
- package/dist/url/URLService.js +0 -48
- package/dist/url/index.d.ts +0 -1
- package/dist/url/index.js +0 -1
package/dist/cache.d.ts
ADDED
package/dist/cache.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/cache/index.ts
|
|
20
|
+
var cache_exports = {};
|
|
21
|
+
__export(cache_exports, {
|
|
22
|
+
getFromCache: () => getFromCache,
|
|
23
|
+
setToCache: () => setToCache
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(cache_exports);
|
|
26
|
+
|
|
27
|
+
// src/cache/CacheService.ts
|
|
28
|
+
var import_redis = require("redis");
|
|
29
|
+
async function getClient() {
|
|
30
|
+
const client = (0, import_redis.createClient)();
|
|
31
|
+
await client.connect();
|
|
32
|
+
return client;
|
|
33
|
+
}
|
|
34
|
+
async function getFromCache(key) {
|
|
35
|
+
const client = await getClient();
|
|
36
|
+
return client.get(key);
|
|
37
|
+
}
|
|
38
|
+
async function setToCache(key, value) {
|
|
39
|
+
const client = await getClient();
|
|
40
|
+
await client.set(key, value);
|
|
41
|
+
}
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
getFromCache,
|
|
45
|
+
setToCache
|
|
46
|
+
});
|
package/dist/forms.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Errorable } from '@sqrzro/interfaces';
|
|
2
|
+
import Joi from 'joi';
|
|
3
|
+
|
|
4
|
+
declare class ValidationError extends Error {
|
|
5
|
+
constructor(messages: Record<string, string>);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface SubmitFormArgs<F extends object> {
|
|
9
|
+
formData: F;
|
|
10
|
+
onSuccess?: (model: F) => Promise<void> | void;
|
|
11
|
+
onValidationError?: (error: ValidationError) => void;
|
|
12
|
+
request?: Joi.ObjectSchema<F>;
|
|
13
|
+
}
|
|
14
|
+
interface SubmitFormArgsWithFn<F extends object, M> extends Omit<SubmitFormArgs<F>, 'onSuccess'> {
|
|
15
|
+
fn: (data: F) => Promise<M | null>;
|
|
16
|
+
onSuccess?: (model: M) => Promise<void> | void;
|
|
17
|
+
}
|
|
18
|
+
declare function submitForm<F extends object>(args: SubmitFormArgs<F>): Promise<Errorable<F>>;
|
|
19
|
+
declare function submitForm<F extends object, M>(args: SubmitFormArgsWithFn<F, M>): Promise<Errorable<M>>;
|
|
20
|
+
|
|
21
|
+
interface ImageValidationConfig {
|
|
22
|
+
maxSize?: number;
|
|
23
|
+
types?: string[];
|
|
24
|
+
}
|
|
25
|
+
declare function validateImage(image: File | null, config?: ImageValidationConfig): Promise<Errorable<File>>;
|
|
26
|
+
|
|
27
|
+
declare function validate(): typeof Joi;
|
|
28
|
+
type ValidationCustomHelpers<V = any> = Joi.CustomHelpers<V>;
|
|
29
|
+
type ValidationExternalHelpers<V = any> = Joi.ExternalHelpers<V>;
|
|
30
|
+
type ValidationErrorReport = Joi.ErrorReport;
|
|
31
|
+
/**
|
|
32
|
+
* This function takes FormData and a schema. It then attempts to transform the FormData into an
|
|
33
|
+
* object that matches `T`. This is because the FormData object is not typed and we want to be able
|
|
34
|
+
* to validate it properly typed.
|
|
35
|
+
*
|
|
36
|
+
* Once transformed, the object is validated against the schema. This will result in either a
|
|
37
|
+
* properly typed `T` object or an array of validation errors.
|
|
38
|
+
* @param formData
|
|
39
|
+
* @param validation
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
declare function validateSchema<T>(formData: T, validation: Joi.ObjectSchema<T>): Promise<Errorable<T>>;
|
|
43
|
+
declare function createSchema<T>(schema: Joi.SchemaMap<T, true>): Joi.ObjectSchema<T>;
|
|
44
|
+
declare function extendSchema<T, U extends T>(schema: Joi.ObjectSchema<T>, appends: Joi.PartialSchemaMap<U>): Joi.ObjectSchema<U>;
|
|
45
|
+
|
|
46
|
+
export { type ValidationCustomHelpers, type ValidationErrorReport, type ValidationExternalHelpers, createSchema, extendSchema, submitForm, validate, validateImage, validateSchema };
|
package/dist/forms.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/forms/index.ts
|
|
30
|
+
var forms_exports = {};
|
|
31
|
+
__export(forms_exports, {
|
|
32
|
+
createSchema: () => createSchema,
|
|
33
|
+
extendSchema: () => extendSchema,
|
|
34
|
+
submitForm: () => submitForm,
|
|
35
|
+
validate: () => validate,
|
|
36
|
+
validateImage: () => validateImage,
|
|
37
|
+
validateSchema: () => validateSchema
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(forms_exports);
|
|
40
|
+
|
|
41
|
+
// src/forms/ValidationError.ts
|
|
42
|
+
var ValidationError = class extends Error {
|
|
43
|
+
constructor(messages2) {
|
|
44
|
+
super(JSON.stringify(messages2));
|
|
45
|
+
this.name = "ValidationError";
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var ValidationError_default = ValidationError;
|
|
49
|
+
|
|
50
|
+
// src/forms/ValidationService.ts
|
|
51
|
+
var import_joi = __toESM(require("joi"));
|
|
52
|
+
|
|
53
|
+
// src/forms/lang.ts
|
|
54
|
+
var messages = {
|
|
55
|
+
"alternatives.all": "",
|
|
56
|
+
"alternatives.any": "",
|
|
57
|
+
"alternatives.match": "",
|
|
58
|
+
"alternatives.one": "",
|
|
59
|
+
"alternatives.types": "",
|
|
60
|
+
"any.custom": "",
|
|
61
|
+
"any.default": "",
|
|
62
|
+
"any.failover": "",
|
|
63
|
+
"any.invalid": "",
|
|
64
|
+
"any.only": "",
|
|
65
|
+
"any.ref": "",
|
|
66
|
+
"any.required": "{{#label}} is required",
|
|
67
|
+
"any.unknown": "",
|
|
68
|
+
"array.base": "",
|
|
69
|
+
"array.excludes": "",
|
|
70
|
+
"array.includesRequiredBoth": "",
|
|
71
|
+
"array.includesRequiredKnowns": "",
|
|
72
|
+
"array.includesRequiredUnknowns": "",
|
|
73
|
+
"array.includes": "",
|
|
74
|
+
"array.length": "",
|
|
75
|
+
"array.max": "",
|
|
76
|
+
"array.min": "",
|
|
77
|
+
"array.orderedLength": "",
|
|
78
|
+
"array.sort": "",
|
|
79
|
+
"array.sort.mismatching": "",
|
|
80
|
+
"array.sort.unsupported": "",
|
|
81
|
+
"array.sparse": "",
|
|
82
|
+
"array.unique": "",
|
|
83
|
+
"array.hasKnown": "",
|
|
84
|
+
"array.hasUnknown": "",
|
|
85
|
+
"binary.base": "",
|
|
86
|
+
"binary.length": "",
|
|
87
|
+
"binary.max": "",
|
|
88
|
+
"binary.min": "",
|
|
89
|
+
"boolean.base": "",
|
|
90
|
+
"date.base": "",
|
|
91
|
+
"date.format": "",
|
|
92
|
+
"date.greater": "",
|
|
93
|
+
"date.less": "",
|
|
94
|
+
"date.max": "",
|
|
95
|
+
"date.min": "",
|
|
96
|
+
"date.strict": "",
|
|
97
|
+
"function.arity": "",
|
|
98
|
+
"function.class": "",
|
|
99
|
+
"function.maxArity": "",
|
|
100
|
+
"function.minArity": "",
|
|
101
|
+
"number.base": "{{#label}} should be a number",
|
|
102
|
+
"number.greater": "",
|
|
103
|
+
"number.infinity": "",
|
|
104
|
+
"number.integer": "",
|
|
105
|
+
"number.less": "",
|
|
106
|
+
"number.max": "",
|
|
107
|
+
"number.min": "{{#label}} should be greater than or equal to {{#limit}}",
|
|
108
|
+
"number.multiple": "",
|
|
109
|
+
"number.negative": "",
|
|
110
|
+
"number.port": "",
|
|
111
|
+
"number.positive": "",
|
|
112
|
+
"number.precision": "",
|
|
113
|
+
"number.unsafe": "",
|
|
114
|
+
"object.unknown": "",
|
|
115
|
+
"object.and": "",
|
|
116
|
+
"object.assert": "",
|
|
117
|
+
"object.base": "",
|
|
118
|
+
"object.length": "",
|
|
119
|
+
"object.max": "",
|
|
120
|
+
"object.min": "",
|
|
121
|
+
"object.missing": "",
|
|
122
|
+
"object.nand": "",
|
|
123
|
+
"object.pattern.match": "",
|
|
124
|
+
"object.refType": "",
|
|
125
|
+
"object.regex": "",
|
|
126
|
+
"object.rename.multiple": "",
|
|
127
|
+
"object.rename.override": "",
|
|
128
|
+
"object.schema": "",
|
|
129
|
+
"object.instance": "",
|
|
130
|
+
"object.with": "",
|
|
131
|
+
"object.without": "",
|
|
132
|
+
"object.xor": "",
|
|
133
|
+
"object.oxor": "",
|
|
134
|
+
"string.alphanum": "",
|
|
135
|
+
"string.base64": "",
|
|
136
|
+
"string.base": "",
|
|
137
|
+
"string.creditCard": "",
|
|
138
|
+
"string.dataUri": "",
|
|
139
|
+
"string.domain": "",
|
|
140
|
+
"string.email": "",
|
|
141
|
+
"string.empty": "{{#label}} is required",
|
|
142
|
+
"string.guid": "",
|
|
143
|
+
"string.hexAlign": "",
|
|
144
|
+
"string.hex": "",
|
|
145
|
+
"string.hostname": "",
|
|
146
|
+
"string.ipVersion": "",
|
|
147
|
+
"string.ip": "",
|
|
148
|
+
"string.isoDate": "",
|
|
149
|
+
"string.isoDuration": "",
|
|
150
|
+
"string.length": "",
|
|
151
|
+
"string.lowercase": "",
|
|
152
|
+
"string.max": "",
|
|
153
|
+
"string.min": "",
|
|
154
|
+
"string.normalize": "",
|
|
155
|
+
"string.pattern.base": "",
|
|
156
|
+
"string.pattern.name": "",
|
|
157
|
+
"string.pattern.invert.base": "",
|
|
158
|
+
"string.pattern.invert.name": "",
|
|
159
|
+
"string.token": "",
|
|
160
|
+
"string.trim": "",
|
|
161
|
+
"string.uppercase": "",
|
|
162
|
+
"string.uri": "",
|
|
163
|
+
"string.uriCustomScheme": "",
|
|
164
|
+
"string.uriRelativeOnly": "",
|
|
165
|
+
"symbol.base": "",
|
|
166
|
+
"symbol.map": ""
|
|
167
|
+
};
|
|
168
|
+
var lang_default = messages;
|
|
169
|
+
|
|
170
|
+
// src/forms/ValidationService.ts
|
|
171
|
+
function validate() {
|
|
172
|
+
return import_joi.default;
|
|
173
|
+
}
|
|
174
|
+
function getErrorMessages() {
|
|
175
|
+
return Object.entries(lang_default).reduce((acc, [key, value]) => {
|
|
176
|
+
if (!value) {
|
|
177
|
+
return acc;
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
...acc,
|
|
181
|
+
[key]: value
|
|
182
|
+
};
|
|
183
|
+
}, {});
|
|
184
|
+
}
|
|
185
|
+
function transformErrors(error) {
|
|
186
|
+
const messages2 = error.details.reduce(
|
|
187
|
+
(acc, cur) => ({
|
|
188
|
+
...acc,
|
|
189
|
+
[cur.path.join(".")]: cur.message.replace(/"/gu, "")
|
|
190
|
+
}),
|
|
191
|
+
{}
|
|
192
|
+
);
|
|
193
|
+
return new ValidationError_default(messages2);
|
|
194
|
+
}
|
|
195
|
+
async function validateSchema(formData, validation) {
|
|
196
|
+
try {
|
|
197
|
+
const validated = await validation.validateAsync(formData, {
|
|
198
|
+
abortEarly: false,
|
|
199
|
+
messages: getErrorMessages()
|
|
200
|
+
});
|
|
201
|
+
return [validated, null];
|
|
202
|
+
} catch (err) {
|
|
203
|
+
if (err instanceof import_joi.default.ValidationError) {
|
|
204
|
+
return [null, transformErrors(err)];
|
|
205
|
+
}
|
|
206
|
+
if (err instanceof Error) {
|
|
207
|
+
return [null, err];
|
|
208
|
+
}
|
|
209
|
+
return [null, new Error("Unknown validation error occured")];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function createSchema(schema) {
|
|
213
|
+
return import_joi.default.object(schema);
|
|
214
|
+
}
|
|
215
|
+
function extendSchema(schema, appends) {
|
|
216
|
+
return schema.append(appends);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/forms/FormService.ts
|
|
220
|
+
function serializeError(err) {
|
|
221
|
+
return {
|
|
222
|
+
cause: err.cause,
|
|
223
|
+
message: err.message,
|
|
224
|
+
name: err.name,
|
|
225
|
+
stack: err.stack
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function hasFn(args) {
|
|
229
|
+
return Boolean(Object.prototype.hasOwnProperty.call(args, "fn"));
|
|
230
|
+
}
|
|
231
|
+
async function submitForm(args) {
|
|
232
|
+
let data = { ...args.formData };
|
|
233
|
+
if (args.request) {
|
|
234
|
+
const [validated, validationError] = await validateSchema(args.formData, args.request);
|
|
235
|
+
if (validationError !== null) {
|
|
236
|
+
if (validationError instanceof ValidationError_default) {
|
|
237
|
+
args.onValidationError?.(validationError);
|
|
238
|
+
}
|
|
239
|
+
return [null, serializeError(validationError)];
|
|
240
|
+
}
|
|
241
|
+
data = validated;
|
|
242
|
+
}
|
|
243
|
+
if (!hasFn(args)) {
|
|
244
|
+
try {
|
|
245
|
+
await args.onSuccess?.(data);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
if (err instanceof Error) {
|
|
248
|
+
return [null, serializeError(err)];
|
|
249
|
+
}
|
|
250
|
+
return [
|
|
251
|
+
null,
|
|
252
|
+
serializeError(
|
|
253
|
+
new Error("The submitForm onSuccess function encountered an unknown error")
|
|
254
|
+
)
|
|
255
|
+
];
|
|
256
|
+
}
|
|
257
|
+
return [data, null];
|
|
258
|
+
}
|
|
259
|
+
let model = null;
|
|
260
|
+
try {
|
|
261
|
+
model = await args.fn(data);
|
|
262
|
+
} catch (err) {
|
|
263
|
+
if (err instanceof ValidationError_default) {
|
|
264
|
+
args.onValidationError?.(err);
|
|
265
|
+
return [null, serializeError(err)];
|
|
266
|
+
}
|
|
267
|
+
if (err instanceof Error) {
|
|
268
|
+
return [null, serializeError(err)];
|
|
269
|
+
}
|
|
270
|
+
return [
|
|
271
|
+
null,
|
|
272
|
+
serializeError(
|
|
273
|
+
new Error("The function supplied to submitForm encountered an unknown error")
|
|
274
|
+
)
|
|
275
|
+
];
|
|
276
|
+
}
|
|
277
|
+
if (!model) {
|
|
278
|
+
return [
|
|
279
|
+
null,
|
|
280
|
+
serializeError(
|
|
281
|
+
new Error("No model has been returned from the function supplied to submitForm")
|
|
282
|
+
)
|
|
283
|
+
];
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
await args.onSuccess?.(model);
|
|
287
|
+
} catch (err) {
|
|
288
|
+
if (err instanceof Error) {
|
|
289
|
+
return [null, serializeError(err)];
|
|
290
|
+
}
|
|
291
|
+
return [
|
|
292
|
+
null,
|
|
293
|
+
serializeError(
|
|
294
|
+
new Error("The submitForm onSuccess function encountered an unknown error")
|
|
295
|
+
)
|
|
296
|
+
];
|
|
297
|
+
}
|
|
298
|
+
return [model, null];
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/forms/ImageService.ts
|
|
302
|
+
var DEFAULT_TYPES = ["image/png", "image/jpeg", "image/jpg"];
|
|
303
|
+
var DEFAULT_MAX_SIZE = 5242880;
|
|
304
|
+
async function validateImage(image, config) {
|
|
305
|
+
if (!image) {
|
|
306
|
+
return Promise.resolve([null, new Error("IMAGE_UNDEFINED")]);
|
|
307
|
+
}
|
|
308
|
+
if (!(image instanceof File)) {
|
|
309
|
+
return Promise.resolve([null, new Error("IMAGE_NOT_VALID")]);
|
|
310
|
+
}
|
|
311
|
+
if (!(config?.types || DEFAULT_TYPES).includes(image.type)) {
|
|
312
|
+
return Promise.resolve([null, new Error("IMAGE_TYPE_NOT_VALID")]);
|
|
313
|
+
}
|
|
314
|
+
if (image.size > (config?.maxSize || DEFAULT_MAX_SIZE)) {
|
|
315
|
+
return Promise.resolve([null, new Error("IMAGE_TOO_HEAVY")]);
|
|
316
|
+
}
|
|
317
|
+
return Promise.resolve([image, null]);
|
|
318
|
+
}
|
|
319
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
320
|
+
0 && (module.exports = {
|
|
321
|
+
createSchema,
|
|
322
|
+
extendSchema,
|
|
323
|
+
submitForm,
|
|
324
|
+
validate,
|
|
325
|
+
validateImage,
|
|
326
|
+
validateSchema
|
|
327
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Errorable } from '@sqrzro/interfaces';
|
|
2
|
+
|
|
2
3
|
interface PaginationArgs {
|
|
3
4
|
skip: number;
|
|
4
5
|
take: number;
|
|
@@ -12,5 +13,6 @@ interface GetListArgs<T> {
|
|
|
12
13
|
fn: (args: FindManyArgs) => Promise<T[]>;
|
|
13
14
|
searchParams?: Record<string, string>;
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
declare function getList<T>({ allowedFilters, fn, searchParams, }: GetListArgs<T>): Promise<Errorable<T[]>>;
|
|
17
|
+
|
|
18
|
+
export { getList };
|
package/dist/lists.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/lists/index.ts
|
|
20
|
+
var lists_exports = {};
|
|
21
|
+
__export(lists_exports, {
|
|
22
|
+
getList: () => getList
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(lists_exports);
|
|
25
|
+
|
|
26
|
+
// src/lists/ListService.ts
|
|
27
|
+
var import_utility = require("@sqrzro/utility");
|
|
28
|
+
var DEFAULT_SORT = "id";
|
|
29
|
+
var DEFAULT_DIR = "asc";
|
|
30
|
+
var DEFAULT_PAGE = 1;
|
|
31
|
+
var DEFAULT_LIMIT = 10;
|
|
32
|
+
function createPaginationArgs(page) {
|
|
33
|
+
return { skip: (page - 1) * DEFAULT_LIMIT, take: DEFAULT_LIMIT };
|
|
34
|
+
}
|
|
35
|
+
function createFindManyArgs(searchParams, allowedFilters = []) {
|
|
36
|
+
const { dir, page, sort, ...filters } = searchParams || {};
|
|
37
|
+
return {
|
|
38
|
+
...createPaginationArgs(page ? parseInt(page, 10) : DEFAULT_PAGE),
|
|
39
|
+
orderBy: { [sort || DEFAULT_SORT]: dir || DEFAULT_DIR },
|
|
40
|
+
where: (0, import_utility.getFromObject)(filters, allowedFilters)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
async function getList({
|
|
44
|
+
allowedFilters,
|
|
45
|
+
fn,
|
|
46
|
+
searchParams
|
|
47
|
+
}) {
|
|
48
|
+
try {
|
|
49
|
+
const args = createFindManyArgs(searchParams, allowedFilters);
|
|
50
|
+
return [await fn(args), null];
|
|
51
|
+
} catch (err) {
|
|
52
|
+
if (err instanceof Error) {
|
|
53
|
+
return [null, err];
|
|
54
|
+
}
|
|
55
|
+
return [null, new Error("[getList] An unknown error occurred")];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
59
|
+
0 && (module.exports = {
|
|
60
|
+
getList
|
|
61
|
+
});
|
package/dist/mail.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type MailFn<T extends object> = () => Promise<{
|
|
2
|
+
subject: string;
|
|
3
|
+
template: (data: T) => React.ReactElement;
|
|
4
|
+
}>;
|
|
5
|
+
declare function createMail<T extends object>(subject: string, template: (data: T) => React.ReactElement): Promise<MailFn<T>>;
|
|
6
|
+
declare function getMail<T extends object>(mail: MailFn<T>, data: T): Promise<{
|
|
7
|
+
subject: string;
|
|
8
|
+
html: string;
|
|
9
|
+
}>;
|
|
10
|
+
declare function sendMail<T extends object>(mail: Promise<MailFn<T>>, data: T, to: string): Promise<boolean>;
|
|
11
|
+
|
|
12
|
+
export { createMail, getMail, sendMail };
|
package/dist/mail.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/mail/index.ts
|
|
30
|
+
var mail_exports = {};
|
|
31
|
+
__export(mail_exports, {
|
|
32
|
+
createMail: () => createMail,
|
|
33
|
+
getMail: () => getMail,
|
|
34
|
+
sendMail: () => sendMail
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(mail_exports);
|
|
37
|
+
|
|
38
|
+
// src/mail/MailService.ts
|
|
39
|
+
async function createMail(subject, template) {
|
|
40
|
+
return Promise.resolve(async () => Promise.resolve({ subject, template }));
|
|
41
|
+
}
|
|
42
|
+
async function getMail(mail, data) {
|
|
43
|
+
const { subject, template } = await mail();
|
|
44
|
+
const ReactDOMServer = (await import("react-dom/server")).default;
|
|
45
|
+
return {
|
|
46
|
+
subject,
|
|
47
|
+
html: ReactDOMServer.renderToStaticMarkup(template(data))
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function getSenderDetails() {
|
|
51
|
+
if (!process.env.MAIL_FROM) {
|
|
52
|
+
throw new Error("Mail from address has not been set");
|
|
53
|
+
}
|
|
54
|
+
const matches = /^(?<name>[^<]+)<(?<email>[^>]+)>$/u.exec(process.env.MAIL_FROM);
|
|
55
|
+
if (!matches?.groups?.name || !matches?.groups?.email) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
'Mail from address is not the correct format. It should be "Name <email>".'
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
email: matches.groups.email.trim(),
|
|
62
|
+
name: matches.groups.name.trim()
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function sendMail(mail, data, to) {
|
|
66
|
+
if (!process.env.MAIL_API_URL) {
|
|
67
|
+
throw new Error("Mail URL has not been set");
|
|
68
|
+
}
|
|
69
|
+
if (!process.env.MAIL_API_KEY) {
|
|
70
|
+
throw new Error("Mail API key has not been set");
|
|
71
|
+
}
|
|
72
|
+
const from = getSenderDetails();
|
|
73
|
+
const { subject, html } = await getMail(await mail, data);
|
|
74
|
+
try {
|
|
75
|
+
const response = await fetch(process.env.MAIL_API_URL, {
|
|
76
|
+
method: "POST",
|
|
77
|
+
headers: { "Api-Token": process.env.MAIL_API_KEY, "Content-Type": "application/json" },
|
|
78
|
+
body: JSON.stringify({
|
|
79
|
+
from,
|
|
80
|
+
html,
|
|
81
|
+
subject,
|
|
82
|
+
to: [{ email: to }]
|
|
83
|
+
})
|
|
84
|
+
});
|
|
85
|
+
const json = await response.json();
|
|
86
|
+
return json.success;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
console.log("err", err);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
93
|
+
0 && (module.exports = {
|
|
94
|
+
createMail,
|
|
95
|
+
getMail,
|
|
96
|
+
sendMail
|
|
97
|
+
});
|
package/dist/middleware.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
import { NextResponse } from 'next/server';
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
|
|
3
|
+
declare function handleMiddleware(request: NextRequest, nextFn?: () => NextResponse): Promise<NextResponse>;
|
|
4
|
+
|
|
5
|
+
export { handleMiddleware };
|