@modern-js/bff-runtime 1.1.2-rc.0 → 1.2.2

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/.eslintrc.js ADDED
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['@modern-js'],
4
+ parserOptions: {
5
+ tsconfigRootDir: __dirname,
6
+ project: ['./tsconfig.json'],
7
+ },
8
+ };
package/CHANGELOG.md CHANGED
@@ -1,10 +1,43 @@
1
1
  # @modern-js/bff-runtime
2
2
 
3
- ## 1.1.2-rc.0
3
+ ## 1.2.2
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - @modern-js/server-utils@1.1.3-rc.0
7
+ - 6cffe99d: chore:
8
+ remove react eslint rules for `modern-js` rule set.
9
+ add .eslintrc for each package to speed up linting
10
+ - 60f7d8bf: feat: add tests dir to npmignore
11
+ - Updated dependencies [6cffe99d]
12
+ - Updated dependencies [04ae5262]
13
+ - Updated dependencies [60f7d8bf]
14
+ - @modern-js/server-utils@1.2.3
15
+
16
+ ## 1.2.1
17
+
18
+ ### Patch Changes
19
+
20
+ - 83166714: change .npmignore
21
+ - Updated dependencies [83166714]
22
+ - @modern-js/server-utils@1.2.1
23
+
24
+ ## 1.2.0
25
+
26
+ ### Minor Changes
27
+
28
+ - cfe11628: Make Modern.js self bootstraping
29
+
30
+ ### Patch Changes
31
+
32
+ - 146dcd85: modify server framework plugin hook types and hook context
33
+ - 146dcd85: modify server framework plugin hook types
34
+ - 146dcd85: fix test case in babel compiler
35
+ - Updated dependencies [146dcd85]
36
+ - Updated dependencies [cfe11628]
37
+ - Updated dependencies [146dcd85]
38
+ - Updated dependencies [146dcd85]
39
+ - Updated dependencies [1ebc7ee2]
40
+ - @modern-js/server-utils@1.2.0
8
41
 
9
42
  ## 1.1.1
10
43
 
@@ -1,5 +1,4 @@
1
1
  export * from 'farrow-schema';
2
2
  export * from 'farrow-api';
3
3
  export * from 'farrow-pipeline';
4
- export { hook } from '@modern-js/server-utils';
5
4
  export { match, isHandler, isSchemaHandler } from "./match";
@@ -4,17 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  var _exportNames = {
7
- hook: true,
8
7
  match: true,
9
8
  isHandler: true,
10
9
  isSchemaHandler: true
11
10
  };
12
- Object.defineProperty(exports, "hook", {
13
- enumerable: true,
14
- get: function () {
15
- return _serverUtils.hook;
16
- }
17
- });
18
11
  Object.defineProperty(exports, "isHandler", {
19
12
  enumerable: true,
20
13
  get: function () {
@@ -76,6 +69,4 @@ Object.keys(_farrowPipeline).forEach(function (key) {
76
69
  });
77
70
  });
78
71
 
79
- var _serverUtils = require("@modern-js/server-utils");
80
-
81
72
  var _match = require("./match");
@@ -1,5 +1,4 @@
1
1
  export * from 'farrow-schema';
2
2
  export * from 'farrow-api';
3
3
  export * from 'farrow-pipeline';
4
- export { hook } from '@modern-js/server-utils';
5
4
  export { match, isHandler, isSchemaHandler } from "./match";
@@ -1,7 +1,6 @@
1
1
  export * from 'farrow-schema';
2
2
  export * from 'farrow-api';
3
3
  export * from 'farrow-pipeline';
4
- export { hook } from '@modern-js/server-utils';
5
4
  export { match, isHandler, isSchemaHandler } from './match';
6
5
  export type { Handler, SchemaHandler } from './match';
7
6
  export type { RequestSchema, TypeOfRequestSchema, InputType } from './request';
package/jest.config.js ADDED
@@ -0,0 +1,7 @@
1
+ const sharedConfig = require('@scripts/jest-config');
2
+
3
+ /** @type {import('@jest/types').Config.InitialOptions} */
4
+ module.exports = {
5
+ ...sharedConfig,
6
+ rootDir: __dirname,
7
+ };
package/modern.config.js CHANGED
@@ -1,15 +1,2 @@
1
1
  /** @type {import('@modern-js/module-tools').UserConfig} */
2
- module.exports = {
3
- testing: {
4
- jest: {
5
- collectCoverage: true,
6
- collectCoverageFrom: ['src/**/*.ts'],
7
- coveragePathIgnorePatterns: ['/node_modules/'],
8
- moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json'],
9
- testEnvironment: 'jsdom',
10
- transform: {
11
- '^.+\\.tsx?$': 'ts-jest',
12
- },
13
- },
14
- },
15
- };
2
+ module.exports = {};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.1.2-rc.0",
14
+ "version": "1.2.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -20,6 +20,7 @@
20
20
  "exports": {
21
21
  ".": {
22
22
  "node": {
23
+ "jsnext:source": "./src/index.ts",
23
24
  "import": "./dist/js/modern/index.js",
24
25
  "require": "./dist/js/node/index.js"
25
26
  },
@@ -28,7 +29,7 @@
28
29
  },
29
30
  "dependencies": {
30
31
  "@babel/runtime": "^7",
31
- "@modern-js/server-utils": "^1.1.3-rc.0",
32
+ "@modern-js/server-utils": "^1.2.3",
32
33
  "farrow-api": "^1.10.8",
33
34
  "farrow-http": "^1.10.8",
34
35
  "farrow-pipeline": "^1.10.6",
@@ -41,8 +42,9 @@
41
42
  "@types/react-dom": "^17",
42
43
  "ts-jest": "^27.0.5",
43
44
  "typescript": "^4",
44
- "@modern-js/plugin-testing": "^1.1.1",
45
- "@modern-js/module-tools": "^1.1.1"
45
+ "@scripts/build": "0.0.0",
46
+ "jest": "^27",
47
+ "@scripts/jest-config": "0.0.0"
46
48
  },
47
49
  "publishConfig": {
48
50
  "registry": "https://registry.npmjs.org/",
@@ -51,7 +53,7 @@
51
53
  "scripts": {
52
54
  "new": "modern new",
53
55
  "build": "modern build",
54
- "test": "modern test --passWithNoTests"
56
+ "test": "jest --passWithNoTests"
55
57
  },
56
58
  "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"
57
59
  }
package/tsconfig.json CHANGED
@@ -5,9 +5,7 @@
5
5
  "jsx": "preserve",
6
6
  "baseUrl": "./",
7
7
  "isolatedModules": true,
8
- "paths": {
9
- "@/*": ["./src/*"]
10
- }
8
+ "paths": {}
11
9
  },
12
10
  "include": ["src"]
13
11
  }
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,17 +0,0 @@
1
- export * from 'farrow-schema';
2
- export * from 'farrow-api';
3
- export * from 'farrow-pipeline';
4
- export { hook } from '@modern-js/server-utils';
5
-
6
- export { match, isHandler, isSchemaHandler } from './match';
7
-
8
- export type { Handler, SchemaHandler } from './match';
9
- export type { RequestSchema, TypeOfRequestSchema, InputType } from './request';
10
- export type {
11
- HandleResult,
12
- HandleSuccess,
13
- InputValidationError,
14
- OutputValidationError,
15
- } from './response';
16
-
17
- 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 '@/match';
3
- import { match, isHandler, isSchemaHandler, Any } from '@/index';
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,12 +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
- "@/*": ["../src/*"]
10
- }
11
- }
12
- }