@bleedingdev/modern-js-bff-runtime 3.2.0-ultramodern.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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/cjs/index.js +79 -0
- package/dist/cjs/match.js +82 -0
- package/dist/cjs/request.js +18 -0
- package/dist/cjs/response.js +53 -0
- package/dist/cjs/types.js +18 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/match.mjs +39 -0
- package/dist/esm/request.mjs +0 -0
- package/dist/esm/response.mjs +13 -0
- package/dist/esm/types.mjs +0 -0
- package/dist/esm-node/index.mjs +3 -0
- package/dist/esm-node/match.mjs +40 -0
- package/dist/esm-node/request.mjs +1 -0
- package/dist/esm-node/response.mjs +14 -0
- package/dist/esm-node/types.mjs +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/match.d.ts +27 -0
- package/dist/types/request.d.ts +47 -0
- package/dist/types/response.d.ts +16 -0
- package/dist/types/types.d.ts +8 -0
- package/package.json +53 -0
- package/rslib.config.mts +4 -0
- package/rstest.config.mts +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-present Modern.js
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<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>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">Modern.js</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
A Progressive React Framework for modern web development.
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
## Getting Started
|
|
12
|
+
|
|
13
|
+
Please follow [Quick Start](https://modernjs.dev/en/guides/get-started/quick-start) to get started with Modern.js.
|
|
14
|
+
|
|
15
|
+
## Documentation
|
|
16
|
+
|
|
17
|
+
- [English Documentation](https://modernjs.dev/en/)
|
|
18
|
+
- [中文文档](https://modernjs.dev)
|
|
19
|
+
|
|
20
|
+
## Contributing
|
|
21
|
+
|
|
22
|
+
Please read the [Contributing Guide](https://github.com/web-infra-dev/modern.js/blob/main/CONTRIBUTING.md).
|
|
23
|
+
|
|
24
|
+
## License
|
|
25
|
+
|
|
26
|
+
Modern.js is [MIT licensed](https://github.com/web-infra-dev/modern.js/blob/main/LICENSE).
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_modules__ = {
|
|
3
|
+
"./match" (module) {
|
|
4
|
+
module.exports = require("./match.js");
|
|
5
|
+
},
|
|
6
|
+
"farrow-schema" (module) {
|
|
7
|
+
module.exports = require("farrow-schema");
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var __webpack_module_cache__ = {};
|
|
11
|
+
function __webpack_require__(moduleId) {
|
|
12
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
13
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
14
|
+
var module = __webpack_module_cache__[moduleId] = {
|
|
15
|
+
exports: {}
|
|
16
|
+
};
|
|
17
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
18
|
+
return module.exports;
|
|
19
|
+
}
|
|
20
|
+
(()=>{
|
|
21
|
+
__webpack_require__.n = (module)=>{
|
|
22
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
23
|
+
__webpack_require__.d(getter, {
|
|
24
|
+
a: getter
|
|
25
|
+
});
|
|
26
|
+
return getter;
|
|
27
|
+
};
|
|
28
|
+
})();
|
|
29
|
+
(()=>{
|
|
30
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
31
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: definition[key]
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
})();
|
|
37
|
+
(()=>{
|
|
38
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
39
|
+
})();
|
|
40
|
+
(()=>{
|
|
41
|
+
__webpack_require__.r = (exports1)=>{
|
|
42
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
43
|
+
value: 'Module'
|
|
44
|
+
});
|
|
45
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
46
|
+
value: true
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
})();
|
|
50
|
+
var __webpack_exports__ = {};
|
|
51
|
+
(()=>{
|
|
52
|
+
__webpack_require__.r(__webpack_exports__);
|
|
53
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
54
|
+
isHandler: ()=>_match__rspack_import_1.isHandler,
|
|
55
|
+
isSchemaHandler: ()=>_match__rspack_import_1.isSchemaHandler,
|
|
56
|
+
match: ()=>_match__rspack_import_1.match
|
|
57
|
+
});
|
|
58
|
+
var farrow_schema__rspack_import_0 = __webpack_require__("farrow-schema");
|
|
59
|
+
var __rspack_reexport = {};
|
|
60
|
+
for(const __rspack_import_key in farrow_schema__rspack_import_0)if ([
|
|
61
|
+
"isHandler",
|
|
62
|
+
"default",
|
|
63
|
+
"isSchemaHandler",
|
|
64
|
+
"match"
|
|
65
|
+
].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>farrow_schema__rspack_import_0[__rspack_import_key];
|
|
66
|
+
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
67
|
+
var _match__rspack_import_1 = __webpack_require__("./match");
|
|
68
|
+
})();
|
|
69
|
+
exports.isHandler = __webpack_exports__.isHandler;
|
|
70
|
+
exports.isSchemaHandler = __webpack_exports__.isSchemaHandler;
|
|
71
|
+
exports.match = __webpack_exports__.match;
|
|
72
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
73
|
+
"isHandler",
|
|
74
|
+
"isSchemaHandler",
|
|
75
|
+
"match"
|
|
76
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
77
|
+
Object.defineProperty(exports, '__esModule', {
|
|
78
|
+
value: true
|
|
79
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
baseMatch: ()=>baseMatch,
|
|
28
|
+
isHandler: ()=>isHandler,
|
|
29
|
+
isSchemaHandler: ()=>isSchemaHandler,
|
|
30
|
+
match: ()=>match
|
|
31
|
+
});
|
|
32
|
+
const external_farrow_schema_namespaceObject = require("farrow-schema");
|
|
33
|
+
const validator_namespaceObject = require("farrow-schema/validator");
|
|
34
|
+
const external_response_js_namespaceObject = require("./response.js");
|
|
35
|
+
const getErrorMessage = (error)=>{
|
|
36
|
+
let { message } = error;
|
|
37
|
+
if (Array.isArray(error.path) && error.path.length > 0) message = `path: ${JSON.stringify(error.path)}\n${message}`;
|
|
38
|
+
return message;
|
|
39
|
+
};
|
|
40
|
+
const HANDLER_WITH_SCHEMA = 'HANDLER_WITH_SCHEMA';
|
|
41
|
+
const isSchemaHandler = (input)=>input && input?.[HANDLER_WITH_SCHEMA] === true;
|
|
42
|
+
const isHandler = (input)=>input && 'function' == typeof input;
|
|
43
|
+
const baseMatch = (schema, handler)=>{
|
|
44
|
+
const validateApiInput = createRequestSchemaValidator(schema.request);
|
|
45
|
+
const validateApiOutput = (0, validator_namespaceObject.createSchemaValidator)((0, external_farrow_schema_namespaceObject.toSchemaCtor)(schema.response));
|
|
46
|
+
const handle = async (input)=>{
|
|
47
|
+
const inputResult = validateApiInput(input);
|
|
48
|
+
if (inputResult.isErr) return (0, external_response_js_namespaceObject.InputValidationError)(getErrorMessage(inputResult.value));
|
|
49
|
+
const output = await handler(input);
|
|
50
|
+
const outputResult = validateApiOutput(output);
|
|
51
|
+
if (outputResult.isErr) return (0, external_response_js_namespaceObject.OutputValidationError)(getErrorMessage(outputResult.value));
|
|
52
|
+
return (0, external_response_js_namespaceObject.HandleSuccess)(output);
|
|
53
|
+
};
|
|
54
|
+
return Object.assign(handle, {
|
|
55
|
+
schema,
|
|
56
|
+
[HANDLER_WITH_SCHEMA]: true
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
const match = baseMatch;
|
|
60
|
+
const createRequestSchemaValidator = (schema)=>{
|
|
61
|
+
const descriptors = {};
|
|
62
|
+
if (schema.params) descriptors.params = schema.params;
|
|
63
|
+
if (schema.query) descriptors.query = schema.query;
|
|
64
|
+
if (schema.data) descriptors.data = schema.data;
|
|
65
|
+
if (schema.headers) descriptors.headers = schema.headers;
|
|
66
|
+
if (schema.cookies) descriptors.cookies = schema.cookies;
|
|
67
|
+
const RequestStruct = (0, external_farrow_schema_namespaceObject.Struct)(descriptors);
|
|
68
|
+
return (0, validator_namespaceObject.createSchemaValidator)((0, external_farrow_schema_namespaceObject.NonStrict)(RequestStruct));
|
|
69
|
+
};
|
|
70
|
+
exports.baseMatch = __webpack_exports__.baseMatch;
|
|
71
|
+
exports.isHandler = __webpack_exports__.isHandler;
|
|
72
|
+
exports.isSchemaHandler = __webpack_exports__.isSchemaHandler;
|
|
73
|
+
exports.match = __webpack_exports__.match;
|
|
74
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
75
|
+
"baseMatch",
|
|
76
|
+
"isHandler",
|
|
77
|
+
"isSchemaHandler",
|
|
78
|
+
"match"
|
|
79
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
80
|
+
Object.defineProperty(exports, '__esModule', {
|
|
81
|
+
value: true
|
|
82
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.r = (exports1)=>{
|
|
5
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
+
value: 'Module'
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
var __webpack_exports__ = {};
|
|
14
|
+
__webpack_require__.r(__webpack_exports__);
|
|
15
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
+
Object.defineProperty(exports, '__esModule', {
|
|
17
|
+
value: true
|
|
18
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
HandleSuccess: ()=>HandleSuccess,
|
|
28
|
+
InputValidationError: ()=>InputValidationError,
|
|
29
|
+
OutputValidationError: ()=>OutputValidationError
|
|
30
|
+
});
|
|
31
|
+
const HandleSuccess = (output)=>({
|
|
32
|
+
type: 'HandleSuccess',
|
|
33
|
+
value: output
|
|
34
|
+
});
|
|
35
|
+
const InputValidationError = (message)=>({
|
|
36
|
+
type: 'InputValidationError',
|
|
37
|
+
message
|
|
38
|
+
});
|
|
39
|
+
const OutputValidationError = (message)=>({
|
|
40
|
+
type: 'OutputValidationError',
|
|
41
|
+
message
|
|
42
|
+
});
|
|
43
|
+
exports.HandleSuccess = __webpack_exports__.HandleSuccess;
|
|
44
|
+
exports.InputValidationError = __webpack_exports__.InputValidationError;
|
|
45
|
+
exports.OutputValidationError = __webpack_exports__.OutputValidationError;
|
|
46
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
47
|
+
"HandleSuccess",
|
|
48
|
+
"InputValidationError",
|
|
49
|
+
"OutputValidationError"
|
|
50
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
51
|
+
Object.defineProperty(exports, '__esModule', {
|
|
52
|
+
value: true
|
|
53
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.r = (exports1)=>{
|
|
5
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
+
value: 'Module'
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
var __webpack_exports__ = {};
|
|
14
|
+
__webpack_require__.r(__webpack_exports__);
|
|
15
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
+
Object.defineProperty(exports, '__esModule', {
|
|
17
|
+
value: true
|
|
18
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { NonStrict, Struct, toSchemaCtor } from "farrow-schema";
|
|
2
|
+
import { createSchemaValidator } from "farrow-schema/validator";
|
|
3
|
+
import { HandleSuccess, InputValidationError, OutputValidationError } from "./response.mjs";
|
|
4
|
+
const getErrorMessage = (error)=>{
|
|
5
|
+
let { message } = error;
|
|
6
|
+
if (Array.isArray(error.path) && error.path.length > 0) message = `path: ${JSON.stringify(error.path)}\n${message}`;
|
|
7
|
+
return message;
|
|
8
|
+
};
|
|
9
|
+
const HANDLER_WITH_SCHEMA = 'HANDLER_WITH_SCHEMA';
|
|
10
|
+
const isSchemaHandler = (input)=>input && input?.[HANDLER_WITH_SCHEMA] === true;
|
|
11
|
+
const isHandler = (input)=>input && 'function' == typeof input;
|
|
12
|
+
const baseMatch = (schema, handler)=>{
|
|
13
|
+
const validateApiInput = createRequestSchemaValidator(schema.request);
|
|
14
|
+
const validateApiOutput = createSchemaValidator(toSchemaCtor(schema.response));
|
|
15
|
+
const handle = async (input)=>{
|
|
16
|
+
const inputResult = validateApiInput(input);
|
|
17
|
+
if (inputResult.isErr) return InputValidationError(getErrorMessage(inputResult.value));
|
|
18
|
+
const output = await handler(input);
|
|
19
|
+
const outputResult = validateApiOutput(output);
|
|
20
|
+
if (outputResult.isErr) return OutputValidationError(getErrorMessage(outputResult.value));
|
|
21
|
+
return HandleSuccess(output);
|
|
22
|
+
};
|
|
23
|
+
return Object.assign(handle, {
|
|
24
|
+
schema,
|
|
25
|
+
[HANDLER_WITH_SCHEMA]: true
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const match = baseMatch;
|
|
29
|
+
const createRequestSchemaValidator = (schema)=>{
|
|
30
|
+
const descriptors = {};
|
|
31
|
+
if (schema.params) descriptors.params = schema.params;
|
|
32
|
+
if (schema.query) descriptors.query = schema.query;
|
|
33
|
+
if (schema.data) descriptors.data = schema.data;
|
|
34
|
+
if (schema.headers) descriptors.headers = schema.headers;
|
|
35
|
+
if (schema.cookies) descriptors.cookies = schema.cookies;
|
|
36
|
+
const RequestStruct = Struct(descriptors);
|
|
37
|
+
return createSchemaValidator(NonStrict(RequestStruct));
|
|
38
|
+
};
|
|
39
|
+
export { baseMatch, isHandler, isSchemaHandler, match };
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const HandleSuccess = (output)=>({
|
|
2
|
+
type: 'HandleSuccess',
|
|
3
|
+
value: output
|
|
4
|
+
});
|
|
5
|
+
const InputValidationError = (message)=>({
|
|
6
|
+
type: 'InputValidationError',
|
|
7
|
+
message
|
|
8
|
+
});
|
|
9
|
+
const OutputValidationError = (message)=>({
|
|
10
|
+
type: 'OutputValidationError',
|
|
11
|
+
message
|
|
12
|
+
});
|
|
13
|
+
export { HandleSuccess, InputValidationError, OutputValidationError };
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { NonStrict, Struct, toSchemaCtor } from "farrow-schema";
|
|
3
|
+
import { createSchemaValidator } from "farrow-schema/validator";
|
|
4
|
+
import { HandleSuccess, InputValidationError, OutputValidationError } from "./response.mjs";
|
|
5
|
+
const getErrorMessage = (error)=>{
|
|
6
|
+
let { message } = error;
|
|
7
|
+
if (Array.isArray(error.path) && error.path.length > 0) message = `path: ${JSON.stringify(error.path)}\n${message}`;
|
|
8
|
+
return message;
|
|
9
|
+
};
|
|
10
|
+
const HANDLER_WITH_SCHEMA = 'HANDLER_WITH_SCHEMA';
|
|
11
|
+
const isSchemaHandler = (input)=>input && input?.[HANDLER_WITH_SCHEMA] === true;
|
|
12
|
+
const isHandler = (input)=>input && 'function' == typeof input;
|
|
13
|
+
const baseMatch = (schema, handler)=>{
|
|
14
|
+
const validateApiInput = createRequestSchemaValidator(schema.request);
|
|
15
|
+
const validateApiOutput = createSchemaValidator(toSchemaCtor(schema.response));
|
|
16
|
+
const handle = async (input)=>{
|
|
17
|
+
const inputResult = validateApiInput(input);
|
|
18
|
+
if (inputResult.isErr) return InputValidationError(getErrorMessage(inputResult.value));
|
|
19
|
+
const output = await handler(input);
|
|
20
|
+
const outputResult = validateApiOutput(output);
|
|
21
|
+
if (outputResult.isErr) return OutputValidationError(getErrorMessage(outputResult.value));
|
|
22
|
+
return HandleSuccess(output);
|
|
23
|
+
};
|
|
24
|
+
return Object.assign(handle, {
|
|
25
|
+
schema,
|
|
26
|
+
[HANDLER_WITH_SCHEMA]: true
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const match = baseMatch;
|
|
30
|
+
const createRequestSchemaValidator = (schema)=>{
|
|
31
|
+
const descriptors = {};
|
|
32
|
+
if (schema.params) descriptors.params = schema.params;
|
|
33
|
+
if (schema.query) descriptors.query = schema.query;
|
|
34
|
+
if (schema.data) descriptors.data = schema.data;
|
|
35
|
+
if (schema.headers) descriptors.headers = schema.headers;
|
|
36
|
+
if (schema.cookies) descriptors.cookies = schema.cookies;
|
|
37
|
+
const RequestStruct = Struct(descriptors);
|
|
38
|
+
return createSchemaValidator(NonStrict(RequestStruct));
|
|
39
|
+
};
|
|
40
|
+
export { baseMatch, isHandler, isSchemaHandler, match };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "node:module";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const HandleSuccess = (output)=>({
|
|
3
|
+
type: 'HandleSuccess',
|
|
4
|
+
value: output
|
|
5
|
+
});
|
|
6
|
+
const InputValidationError = (message)=>({
|
|
7
|
+
type: 'InputValidationError',
|
|
8
|
+
message
|
|
9
|
+
});
|
|
10
|
+
const OutputValidationError = (message)=>({
|
|
11
|
+
type: 'OutputValidationError',
|
|
12
|
+
message
|
|
13
|
+
});
|
|
14
|
+
export { HandleSuccess, InputValidationError, OutputValidationError };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "node:module";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from 'farrow-schema';
|
|
2
|
+
export type { Handler, SchemaHandler } from './match';
|
|
3
|
+
export { isHandler, isSchemaHandler, match } from './match';
|
|
4
|
+
export type { InputType, RequestSchema, TypeOfRequestSchema } from './request';
|
|
5
|
+
export type { HandleResult, HandleSuccess, InputValidationError, OutputValidationError, } from './response';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { MaybeAsync } from 'farrow-pipeline';
|
|
2
|
+
import { type SchemaCtorInput } from 'farrow-schema';
|
|
3
|
+
import type { PureTypeOfRequestSchema, RequestSchema, TypeOfRequestSchema } from './request';
|
|
4
|
+
import { type HandleResult } from './response';
|
|
5
|
+
import type { TypeOfRouterRequestField } from './types';
|
|
6
|
+
export type NormalHandler = (...args: any[]) => any;
|
|
7
|
+
export type Handler<I, O> = (input: I) => MaybeAsync<O>;
|
|
8
|
+
export type Schema<Req extends RequestSchema, Res extends SchemaCtorInput> = {
|
|
9
|
+
request: Req;
|
|
10
|
+
response: Res;
|
|
11
|
+
description?: string;
|
|
12
|
+
deprecated?: string;
|
|
13
|
+
};
|
|
14
|
+
declare const HANDLER_WITH_SCHEMA = "HANDLER_WITH_SCHEMA";
|
|
15
|
+
export type BaseSchemaHandler<Req extends RequestSchema, Res extends SchemaCtorInput> = ((input: TypeOfRequestSchema<Req>) => Promise<HandleResult<TypeOfRouterRequestField<Res>>>) & {
|
|
16
|
+
schema: Schema<Req, Res>;
|
|
17
|
+
[HANDLER_WITH_SCHEMA]: true;
|
|
18
|
+
};
|
|
19
|
+
export type SchemaHandler<Req extends RequestSchema, Res extends SchemaCtorInput> = ((input: TypeOfRequestSchema<Req>) => Promise<TypeOfRouterRequestField<Res>>) & {
|
|
20
|
+
schema: Schema<Req, Res>;
|
|
21
|
+
[HANDLER_WITH_SCHEMA]: true;
|
|
22
|
+
};
|
|
23
|
+
export declare const isSchemaHandler: (input: any) => input is SchemaHandler<any, any>;
|
|
24
|
+
export declare const isHandler: (input: any) => input is Handler<any, any>;
|
|
25
|
+
export declare const baseMatch: <Req extends RequestSchema, Res extends SchemaCtorInput>(schema: Schema<Req, Res>, handler: Handler<TypeOfRequestSchema<Req>, TypeOfRouterRequestField<Res>>) => BaseSchemaHandler<Req, Res>;
|
|
26
|
+
export declare const match: <Req extends RequestSchema, Res extends SchemaCtorInput>(schema: Schema<Req, Res>, handler: Handler<PureTypeOfRequestSchema<Req>, TypeOfRouterRequestField<Res>>) => SchemaHandler<Req, Res>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { FieldDescriptor, FieldDescriptors, TypeOfFieldDescriptor, TypeOfFieldDescriptors } from 'farrow-schema';
|
|
2
|
+
import type { MarkReadOnlyDeep, RouterSchemaDescriptor } from './types';
|
|
3
|
+
export type RequestBaseSchema = {
|
|
4
|
+
params?: RouterSchemaDescriptor;
|
|
5
|
+
query?: RouterSchemaDescriptor;
|
|
6
|
+
headers?: RouterSchemaDescriptor;
|
|
7
|
+
cookies?: RouterSchemaDescriptor;
|
|
8
|
+
};
|
|
9
|
+
export type RequestDataSchema = {
|
|
10
|
+
data?: RouterSchemaDescriptor;
|
|
11
|
+
};
|
|
12
|
+
export type RequestBodyType = {
|
|
13
|
+
body?: string;
|
|
14
|
+
};
|
|
15
|
+
export type PureRequestFormDataType = {
|
|
16
|
+
formData?: Record<string, any>;
|
|
17
|
+
};
|
|
18
|
+
export type RequestFormDataType = {
|
|
19
|
+
formData?: FormData;
|
|
20
|
+
};
|
|
21
|
+
export type RequestFormUrlencodedType = {
|
|
22
|
+
formUrlencoded?: URLSearchParams | Record<string, string> | string;
|
|
23
|
+
};
|
|
24
|
+
export type PureRequestFormUrlencodedType = {
|
|
25
|
+
formUrlencoded?: Record<string, string>;
|
|
26
|
+
};
|
|
27
|
+
export type RequestExtraType = RequestBodyType & RequestFormDataType & RequestFormUrlencodedType;
|
|
28
|
+
export type PureRequestExtraType = RequestBodyType & PureRequestFormDataType & PureRequestFormUrlencodedType;
|
|
29
|
+
export type RequestSchema = RequestBaseSchema & RequestDataSchema;
|
|
30
|
+
export type TypeOfRequestField<T> = T extends string ? string : T extends FormData ? FormData : T extends FieldDescriptor ? TypeOfFieldDescriptor<T> : T extends FieldDescriptors ? TypeOfFieldDescriptors<T> : never;
|
|
31
|
+
export type TypeOfRequestDataSchema<T extends RequestDataSchema> = MarkReadOnlyDeep<T extends {
|
|
32
|
+
data: any;
|
|
33
|
+
} ? Pick<{
|
|
34
|
+
[key in keyof T]: TypeOfRequestField<T[key]>;
|
|
35
|
+
}, 'data'> : RequestExtraType>;
|
|
36
|
+
export type TypeOfRequestSchema<T extends RequestSchema> = MarkReadOnlyDeep<Omit<{
|
|
37
|
+
[key in keyof T]: TypeOfRequestField<T[key]>;
|
|
38
|
+
}, 'data'> & TypeOfRequestDataSchema<T>>;
|
|
39
|
+
export type PureTypeOfRequestDataSchema<T extends RequestDataSchema> = MarkReadOnlyDeep<T extends {
|
|
40
|
+
data: any;
|
|
41
|
+
} ? Pick<{
|
|
42
|
+
[key in keyof T]: TypeOfRequestField<T[key]>;
|
|
43
|
+
}, 'data'> : PureRequestExtraType>;
|
|
44
|
+
export type PureTypeOfRequestSchema<T extends RequestSchema> = MarkReadOnlyDeep<Omit<{
|
|
45
|
+
[key in keyof T]: TypeOfRequestField<T[key]>;
|
|
46
|
+
}, 'data'> & PureTypeOfRequestDataSchema<T>>;
|
|
47
|
+
export type InputType = TypeOfRequestSchema<RequestSchema>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type HandleSuccess<T> = {
|
|
2
|
+
type: 'HandleSuccess';
|
|
3
|
+
value: T;
|
|
4
|
+
};
|
|
5
|
+
export declare const HandleSuccess: <T>(output: T) => HandleSuccess<T>;
|
|
6
|
+
export type InputValidationError = {
|
|
7
|
+
type: 'InputValidationError';
|
|
8
|
+
message: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const InputValidationError: (message: string) => InputValidationError;
|
|
11
|
+
export type OutputValidationError = {
|
|
12
|
+
type: 'OutputValidationError';
|
|
13
|
+
message: string;
|
|
14
|
+
};
|
|
15
|
+
export declare const OutputValidationError: (message: string) => OutputValidationError;
|
|
16
|
+
export type HandleResult<T> = HandleSuccess<T> | InputValidationError | OutputValidationError;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type * as Schema from 'farrow-schema';
|
|
2
|
+
type Path = string | RegExp | Array<string | RegExp>;
|
|
3
|
+
export type TypeOfRouterRequestField<T> = T extends string | string[] ? string : T extends Path ? string : T extends Schema.FieldDescriptor ? Schema.TypeOfFieldDescriptor<T> : T extends Schema.FieldDescriptors ? Schema.TypeOfFieldDescriptors<T> : never;
|
|
4
|
+
export type RouterSchemaDescriptor = Schema.FieldDescriptors | (new () => Schema.ObjectType) | (new () => Schema.StructType);
|
|
5
|
+
export type MarkReadOnlyDeep<T> = T extends {} | any[] ? {
|
|
6
|
+
readonly [key in keyof T]: MarkReadOnlyDeep<T[key]>;
|
|
7
|
+
} : T;
|
|
8
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bleedingdev/modern-js-bff-runtime",
|
|
3
|
+
"description": "A Progressive React Framework for modern web development.",
|
|
4
|
+
"homepage": "https://github.com/BleedingDev/ultramodern.js#readme",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/BleedingDev/ultramodern.js/issues"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/BleedingDev/ultramodern.js.git",
|
|
11
|
+
"directory": "packages/server/bff-runtime"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"react",
|
|
16
|
+
"framework",
|
|
17
|
+
"modern",
|
|
18
|
+
"modern.js"
|
|
19
|
+
],
|
|
20
|
+
"version": "3.2.0-ultramodern.0",
|
|
21
|
+
"types": "./dist/types/index.d.ts",
|
|
22
|
+
"main": "./dist/cjs/index.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/types/index.d.ts",
|
|
26
|
+
"node": {
|
|
27
|
+
"import": "./dist/esm-node/index.mjs",
|
|
28
|
+
"require": "./dist/cjs/index.js"
|
|
29
|
+
},
|
|
30
|
+
"default": "./dist/cjs/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@swc/helpers": "^0.5.21",
|
|
35
|
+
"farrow-api": "^2.3.1",
|
|
36
|
+
"farrow-pipeline": "^2.3.0",
|
|
37
|
+
"farrow-schema": "^2.3.3"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@rslib/core": "0.21.5",
|
|
41
|
+
"@types/node": "^25.8.0",
|
|
42
|
+
"@typescript/native-preview": "7.0.0-dev.20260516.1"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"registry": "https://registry.npmjs.org/",
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"dev": "rslib build --watch",
|
|
50
|
+
"build": "rslib build",
|
|
51
|
+
"test": "rstest --passWithNoTests"
|
|
52
|
+
}
|
|
53
|
+
}
|
package/rslib.config.mts
ADDED