@modern-js/bff-core 1.0.1-beta.4 → 1.0.1-beta.7

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,7 +5,11 @@ const validateInput = async (schema, input) => {
6
5
  try {
7
6
  await schema.parseAsync(input);
8
7
  } catch (error) {
9
- if (error instanceof z.ZodError) {
8
+ const {
9
+ z: zod
10
+ } = require('zod');
11
+
12
+ if (error instanceof zod.ZodError) {
10
13
  throw new ValidationError(400, error.message);
11
14
  }
12
15
 
@@ -38,17 +38,30 @@ const clearRouteName = routeName => {
38
38
 
39
39
  export const isHandler = input => input && typeof input === 'function';
40
40
 
41
- const isFunction = input => input && {}.toString.call(input) === '[object Function]';
41
+ const enableRegister = requireFn => {
42
+ return modulePath => {
43
+ // eslint-disable-next-line node/no-deprecated-api
44
+ if (!require.extensions['.ts']) {
45
+ const {
46
+ register
47
+ } = require('esbuild-register/dist/node');
42
48
 
43
- export const requireHandlerModule = modulePath => {
44
- const {
45
- register
46
- } = require('esbuild-register/dist/node');
49
+ const {
50
+ unregister
51
+ } = register({});
52
+ const requiredModule = requireFn(modulePath);
53
+ unregister();
54
+ return requiredModule;
55
+ }
47
56
 
48
- const {
49
- unregister
50
- } = register({});
57
+ const requiredModule = requireFn(modulePath);
58
+ return requiredModule;
59
+ };
60
+ };
61
+
62
+ const isFunction = input => input && {}.toString.call(input) === '[object Function]';
51
63
 
64
+ export const requireHandlerModule = enableRegister(modulePath => {
52
65
  const module = require(modulePath);
53
66
 
54
67
  if (isFunction(module)) {
@@ -57,16 +70,19 @@ export const requireHandlerModule = modulePath => {
57
70
  };
58
71
  }
59
72
 
60
- unregister();
61
73
  return module;
74
+ });
75
+
76
+ const routeValue = routePath => {
77
+ if (routePath.includes(':')) {
78
+ return 11;
79
+ }
80
+
81
+ return 1;
62
82
  };
83
+
63
84
  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
- }
85
+ return apiHandlers.sort((handlerA, handlerB) => {
86
+ return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
70
87
  });
71
- return sortedHandlers;
72
88
  };
@@ -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,8 +5,6 @@ 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");
@@ -15,7 +13,11 @@ const validateInput = async (schema, input) => {
15
13
  try {
16
14
  await schema.parseAsync(input);
17
15
  } catch (error) {
18
- if (error instanceof _zod.z.ZodError) {
16
+ const {
17
+ z: zod
18
+ } = require('zod');
19
+
20
+ if (error instanceof zod.ZodError) {
19
21
  throw new _http.ValidationError(400, error.message);
20
22
  }
21
23
 
@@ -57,17 +57,30 @@ const isHandler = input => input && typeof input === 'function';
57
57
 
58
58
  exports.isHandler = isHandler;
59
59
 
60
- const isFunction = input => input && {}.toString.call(input) === '[object Function]';
60
+ const enableRegister = requireFn => {
61
+ return modulePath => {
62
+ // eslint-disable-next-line node/no-deprecated-api
63
+ if (!require.extensions['.ts']) {
64
+ const {
65
+ register
66
+ } = require('esbuild-register/dist/node');
67
+
68
+ const {
69
+ unregister
70
+ } = register({});
71
+ const requiredModule = requireFn(modulePath);
72
+ unregister();
73
+ return requiredModule;
74
+ }
61
75
 
62
- const requireHandlerModule = modulePath => {
63
- const {
64
- register
65
- } = require('esbuild-register/dist/node');
76
+ const requiredModule = requireFn(modulePath);
77
+ return requiredModule;
78
+ };
79
+ };
66
80
 
67
- const {
68
- unregister
69
- } = register({});
81
+ const isFunction = input => input && {}.toString.call(input) === '[object Function]';
70
82
 
83
+ const requireHandlerModule = enableRegister(modulePath => {
71
84
  const module = require(modulePath);
72
85
 
73
86
  if (isFunction(module)) {
@@ -76,21 +89,22 @@ const requireHandlerModule = modulePath => {
76
89
  };
77
90
  }
78
91
 
79
- unregister();
80
92
  return module;
81
- };
82
-
93
+ });
83
94
  exports.requireHandlerModule = requireHandlerModule;
84
95
 
96
+ const routeValue = routePath => {
97
+ if (routePath.includes(':')) {
98
+ return 11;
99
+ }
100
+
101
+ return 1;
102
+ };
103
+
85
104
  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
- }
105
+ return apiHandlers.sort((handlerA, handlerB) => {
106
+ return routeValue(handlerA.routeName) - routeValue(handlerB.routeName);
92
107
  });
93
- return sortedHandlers;
94
108
  };
95
109
 
96
110
  exports.sortRoutes = sortRoutes;
@@ -1,15 +1,15 @@
1
- export declare type Err<T = any> = {
1
+ export declare type Err<T = unknown> = {
2
2
  kind: 'Err';
3
3
  value: T;
4
4
  isErr: true;
5
5
  isOk: false;
6
6
  };
7
- export declare type Ok<T = any> = {
7
+ export declare type Ok<T = unknown> = {
8
8
  kind: 'Ok';
9
9
  value: T;
10
10
  isErr: false;
11
11
  isOk: true;
12
12
  };
13
- export declare type Result<T = any, E = string> = Err<E> | Ok<T>;
13
+ export declare type Result<T = unknown, E = string> = Err<E> | Ok<T>;
14
14
  export declare const Err: <E = string>(value: E) => Err<E>;
15
15
  export declare const Ok: <T, E = string>(value: T) => Result<T, E>;
@@ -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;
@@ -1,10 +1,10 @@
1
- import { APIHandlerInfo } from './types';
1
+ import { APIHandlerInfo, HandlerModule } from './types';
2
2
  declare type MaybeAsync<I> = I | Promise<I>;
3
3
  export declare type NormalHandler = (...args: any[]) => any;
4
4
  export declare type Handler<I, O> = (input: I) => MaybeAsync<O>;
5
5
  export declare const getFiles: (lambdaDir: string, rules: string | string[]) => string[];
6
6
  export declare const getPathFromFilename: (baseDir: string, filename: string) => string;
7
7
  export declare const isHandler: (input: any) => input is Handler<any, any>;
8
- export declare const requireHandlerModule: (modulePath: string) => any;
8
+ export declare const requireHandlerModule: (modulePath: string) => HandlerModule;
9
9
  export declare const sortRoutes: (apiHandlers: APIHandlerInfo[]) => APIHandlerInfo[];
10
10
  export {};
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.7",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -24,12 +24,11 @@
24
24
  }
25
25
  },
26
26
  "dependencies": {
27
- "@babel/runtime": "^7.15.4",
27
+ "@babel/runtime": "^7.18.0",
28
28
  "@modern-js/bff-runtime": "^1.2.3",
29
29
  "@modern-js/utils": "^1.7.6",
30
30
  "esbuild": "^0.14.38",
31
31
  "esbuild-register": "^3.3.3",
32
- "http-errors": "^2.0.0",
33
32
  "koa-compose": "^4.1.0",
34
33
  "reflect-metadata": "^0.1.13"
35
34
  },