@gopowerteam/request 0.1.2 → 0.1.4
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/dist/adapters/index.cjs +175 -0
- package/dist/adapters/index.d.ts +24 -0
- package/dist/adapters/index.js +51 -0
- package/dist/chunk-QZESQRIV.js +109 -0
- package/dist/index.cjs +0 -58
- package/dist/index.d.ts +4 -74
- package/dist/index.js +4 -154
- package/dist/request-adapter.interface-f0b0f677.d.ts +48 -0
- package/package.json +18 -6
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
var __publicField = (obj, key, value) => {
|
|
27
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// src/adapters/index.ts
|
|
32
|
+
var adapters_exports = {};
|
|
33
|
+
__export(adapters_exports, {
|
|
34
|
+
AxiosAdapter: () => AxiosAdapter
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(adapters_exports);
|
|
37
|
+
|
|
38
|
+
// src/adapters/axios.adapter.ts
|
|
39
|
+
var import_axios = __toESM(require("axios"), 1);
|
|
40
|
+
|
|
41
|
+
// src/request-service.ts
|
|
42
|
+
var _RequestService = class {
|
|
43
|
+
static getInstance() {
|
|
44
|
+
if (this.instance) {
|
|
45
|
+
return this.instance;
|
|
46
|
+
}
|
|
47
|
+
return new _RequestService();
|
|
48
|
+
}
|
|
49
|
+
getRequestAdapter() {
|
|
50
|
+
if (!_RequestService.config.adapter) {
|
|
51
|
+
throw new Error("\u8BF7\u68C0\u67E5\u662F\u5426\u914D\u7F6E\u8BF7\u6C42Adatper");
|
|
52
|
+
}
|
|
53
|
+
return _RequestService.config.adapter;
|
|
54
|
+
}
|
|
55
|
+
execRequestPlugin(plugins = [], options) {
|
|
56
|
+
_RequestService.config.plugins.forEach(
|
|
57
|
+
(service) => service.before && service.before(options)
|
|
58
|
+
);
|
|
59
|
+
plugins.forEach((service) => service.before && service.before(options));
|
|
60
|
+
}
|
|
61
|
+
execResponsePlugin(leftcycle, plugins = [], options, response) {
|
|
62
|
+
plugins.forEach((plugin) => {
|
|
63
|
+
const leftcycleFn = plugin[leftcycle];
|
|
64
|
+
leftcycleFn && leftcycleFn(response, options);
|
|
65
|
+
});
|
|
66
|
+
_RequestService.config.plugins.forEach((plugin) => {
|
|
67
|
+
const leftcycleFn = plugin[leftcycle];
|
|
68
|
+
leftcycleFn && leftcycleFn(response, options);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
parseRequestPath(path, paramsPath) {
|
|
72
|
+
if (paramsPath) {
|
|
73
|
+
return Object.entries(paramsPath).reduce(
|
|
74
|
+
(r, [key, value]) => r.replace(`{${key}}`, value.toString()),
|
|
75
|
+
path
|
|
76
|
+
);
|
|
77
|
+
} else {
|
|
78
|
+
return path;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
startRequest(adapter, options) {
|
|
82
|
+
return adapter.request({
|
|
83
|
+
baseURL: _RequestService.config.gateway,
|
|
84
|
+
pathURL: this.parseRequestPath(options.path, options.paramsPath),
|
|
85
|
+
method: options.method,
|
|
86
|
+
headers: options.headers || {},
|
|
87
|
+
paramsQuery: options.paramsQuery,
|
|
88
|
+
paramsBody: options.paramsBody
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
execInterceptors(response, hasException = false) {
|
|
92
|
+
var _a;
|
|
93
|
+
const interceptors = (_a = _RequestService.config) == null ? void 0 : _a.interceptors;
|
|
94
|
+
if (!(interceptors == null ? void 0 : interceptors.status) || !(interceptors == null ? void 0 : interceptors.error) || !(interceptors == null ? void 0 : interceptors.success) || !(interceptors == null ? void 0 : interceptors.exception)) {
|
|
95
|
+
throw new Error("\u8BF7\u68C0\u67E5\u62E6\u622A\u5668\u914D\u7F6E");
|
|
96
|
+
}
|
|
97
|
+
const status = interceptors.status.exec(response) && !hasException;
|
|
98
|
+
if (hasException) {
|
|
99
|
+
interceptors.exception.exec(response);
|
|
100
|
+
}
|
|
101
|
+
if (status) {
|
|
102
|
+
return Promise.resolve(interceptors.success.exec(response));
|
|
103
|
+
} else {
|
|
104
|
+
return Promise.reject(interceptors.error.exec(response));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async send(options, plugins = []) {
|
|
108
|
+
if (!_RequestService.config) {
|
|
109
|
+
throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
|
|
110
|
+
}
|
|
111
|
+
let hasException = false;
|
|
112
|
+
const adapter = this.getRequestAdapter();
|
|
113
|
+
this.execRequestPlugin(plugins, options);
|
|
114
|
+
const response = await this.startRequest(adapter, options).catch((...response2) => {
|
|
115
|
+
hasException = true;
|
|
116
|
+
return response2;
|
|
117
|
+
}).then((response2) => adapter.transformResponse(response2));
|
|
118
|
+
if (!hasException) {
|
|
119
|
+
this.execResponsePlugin("after" /* after */, plugins, options, response);
|
|
120
|
+
} else {
|
|
121
|
+
this.execResponsePlugin("catch" /* catch */, plugins, options, response);
|
|
122
|
+
}
|
|
123
|
+
return this.execInterceptors(response, hasException);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
var RequestService = _RequestService;
|
|
127
|
+
__publicField(RequestService, "config");
|
|
128
|
+
__publicField(RequestService, "instance");
|
|
129
|
+
|
|
130
|
+
// src/adapters/axios.adapter.ts
|
|
131
|
+
var _AxiosAdapter = class {
|
|
132
|
+
getAxiosInstance() {
|
|
133
|
+
if (!_AxiosAdapter.axiosInstance) {
|
|
134
|
+
_AxiosAdapter.axiosInstance = import_axios.default.create({
|
|
135
|
+
timeout: RequestService.config.timeout,
|
|
136
|
+
headers: {
|
|
137
|
+
"Content-Type": "application/json"
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return _AxiosAdapter.axiosInstance;
|
|
142
|
+
}
|
|
143
|
+
request({
|
|
144
|
+
baseURL,
|
|
145
|
+
pathURL,
|
|
146
|
+
headers,
|
|
147
|
+
method,
|
|
148
|
+
paramsQuery,
|
|
149
|
+
paramsBody
|
|
150
|
+
}) {
|
|
151
|
+
const axiosInstance = this.getAxiosInstance();
|
|
152
|
+
return axiosInstance.request({
|
|
153
|
+
method,
|
|
154
|
+
baseURL,
|
|
155
|
+
headers,
|
|
156
|
+
params: paramsQuery,
|
|
157
|
+
data: paramsBody,
|
|
158
|
+
url: pathURL
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
transformResponse(response) {
|
|
162
|
+
return {
|
|
163
|
+
data: response.data,
|
|
164
|
+
statusText: response.statusText,
|
|
165
|
+
status: response.status,
|
|
166
|
+
headers: response.headers
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var AxiosAdapter = _AxiosAdapter;
|
|
171
|
+
__publicField(AxiosAdapter, "axiosInstance");
|
|
172
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
173
|
+
0 && (module.exports = {
|
|
174
|
+
AxiosAdapter
|
|
175
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as axios from 'axios';
|
|
2
|
+
import { a as RequestAdapter, b as RequestAdapterOptions, A as AdapterResponse } from '../request-adapter.interface-f0b0f677.js';
|
|
3
|
+
|
|
4
|
+
declare class AxiosAdapter implements RequestAdapter {
|
|
5
|
+
private static axiosInstance;
|
|
6
|
+
/**
|
|
7
|
+
* 获取Axios实例
|
|
8
|
+
*/
|
|
9
|
+
private getAxiosInstance;
|
|
10
|
+
/**
|
|
11
|
+
* 发送请求
|
|
12
|
+
* @param options 请求参数
|
|
13
|
+
* @returns AxiosResponse
|
|
14
|
+
*/
|
|
15
|
+
request({ baseURL, pathURL, headers, method, paramsQuery, paramsBody }: RequestAdapterOptions): Promise<axios.AxiosResponse<any, any>>;
|
|
16
|
+
/**
|
|
17
|
+
* 转换Response
|
|
18
|
+
* @param response
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
transformResponse(response: any): AdapterResponse;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { AxiosAdapter };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RequestService,
|
|
3
|
+
__publicField
|
|
4
|
+
} from "../chunk-QZESQRIV.js";
|
|
5
|
+
|
|
6
|
+
// src/adapters/axios.adapter.ts
|
|
7
|
+
import axios from "axios";
|
|
8
|
+
var _AxiosAdapter = class {
|
|
9
|
+
getAxiosInstance() {
|
|
10
|
+
if (!_AxiosAdapter.axiosInstance) {
|
|
11
|
+
_AxiosAdapter.axiosInstance = axios.create({
|
|
12
|
+
timeout: RequestService.config.timeout,
|
|
13
|
+
headers: {
|
|
14
|
+
"Content-Type": "application/json"
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return _AxiosAdapter.axiosInstance;
|
|
19
|
+
}
|
|
20
|
+
request({
|
|
21
|
+
baseURL,
|
|
22
|
+
pathURL,
|
|
23
|
+
headers,
|
|
24
|
+
method,
|
|
25
|
+
paramsQuery,
|
|
26
|
+
paramsBody
|
|
27
|
+
}) {
|
|
28
|
+
const axiosInstance = this.getAxiosInstance();
|
|
29
|
+
return axiosInstance.request({
|
|
30
|
+
method,
|
|
31
|
+
baseURL,
|
|
32
|
+
headers,
|
|
33
|
+
params: paramsQuery,
|
|
34
|
+
data: paramsBody,
|
|
35
|
+
url: pathURL
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
transformResponse(response) {
|
|
39
|
+
return {
|
|
40
|
+
data: response.data,
|
|
41
|
+
statusText: response.statusText,
|
|
42
|
+
status: response.status,
|
|
43
|
+
headers: response.headers
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var AxiosAdapter = _AxiosAdapter;
|
|
48
|
+
__publicField(AxiosAdapter, "axiosInstance");
|
|
49
|
+
export {
|
|
50
|
+
AxiosAdapter
|
|
51
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// src/interfaces/request-plugin.interface.ts
|
|
9
|
+
var PluginLifecycle = /* @__PURE__ */ ((PluginLifecycle2) => {
|
|
10
|
+
PluginLifecycle2["before"] = "before";
|
|
11
|
+
PluginLifecycle2["after"] = "after";
|
|
12
|
+
PluginLifecycle2["catch"] = "catch";
|
|
13
|
+
return PluginLifecycle2;
|
|
14
|
+
})(PluginLifecycle || {});
|
|
15
|
+
|
|
16
|
+
// src/request-service.ts
|
|
17
|
+
var _RequestService = class {
|
|
18
|
+
static getInstance() {
|
|
19
|
+
if (this.instance) {
|
|
20
|
+
return this.instance;
|
|
21
|
+
}
|
|
22
|
+
return new _RequestService();
|
|
23
|
+
}
|
|
24
|
+
getRequestAdapter() {
|
|
25
|
+
if (!_RequestService.config.adapter) {
|
|
26
|
+
throw new Error("\u8BF7\u68C0\u67E5\u662F\u5426\u914D\u7F6E\u8BF7\u6C42Adatper");
|
|
27
|
+
}
|
|
28
|
+
return _RequestService.config.adapter;
|
|
29
|
+
}
|
|
30
|
+
execRequestPlugin(plugins = [], options) {
|
|
31
|
+
_RequestService.config.plugins.forEach(
|
|
32
|
+
(service) => service.before && service.before(options)
|
|
33
|
+
);
|
|
34
|
+
plugins.forEach((service) => service.before && service.before(options));
|
|
35
|
+
}
|
|
36
|
+
execResponsePlugin(leftcycle, plugins = [], options, response) {
|
|
37
|
+
plugins.forEach((plugin) => {
|
|
38
|
+
const leftcycleFn = plugin[leftcycle];
|
|
39
|
+
leftcycleFn && leftcycleFn(response, options);
|
|
40
|
+
});
|
|
41
|
+
_RequestService.config.plugins.forEach((plugin) => {
|
|
42
|
+
const leftcycleFn = plugin[leftcycle];
|
|
43
|
+
leftcycleFn && leftcycleFn(response, options);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
parseRequestPath(path, paramsPath) {
|
|
47
|
+
if (paramsPath) {
|
|
48
|
+
return Object.entries(paramsPath).reduce(
|
|
49
|
+
(r, [key, value]) => r.replace(`{${key}}`, value.toString()),
|
|
50
|
+
path
|
|
51
|
+
);
|
|
52
|
+
} else {
|
|
53
|
+
return path;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
startRequest(adapter, options) {
|
|
57
|
+
return adapter.request({
|
|
58
|
+
baseURL: _RequestService.config.gateway,
|
|
59
|
+
pathURL: this.parseRequestPath(options.path, options.paramsPath),
|
|
60
|
+
method: options.method,
|
|
61
|
+
headers: options.headers || {},
|
|
62
|
+
paramsQuery: options.paramsQuery,
|
|
63
|
+
paramsBody: options.paramsBody
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
execInterceptors(response, hasException = false) {
|
|
67
|
+
var _a;
|
|
68
|
+
const interceptors = (_a = _RequestService.config) == null ? void 0 : _a.interceptors;
|
|
69
|
+
if (!(interceptors == null ? void 0 : interceptors.status) || !(interceptors == null ? void 0 : interceptors.error) || !(interceptors == null ? void 0 : interceptors.success) || !(interceptors == null ? void 0 : interceptors.exception)) {
|
|
70
|
+
throw new Error("\u8BF7\u68C0\u67E5\u62E6\u622A\u5668\u914D\u7F6E");
|
|
71
|
+
}
|
|
72
|
+
const status = interceptors.status.exec(response) && !hasException;
|
|
73
|
+
if (hasException) {
|
|
74
|
+
interceptors.exception.exec(response);
|
|
75
|
+
}
|
|
76
|
+
if (status) {
|
|
77
|
+
return Promise.resolve(interceptors.success.exec(response));
|
|
78
|
+
} else {
|
|
79
|
+
return Promise.reject(interceptors.error.exec(response));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async send(options, plugins = []) {
|
|
83
|
+
if (!_RequestService.config) {
|
|
84
|
+
throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
|
|
85
|
+
}
|
|
86
|
+
let hasException = false;
|
|
87
|
+
const adapter = this.getRequestAdapter();
|
|
88
|
+
this.execRequestPlugin(plugins, options);
|
|
89
|
+
const response = await this.startRequest(adapter, options).catch((...response2) => {
|
|
90
|
+
hasException = true;
|
|
91
|
+
return response2;
|
|
92
|
+
}).then((response2) => adapter.transformResponse(response2));
|
|
93
|
+
if (!hasException) {
|
|
94
|
+
this.execResponsePlugin("after" /* after */, plugins, options, response);
|
|
95
|
+
} else {
|
|
96
|
+
this.execResponsePlugin("catch" /* catch */, plugins, options, response);
|
|
97
|
+
}
|
|
98
|
+
return this.execInterceptors(response, hasException);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
var RequestService = _RequestService;
|
|
102
|
+
__publicField(RequestService, "config");
|
|
103
|
+
__publicField(RequestService, "instance");
|
|
104
|
+
|
|
105
|
+
export {
|
|
106
|
+
__publicField,
|
|
107
|
+
PluginLifecycle,
|
|
108
|
+
RequestService
|
|
109
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
7
|
var __export = (target, all) => {
|
|
@@ -18,10 +16,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
16
|
}
|
|
19
17
|
return to;
|
|
20
18
|
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
-
mod
|
|
24
|
-
));
|
|
25
19
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
20
|
var __publicField = (obj, key, value) => {
|
|
27
21
|
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
@@ -31,7 +25,6 @@ var __publicField = (obj, key, value) => {
|
|
|
31
25
|
// src/index.ts
|
|
32
26
|
var src_exports = {};
|
|
33
27
|
__export(src_exports, {
|
|
34
|
-
AxiosAdapter: () => AxiosAdapter,
|
|
35
28
|
PluginLifecycle: () => PluginLifecycle,
|
|
36
29
|
RequestMethod: () => RequestMethod,
|
|
37
30
|
RequestService: () => RequestService,
|
|
@@ -39,50 +32,6 @@ __export(src_exports, {
|
|
|
39
32
|
});
|
|
40
33
|
module.exports = __toCommonJS(src_exports);
|
|
41
34
|
|
|
42
|
-
// src/adapters/axios.adapter.ts
|
|
43
|
-
var import_axios = __toESM(require("axios"), 1);
|
|
44
|
-
var _AxiosAdapter = class {
|
|
45
|
-
getAxiosInstance() {
|
|
46
|
-
if (!_AxiosAdapter.axiosInstance) {
|
|
47
|
-
_AxiosAdapter.axiosInstance = import_axios.default.create({
|
|
48
|
-
timeout: RequestService.config.timeout,
|
|
49
|
-
headers: {
|
|
50
|
-
"Content-Type": "application/json"
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
return _AxiosAdapter.axiosInstance;
|
|
55
|
-
}
|
|
56
|
-
request({
|
|
57
|
-
baseURL,
|
|
58
|
-
pathURL,
|
|
59
|
-
headers,
|
|
60
|
-
method,
|
|
61
|
-
paramsQuery,
|
|
62
|
-
paramsBody
|
|
63
|
-
}) {
|
|
64
|
-
const axiosInstance = this.getAxiosInstance();
|
|
65
|
-
return axiosInstance.request({
|
|
66
|
-
method,
|
|
67
|
-
baseURL,
|
|
68
|
-
headers,
|
|
69
|
-
params: paramsQuery,
|
|
70
|
-
data: paramsBody,
|
|
71
|
-
url: pathURL
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
transformResponse(response) {
|
|
75
|
-
return {
|
|
76
|
-
data: response.data,
|
|
77
|
-
statusText: response.statusText,
|
|
78
|
-
status: response.status,
|
|
79
|
-
headers: response.headers
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
var AxiosAdapter = _AxiosAdapter;
|
|
84
|
-
__publicField(AxiosAdapter, "axiosInstance");
|
|
85
|
-
|
|
86
35
|
// src/interfaces/request-plugin.interface.ts
|
|
87
36
|
var PluginLifecycle = /* @__PURE__ */ ((PluginLifecycle2) => {
|
|
88
37
|
PluginLifecycle2["before"] = "before";
|
|
@@ -100,12 +49,6 @@ var _RequestService = class {
|
|
|
100
49
|
return new _RequestService();
|
|
101
50
|
}
|
|
102
51
|
getRequestAdapter() {
|
|
103
|
-
if (typeof _RequestService.config.adapter === "string") {
|
|
104
|
-
switch (_RequestService.config.adapter) {
|
|
105
|
-
case "axios":
|
|
106
|
-
return new AxiosAdapter();
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
52
|
if (!_RequestService.config.adapter) {
|
|
110
53
|
throw new Error("\u8BF7\u68C0\u67E5\u662F\u5426\u914D\u7F6E\u8BF7\u6C42Adatper");
|
|
111
54
|
}
|
|
@@ -204,7 +147,6 @@ var RequestMethod = /* @__PURE__ */ ((RequestMethod2) => {
|
|
|
204
147
|
})(RequestMethod || {});
|
|
205
148
|
// Annotate the CommonJS export names for ESM import in node:
|
|
206
149
|
0 && (module.exports = {
|
|
207
|
-
AxiosAdapter,
|
|
208
150
|
PluginLifecycle,
|
|
209
151
|
RequestMethod,
|
|
210
152
|
RequestService,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,52 +1,6 @@
|
|
|
1
|
+
import { R as RequestSendOptions, A as AdapterResponse, a as RequestAdapter } from './request-adapter.interface-f0b0f677.js';
|
|
2
|
+
export { A as AdapterResponse, a as RequestAdapter, b as RequestAdapterOptions, c as RequestMethod, R as RequestSendOptions } from './request-adapter.interface-f0b0f677.js';
|
|
1
3
|
import { IStringifyOptions } from 'qs';
|
|
2
|
-
import * as axios from 'axios';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 请求方法类型
|
|
6
|
-
*/
|
|
7
|
-
declare enum RequestMethod {
|
|
8
|
-
Get = "GET",
|
|
9
|
-
Post = "POST",
|
|
10
|
-
Put = "PUT",
|
|
11
|
-
Delete = "DELETE",
|
|
12
|
-
Options = "OPTIONS",
|
|
13
|
-
Head = "HEAD",
|
|
14
|
-
Patch = "PATCH"
|
|
15
|
-
}
|
|
16
|
-
interface RequestSendOptions {
|
|
17
|
-
path: string;
|
|
18
|
-
method: RequestMethod | string;
|
|
19
|
-
headers?: Record<string, string>;
|
|
20
|
-
paramsPath?: Record<string, string | number>;
|
|
21
|
-
paramsQuery?: Record<string, any>;
|
|
22
|
-
paramsBody?: any;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
interface RequestAdapter {
|
|
26
|
-
/**
|
|
27
|
-
* 发送请求
|
|
28
|
-
*/
|
|
29
|
-
request(options: RequestAdapterOptions): Promise<any>;
|
|
30
|
-
/**
|
|
31
|
-
* 转换Response
|
|
32
|
-
* @param any
|
|
33
|
-
*/
|
|
34
|
-
transformResponse(response: any): AdapterResponse;
|
|
35
|
-
}
|
|
36
|
-
interface RequestAdapterOptions {
|
|
37
|
-
baseURL: string;
|
|
38
|
-
pathURL: string;
|
|
39
|
-
headers: Record<string, string>;
|
|
40
|
-
method: RequestMethod | string;
|
|
41
|
-
paramsQuery?: Record<string, any>;
|
|
42
|
-
paramsBody?: any;
|
|
43
|
-
}
|
|
44
|
-
interface AdapterResponse {
|
|
45
|
-
data: Record<string, any>;
|
|
46
|
-
status: number;
|
|
47
|
-
statusText: string;
|
|
48
|
-
headers: Record<string, string>;
|
|
49
|
-
}
|
|
50
4
|
|
|
51
5
|
/**
|
|
52
6
|
* 请求插件
|
|
@@ -68,15 +22,11 @@ interface ResponseInterceptor {
|
|
|
68
22
|
exec(response: AdapterResponse): any;
|
|
69
23
|
}
|
|
70
24
|
|
|
71
|
-
/**
|
|
72
|
-
* 内部支持适配器
|
|
73
|
-
*/
|
|
74
|
-
declare type InternalAdapter = 'axios';
|
|
75
25
|
interface RequestSetupConfig {
|
|
76
26
|
gateway: string;
|
|
77
27
|
timeout?: number;
|
|
78
28
|
qs?: IStringifyOptions;
|
|
79
|
-
adapter?:
|
|
29
|
+
adapter?: RequestAdapter;
|
|
80
30
|
interceptors: {
|
|
81
31
|
status: ResponseInterceptor;
|
|
82
32
|
success: ResponseInterceptor;
|
|
@@ -138,24 +88,4 @@ declare class RequestService {
|
|
|
138
88
|
|
|
139
89
|
declare function setup(config: RequestSetupConfig): void;
|
|
140
90
|
|
|
141
|
-
|
|
142
|
-
private static axiosInstance;
|
|
143
|
-
/**
|
|
144
|
-
* 获取Axios实例
|
|
145
|
-
*/
|
|
146
|
-
private getAxiosInstance;
|
|
147
|
-
/**
|
|
148
|
-
* 发送请求
|
|
149
|
-
* @param options 请求参数
|
|
150
|
-
* @returns AxiosResponse
|
|
151
|
-
*/
|
|
152
|
-
request({ baseURL, pathURL, headers, method, paramsQuery, paramsBody }: RequestAdapterOptions): Promise<axios.AxiosResponse<any, any>>;
|
|
153
|
-
/**
|
|
154
|
-
* 转换Response
|
|
155
|
-
* @param response
|
|
156
|
-
* @returns
|
|
157
|
-
*/
|
|
158
|
-
transformResponse(response: any): AdapterResponse;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export { AdapterResponse, AxiosAdapter, PluginLifecycle, RequestAdapter, RequestAdapterOptions, RequestLifecycle, RequestMethod, RequestPlugin, RequestSendOptions, RequestService, RequestSetupConfig, ResponseInterceptor, ResponseLifecycle, setup };
|
|
91
|
+
export { PluginLifecycle, RequestLifecycle, RequestPlugin, RequestService, RequestSetupConfig, ResponseInterceptor, ResponseLifecycle, setup };
|
package/dist/index.js
CHANGED
|
@@ -1,156 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
// src/adapters/axios.adapter.ts
|
|
9
|
-
import axios from "axios";
|
|
10
|
-
var _AxiosAdapter = class {
|
|
11
|
-
getAxiosInstance() {
|
|
12
|
-
if (!_AxiosAdapter.axiosInstance) {
|
|
13
|
-
_AxiosAdapter.axiosInstance = axios.create({
|
|
14
|
-
timeout: RequestService.config.timeout,
|
|
15
|
-
headers: {
|
|
16
|
-
"Content-Type": "application/json"
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
return _AxiosAdapter.axiosInstance;
|
|
21
|
-
}
|
|
22
|
-
request({
|
|
23
|
-
baseURL,
|
|
24
|
-
pathURL,
|
|
25
|
-
headers,
|
|
26
|
-
method,
|
|
27
|
-
paramsQuery,
|
|
28
|
-
paramsBody
|
|
29
|
-
}) {
|
|
30
|
-
const axiosInstance = this.getAxiosInstance();
|
|
31
|
-
return axiosInstance.request({
|
|
32
|
-
method,
|
|
33
|
-
baseURL,
|
|
34
|
-
headers,
|
|
35
|
-
params: paramsQuery,
|
|
36
|
-
data: paramsBody,
|
|
37
|
-
url: pathURL
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
transformResponse(response) {
|
|
41
|
-
return {
|
|
42
|
-
data: response.data,
|
|
43
|
-
statusText: response.statusText,
|
|
44
|
-
status: response.status,
|
|
45
|
-
headers: response.headers
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var AxiosAdapter = _AxiosAdapter;
|
|
50
|
-
__publicField(AxiosAdapter, "axiosInstance");
|
|
51
|
-
|
|
52
|
-
// src/interfaces/request-plugin.interface.ts
|
|
53
|
-
var PluginLifecycle = /* @__PURE__ */ ((PluginLifecycle2) => {
|
|
54
|
-
PluginLifecycle2["before"] = "before";
|
|
55
|
-
PluginLifecycle2["after"] = "after";
|
|
56
|
-
PluginLifecycle2["catch"] = "catch";
|
|
57
|
-
return PluginLifecycle2;
|
|
58
|
-
})(PluginLifecycle || {});
|
|
59
|
-
|
|
60
|
-
// src/request-service.ts
|
|
61
|
-
var _RequestService = class {
|
|
62
|
-
static getInstance() {
|
|
63
|
-
if (this.instance) {
|
|
64
|
-
return this.instance;
|
|
65
|
-
}
|
|
66
|
-
return new _RequestService();
|
|
67
|
-
}
|
|
68
|
-
getRequestAdapter() {
|
|
69
|
-
if (typeof _RequestService.config.adapter === "string") {
|
|
70
|
-
switch (_RequestService.config.adapter) {
|
|
71
|
-
case "axios":
|
|
72
|
-
return new AxiosAdapter();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
if (!_RequestService.config.adapter) {
|
|
76
|
-
throw new Error("\u8BF7\u68C0\u67E5\u662F\u5426\u914D\u7F6E\u8BF7\u6C42Adatper");
|
|
77
|
-
}
|
|
78
|
-
return _RequestService.config.adapter;
|
|
79
|
-
}
|
|
80
|
-
execRequestPlugin(plugins = [], options) {
|
|
81
|
-
_RequestService.config.plugins.forEach(
|
|
82
|
-
(service) => service.before && service.before(options)
|
|
83
|
-
);
|
|
84
|
-
plugins.forEach((service) => service.before && service.before(options));
|
|
85
|
-
}
|
|
86
|
-
execResponsePlugin(leftcycle, plugins = [], options, response) {
|
|
87
|
-
plugins.forEach((plugin) => {
|
|
88
|
-
const leftcycleFn = plugin[leftcycle];
|
|
89
|
-
leftcycleFn && leftcycleFn(response, options);
|
|
90
|
-
});
|
|
91
|
-
_RequestService.config.plugins.forEach((plugin) => {
|
|
92
|
-
const leftcycleFn = plugin[leftcycle];
|
|
93
|
-
leftcycleFn && leftcycleFn(response, options);
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
parseRequestPath(path, paramsPath) {
|
|
97
|
-
if (paramsPath) {
|
|
98
|
-
return Object.entries(paramsPath).reduce(
|
|
99
|
-
(r, [key, value]) => r.replace(`{${key}}`, value.toString()),
|
|
100
|
-
path
|
|
101
|
-
);
|
|
102
|
-
} else {
|
|
103
|
-
return path;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
startRequest(adapter, options) {
|
|
107
|
-
return adapter.request({
|
|
108
|
-
baseURL: _RequestService.config.gateway,
|
|
109
|
-
pathURL: this.parseRequestPath(options.path, options.paramsPath),
|
|
110
|
-
method: options.method,
|
|
111
|
-
headers: options.headers || {},
|
|
112
|
-
paramsQuery: options.paramsQuery,
|
|
113
|
-
paramsBody: options.paramsBody
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
execInterceptors(response, hasException = false) {
|
|
117
|
-
var _a;
|
|
118
|
-
const interceptors = (_a = _RequestService.config) == null ? void 0 : _a.interceptors;
|
|
119
|
-
if (!(interceptors == null ? void 0 : interceptors.status) || !(interceptors == null ? void 0 : interceptors.error) || !(interceptors == null ? void 0 : interceptors.success) || !(interceptors == null ? void 0 : interceptors.exception)) {
|
|
120
|
-
throw new Error("\u8BF7\u68C0\u67E5\u62E6\u622A\u5668\u914D\u7F6E");
|
|
121
|
-
}
|
|
122
|
-
const status = interceptors.status.exec(response) && !hasException;
|
|
123
|
-
if (hasException) {
|
|
124
|
-
interceptors.exception.exec(response);
|
|
125
|
-
}
|
|
126
|
-
if (status) {
|
|
127
|
-
return Promise.resolve(interceptors.success.exec(response));
|
|
128
|
-
} else {
|
|
129
|
-
return Promise.reject(interceptors.error.exec(response));
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
async send(options, plugins = []) {
|
|
133
|
-
if (!_RequestService.config) {
|
|
134
|
-
throw new Error("\u8BF7\u68C0\u67E5\u8BF7\u6C42\u914D\u7F6E\u662F\u5426\u5B8C\u6210");
|
|
135
|
-
}
|
|
136
|
-
let hasException = false;
|
|
137
|
-
const adapter = this.getRequestAdapter();
|
|
138
|
-
this.execRequestPlugin(plugins, options);
|
|
139
|
-
const response = await this.startRequest(adapter, options).catch((...response2) => {
|
|
140
|
-
hasException = true;
|
|
141
|
-
return response2;
|
|
142
|
-
}).then((response2) => adapter.transformResponse(response2));
|
|
143
|
-
if (!hasException) {
|
|
144
|
-
this.execResponsePlugin("after" /* after */, plugins, options, response);
|
|
145
|
-
} else {
|
|
146
|
-
this.execResponsePlugin("catch" /* catch */, plugins, options, response);
|
|
147
|
-
}
|
|
148
|
-
return this.execInterceptors(response, hasException);
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
var RequestService = _RequestService;
|
|
152
|
-
__publicField(RequestService, "config");
|
|
153
|
-
__publicField(RequestService, "instance");
|
|
1
|
+
import {
|
|
2
|
+
PluginLifecycle,
|
|
3
|
+
RequestService
|
|
4
|
+
} from "./chunk-QZESQRIV.js";
|
|
154
5
|
|
|
155
6
|
// src/request-setup.ts
|
|
156
7
|
function setup(config) {
|
|
@@ -169,7 +20,6 @@ var RequestMethod = /* @__PURE__ */ ((RequestMethod2) => {
|
|
|
169
20
|
return RequestMethod2;
|
|
170
21
|
})(RequestMethod || {});
|
|
171
22
|
export {
|
|
172
|
-
AxiosAdapter,
|
|
173
23
|
PluginLifecycle,
|
|
174
24
|
RequestMethod,
|
|
175
25
|
RequestService,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 请求方法类型
|
|
3
|
+
*/
|
|
4
|
+
declare enum RequestMethod {
|
|
5
|
+
Get = "GET",
|
|
6
|
+
Post = "POST",
|
|
7
|
+
Put = "PUT",
|
|
8
|
+
Delete = "DELETE",
|
|
9
|
+
Options = "OPTIONS",
|
|
10
|
+
Head = "HEAD",
|
|
11
|
+
Patch = "PATCH"
|
|
12
|
+
}
|
|
13
|
+
interface RequestSendOptions {
|
|
14
|
+
path: string;
|
|
15
|
+
method: RequestMethod | string;
|
|
16
|
+
headers?: Record<string, string>;
|
|
17
|
+
paramsPath?: Record<string, string | number>;
|
|
18
|
+
paramsQuery?: Record<string, any>;
|
|
19
|
+
paramsBody?: any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface RequestAdapter {
|
|
23
|
+
/**
|
|
24
|
+
* 发送请求
|
|
25
|
+
*/
|
|
26
|
+
request(options: RequestAdapterOptions): Promise<any>;
|
|
27
|
+
/**
|
|
28
|
+
* 转换Response
|
|
29
|
+
* @param any
|
|
30
|
+
*/
|
|
31
|
+
transformResponse(response: any): AdapterResponse;
|
|
32
|
+
}
|
|
33
|
+
interface RequestAdapterOptions {
|
|
34
|
+
baseURL: string;
|
|
35
|
+
pathURL: string;
|
|
36
|
+
headers: Record<string, string>;
|
|
37
|
+
method: RequestMethod | string;
|
|
38
|
+
paramsQuery?: Record<string, any>;
|
|
39
|
+
paramsBody?: any;
|
|
40
|
+
}
|
|
41
|
+
interface AdapterResponse {
|
|
42
|
+
data: Record<string, any>;
|
|
43
|
+
status: number;
|
|
44
|
+
statusText: string;
|
|
45
|
+
headers: Record<string, string>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { AdapterResponse as A, RequestSendOptions as R, RequestAdapter as a, RequestAdapterOptions as b, RequestMethod as c };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gopowerteam/request",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -11,13 +11,21 @@
|
|
|
11
11
|
"gopowerteam",
|
|
12
12
|
"request"
|
|
13
13
|
],
|
|
14
|
-
"scripts": {
|
|
15
|
-
"test": "jest --coverage",
|
|
16
|
-
"build": "tsup"
|
|
17
|
-
},
|
|
18
14
|
"main": "./dist/index.cjs",
|
|
19
15
|
"module": "./dist/index.js",
|
|
20
16
|
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"require": "./dist/index.cjs",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts"
|
|
22
|
+
},
|
|
23
|
+
"./adapters": {
|
|
24
|
+
"require": "./dist/adapters/index.cjs",
|
|
25
|
+
"import": "./dist/adapters/index.js",
|
|
26
|
+
"types": "./dist/adapters/index.d.ts"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
21
29
|
"devDependencies": {
|
|
22
30
|
"@types/axios": "^0.14.0",
|
|
23
31
|
"@types/jest": "^29.1.2",
|
|
@@ -32,5 +40,9 @@
|
|
|
32
40
|
"dependencies": {
|
|
33
41
|
"axios": "^1.1.2",
|
|
34
42
|
"qs": "^6.11.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"test": "jest --coverage",
|
|
46
|
+
"build": "tsup"
|
|
35
47
|
}
|
|
36
|
-
}
|
|
48
|
+
}
|