@modern-js/bff-core 1.0.1-beta.4 → 1.0.1-beta.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.
@@ -10,4 +10,13 @@ export class HttpError extends Error {
10
10
  }
11
11
 
12
12
  }
13
- export class ValidationError extends HttpError {}
13
+ export class ValidationError extends HttpError {
14
+ constructor(status, message) {
15
+ super(status, message);
16
+
17
+ _defineProperty(this, "code", void 0);
18
+
19
+ this.code = 'VALIDATION_ERROR';
20
+ }
21
+
22
+ }
@@ -1,4 +1,3 @@
1
- import { z } from 'zod';
2
1
  import { HttpMetadata, OperatorType, HttpMethod, TriggerType, ResponseMetaType } from "../types";
3
2
  import { ValidationError } from "../errors/http";
4
3
 
@@ -6,6 +5,10 @@ const validateInput = async (schema, input) => {
6
5
  try {
7
6
  await schema.parseAsync(input);
8
7
  } catch (error) {
8
+ const {
9
+ z
10
+ } = await import('zod');
11
+
9
12
  if (error instanceof z.ZodError) {
10
13
  throw new ValidationError(400, error.message);
11
14
  }
@@ -60,13 +60,17 @@ export const requireHandlerModule = modulePath => {
60
60
  unregister();
61
61
  return module;
62
62
  };
63
+
64
+ const routeValue = routePath => {
65
+ if (routePath.includes(':')) {
66
+ return 11;
67
+ }
68
+
69
+ return 1;
70
+ };
71
+
63
72
  export const sortRoutes = apiHandlers => {
64
- const sortedHandlers = apiHandlers.slice();
65
- sortedHandlers.forEach((apiHandler, handlerIndex) => {
66
- if (apiHandler.routeName.includes(':')) {
67
- sortedHandlers.splice(handlerIndex, 1);
68
- sortedHandlers.push(apiHandler);
69
- }
73
+ return apiHandlers.sort((handlerA, handlerB) => {
74
+ return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
70
75
  });
71
- return sortedHandlers;
72
76
  };
@@ -20,6 +20,15 @@ class HttpError extends Error {
20
20
 
21
21
  exports.HttpError = HttpError;
22
22
 
23
- class ValidationError extends HttpError {}
23
+ class ValidationError extends HttpError {
24
+ constructor(status, message) {
25
+ super(status, message);
26
+
27
+ _defineProperty(this, "code", void 0);
28
+
29
+ this.code = 'VALIDATION_ERROR';
30
+ }
31
+
32
+ }
24
33
 
25
34
  exports.ValidationError = ValidationError;
@@ -5,17 +5,23 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createHttpOperator = exports.Trace = exports.SetHeaders = exports.Redirect = exports.Query = exports.Put = exports.Post = exports.Patch = exports.Params = exports.Option = exports.Middleware = exports.HttpCode = exports.Headers = exports.Head = exports.Get = exports.Delete = exports.Data = exports.Connect = void 0;
7
7
 
8
- var _zod = require("zod");
9
-
10
8
  var _types = require("../types");
11
9
 
12
10
  var _http = require("../errors/http");
13
11
 
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+
14
16
  const validateInput = async (schema, input) => {
15
17
  try {
16
18
  await schema.parseAsync(input);
17
19
  } catch (error) {
18
- if (error instanceof _zod.z.ZodError) {
20
+ const {
21
+ z
22
+ } = await Promise.resolve().then(() => _interopRequireWildcard(require('zod')));
23
+
24
+ if (error instanceof z.ZodError) {
19
25
  throw new _http.ValidationError(400, error.message);
20
26
  }
21
27
 
@@ -82,15 +82,18 @@ const requireHandlerModule = modulePath => {
82
82
 
83
83
  exports.requireHandlerModule = requireHandlerModule;
84
84
 
85
+ const routeValue = routePath => {
86
+ if (routePath.includes(':')) {
87
+ return 11;
88
+ }
89
+
90
+ return 1;
91
+ };
92
+
85
93
  const sortRoutes = apiHandlers => {
86
- const sortedHandlers = apiHandlers.slice();
87
- sortedHandlers.forEach((apiHandler, handlerIndex) => {
88
- if (apiHandler.routeName.includes(':')) {
89
- sortedHandlers.splice(handlerIndex, 1);
90
- sortedHandlers.push(apiHandler);
91
- }
94
+ return apiHandlers.sort((handlerA, handlerB) => {
95
+ return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
92
96
  });
93
- return sortedHandlers;
94
97
  };
95
98
 
96
99
  exports.sortRoutes = sortRoutes;
@@ -1,5 +1,8 @@
1
1
  export declare class HttpError extends Error {
2
- status: number;
2
+ protected status: number;
3
3
  constructor(status: number, message: string);
4
4
  }
5
- export declare class ValidationError extends HttpError {}
5
+ export declare class ValidationError extends HttpError {
6
+ private code;
7
+ constructor(status: number, message: string);
8
+ }
@@ -1,4 +1,4 @@
1
- import { z } from 'zod';
1
+ import type { z } from 'zod';
2
2
  import { Operator, HttpMethod, ApiMiddleware, ResponseMetaType } from '../types';
3
3
  export interface ResponseMeta {
4
4
  type: ResponseMetaType;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.0.1-beta.4",
14
+ "version": "1.0.1-beta.5",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",