@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.
@@ -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: FormData, lookupFn: (email: string) => Promise<UserObject | null>): Promise<Errorable<UserObject>>;
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;
@@ -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: 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>>;
@@ -26,7 +26,7 @@ async function mutate(args) {
26
26
  }
27
27
  const model = await args.fn(validated);
28
28
  if (!model) {
29
- return [null, { $root: 'SERVER_ERROR' }];
29
+ return [null, { $root: 'No model has been returned from the fn' }];
30
30
  }
31
31
  try {
32
32
  await args.onSuccess?.(model);
@@ -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: FormData, validation: Joi.ObjectSchema<T>): ErrorablePromise<T>;
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[];
@@ -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 transformed = transform(formData, validation);
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqrzro/server",
3
- "version": "2.0.0-bz.3",
3
+ "version": "2.0.0-bz.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "ISC",