@sqrzro/server 2.0.0-bz.3 → 2.0.0-bz.4
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/AuthService.d.ts +2 -2
- package/dist/DataService.d.ts +1 -1
- package/dist/DataService.js +1 -1
- package/dist/RequestService.d.ts +1 -1
- package/dist/RequestService.js +1 -51
- package/package.json +1 -1
package/dist/AuthService.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { NextRequest } from 'next/server';
|
|
2
2
|
import { NextResponse } from 'next/server';
|
|
3
|
-
import type { Errorable, UserObject } from './interfaces';
|
|
4
|
-
export declare function handleLogin(formData:
|
|
3
|
+
import type { Errorable, LoginFormFields, UserObject } from './interfaces';
|
|
4
|
+
export declare function handleLogin(formData: LoginFormFields, lookupFn: (email: string) => Promise<UserObject | null>): Promise<Errorable<UserObject>>;
|
|
5
5
|
interface MeObject {
|
|
6
6
|
name: string;
|
|
7
7
|
email: string;
|
package/dist/DataService.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type Joi from 'joi';
|
|
2
2
|
import type { Errorable } from './interfaces';
|
|
3
3
|
interface MutateArgs<F extends object> {
|
|
4
|
-
formData:
|
|
4
|
+
formData: F;
|
|
5
5
|
onSuccess?: (model: F) => Promise<void> | void;
|
|
6
6
|
onValidationError?: (errors: Record<string, string>) => void;
|
|
7
7
|
request: Promise<Joi.ObjectSchema<F>>;
|
package/dist/DataService.js
CHANGED
package/dist/RequestService.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export declare function validate(): typeof Joi;
|
|
|
12
12
|
* @param validation
|
|
13
13
|
* @returns
|
|
14
14
|
*/
|
|
15
|
-
export declare function validateSchema<T extends object>(formData:
|
|
15
|
+
export declare function validateSchema<T extends object>(formData: T, validation: Joi.ObjectSchema<T>): ErrorablePromise<T>;
|
|
16
16
|
export declare function createSchema<T>(schema: Joi.PartialSchemaMap<T>): Promise<Joi.ObjectSchema<T>>;
|
|
17
17
|
interface ExtendSchemaOptions {
|
|
18
18
|
required?: string[];
|
package/dist/RequestService.js
CHANGED
|
@@ -23,55 +23,6 @@ function checkExternalErrors(validated) {
|
|
|
23
23
|
function transformErrors(error) {
|
|
24
24
|
return error.details.reduce((acc, cur) => ({ ...acc, [cur.path[0]]: cur.message }), {});
|
|
25
25
|
}
|
|
26
|
-
function transformString(value) {
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
function transformNumber(value) {
|
|
30
|
-
return parseInt(value, 10);
|
|
31
|
-
}
|
|
32
|
-
function transformBoolean(value) {
|
|
33
|
-
return value === 'true' || value === '1';
|
|
34
|
-
}
|
|
35
|
-
function transformDate(value) {
|
|
36
|
-
if (!value) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
return new Date(value);
|
|
40
|
-
}
|
|
41
|
-
const transformMap = {
|
|
42
|
-
string: transformString,
|
|
43
|
-
number: transformNumber,
|
|
44
|
-
boolean: transformBoolean,
|
|
45
|
-
date: transformDate,
|
|
46
|
-
};
|
|
47
|
-
function transformValue(value, type) {
|
|
48
|
-
return transformMap[type](String(value));
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Take the formData and the validation schema and transform the formData into an object that
|
|
52
|
-
* contains the correct types, as defined by the validation schema. This is because the FormData
|
|
53
|
-
* object will only contain strings (or File/Blob objects) and we want to be able to use the object
|
|
54
|
-
* correctly typed. For example, if the validation schema defines a property as a boolean, we want
|
|
55
|
-
* the value to be either true or false, not the string "true" or "false".
|
|
56
|
-
*
|
|
57
|
-
* @param formData
|
|
58
|
-
* @param validation
|
|
59
|
-
*/
|
|
60
|
-
function transform(formData, validation) {
|
|
61
|
-
const keys = validation.describe().keys;
|
|
62
|
-
const entries = Object.entries(Object.fromEntries(formData.entries()));
|
|
63
|
-
const result = {};
|
|
64
|
-
entries.forEach(([key, value]) => {
|
|
65
|
-
const entry = keys[key];
|
|
66
|
-
if (entry) {
|
|
67
|
-
result[key] = transformValue(value, entry.type);
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
result[key] = value;
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
return result;
|
|
74
|
-
}
|
|
75
26
|
/**
|
|
76
27
|
* This function takes FormData and a schema. It then attempts to transform the FormData into an
|
|
77
28
|
* object that matches `T`. This is because the FormData object is not typed and we want to be able
|
|
@@ -85,8 +36,7 @@ function transform(formData, validation) {
|
|
|
85
36
|
*/
|
|
86
37
|
async function validateSchema(formData, validation) {
|
|
87
38
|
try {
|
|
88
|
-
const
|
|
89
|
-
const validated = await validation.validateAsync(transformed, {
|
|
39
|
+
const validated = await validation.validateAsync(formData, {
|
|
90
40
|
abortEarly: false,
|
|
91
41
|
});
|
|
92
42
|
return checkExternalErrors(validated);
|