kb-server 0.0.1-beta.3 → 0.0.1-beta.5
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.
|
@@ -23,7 +23,7 @@ export interface APIErrorResponse {
|
|
|
23
23
|
RequestId: string;
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
-
export type AuthFunction = (action: string, req: express.Request) => Promise<Record<string, any> |
|
|
26
|
+
export type AuthFunction = (action: string, req: express.Request) => Promise<Record<string, any> | boolean> | boolean | Record<string, any>;
|
|
27
27
|
interface IPackAPIOptions {
|
|
28
28
|
authFn?: AuthFunction;
|
|
29
29
|
}
|
|
@@ -38,8 +38,10 @@ const packAPI = (apis, options) => {
|
|
|
38
38
|
if (!authResult) {
|
|
39
39
|
throw new create_errors_1.CommonErrors.FailOperation.NoPermission();
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
if (typeof authResult !== "boolean") {
|
|
42
|
+
// 把权限信息写入上下文
|
|
43
|
+
ctx.AuthInfo = authResult || {};
|
|
44
|
+
}
|
|
43
45
|
}
|
|
44
46
|
// API 处理
|
|
45
47
|
const execution = apiMap.get(Action);
|
|
@@ -6,7 +6,11 @@ export interface ServerContext {
|
|
|
6
6
|
AuthInfo?: Record<string, any>;
|
|
7
7
|
}
|
|
8
8
|
export type API<P, R> = (param: P) => Promise<R>;
|
|
9
|
-
export type APIExecution<P, R> = (
|
|
9
|
+
export type APIExecution<P, R> = (
|
|
10
|
+
/** 请求入参 */
|
|
11
|
+
params: P,
|
|
12
|
+
/** 请求上下文 */
|
|
13
|
+
ctx: ServerContext) => Promise<R>;
|
|
10
14
|
export type AnyAPIExecution = APIExecution<any, any>;
|
|
11
15
|
export type APIs = Record<string, AnyAPIExecution>;
|
|
12
16
|
export declare function createAPI<P, R>(ParamsClass: Class<P>, execution: APIExecution<P, R>): API<P, R>;
|
|
@@ -2,6 +2,11 @@ import { AuthFunction } from "./api-middleware";
|
|
|
2
2
|
import { APIs } from "./create-api";
|
|
3
3
|
export interface ICreateServerParams {
|
|
4
4
|
apis: APIs;
|
|
5
|
+
/**
|
|
6
|
+
* 鉴权函数
|
|
7
|
+
*
|
|
8
|
+
* 可以通过返回布尔值来定义权限,也可以直接返回数据对象,数据对象会被传递到上下文 ctx 中(AuthInfo)
|
|
9
|
+
*/
|
|
5
10
|
authFn?: AuthFunction;
|
|
6
11
|
}
|
|
7
12
|
export interface ICreateServerOptions {
|