@modern-js/bff-runtime 1.2.0 → 1.2.1
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 +8 -0
- package/package.json +2 -2
- package/src/compatible.ts +0 -8
- package/src/index.ts +0 -16
- package/src/match.ts +0 -146
- package/src/request.ts +0 -103
- package/src/response.ts +0 -41
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.2.
|
|
14
|
+
"version": "1.2.1",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@babel/runtime": "^7",
|
|
32
|
-
"@modern-js/server-utils": "^1.2.
|
|
32
|
+
"@modern-js/server-utils": "^1.2.1",
|
|
33
33
|
"farrow-api": "^1.10.8",
|
|
34
34
|
"farrow-http": "^1.10.8",
|
|
35
35
|
"farrow-pipeline": "^1.10.6",
|
package/src/compatible.ts
DELETED
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;
|