@modern-js/bff-core 1.0.1-beta.4 → 1.1.0-alpha.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # @modern-js/bff-core
2
+
3
+ ## 1.1.0-alpha.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 77a8e9e1b: feat: support bff operators
8
+
9
+ ### Patch Changes
10
+
11
+ - 7b9e302e2: fix: incorrect @babel/runtime version
12
+ - Updated dependencies [77a8e9e1b]
13
+ - Updated dependencies [9cd364e06]
14
+ - Updated dependencies [a90bc96bd]
15
+ - @modern-js/bff-runtime@1.3.0-alpha.0
16
+ - @modern-js/utils@1.7.9-alpha.0
@@ -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
 
@@ -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,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
 
@@ -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,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;
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.1.0-alpha.0",
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",
28
- "@modern-js/bff-runtime": "^1.2.3",
29
- "@modern-js/utils": "^1.7.6",
27
+ "@babel/runtime": "^7.18.0",
28
+ "@modern-js/bff-runtime": "^1.3.0-alpha.0",
29
+ "@modern-js/utils": "^1.7.9-alpha.0",
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
  },
@@ -59,8 +58,7 @@
59
58
  },
60
59
  "publishConfig": {
61
60
  "registry": "https://registry.npmjs.org/",
62
- "access": "public",
63
- "types": "./dist/types/index.d.ts"
61
+ "access": "public"
64
62
  },
65
63
  "wireit": {
66
64
  "build": {