@modern-js/bff-runtime 1.2.0 → 1.2.3

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 CHANGED
@@ -1,5 +1,35 @@
1
1
  # @modern-js/bff-runtime
2
2
 
3
+ ## 1.2.3
4
+
5
+ ### Patch Changes
6
+
7
+ - d32f35134: chore: add modern/jest/eslint/ts config files to .npmignore
8
+ - Updated dependencies [d32f35134]
9
+ - Updated dependencies [1a30be07b]
10
+ - @modern-js/server-utils@1.2.6
11
+
12
+ ## 1.2.2
13
+
14
+ ### Patch Changes
15
+
16
+ - 6cffe99d: chore:
17
+ remove react eslint rules for `modern-js` rule set.
18
+ add .eslintrc for each package to speed up linting
19
+ - 60f7d8bf: feat: add tests dir to npmignore
20
+ - Updated dependencies [6cffe99d]
21
+ - Updated dependencies [04ae5262]
22
+ - Updated dependencies [60f7d8bf]
23
+ - @modern-js/server-utils@1.2.3
24
+
25
+ ## 1.2.1
26
+
27
+ ### Patch Changes
28
+
29
+ - 83166714: change .npmignore
30
+ - Updated dependencies [83166714]
31
+ - @modern-js/server-utils@1.2.1
32
+
3
33
  ## 1.2.0
4
34
 
5
35
  ### Minor Changes
@@ -1,11 +1,6 @@
1
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
-
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
4
-
5
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
6
-
7
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
8
-
9
4
  import { toSchemaCtor, Struct, NonStrict } from 'farrow-schema';
10
5
  import { createSchemaValidator } from 'farrow-schema/validator';
11
6
  import { HandleSuccess, InputValidationError, OutputValidationError } from "./response";
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.0",
14
+ "version": "1.2.3",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -29,14 +29,14 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@babel/runtime": "^7",
32
- "@modern-js/server-utils": "^1.2.0",
32
+ "@modern-js/server-utils": "^1.2.6",
33
33
  "farrow-api": "^1.10.8",
34
34
  "farrow-http": "^1.10.8",
35
35
  "farrow-pipeline": "^1.10.6",
36
36
  "farrow-schema": "^1.10.8"
37
37
  },
38
38
  "devDependencies": {
39
- "@types/jest": "^26",
39
+ "@types/jest": "^27",
40
40
  "@types/node": "^14",
41
41
  "@types/react": "^17",
42
42
  "@types/react-dom": "^17",
@@ -48,13 +48,34 @@
48
48
  },
49
49
  "publishConfig": {
50
50
  "registry": "https://registry.npmjs.org/",
51
- "access": "public",
52
- "types": "./dist/types/index.d.ts"
51
+ "access": "public"
52
+ },
53
+ "wireit": {
54
+ "build": {
55
+ "command": "modern build",
56
+ "files": [
57
+ "src/**/*",
58
+ "tsconfig.json",
59
+ "package.json"
60
+ ],
61
+ "output": [
62
+ "dist/**/*"
63
+ ]
64
+ },
65
+ "test": {
66
+ "command": "jest --passWithNoTests",
67
+ "files": [
68
+ "src/**/*",
69
+ "tsconfig.json",
70
+ "package.json"
71
+ ],
72
+ "output": []
73
+ }
53
74
  },
54
75
  "scripts": {
55
76
  "new": "modern new",
56
- "build": "modern build",
57
- "test": "jest --passWithNoTests"
77
+ "build": "wireit",
78
+ "test": "wireit"
58
79
  },
59
80
  "readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
60
81
  }
