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 CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  快速创建一个 Node 服务
4
4
 
5
- 快速创建API、标准错误码、Server
5
+ 快速创建API、标准错误码、Server、支持统一鉴权函数(API级别)
@@ -1,5 +1,8 @@
1
1
  import express from "express";
2
- import { AnyAPIExecution } from "./create-api";
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: Record<string, AnyAPIExecution>) => (req: express.Request, res: express.Response) => Promise<express.Response<any, Record<string, any>>>;
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
- /** 用户ID */
6
- UserId?: number;
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: "无权操作";
@@ -26,6 +26,7 @@ const CommonErrorsMap = {
26
26
  },
27
27
  ResourceNotFound: {
28
28
  APINotFound: "不存在的API",
29
+ AuthFunctionNotFound: "鉴权函数不存在",
29
30
  },
30
31
  FailOperation: {
31
32
  NoPermission: "无权操作",
@@ -1,6 +1,8 @@
1
- import { AnyAPIExecution } from "./create-api";
1
+ import { AuthFunction } from "./api-middleware";
2
+ import { APIs } from "./create-api";
2
3
  export interface ICreateServerParams {
3
- apis: Record<string, AnyAPIExecution>;
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
@@ -1,3 +1,3 @@
1
1
  export { createServer } from "./common/create-server";
2
- export { createErrors, CommonErrors } from "./common/create-errors";
2
+ export { createErrors } from "./common/create-errors";
3
3
  export { implementAPI } from "./common/create-api";
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.CommonErrors = exports.createErrors = exports.createServer = void 0;
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; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kb-server",
3
- "version": "0.0.1-beta.1",
3
+ "version": "0.0.1-beta.2",
4
4
  "description": "A fast server for Node.JS,made by express.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {