node-cnb 1.2.11 → 1.2.13

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/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "node-cnb",
3
- "version": "1.2.11",
3
+ "version": "1.2.13",
4
4
  "description": "node sdk for cnb open api",
5
5
  "main": "dist/index.js",
6
- "typings": "dist/index.d.ts",
6
+ "typings": "./src/index.d.ts",
7
7
  "scripts": {
8
8
  "test": "echo \"Error: no test specified\" && exit 1",
9
- "build": "node build.js && tsc && cp src/*.d.ts dist/ && cp src/*.json dist/",
9
+ "build": "node build.js && tsc && cp src/*.json dist/",
10
10
  "release": "standard-version && git push --follow-tags origin main"
11
11
  },
12
12
  "repository": {
package/src/index.ts ADDED
@@ -0,0 +1,94 @@
1
+ import axios from "axios";
2
+ import paths from "./paths.json";
3
+ import { Client } from "./client";
4
+
5
+ const callApi = (
6
+ baseUrl: string,
7
+ token: string,
8
+ methodPath: string,
9
+ params: any,
10
+ ): Promise<any> => {
11
+ console.log("call", methodPath);
12
+ const methodValues = (paths as any)[methodPath].filter((methodValue: any) => {
13
+ const requireParams = methodValue.parameters.filter(
14
+ (item: any) => item.required,
15
+ );
16
+ return requireParams.every(
17
+ (item: any) => (params as any)[item.name] !== undefined,
18
+ );
19
+ });
20
+
21
+ const methodValue =
22
+ methodValues.length === 1
23
+ ? methodValues[0]
24
+ : methodValues.filter((item: any) =>
25
+ item.parameters.some((item: any) => item.required),
26
+ )[0] ||
27
+ methodValues.filter((item: any) =>
28
+ item.parameters.every((item: any) => !item.required),
29
+ )[0];
30
+ if (!methodValue) {
31
+ throw new Error("未能找出对应api路径");
32
+ }
33
+
34
+ const { parameters, method } = methodValue;
35
+ let { path } = methodValue;
36
+ parameters
37
+ .filter((item: any) => item.in === "path")
38
+ .forEach((item: any) => {
39
+ path = path.replace(`{${item.name}}`, (params as any)[item.name]);
40
+ });
41
+
42
+ const queryParams: any = {};
43
+ parameters
44
+ .filter((item: any) => item.in === "query")
45
+ .forEach((item: any) => {
46
+ queryParams[item.name] = (params as any)?.[item.name];
47
+ });
48
+ const bodyParams: any = {};
49
+ parameters
50
+ .filter((item: any) => item.in === "body")
51
+ .forEach((item: any) => {
52
+ Object.assign(bodyParams, (params as any)?.[item.name] || {});
53
+ });
54
+ const apiPath = `${baseUrl}${path}`;
55
+ console.log("api call", apiPath, method);
56
+ return axios({
57
+ method,
58
+ url: apiPath,
59
+ headers: {
60
+ Authorization: `Bearer ${token}`,
61
+ accept: "application/json",
62
+ },
63
+ params: queryParams,
64
+ data: bodyParams,
65
+ }).then((res: any) => res.data) as Promise<any>;
66
+ };
67
+
68
+ export const getClient = (baseUrl: string, token: string): Client => {
69
+ function getProxy(path: string) {
70
+ const methodValue = (paths as any)[path];
71
+ if (methodValue) {
72
+ return (params: any) => callApi(baseUrl, token, path, params);
73
+ } else {
74
+ return new Proxy(
75
+ {},
76
+ {
77
+ get(target, prop) {
78
+ return getProxy(path + "." + String(prop));
79
+ },
80
+ },
81
+ );
82
+ }
83
+ }
84
+
85
+ return new Proxy(
86
+ {},
87
+ {
88
+ get: (target, prop: string) => {
89
+ console.log("get prop", prop);
90
+ return getProxy(String(prop));
91
+ },
92
+ },
93
+ ) as Client;
94
+ };
package/tsconfig.json CHANGED
@@ -11,6 +11,7 @@
11
11
  "outDir": "./dist"
12
12
  },
13
13
  "include": [
14
- "src/*"
14
+ "src/index.ts",
15
+ "src/paths.json"
15
16
  ],
16
17
  }
File without changes
File without changes
File without changes