package/jest.config.js DELETED
@@ -1,8 +0,0 @@
1
- const sharedConfig = require('@scripts/jest-config');
2
-
3
- /** @type {import('@jest/types').Config.InitialOptions} */
4
- module.exports = {
5
- // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
- ...sharedConfig,
7
- rootDir: __dirname,
8
- };
package/modern.config.js DELETED
@@ -1,2 +0,0 @@
1
- /** @type {import('@modern-js/module-tools').UserConfig} */
2
- module.exports = {};
package/src/compatible.ts DELETED
@@ -1,8 +0,0 @@
1
- export type RequestOption<TQuery = never, TData = never> = ClearRecord<{
2
- query: TQuery;
3
- data: TData;
4
- }> & { dataType?: string };
5
-
6
- export type ClearRecord<O extends Record<string, any>> = {
7
- [K in keyof O as O[K] extends never ? never : K]: O[K];
8
- };
package/src/index.ts DELETED
@@ -1,16 +0,0 @@
1
- export * from 'farrow-schema';
2
- export * from 'farrow-api';
3
- export * from 'farrow-pipeline';
4
-
5
- export { match, isHandler, isSchemaHandler } from './match';
6
-
7
- export type { Handler, SchemaHandler } from './match';
8
- export type { RequestSchema, TypeOfRequestSchema, InputType } from './request';
9
- export type {
10
- HandleResult,
11
- HandleSuccess,
12
- InputValidationError,
13
- OutputValidationError,
14
- } from './response';
15
-
16
- export type { RequestOption } from './compatible';
package/src/match.ts DELETED
@@ -1,146 +0,0 @@
1
- import { TypeOfRouterRequestField } from 'farrow-http/dist/router';
2
- import {
3
- FieldDescriptors,
4
- SchemaCtorInput,
5
- toSchemaCtor,
6
- Struct,
7
- NonStrict,
8
- } from 'farrow-schema';
9
- import { MaybeAsync } from 'farrow-pipeline';
10
- import {
11
- createSchemaValidator,
12
- Validator,
13
- ValidationError,
14
- } from 'farrow-schema/validator';
15
-
16
- import {
17
- RequestSchema,
18
- TypeOfRequestSchema,
19
- PureTypeOfRequestSchema,
20
- } from './request';
21
- import {
22
- HandleResult,
23
- HandleSuccess,
24
- InputValidationError,
25
- OutputValidationError,
26
- } from './response';
27
-
28
- export type NormalHandler = (...args: any[]) => any;
29
- export type Handler<I, O> = (input: I) => MaybeAsync<O>;
30
-
31
- export type Schema<Req extends RequestSchema, Res extends SchemaCtorInput> = {
32
- request: Req;
33
- response: Res;
34
- description?: string;
35
- deprecated?: string;
36
- };
37
-
38
- const getErrorMessage = (error: ValidationError) => {
39
- let { message } = error;
40
-
41
- if (Array.isArray(error.path) && error.path.length > 0) {
42
- message = `path: ${JSON.stringify(error.path)}\n${message}`;
43
- }
44
-
45
- return message;
46
- };
47
-
48
- const HANDLER_WITH_SCHEMA = 'HANDLER_WITH_SCHEMA';
49
-
50
- export type BaseSchemaHandler<
51
- Req extends RequestSchema,
52
- Res extends SchemaCtorInput,
53
- > = ((
54
- input: TypeOfRequestSchema<Req>,
55
- ) => Promise<HandleResult<TypeOfRouterRequestField<Res>>>) & {
56
- schema: Schema<Req, Res>;
57
- [HANDLER_WITH_SCHEMA]: true;
58
- };
59
-
60
- export type SchemaHandler<
61
- Req extends RequestSchema,
62
- Res extends SchemaCtorInput,
63
- > = ((
64
- input: TypeOfRequestSchema<Req>,
65
- ) => Promise<TypeOfRouterRequestField<Res>>) & {
66
- schema: Schema<Req, Res>;
67
- [HANDLER_WITH_SCHEMA]: true;
68
- };
69
-
70
- export const isSchemaHandler = (input: any): input is SchemaHandler<any, any> =>
71
- input && input?.[HANDLER_WITH_SCHEMA] === true;
72
-
73
- export const isHandler = (input: any): input is Handler<any, any> =>
74
- input && typeof input === 'function';
75
-
76
- export const baseMatch = <
77
- Req extends RequestSchema,
78
- Res extends SchemaCtorInput,
79
- >(
80
- schema: Schema<Req, Res>,
81
- handler: Handler<TypeOfRequestSchema<Req>, TypeOfRouterRequestField<Res>>,
82
- ): BaseSchemaHandler<Req, Res> => {
83
- const validateApiInput = createRequestSchemaValidator(schema.request);
84
-
85
- const validateApiOutput = createSchemaValidator(
86
- toSchemaCtor(schema.response),
87
- );
88
-
89
- const handle = async (
90
- input: TypeOfRequestSchema<Req>,
91
- ): Promise<HandleResult<TypeOfRouterRequestField<Res>>> => {
92
- const inputResult = validateApiInput(input);
93
- if (inputResult.isErr) {
94
- return InputValidationError(getErrorMessage(inputResult.value));
95
- }
96
-
97
- const output = await handler(input);
98
-
99
- const outputResult = validateApiOutput(output);
100
- if (outputResult.isErr) {
101
- return OutputValidationError(getErrorMessage(outputResult.value));
102
- }
103
-
104
- return HandleSuccess(output);
105
- };
106
-
107
- return Object.assign(handle, {
108
- schema,
109
- [HANDLER_WITH_SCHEMA]: true as const,
110
- });
111
- };
112
-
113
- export const match: <Req extends RequestSchema, Res extends SchemaCtorInput>(
114
- schema: Schema<Req, Res>,
115
- handler: Handler<PureTypeOfRequestSchema<Req>, TypeOfRouterRequestField<Res>>,
116
- ) => SchemaHandler<Req, Res> = baseMatch as any;
117
-
118
- const createRequestSchemaValidator = <T extends RequestSchema>(schema: T) => {
119
- const descriptors: FieldDescriptors = {};
120
-
121
- if (schema.params) {
122
- descriptors.params = schema.params;
123
- }
124
-
125
- if (schema.query) {
126
- descriptors.query = schema.query;
127
- }
128
-
129
- if (schema.data) {
130
- descriptors.data = schema.data;
131
- }
132
-
133
- if (schema.headers) {
134
- descriptors.headers = schema.headers;
135
- }
136
-
137
- if (schema.cookies) {
138
- descriptors.cookies = schema.cookies;
139
- }
140
-
141
- const RequestStruct = Struct(descriptors);
142
-
143
- return createSchemaValidator(NonStrict(RequestStruct) as any) as Validator<
144
- TypeOfRequestSchema<T>
145
- >;
146
- };
package/src/request.ts DELETED
@@ -1,103 +0,0 @@
1
- import { RouterSchemaDescriptor } from 'farrow-http/dist/router';
2
- import { MarkReadOnlyDeep } from 'farrow-http/dist/types';
3
- import {
4
- TypeOfFieldDescriptor,
5
- TypeOfFieldDescriptors,
6
- FieldDescriptor,
7
- FieldDescriptors,
8
- } from 'farrow-schema';
9
-
10
- export type RequestBaseSchema = {
11
- params?: RouterSchemaDescriptor;
12
- query?: RouterSchemaDescriptor;
13
- headers?: RouterSchemaDescriptor;
14
- cookies?: RouterSchemaDescriptor;
15
- };
16
-
17
- export type RequestDataSchema = {
18
- data?: RouterSchemaDescriptor;
19
- };
20
-
21
- export type RequestBodyType = {
22
- body?: string;
23
- };
24
-
25
- export type PureRequestFormDataType = {
26
- formData?: Record<string, any>;
27
- };
28
- export type RequestFormDataType = {
29
- formData?: FormData;
30
- };
31
- export type RequestFormUrlencodedType = {
32
- // eslint-disable-next-line node/prefer-global/url-search-params,node/no-unsupported-features/node-builtins
33
- formUrlencoded?: URLSearchParams | Record<string, string> | string;
34
- };
35
- export type PureRequestFormUrlencodedType = {
36
- formUrlencoded?: Record<string, string>;
37
- };
38
-
39
- export type RequestExtraType = RequestBodyType &
40
- RequestFormDataType &
41
- RequestFormUrlencodedType;
42
-
43
- export type PureRequestExtraType = RequestBodyType &
44
- PureRequestFormDataType &
45
- PureRequestFormUrlencodedType;
46
-
47
- export type RequestSchema = RequestBaseSchema & RequestDataSchema;
48
-
49
- export type TypeOfRequestField<T> = T extends string
50
- ? string
51
- : T extends FormData
52
- ? FormData
53
- : T extends FieldDescriptor
54
- ? TypeOfFieldDescriptor<T>
55
- : T extends FieldDescriptors
56
- ? TypeOfFieldDescriptors<T>
57
- : never;
58
-
59
- export type TypeOfRequestDataSchema<T extends RequestDataSchema> =
60
- MarkReadOnlyDeep<
61
- T extends { data: any }
62
- ? Pick<
63
- {
64
- [key in keyof T]: TypeOfRequestField<T[key]>;
65
- },
66
- 'data'
67
- >
68
- : RequestExtraType
69
- >;
70
-
71
- export type TypeOfRequestSchema<T extends RequestSchema> = MarkReadOnlyDeep<
72
- Omit<
73
- {
74
- [key in keyof T]: TypeOfRequestField<T[key]>;
75
- },
76
- 'data'
77
- > &
78
- TypeOfRequestDataSchema<T>
79
- >;
80
-
81
- export type PureTypeOfRequestDataSchema<T extends RequestDataSchema> =
82
- MarkReadOnlyDeep<
83
- T extends { data: any }
84
- ? Pick<
85
- {
86
- [key in keyof T]: TypeOfRequestField<T[key]>;
87
- },
88
- 'data'
89
- >
90
- : PureRequestExtraType
91
- >;
92
-
93
- export type PureTypeOfRequestSchema<T extends RequestSchema> = MarkReadOnlyDeep<
94
- Omit<
95
- {
96
- [key in keyof T]: TypeOfRequestField<T[key]>;
97
- },
98
- 'data'
99
- > &
100
- PureTypeOfRequestDataSchema<T>
101
- >;
102
-
103
- export type InputType = TypeOfRequestSchema<RequestSchema>;
package/src/response.ts DELETED
@@ -1,41 +0,0 @@
1
- export type HandleSuccess<T> = {
2
- type: 'HandleSuccess';
3
- value: T;
4
- };
5
-
6
- // eslint-disable-next-line @typescript-eslint/no-redeclare
7
- export const HandleSuccess = <T>(output: T): HandleSuccess<T> => ({
8
- type: 'HandleSuccess',
9
- value: output,
10
- });
11
-
12
- export type InputValidationError = {
13
- type: 'InputValidationError';
14
- message: string;
15
- };
16
-
17
- // eslint-disable-next-line @typescript-eslint/no-redeclare
18
- export const InputValidationError = (
19
- message: string,
20
- ): InputValidationError => ({
21
- type: 'InputValidationError',
22
- message,
23
- });
24
-
25
- export type OutputValidationError = {
26
- type: 'OutputValidationError';
27
- message: string;
28
- };
29
-
30
- // eslint-disable-next-line @typescript-eslint/no-redeclare
31
- export const OutputValidationError = (
32
- message: string,
33
- ): OutputValidationError => ({
34
- type: 'OutputValidationError',
35
- message,
36
- });
37
-
38
- export type HandleResult<T> =
39
- | HandleSuccess<T>
40
- | InputValidationError
41
- | OutputValidationError;
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- extends: ['@modern-js'],
3
- parserOptions: {
4
- project: require.resolve('./tsconfig.json'),
5
- },
6
- };
@@ -1,133 +0,0 @@
1
- import assert from 'assert';
2
- import { baseMatch } from '../src/match';
3
- import { match, isHandler, isSchemaHandler, Any } from '../src';
4
-
5
- describe('match', () => {
6
- it('should work well', async () => {
7
- const foo = baseMatch(
8
- {
9
- request: { data: { foo: Number } },
10
- response: { foo: String },
11
- },
12
- input => ({ foo: String(input.data.foo) }),
13
- );
14
-
15
- const result = await foo({ data: { foo: 0 } });
16
-
17
- expect(result.type).toBe('HandleSuccess');
18
- assert(result.type === 'HandleSuccess');
19
- expect(result.value.foo).toBe('0');
20
- });
21
-
22
- it('should support all file in request', async () => {
23
- const foo = baseMatch(
24
- {
25
- request: {
26
- data: { foo: Number },
27
- params: { id: String },
28
- query: { key: String },
29
- headers: { 'Context-Length': String },
30
- cookies: { sid: String },
31
- },
32
- response: { foo: String },
33
- },
34
- input => ({ foo: String(input.data.foo) }),
35
- );
36
-
37
- const result = await foo({
38
- data: { foo: 0 },
39
- params: { id: 'foo' },
40
- query: { key: 'foo' },
41
- headers: { 'Context-Length': '100' },
42
- cookies: { sid: 'sid0' },
43
- });
44
-
45
- expect(result.type).toBe('HandleSuccess');
46
- assert(result.type === 'HandleSuccess');
47
- expect(result.value.foo).toBe('0');
48
- });
49
-
50
- it('should support body,formData,formUrlencoded when data is not exist', () => {
51
- const foo = baseMatch(
52
- {
53
- request: {},
54
- response: Any,
55
- },
56
- input => input,
57
- );
58
- foo({ body: 'test' });
59
- foo({ formData: { foo: 'test' } as any });
60
- foo({ formUrlencoded: { foo: 'test' } });
61
- });
62
-
63
- it('should fail when input does not match schema', async () => {
64
- const foo = baseMatch(
65
- {
66
- request: { data: { foo: Number } },
67
- response: { foo: String },
68
- },
69
- input => ({ foo: String(input.data.foo) }),
70
- );
71
-
72
- const result = await foo({ data: { foo: true as any } });
73
-
74
- expect(result.type).toBe('InputValidationError');
75
- assert(result.type === 'InputValidationError');
76
- expect(result.message).toBe('path: ["data","foo"]\ntrue is not a number');
77
- });
78
-
79
- it('should fail when output does not match schema', async () => {
80
- const foo = baseMatch(
81
- {
82
- request: { data: { foo: Number } },
83
- response: { foo: String },
84
- },
85
- input => ({ foo: input.data.foo } as any),
86
- );
87
-
88
- const result = await foo({ data: { foo: 0 } });
89
-
90
- expect(result.type).toBe('OutputValidationError');
91
- assert(result.type === 'OutputValidationError');
92
- expect(result.message).toBe('path: ["foo"]\n0 is not a string');
93
- });
94
-
95
- it('should type nest', async () => {
96
- const getFoo = (input: { foo: string }) => input;
97
-
98
- const foo = match(
99
- {
100
- request: { data: { foo: Number } },
101
- response: { foo: String },
102
- },
103
- input => ({ foo: input.data.foo } as any),
104
- );
105
-
106
- const result = await foo({ data: { foo: 0 } });
107
-
108
- getFoo(result);
109
- });
110
-
111
- it('isSchemaHandler', () => {
112
- const foo = baseMatch(
113
- {
114
- request: { data: { foo: Number } },
115
- response: { foo: String },
116
- },
117
- input => ({ foo: String(input.data.foo) }),
118
- );
119
-
120
- expect(isSchemaHandler(foo)).toBeTruthy();
121
- expect(isSchemaHandler({})).toBeFalsy();
122
- expect(isSchemaHandler('test')).toBeFalsy();
123
- expect(isSchemaHandler(null)).toBeFalsy();
124
- });
125
-
126
- it('isHandler', () => {
127
- // eslint-disable-next-line @typescript-eslint/no-empty-function
128
- expect(isHandler(() => {})).toBeTruthy();
129
- expect(isHandler({})).toBeFalsy();
130
- expect(isHandler('test')).toBeFalsy();
131
- expect(isHandler(null)).toBeFalsy();
132
- });
133
- });
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "@modern-js/tsconfig/base",
3
- "compilerOptions": {
4
- "declaration": false,
5
- "jsx": "preserve",
6
- "baseUrl": "./",
7
- "isolatedModules": true,
8
- "paths": {}
9
- }
10
- }
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "extends": "@modern-js/tsconfig/base",
3
- "compilerOptions": {
4
- "declaration": false,
5
- "jsx": "preserve",
6
- "baseUrl": "./",
7
- "isolatedModules": true,
8
- "paths": {}
9
- },
10
- "include": ["src"]
11
- }