utils-lib-js 1.3.1 → 1.4.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/dist/common/request.d.ts +19 -34
- package/dist/common/request.js +17 -4
- package/dist/common/types.d.ts +2 -2
- package/dist/esm/request.d.ts +19 -34
- package/dist/esm/request.js +17 -4
- package/dist/esm/types.d.ts +2 -2
- package/package.json +5 -2
- package/pnpm-lock.yaml +1536 -1520
package/dist/common/request.d.ts
CHANGED
|
@@ -1,66 +1,51 @@
|
|
|
1
|
-
import { IRequest, IRequestBase, IRequestInit, IInterceptors, IUrl, IObject, IRequestBody, IRequestOptions } from "./index.js";
|
|
1
|
+
import { IRequest, IRequestBase, IRequestInit, IInterceptors, IUrl, IObject, IRequestBody, IRequestOptions, IRequestBaseFn, IEnv } from "./index.js";
|
|
2
2
|
declare class Interceptors implements IInterceptors {
|
|
3
3
|
private requestSuccess;
|
|
4
4
|
private responseSuccess;
|
|
5
5
|
private error;
|
|
6
|
-
use(type:
|
|
6
|
+
use(type: "request" | "response" | "error", fn: Function): IInterceptors;
|
|
7
7
|
get reqFn(): Function;
|
|
8
8
|
get resFn(): Function;
|
|
9
9
|
get errFn(): Function;
|
|
10
10
|
}
|
|
11
11
|
declare abstract class RequestBase extends Interceptors implements IRequestBase {
|
|
12
|
-
readonly origin:
|
|
13
|
-
constructor(origin:
|
|
14
|
-
abstract fetch(url:
|
|
15
|
-
abstract http(url:
|
|
16
|
-
chackUrl: (url:
|
|
12
|
+
readonly origin: IUrl;
|
|
13
|
+
constructor(origin: IUrl);
|
|
14
|
+
abstract fetch(url: string, opts: IRequestOptions): Promise<void>;
|
|
15
|
+
abstract http(url: string, opts: IRequestOptions): Promise<void>;
|
|
16
|
+
chackUrl: (url: IUrl) => boolean;
|
|
17
|
+
checkIsHttps: (url: IUrl) => boolean;
|
|
17
18
|
fixOrigin: (fixStr: string) => string;
|
|
18
|
-
envDesc: () =>
|
|
19
|
+
envDesc: () => IEnv;
|
|
19
20
|
errorFn: (reject: any) => (err: any) => any;
|
|
20
21
|
clearTimer: (opts: any) => any;
|
|
21
|
-
initAbort: (params:
|
|
22
|
-
requestType: () =>
|
|
22
|
+
initAbort: (params: IRequestOptions) => IRequestOptions;
|
|
23
|
+
requestType: () => IRequestBaseFn;
|
|
23
24
|
getDataByType: (type: any, response: any) => any;
|
|
24
|
-
formatBodyString: (bodyString:
|
|
25
|
-
text: () => any;
|
|
26
|
-
json: () => any;
|
|
27
|
-
blob: () => IObject<any>;
|
|
28
|
-
formData: () => IObject<any>;
|
|
29
|
-
arrayBuffer: () => IObject<any>;
|
|
30
|
-
};
|
|
25
|
+
formatBodyString: (bodyString: string) => IObject<Function>;
|
|
31
26
|
}
|
|
32
27
|
declare abstract class RequestInit extends RequestBase implements IRequestInit {
|
|
33
28
|
constructor(origin: any);
|
|
34
29
|
abstract fetch(url: any, opts: any): Promise<void>;
|
|
35
30
|
abstract http(url: any, opts: any): Promise<void>;
|
|
36
|
-
initDefaultParams: (url:
|
|
31
|
+
initDefaultParams: (url: IUrl, { method, query, headers, body, timeout, controller, type, ...others }: {
|
|
37
32
|
[x: string]: any;
|
|
38
33
|
method?: string;
|
|
39
34
|
query?: {};
|
|
40
35
|
headers?: {};
|
|
41
36
|
body?: any;
|
|
42
37
|
timeout?: number;
|
|
43
|
-
controller?:
|
|
38
|
+
controller?: any;
|
|
44
39
|
type?: string;
|
|
45
|
-
}) =>
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
headers: {};
|
|
49
|
-
body: string;
|
|
50
|
-
timeout: number;
|
|
51
|
-
signal: AbortSignal;
|
|
52
|
-
controller: AbortController;
|
|
53
|
-
type: string;
|
|
54
|
-
timer: any;
|
|
55
|
-
};
|
|
56
|
-
initFetchParams: (url: any, opts: any) => any;
|
|
57
|
-
initHttpParams: (url: any, opts: any) => any;
|
|
40
|
+
}) => IRequestOptions;
|
|
41
|
+
initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
42
|
+
initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
58
43
|
}
|
|
59
44
|
export declare class Request extends RequestInit implements IRequest {
|
|
60
45
|
private request;
|
|
61
46
|
constructor(origin: any);
|
|
62
|
-
fetch: (_url:
|
|
63
|
-
http: (_url:
|
|
47
|
+
fetch: (_url: string, _opts: IRequestOptions) => Promise<any>;
|
|
48
|
+
http: (_url: string, _opts: IRequestOptions) => Promise<any>;
|
|
64
49
|
GET: (url?: IUrl, query?: IObject<any>, _?: IRequestBody | void, opts?: IRequestOptions) => any;
|
|
65
50
|
POST: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
66
51
|
PUT: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
package/dist/common/request.js
CHANGED
|
@@ -39,10 +39,18 @@ var __rest = this && this.__rest || function (s, e) {
|
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
40
|
exports.Request = void 0;
|
|
41
41
|
var index_js_1 = require("./index.js");
|
|
42
|
-
var
|
|
42
|
+
var httpRequest, httpsRequest, parse, CustomAbortController;
|
|
43
43
|
if (typeof require !== "undefined") {
|
|
44
|
-
|
|
44
|
+
CustomAbortController = require("abort-controller");
|
|
45
|
+
httpRequest = require("http").request;
|
|
46
|
+
httpsRequest = require("https").request;
|
|
45
47
|
parse = require("url").parse;
|
|
48
|
+
} else if (typeof AbortController !== "undefined") {
|
|
49
|
+
CustomAbortController = AbortController;
|
|
50
|
+
} else {
|
|
51
|
+
CustomAbortController = function () {
|
|
52
|
+
throw new Error('AbortController is not defined');
|
|
53
|
+
};
|
|
46
54
|
}
|
|
47
55
|
var Interceptors = function () {
|
|
48
56
|
function Interceptors() {}
|
|
@@ -90,6 +98,9 @@ var RequestBase = function (_super) {
|
|
|
90
98
|
_this.chackUrl = function (url) {
|
|
91
99
|
return url.startsWith('/');
|
|
92
100
|
};
|
|
101
|
+
_this.checkIsHttps = function (url) {
|
|
102
|
+
return url.startsWith('https');
|
|
103
|
+
};
|
|
93
104
|
_this.fixOrigin = function (fixStr) {
|
|
94
105
|
if (_this.chackUrl(fixStr)) return _this.origin + fixStr;
|
|
95
106
|
return fixStr;
|
|
@@ -177,7 +188,7 @@ var RequestInit = function (_super) {
|
|
|
177
188
|
_f = _a.timeout,
|
|
178
189
|
timeout = _f === void 0 ? 30 * 1000 : _f,
|
|
179
190
|
_g = _a.controller,
|
|
180
|
-
controller = _g === void 0 ? new
|
|
191
|
+
controller = _g === void 0 ? new CustomAbortController() : _g,
|
|
181
192
|
_h = _a.type,
|
|
182
193
|
type = _h === void 0 ? "json" : _h,
|
|
183
194
|
others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
|
|
@@ -233,10 +244,12 @@ var Request = function (_super) {
|
|
|
233
244
|
resolve = _a.resolve,
|
|
234
245
|
reject = _a.reject;
|
|
235
246
|
var params = _this.initHttpParams(_url, _opts);
|
|
236
|
-
var signal = params.signal
|
|
247
|
+
var signal = params.signal,
|
|
248
|
+
url = params.url;
|
|
237
249
|
promise.finally(function () {
|
|
238
250
|
return _this.clearTimer(params);
|
|
239
251
|
});
|
|
252
|
+
var request = _this.checkIsHttps(url) ? httpsRequest : httpRequest;
|
|
240
253
|
var req = request(params, function (response) {
|
|
241
254
|
if ((response === null || response === void 0 ? void 0 : response.statusCode) >= 200 && (response === null || response === void 0 ? void 0 : response.statusCode) < 300) {
|
|
242
255
|
var data_1 = "";
|
package/dist/common/types.d.ts
CHANGED
|
@@ -67,7 +67,7 @@ export declare type IRequestOptions = {
|
|
|
67
67
|
[key: string]: any;
|
|
68
68
|
};
|
|
69
69
|
export declare type IInterceptors = {
|
|
70
|
-
use(type: "request" | "response" | "error", fn: Function):
|
|
70
|
+
use(type: "request" | "response" | "error", fn: Function): IInterceptors;
|
|
71
71
|
get reqFn(): Function;
|
|
72
72
|
get resFn(): Function;
|
|
73
73
|
get errFn(): Function;
|
|
@@ -78,7 +78,7 @@ export declare type IRequestBase = {
|
|
|
78
78
|
envDesc: () => IEnv;
|
|
79
79
|
errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R;
|
|
80
80
|
clearTimer: (opts: IRequestOptions) => void;
|
|
81
|
-
initAbort:
|
|
81
|
+
initAbort: (opts: IRequestOptions) => IRequestOptions;
|
|
82
82
|
requestType: () => IRequestBaseFn;
|
|
83
83
|
fixOrigin: (fixStr: string) => string;
|
|
84
84
|
fetch: IRequestBaseFn;
|
package/dist/esm/request.d.ts
CHANGED
|
@@ -1,66 +1,51 @@
|
|
|
1
|
-
import { IRequest, IRequestBase, IRequestInit, IInterceptors, IUrl, IObject, IRequestBody, IRequestOptions } from "./index.js";
|
|
1
|
+
import { IRequest, IRequestBase, IRequestInit, IInterceptors, IUrl, IObject, IRequestBody, IRequestOptions, IRequestBaseFn, IEnv } from "./index.js";
|
|
2
2
|
declare class Interceptors implements IInterceptors {
|
|
3
3
|
private requestSuccess;
|
|
4
4
|
private responseSuccess;
|
|
5
5
|
private error;
|
|
6
|
-
use(type:
|
|
6
|
+
use(type: "request" | "response" | "error", fn: Function): IInterceptors;
|
|
7
7
|
get reqFn(): Function;
|
|
8
8
|
get resFn(): Function;
|
|
9
9
|
get errFn(): Function;
|
|
10
10
|
}
|
|
11
11
|
declare abstract class RequestBase extends Interceptors implements IRequestBase {
|
|
12
|
-
readonly origin:
|
|
13
|
-
constructor(origin:
|
|
14
|
-
abstract fetch(url:
|
|
15
|
-
abstract http(url:
|
|
16
|
-
chackUrl: (url:
|
|
12
|
+
readonly origin: IUrl;
|
|
13
|
+
constructor(origin: IUrl);
|
|
14
|
+
abstract fetch(url: string, opts: IRequestOptions): Promise<void>;
|
|
15
|
+
abstract http(url: string, opts: IRequestOptions): Promise<void>;
|
|
16
|
+
chackUrl: (url: IUrl) => boolean;
|
|
17
|
+
checkIsHttps: (url: IUrl) => boolean;
|
|
17
18
|
fixOrigin: (fixStr: string) => string;
|
|
18
|
-
envDesc: () =>
|
|
19
|
+
envDesc: () => IEnv;
|
|
19
20
|
errorFn: (reject: any) => (err: any) => any;
|
|
20
21
|
clearTimer: (opts: any) => any;
|
|
21
|
-
initAbort: (params:
|
|
22
|
-
requestType: () =>
|
|
22
|
+
initAbort: (params: IRequestOptions) => IRequestOptions;
|
|
23
|
+
requestType: () => IRequestBaseFn;
|
|
23
24
|
getDataByType: (type: any, response: any) => any;
|
|
24
|
-
formatBodyString: (bodyString:
|
|
25
|
-
text: () => any;
|
|
26
|
-
json: () => any;
|
|
27
|
-
blob: () => IObject<any>;
|
|
28
|
-
formData: () => IObject<any>;
|
|
29
|
-
arrayBuffer: () => IObject<any>;
|
|
30
|
-
};
|
|
25
|
+
formatBodyString: (bodyString: string) => IObject<Function>;
|
|
31
26
|
}
|
|
32
27
|
declare abstract class RequestInit extends RequestBase implements IRequestInit {
|
|
33
28
|
constructor(origin: any);
|
|
34
29
|
abstract fetch(url: any, opts: any): Promise<void>;
|
|
35
30
|
abstract http(url: any, opts: any): Promise<void>;
|
|
36
|
-
initDefaultParams: (url:
|
|
31
|
+
initDefaultParams: (url: IUrl, { method, query, headers, body, timeout, controller, type, ...others }: {
|
|
37
32
|
[x: string]: any;
|
|
38
33
|
method?: string;
|
|
39
34
|
query?: {};
|
|
40
35
|
headers?: {};
|
|
41
36
|
body?: any;
|
|
42
37
|
timeout?: number;
|
|
43
|
-
controller?:
|
|
38
|
+
controller?: any;
|
|
44
39
|
type?: string;
|
|
45
|
-
}) =>
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
headers: {};
|
|
49
|
-
body: string;
|
|
50
|
-
timeout: number;
|
|
51
|
-
signal: AbortSignal;
|
|
52
|
-
controller: AbortController;
|
|
53
|
-
type: string;
|
|
54
|
-
timer: any;
|
|
55
|
-
};
|
|
56
|
-
initFetchParams: (url: any, opts: any) => any;
|
|
57
|
-
initHttpParams: (url: any, opts: any) => any;
|
|
40
|
+
}) => IRequestOptions;
|
|
41
|
+
initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
42
|
+
initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
58
43
|
}
|
|
59
44
|
export declare class Request extends RequestInit implements IRequest {
|
|
60
45
|
private request;
|
|
61
46
|
constructor(origin: any);
|
|
62
|
-
fetch: (_url:
|
|
63
|
-
http: (_url:
|
|
47
|
+
fetch: (_url: string, _opts: IRequestOptions) => Promise<any>;
|
|
48
|
+
http: (_url: string, _opts: IRequestOptions) => Promise<any>;
|
|
64
49
|
GET: (url?: IUrl, query?: IObject<any>, _?: IRequestBody | void, opts?: IRequestOptions) => any;
|
|
65
50
|
POST: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
|
66
51
|
PUT: (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => any;
|
package/dist/esm/request.js
CHANGED
|
@@ -35,10 +35,18 @@ var __rest = this && this.__rest || function (s, e) {
|
|
|
35
35
|
return t;
|
|
36
36
|
};
|
|
37
37
|
import { urlJoin, defer, jsonToString, stringToJson } from "./index.js";
|
|
38
|
-
var
|
|
38
|
+
var httpRequest, httpsRequest, parse, CustomAbortController;
|
|
39
39
|
if (typeof require !== "undefined") {
|
|
40
|
-
|
|
40
|
+
CustomAbortController = require("abort-controller");
|
|
41
|
+
httpRequest = require("http").request;
|
|
42
|
+
httpsRequest = require("https").request;
|
|
41
43
|
parse = require("url").parse;
|
|
44
|
+
} else if (typeof AbortController !== "undefined") {
|
|
45
|
+
CustomAbortController = AbortController;
|
|
46
|
+
} else {
|
|
47
|
+
CustomAbortController = function () {
|
|
48
|
+
throw new Error('AbortController is not defined');
|
|
49
|
+
};
|
|
42
50
|
}
|
|
43
51
|
var Interceptors = function () {
|
|
44
52
|
function Interceptors() {}
|
|
@@ -86,6 +94,9 @@ var RequestBase = function (_super) {
|
|
|
86
94
|
_this.chackUrl = function (url) {
|
|
87
95
|
return url.startsWith('/');
|
|
88
96
|
};
|
|
97
|
+
_this.checkIsHttps = function (url) {
|
|
98
|
+
return url.startsWith('https');
|
|
99
|
+
};
|
|
89
100
|
_this.fixOrigin = function (fixStr) {
|
|
90
101
|
if (_this.chackUrl(fixStr)) return _this.origin + fixStr;
|
|
91
102
|
return fixStr;
|
|
@@ -173,7 +184,7 @@ var RequestInit = function (_super) {
|
|
|
173
184
|
_f = _a.timeout,
|
|
174
185
|
timeout = _f === void 0 ? 30 * 1000 : _f,
|
|
175
186
|
_g = _a.controller,
|
|
176
|
-
controller = _g === void 0 ? new
|
|
187
|
+
controller = _g === void 0 ? new CustomAbortController() : _g,
|
|
177
188
|
_h = _a.type,
|
|
178
189
|
type = _h === void 0 ? "json" : _h,
|
|
179
190
|
others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
|
|
@@ -229,10 +240,12 @@ var Request = function (_super) {
|
|
|
229
240
|
resolve = _a.resolve,
|
|
230
241
|
reject = _a.reject;
|
|
231
242
|
var params = _this.initHttpParams(_url, _opts);
|
|
232
|
-
var signal = params.signal
|
|
243
|
+
var signal = params.signal,
|
|
244
|
+
url = params.url;
|
|
233
245
|
promise.finally(function () {
|
|
234
246
|
return _this.clearTimer(params);
|
|
235
247
|
});
|
|
248
|
+
var request = _this.checkIsHttps(url) ? httpsRequest : httpRequest;
|
|
236
249
|
var req = request(params, function (response) {
|
|
237
250
|
if ((response === null || response === void 0 ? void 0 : response.statusCode) >= 200 && (response === null || response === void 0 ? void 0 : response.statusCode) < 300) {
|
|
238
251
|
var data_1 = "";
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -67,7 +67,7 @@ export declare type IRequestOptions = {
|
|
|
67
67
|
[key: string]: any;
|
|
68
68
|
};
|
|
69
69
|
export declare type IInterceptors = {
|
|
70
|
-
use(type: "request" | "response" | "error", fn: Function):
|
|
70
|
+
use(type: "request" | "response" | "error", fn: Function): IInterceptors;
|
|
71
71
|
get reqFn(): Function;
|
|
72
72
|
get resFn(): Function;
|
|
73
73
|
get errFn(): Function;
|
|
@@ -78,7 +78,7 @@ export declare type IRequestBase = {
|
|
|
78
78
|
envDesc: () => IEnv;
|
|
79
79
|
errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R;
|
|
80
80
|
clearTimer: (opts: IRequestOptions) => void;
|
|
81
|
-
initAbort:
|
|
81
|
+
initAbort: (opts: IRequestOptions) => IRequestOptions;
|
|
82
82
|
requestType: () => IRequestBaseFn;
|
|
83
83
|
fixOrigin: (fixStr: string) => string;
|
|
84
84
|
fetch: IRequestBaseFn;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utils-lib-js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "JavaScript工具函数,封装的一些常用的js函数",
|
|
5
5
|
"main": "./dist/common/index.js",
|
|
6
6
|
"types": "./dist/common/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"scripts": {
|
|
13
13
|
"debug:esm": "start cmd /k pnpm run build:hot:esm",
|
|
14
14
|
"debug:node": "start cmd /k pnpm run build:hot:node & pnpm run node:hot",
|
|
15
|
-
"node:hot": "nodemon
|
|
15
|
+
"node:hot": "nodemon server.js",
|
|
16
16
|
"build:hot:esm": "tsc -p tsconfig.es.json -w",
|
|
17
17
|
"build:hot:node": "tsc -p tsconfig.json -w",
|
|
18
18
|
"build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
|
|
@@ -36,5 +36,8 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/node": "^18.7.15",
|
|
38
38
|
"babel-cli": "^6.26.0"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"abort-controller": "^3.0.0"
|
|
39
42
|
}
|
|
40
43
|
}
|