kb-server 0.0.1-beta.1 → 0.0.1-beta.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/README.md +1 -1
- package/dist/common/api-middleware.d.ts +13 -2
- package/dist/common/api-middleware.js +14 -2
- package/dist/common/create-api.d.ts +3 -2
- package/dist/common/create-errors.d.ts +3 -0
- package/dist/common/create-errors.js +1 -0
- package/dist/common/create-server.d.ts +4 -2
- package/dist/common/create-server.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import express from "express";
|
|
2
|
-
import {
|
|
2
|
+
import { APIs } from "./create-api";
|
|
3
|
+
/**
|
|
4
|
+
* 正确响应
|
|
5
|
+
*/
|
|
3
6
|
export interface APIResponse {
|
|
4
7
|
Response: {
|
|
5
8
|
Data: {
|
|
@@ -8,6 +11,9 @@ export interface APIResponse {
|
|
|
8
11
|
RequestId: string;
|
|
9
12
|
};
|
|
10
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* 错误响应
|
|
16
|
+
*/
|
|
11
17
|
export interface APIErrorResponse {
|
|
12
18
|
Response: {
|
|
13
19
|
Error: {
|
|
@@ -17,9 +23,14 @@ export interface APIErrorResponse {
|
|
|
17
23
|
RequestId: string;
|
|
18
24
|
};
|
|
19
25
|
}
|
|
26
|
+
export type AuthFunction = (action: string, req: express.Request) => Promise<Record<string, any> | false>;
|
|
27
|
+
interface IPackAPIOptions {
|
|
28
|
+
authFn?: AuthFunction;
|
|
29
|
+
}
|
|
20
30
|
/**
|
|
21
31
|
* API包装函数
|
|
22
32
|
* @param apis API列表
|
|
23
33
|
* @returns
|
|
24
34
|
*/
|
|
25
|
-
export declare const packAPI: (apis:
|
|
35
|
+
export declare const packAPI: (apis: APIs, options?: IPackAPIOptions) => (req: express.Request, res: express.Response) => Promise<express.Response<any, Record<string, any>>>;
|
|
36
|
+
export {};
|
|
@@ -8,7 +8,8 @@ const uuid_1 = require("uuid");
|
|
|
8
8
|
* @param apis API列表
|
|
9
9
|
* @returns
|
|
10
10
|
*/
|
|
11
|
-
const packAPI = (apis) => {
|
|
11
|
+
const packAPI = (apis, options) => {
|
|
12
|
+
const { authFn } = options || {};
|
|
12
13
|
return async (req, res) => {
|
|
13
14
|
// 生成API映射
|
|
14
15
|
const apiMap = new Map(Object.entries(apis).map(([action, execution]) => [action, execution]));
|
|
@@ -22,13 +23,24 @@ const packAPI = (apis) => {
|
|
|
22
23
|
RequestId: requestId,
|
|
23
24
|
};
|
|
24
25
|
try {
|
|
25
|
-
console.log(req.body);
|
|
26
26
|
// API 解析
|
|
27
27
|
const { Action, ...params } = req.body || {};
|
|
28
28
|
// 接口未定义
|
|
29
29
|
if (!Action) {
|
|
30
30
|
throw new create_errors_1.CommonErrors.InvalidParameter.EmptyAPIRequest();
|
|
31
31
|
}
|
|
32
|
+
// 处理鉴权函数
|
|
33
|
+
if (authFn !== null && authFn !== undefined) {
|
|
34
|
+
if (typeof authFn !== "function") {
|
|
35
|
+
throw new create_errors_1.CommonErrors.ResourceNotFound.AuthFunctionNotFound();
|
|
36
|
+
}
|
|
37
|
+
const authResult = await authFn(Action, req);
|
|
38
|
+
if (!authResult) {
|
|
39
|
+
throw new create_errors_1.CommonErrors.FailOperation.NoPermission();
|
|
40
|
+
}
|
|
41
|
+
// 把权限信息写入上下文
|
|
42
|
+
ctx.AuthInfo = authResult || {};
|
|
43
|
+
}
|
|
32
44
|
// API 处理
|
|
33
45
|
const execution = apiMap.get(Action);
|
|
34
46
|
if (typeof execution !== "function") {
|
|
@@ -2,12 +2,13 @@ import { Class } from "utility-types";
|
|
|
2
2
|
export interface ServerContext {
|
|
3
3
|
/** 被调用方的 RequestId */
|
|
4
4
|
RequestId: string;
|
|
5
|
-
/**
|
|
6
|
-
|
|
5
|
+
/** 权限信息 */
|
|
6
|
+
AuthInfo?: Record<string, any>;
|
|
7
7
|
}
|
|
8
8
|
export type API<P, R> = (param: P) => Promise<R>;
|
|
9
9
|
export type APIExecution<P, R> = (params: P, ctx: ServerContext) => Promise<R>;
|
|
10
10
|
export type AnyAPIExecution = APIExecution<any, any>;
|
|
11
|
+
export type APIs = Record<string, AnyAPIExecution>;
|
|
11
12
|
export declare function createAPI<P, R>(ParamsClass: Class<P>, execution: APIExecution<P, R>): API<P, R>;
|
|
12
13
|
type StubParam<T> = T extends (params: infer P) => any ? P : never;
|
|
13
14
|
export declare function implementAPI<A extends (params: any) => any>(_actionStub: A, ParamsClass: Class<StubParam<A>>, execution: APIExecution<StubParam<A>, ReturnType<A>>): API<StubParam<A>, ReturnType<A>>;
|
|
@@ -24,6 +24,7 @@ declare const CommonErrorsMap: {
|
|
|
24
24
|
};
|
|
25
25
|
readonly ResourceNotFound: {
|
|
26
26
|
readonly APINotFound: "不存在的API";
|
|
27
|
+
readonly AuthFunctionNotFound: "鉴权函数不存在";
|
|
27
28
|
};
|
|
28
29
|
readonly FailOperation: {
|
|
29
30
|
readonly NoPermission: "无权操作";
|
|
@@ -79,6 +80,7 @@ declare function createErrorsClasses<T extends MessageMap = MessageMap>(messageM
|
|
|
79
80
|
};
|
|
80
81
|
readonly ResourceNotFound: {
|
|
81
82
|
readonly APINotFound: "不存在的API";
|
|
83
|
+
readonly AuthFunctionNotFound: "鉴权函数不存在";
|
|
82
84
|
};
|
|
83
85
|
readonly FailOperation: {
|
|
84
86
|
readonly NoPermission: "无权操作";
|
|
@@ -103,6 +105,7 @@ export declare const CommonErrors: ErrorClasses<MergeMessageMap<{
|
|
|
103
105
|
};
|
|
104
106
|
readonly ResourceNotFound: {
|
|
105
107
|
readonly APINotFound: "不存在的API";
|
|
108
|
+
readonly AuthFunctionNotFound: "鉴权函数不存在";
|
|
106
109
|
};
|
|
107
110
|
readonly FailOperation: {
|
|
108
111
|
readonly NoPermission: "无权操作";
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AuthFunction } from "./api-middleware";
|
|
2
|
+
import { APIs } from "./create-api";
|
|
2
3
|
export interface ICreateServerParams {
|
|
3
|
-
apis:
|
|
4
|
+
apis: APIs;
|
|
5
|
+
authFn?: AuthFunction;
|
|
4
6
|
}
|
|
5
7
|
export interface ICreateServerOptions {
|
|
6
8
|
limit?: string | number | undefined;
|
|
@@ -30,14 +30,14 @@ const api_middleware_1 = require("./api-middleware");
|
|
|
30
30
|
* 创建 Server
|
|
31
31
|
*/
|
|
32
32
|
function createServer(params, options) {
|
|
33
|
-
const { apis } = params || {};
|
|
33
|
+
const { apis, authFn } = params || {};
|
|
34
34
|
const { limit = "10mb" } = options || {};
|
|
35
35
|
const app = (0, express_1.default)();
|
|
36
36
|
// POST 参数获取
|
|
37
37
|
app.use((0, express_1.urlencoded)({ extended: true, limit }));
|
|
38
38
|
app.use((0, express_1.json)({ limit }));
|
|
39
39
|
// 注入API
|
|
40
|
-
app.use((0, api_middleware_1.packAPI)(apis));
|
|
40
|
+
app.use((0, api_middleware_1.packAPI)(apis, { authFn }));
|
|
41
41
|
return app;
|
|
42
42
|
}
|
|
43
43
|
exports.createServer = createServer;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.implementAPI = exports.
|
|
3
|
+
exports.implementAPI = exports.createErrors = exports.createServer = void 0;
|
|
4
4
|
var create_server_1 = require("./common/create-server");
|
|
5
5
|
Object.defineProperty(exports, "createServer", { enumerable: true, get: function () { return create_server_1.createServer; } });
|
|
6
6
|
var create_errors_1 = require("./common/create-errors");
|
|
7
7
|
Object.defineProperty(exports, "createErrors", { enumerable: true, get: function () { return create_errors_1.createErrors; } });
|
|
8
|
-
Object.defineProperty(exports, "CommonErrors", { enumerable: true, get: function () { return create_errors_1.CommonErrors; } });
|
|
9
8
|
var create_api_1 = require("./common/create-api");
|
|
10
9
|
Object.defineProperty(exports, "implementAPI", { enumerable: true, get: function () { return create_api_1.implementAPI; } });
|