node-cnb 1.0.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/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./index";
2
+ export * from "./client";
3
+ export * from "./types";
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
+ };