utils-lib-js 1.0.5 → 1.0.8

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.
@@ -1,5 +1,5 @@
1
- import { IRequest, IRequestBase, IInterceptors } from "./index.js";
2
- export declare class Interceptors implements IInterceptors {
1
+ import { IRequest, IRequestBase, IRequestInit, IInterceptors } from "./index.js";
2
+ declare class Interceptors implements IInterceptors {
3
3
  private requestSuccess;
4
4
  private responseSuccess;
5
5
  private error;
@@ -8,35 +8,58 @@ export declare class Interceptors implements IInterceptors {
8
8
  get resFn(): Function;
9
9
  get errFn(): Function;
10
10
  }
11
- export declare abstract class RequestBase extends Interceptors implements IRequestBase {
12
- origin: string;
11
+ declare abstract class RequestBase extends Interceptors implements IRequestBase {
12
+ readonly origin: string;
13
13
  constructor(origin: any);
14
14
  abstract fetch(url: any, opts: any): Promise<void>;
15
15
  abstract http(url: any, opts: any): Promise<void>;
16
- chackUrl: (url: any) => any;
16
+ chackUrl: (url: string) => boolean;
17
17
  fixOrigin: (fixStr: string) => string;
18
18
  envDesc: () => "Window" | "Node";
19
+ errorFn: (reject: any) => (err: any) => any;
20
+ clearTimer: (opts: any) => any;
21
+ initAbort: (params: any) => any;
19
22
  requestType: () => (url: any, opts: any) => Promise<void>;
20
- initFetchParams: (url: any, { method, query, headers, body, timeout, abort, type, ...others }: {
23
+ getDataByType: (type: any, response: any) => any;
24
+ }
25
+ declare abstract class RequestInit extends RequestBase implements IRequestInit {
26
+ constructor(origin: any);
27
+ abstract fetch(url: any, opts: any): Promise<void>;
28
+ abstract http(url: any, opts: any): Promise<void>;
29
+ initDefaultParams: (url: any, { method, query, headers, body, timeout, controller, type, ...others }: {
21
30
  [x: string]: any;
22
31
  method?: string;
23
32
  query?: {};
24
33
  headers?: {};
25
34
  body?: any;
26
35
  timeout?: number;
27
- abort?: AbortController;
36
+ controller?: AbortController;
28
37
  type?: string;
29
- }) => any;
30
- getDataByType: (type: any, response: any) => any;
38
+ }) => {
39
+ url: string;
40
+ method: string;
41
+ headers: {};
42
+ body: string;
43
+ timeout: number;
44
+ signal: AbortSignal;
45
+ controller: AbortController;
46
+ type: string;
47
+ timer: any;
48
+ };
49
+ initFetchParams: (url: any, opts: any) => any;
50
+ initHttpParams: (url: any, opts: any) => any;
31
51
  }
32
- export declare class Request extends RequestBase implements IRequest {
33
- request: Function;
52
+ export declare class Request extends RequestInit implements IRequest {
53
+ private request;
34
54
  constructor(origin: any);
35
55
  fetch: (_url: any, _opts: any) => Promise<void>;
36
- http: (url: any, opts: any) => Promise<void>;
56
+ http: (_url: any, _opts: any) => Promise<void>;
37
57
  GET: (url: any, query: {}, _: any, opts: any) => any;
38
58
  POST: (url: any, query: {}, body: any, opts: any) => any;
39
59
  PUT: (url: any, query: {}, body: any, opts: any) => any;
40
60
  DELETE: (url: any, query: {}, body: any, opts: any) => any;
41
- OPTION: (url: any, query: {}, body: any, opts: any) => any;
61
+ OPTIONS: (url: any, query: {}, body: any, opts: any) => any;
62
+ HEAD: (url: any, query: {}, body: any, opts: any) => any;
63
+ PATCH: (url: any, query: {}, body: any, opts: any) => any;
42
64
  }
65
+ export {};
@@ -37,8 +37,10 @@ var __rest = this && this.__rest || function (s, e) {
37
37
  return t;
38
38
  };
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.Request = exports.RequestBase = exports.Interceptors = void 0;
40
+ exports.Request = void 0;
41
41
  var index_js_1 = require("./index.js");
42
+ var node_http_1 = require("node:http");
43
+ var node_url_1 = require("node:url");
42
44
  var Interceptors = function () {
43
45
  function Interceptors() {}
44
46
  Interceptors.prototype.use = function (type, fn) {
@@ -78,7 +80,6 @@ var Interceptors = function () {
78
80
  });
79
81
  return Interceptors;
80
82
  }();
81
- exports.Interceptors = Interceptors;
82
83
  var RequestBase = function (_super) {
83
84
  __extends(RequestBase, _super);
84
85
  function RequestBase(origin) {
@@ -91,11 +92,28 @@ var RequestBase = function (_super) {
91
92
  return fixStr;
92
93
  };
93
94
  _this.envDesc = function () {
94
- if (Window) {
95
+ if (typeof Window !== "undefined") {
95
96
  return "Window";
96
97
  }
97
98
  return "Node";
98
99
  };
100
+ _this.errorFn = function (reject) {
101
+ return function (err) {
102
+ var _a, _b;return reject((_b = (_a = _this.errFn) === null || _a === void 0 ? void 0 : _a.call(_this, err)) !== null && _b !== void 0 ? _b : err);
103
+ };
104
+ };
105
+ _this.clearTimer = function (opts) {
106
+ return !!opts.timer && (clearTimeout(opts.timer), opts.timer = null);
107
+ };
108
+ _this.initAbort = function (params) {
109
+ var controller = params.controller,
110
+ timer = params.timer,
111
+ timeout = params.timeout;
112
+ !!!timer && (params.timer = setTimeout(function () {
113
+ return controller.abort();
114
+ }, timeout));
115
+ return params;
116
+ };
99
117
  _this.requestType = function () {
100
118
  switch (_this.envDesc()) {
101
119
  case "Window":
@@ -104,30 +122,6 @@ var RequestBase = function (_super) {
104
122
  return _this.http;
105
123
  }
106
124
  };
107
- _this.initFetchParams = function (url, _a) {
108
- var _b, _c;
109
- var _d = _a.method,
110
- method = _d === void 0 ? "GET" : _d,
111
- _e = _a.query,
112
- query = _e === void 0 ? {} : _e,
113
- _f = _a.headers,
114
- headers = _f === void 0 ? {} : _f,
115
- _g = _a.body,
116
- body = _g === void 0 ? null : _g,
117
- _h = _a.timeout,
118
- timeout = _h === void 0 ? 30 * 1000 : _h,
119
- _j = _a.abort,
120
- abort = _j === void 0 ? new AbortController() : _j,
121
- _k = _a.type,
122
- type = _k === void 0 ? "json" : _k,
123
- others = __rest(_a, ["method", "query", "headers", "body", "timeout", "abort", "type"]);
124
- url = (0, index_js_1.urlJoin)(_this.fixOrigin(url), query);
125
- var timer = setTimeout(function () {
126
- return abort.abort();
127
- }, timeout);
128
- var params = __assign({ url: url, method: method, headers: headers, body: method === "GET" ? null : (0, index_js_1.jsonToString)(body), timeout: timeout, timer: timer, signal: abort.signal, type: type }, others);
129
- return (_c = (_b = _this.reqFn) === null || _b === void 0 ? void 0 : _b.call(_this, params)) !== null && _c !== void 0 ? _c : params;
130
- };
131
125
  _this.getDataByType = function (type, response) {
132
126
  switch (type) {
133
127
  case "text":
@@ -145,7 +139,43 @@ var RequestBase = function (_super) {
145
139
  }
146
140
  return RequestBase;
147
141
  }(Interceptors);
148
- exports.RequestBase = RequestBase;
142
+ var RequestInit = function (_super) {
143
+ __extends(RequestInit, _super);
144
+ function RequestInit(origin) {
145
+ var _this = _super.call(this, origin) || this;
146
+ _this.initDefaultParams = function (url, _a) {
147
+ var _b = _a.method,
148
+ method = _b === void 0 ? "GET" : _b,
149
+ _c = _a.query,
150
+ query = _c === void 0 ? {} : _c,
151
+ _d = _a.headers,
152
+ headers = _d === void 0 ? {} : _d,
153
+ _e = _a.body,
154
+ body = _e === void 0 ? null : _e,
155
+ _f = _a.timeout,
156
+ timeout = _f === void 0 ? 30 * 1000 : _f,
157
+ _g = _a.controller,
158
+ controller = _g === void 0 ? new AbortController() : _g,
159
+ _h = _a.type,
160
+ type = _h === void 0 ? "json" : _h,
161
+ others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
162
+ return __assign({ url: (0, index_js_1.urlJoin)(_this.fixOrigin(url), query), method: method, headers: headers, body: method === "GET" ? null : (0, index_js_1.jsonToString)(body), timeout: timeout, signal: controller === null || controller === void 0 ? void 0 : controller.signal, controller: controller, type: type, timer: null }, others);
163
+ };
164
+ _this.initFetchParams = function (url, opts) {
165
+ var _a, _b;
166
+ var params = _this.initAbort(_this.initDefaultParams(url, opts));
167
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
168
+ };
169
+ _this.initHttpParams = function (url, opts) {
170
+ var _a, _b;
171
+ var params = _this.initAbort(_this.initDefaultParams(url, opts));
172
+ var options = (0, node_url_1.parse)(params.url, true);
173
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : params;
174
+ };
175
+ return _this;
176
+ }
177
+ return RequestInit;
178
+ }(RequestBase);
149
179
  var Request = function (_super) {
150
180
  __extends(Request, _super);
151
181
  function Request(origin) {
@@ -158,29 +188,47 @@ var Request = function (_super) {
158
188
  var _b = _this.initFetchParams(_url, _opts),
159
189
  url = _b.url,
160
190
  opts = __rest(_b, ["url"]);
161
- var signal = opts.signal,
162
- timer = opts.timer;
163
- var errorFn = function (err) {
164
- var _a, _b;return reject((_b = (_a = _this.errFn) === null || _a === void 0 ? void 0 : _a.call(_this, err)) !== null && _b !== void 0 ? _b : err);
165
- };
166
- signal.addEventListener('abort', errorFn);
191
+ var signal = opts.signal;
192
+ signal.addEventListener('abort', function () {
193
+ return _this.errorFn(reject);
194
+ });
167
195
  fetch(url, opts).then(function (response) {
168
196
  if (response.status >= 200 && response.status < 300) {
169
197
  return _this.getDataByType(opts.type, response);
170
198
  }
171
- return errorFn(response.statusText);
199
+ return _this.errorFn(reject);
172
200
  }).then(function (res) {
173
201
  var _a, _b;return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, res)) !== null && _b !== void 0 ? _b : res);
174
- }).catch(errorFn).finally(function () {
175
- return clearTimeout(timer);
202
+ }).catch(_this.errorFn(reject)).finally(function () {
203
+ return _this.clearTimer(opts);
176
204
  });
177
205
  return promise;
178
206
  };
179
- _this.http = function (url, opts) {
207
+ _this.http = function (_url, _opts) {
180
208
  var _a = (0, index_js_1.defer)(),
181
209
  promise = _a.promise,
182
210
  resolve = _a.resolve,
183
211
  reject = _a.reject;
212
+ var params = _this.initHttpParams(_url, _opts);
213
+ var signal = params.signal;
214
+ var req = (0, node_http_1.request)(params, function (response) {
215
+ if (response.statusCode >= 200 && response.statusCode < 300) {
216
+ var data_1 = "";
217
+ response.setEncoding('utf8');
218
+ response.on('data', function (chunk) {
219
+ return data_1 += chunk;
220
+ });
221
+ return response.on("end", function () {
222
+ var _a, _b;return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, data_1)) !== null && _b !== void 0 ? _b : data_1);
223
+ });
224
+ }
225
+ return _this.errorFn(reject)(response === null || response === void 0 ? void 0 : response.statusMessage);
226
+ });
227
+ signal.addEventListener('abort', function () {
228
+ return _this.errorFn(reject)(req.destroy(new Error('request timeout')));
229
+ });
230
+ req.on('error', _this.errorFn(reject));
231
+ req.end();
184
232
  return promise;
185
233
  };
186
234
  _this.GET = function (url, query, _, opts) {
@@ -207,15 +255,27 @@ var Request = function (_super) {
207
255
  }
208
256
  return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
209
257
  };
210
- _this.OPTION = function (url, query, body, opts) {
258
+ _this.OPTIONS = function (url, query, body, opts) {
211
259
  if (query === void 0) {
212
260
  query = {};
213
261
  }
214
- return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
262
+ return _this.request(url, __assign({ query: query, method: "OPTIONS", body: body }, opts));
263
+ };
264
+ _this.HEAD = function (url, query, body, opts) {
265
+ if (query === void 0) {
266
+ query = {};
267
+ }
268
+ return _this.request(url, __assign({ query: query, method: "HEAD", body: body }, opts));
269
+ };
270
+ _this.PATCH = function (url, query, body, opts) {
271
+ if (query === void 0) {
272
+ query = {};
273
+ }
274
+ return _this.request(url, __assign({ query: query, method: "PATCH", body: body }, opts));
215
275
  };
216
276
  _this.request = _this.requestType();
217
277
  return _this;
218
278
  }
219
279
  return Request;
220
- }(RequestBase);
280
+ }(RequestInit);
221
281
  exports.Request = Request;
@@ -16,7 +16,7 @@ export declare type IUrlSplit = (url: string) => IObject<any>;
16
16
  export declare type IUrlJoin = (url: string, query: object) => string;
17
17
  export declare type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
18
18
  export declare type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
19
- export declare type IGetValue = <T>(object: IObject<T> | IObject<T>[IKey], key: string, defaultValue?: any) => IObject<T>[IKey];
19
+ export declare type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U;
20
20
  export declare type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>;
21
21
  export declare type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U;
22
22
  export declare type IEnumInversion = (target: IObject<string>) => IObject<string>;
@@ -48,18 +48,22 @@ export declare type IStopDefault = (e: Event) => void;
48
48
  export declare type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
49
49
  export declare type IDispatchEvent = <T extends Document>(ele: T, data: any) => void;
50
50
  export declare type IRequestParams<T> = T | IObject<any> | null;
51
+ export declare type IUrl = string;
52
+ export declare type IEnv = 'Window' | 'Node';
51
53
  export declare type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
52
54
  export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION";
53
55
  export declare type IRequestBody = IRequestParams<BodyInit>;
54
56
  export declare type IRequestHeaders = IRequestParams<HeadersInit>;
55
- export declare type IRequestFn = (url: string, params: IObject<any>, body: IRequestBody, opts: IRequestOptions) => Promise<any>;
57
+ export declare type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
58
+ export declare type IRequestFn = (url?: IUrl, params?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>;
56
59
  export declare type IRequestOptions = {
57
60
  method?: IRequestMethods;
58
61
  query?: IRequestParams<IObject<any>>;
59
62
  body?: IRequestBody;
60
63
  headers?: IRequestHeaders;
61
- abort?: AbortController;
64
+ controller?: AbortController;
62
65
  timeout?: number;
66
+ timer?: number | unknown | null;
63
67
  [key: string]: any;
64
68
  };
65
69
  export declare type IInterceptors = {
@@ -69,21 +73,30 @@ export declare type IInterceptors = {
69
73
  get errFn(): Function;
70
74
  };
71
75
  export declare type IRequestBase = {
72
- origin: string;
76
+ readonly origin: string;
77
+ chackUrl: (url: IUrl) => boolean;
78
+ envDesc: () => IEnv;
79
+ errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R;
80
+ clearTimer: (opts: IRequestOptions) => void;
81
+ initAbort: <T = IRequestOptions>(opts: T) => T;
82
+ requestType: () => IRequestBaseFn;
73
83
  fixOrigin: (fixStr: string) => string;
74
- envDesc: () => "Window" | "Node";
75
- chackUrl: (url: string) => boolean;
76
- fetch: (url: string, opts: IRequestOptions) => Promise<any>;
77
- http: (url: string, opts: IRequestOptions) => Promise<any>;
78
- initFetchParams: (url: string, opts: IRequestOptions) => any;
84
+ fetch: IRequestBaseFn;
85
+ http: IRequestBaseFn;
79
86
  getDataByType: (type: IDataType, response: Response) => Promise<any>;
80
87
  };
88
+ export declare type IRequestInit = {
89
+ initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
90
+ initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
91
+ initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
92
+ };
81
93
  export declare type IRequest = {
82
- request: Function;
83
94
  GET: IRequestFn;
84
95
  POST: IRequestFn;
85
96
  DELETE: IRequestFn;
86
97
  PUT: IRequestFn;
87
- OPTION: IRequestFn;
98
+ OPTIONS: IRequestFn;
99
+ HEAD: IRequestFn;
100
+ PATCH: IRequestFn;
88
101
  } & IRequestBase;
89
102
  export {};
@@ -1,5 +1,5 @@
1
- import { IRequest, IRequestBase, IInterceptors } from "./index.js";
2
- export declare class Interceptors implements IInterceptors {
1
+ import { IRequest, IRequestBase, IRequestInit, IInterceptors } from "./index.js";
2
+ declare class Interceptors implements IInterceptors {
3
3
  private requestSuccess;
4
4
  private responseSuccess;
5
5
  private error;
@@ -8,35 +8,58 @@ export declare class Interceptors implements IInterceptors {
8
8
  get resFn(): Function;
9
9
  get errFn(): Function;
10
10
  }
11
- export declare abstract class RequestBase extends Interceptors implements IRequestBase {
12
- origin: string;
11
+ declare abstract class RequestBase extends Interceptors implements IRequestBase {
12
+ readonly origin: string;
13
13
  constructor(origin: any);
14
14
  abstract fetch(url: any, opts: any): Promise<void>;
15
15
  abstract http(url: any, opts: any): Promise<void>;
16
- chackUrl: (url: any) => any;
16
+ chackUrl: (url: string) => boolean;
17
17
  fixOrigin: (fixStr: string) => string;
18
18
  envDesc: () => "Window" | "Node";
19
+ errorFn: (reject: any) => (err: any) => any;
20
+ clearTimer: (opts: any) => any;
21
+ initAbort: (params: any) => any;
19
22
  requestType: () => (url: any, opts: any) => Promise<void>;
20
- initFetchParams: (url: any, { method, query, headers, body, timeout, abort, type, ...others }: {
23
+ getDataByType: (type: any, response: any) => any;
24
+ }
25
+ declare abstract class RequestInit extends RequestBase implements IRequestInit {
26
+ constructor(origin: any);
27
+ abstract fetch(url: any, opts: any): Promise<void>;
28
+ abstract http(url: any, opts: any): Promise<void>;
29
+ initDefaultParams: (url: any, { method, query, headers, body, timeout, controller, type, ...others }: {
21
30
  [x: string]: any;
22
31
  method?: string;
23
32
  query?: {};
24
33
  headers?: {};
25
34
  body?: any;
26
35
  timeout?: number;
27
- abort?: AbortController;
36
+ controller?: AbortController;
28
37
  type?: string;
29
- }) => any;
30
- getDataByType: (type: any, response: any) => any;
38
+ }) => {
39
+ url: string;
40
+ method: string;
41
+ headers: {};
42
+ body: string;
43
+ timeout: number;
44
+ signal: AbortSignal;
45
+ controller: AbortController;
46
+ type: string;
47
+ timer: any;
48
+ };
49
+ initFetchParams: (url: any, opts: any) => any;
50
+ initHttpParams: (url: any, opts: any) => any;
31
51
  }
32
- export declare class Request extends RequestBase implements IRequest {
33
- request: Function;
52
+ export declare class Request extends RequestInit implements IRequest {
53
+ private request;
34
54
  constructor(origin: any);
35
55
  fetch: (_url: any, _opts: any) => Promise<void>;
36
- http: (url: any, opts: any) => Promise<void>;
56
+ http: (_url: any, _opts: any) => Promise<void>;
37
57
  GET: (url: any, query: {}, _: any, opts: any) => any;
38
58
  POST: (url: any, query: {}, body: any, opts: any) => any;
39
59
  PUT: (url: any, query: {}, body: any, opts: any) => any;
40
60
  DELETE: (url: any, query: {}, body: any, opts: any) => any;
41
- OPTION: (url: any, query: {}, body: any, opts: any) => any;
61
+ OPTIONS: (url: any, query: {}, body: any, opts: any) => any;
62
+ HEAD: (url: any, query: {}, body: any, opts: any) => any;
63
+ PATCH: (url: any, query: {}, body: any, opts: any) => any;
42
64
  }
65
+ export {};
@@ -35,6 +35,8 @@ var __rest = this && this.__rest || function (s, e) {
35
35
  return t;
36
36
  };
37
37
  import { urlJoin, defer, jsonToString } from "./index.js";
38
+ import { request } from "node:http";
39
+ import { parse } from "node:url";
38
40
  var Interceptors = function () {
39
41
  function Interceptors() {}
40
42
  Interceptors.prototype.use = function (type, fn) {
@@ -74,7 +76,6 @@ var Interceptors = function () {
74
76
  });
75
77
  return Interceptors;
76
78
  }();
77
- export { Interceptors };
78
79
  var RequestBase = function (_super) {
79
80
  __extends(RequestBase, _super);
80
81
  function RequestBase(origin) {
@@ -87,11 +88,28 @@ var RequestBase = function (_super) {
87
88
  return fixStr;
88
89
  };
89
90
  _this.envDesc = function () {
90
- if (Window) {
91
+ if (typeof Window !== "undefined") {
91
92
  return "Window";
92
93
  }
93
94
  return "Node";
94
95
  };
96
+ _this.errorFn = function (reject) {
97
+ return function (err) {
98
+ var _a, _b;return reject((_b = (_a = _this.errFn) === null || _a === void 0 ? void 0 : _a.call(_this, err)) !== null && _b !== void 0 ? _b : err);
99
+ };
100
+ };
101
+ _this.clearTimer = function (opts) {
102
+ return !!opts.timer && (clearTimeout(opts.timer), opts.timer = null);
103
+ };
104
+ _this.initAbort = function (params) {
105
+ var controller = params.controller,
106
+ timer = params.timer,
107
+ timeout = params.timeout;
108
+ !!!timer && (params.timer = setTimeout(function () {
109
+ return controller.abort();
110
+ }, timeout));
111
+ return params;
112
+ };
95
113
  _this.requestType = function () {
96
114
  switch (_this.envDesc()) {
97
115
  case "Window":
@@ -100,30 +118,6 @@ var RequestBase = function (_super) {
100
118
  return _this.http;
101
119
  }
102
120
  };
103
- _this.initFetchParams = function (url, _a) {
104
- var _b, _c;
105
- var _d = _a.method,
106
- method = _d === void 0 ? "GET" : _d,
107
- _e = _a.query,
108
- query = _e === void 0 ? {} : _e,
109
- _f = _a.headers,
110
- headers = _f === void 0 ? {} : _f,
111
- _g = _a.body,
112
- body = _g === void 0 ? null : _g,
113
- _h = _a.timeout,
114
- timeout = _h === void 0 ? 30 * 1000 : _h,
115
- _j = _a.abort,
116
- abort = _j === void 0 ? new AbortController() : _j,
117
- _k = _a.type,
118
- type = _k === void 0 ? "json" : _k,
119
- others = __rest(_a, ["method", "query", "headers", "body", "timeout", "abort", "type"]);
120
- url = urlJoin(_this.fixOrigin(url), query);
121
- var timer = setTimeout(function () {
122
- return abort.abort();
123
- }, timeout);
124
- var params = __assign({ url: url, method: method, headers: headers, body: method === "GET" ? null : jsonToString(body), timeout: timeout, timer: timer, signal: abort.signal, type: type }, others);
125
- return (_c = (_b = _this.reqFn) === null || _b === void 0 ? void 0 : _b.call(_this, params)) !== null && _c !== void 0 ? _c : params;
126
- };
127
121
  _this.getDataByType = function (type, response) {
128
122
  switch (type) {
129
123
  case "text":
@@ -141,7 +135,43 @@ var RequestBase = function (_super) {
141
135
  }
142
136
  return RequestBase;
143
137
  }(Interceptors);
144
- export { RequestBase };
138
+ var RequestInit = function (_super) {
139
+ __extends(RequestInit, _super);
140
+ function RequestInit(origin) {
141
+ var _this = _super.call(this, origin) || this;
142
+ _this.initDefaultParams = function (url, _a) {
143
+ var _b = _a.method,
144
+ method = _b === void 0 ? "GET" : _b,
145
+ _c = _a.query,
146
+ query = _c === void 0 ? {} : _c,
147
+ _d = _a.headers,
148
+ headers = _d === void 0 ? {} : _d,
149
+ _e = _a.body,
150
+ body = _e === void 0 ? null : _e,
151
+ _f = _a.timeout,
152
+ timeout = _f === void 0 ? 30 * 1000 : _f,
153
+ _g = _a.controller,
154
+ controller = _g === void 0 ? new AbortController() : _g,
155
+ _h = _a.type,
156
+ type = _h === void 0 ? "json" : _h,
157
+ others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
158
+ return __assign({ url: urlJoin(_this.fixOrigin(url), query), method: method, headers: headers, body: method === "GET" ? null : jsonToString(body), timeout: timeout, signal: controller === null || controller === void 0 ? void 0 : controller.signal, controller: controller, type: type, timer: null }, others);
159
+ };
160
+ _this.initFetchParams = function (url, opts) {
161
+ var _a, _b;
162
+ var params = _this.initAbort(_this.initDefaultParams(url, opts));
163
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
164
+ };
165
+ _this.initHttpParams = function (url, opts) {
166
+ var _a, _b;
167
+ var params = _this.initAbort(_this.initDefaultParams(url, opts));
168
+ var options = parse(params.url, true);
169
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : params;
170
+ };
171
+ return _this;
172
+ }
173
+ return RequestInit;
174
+ }(RequestBase);
145
175
  var Request = function (_super) {
146
176
  __extends(Request, _super);
147
177
  function Request(origin) {
@@ -154,29 +184,47 @@ var Request = function (_super) {
154
184
  var _b = _this.initFetchParams(_url, _opts),
155
185
  url = _b.url,
156
186
  opts = __rest(_b, ["url"]);
157
- var signal = opts.signal,
158
- timer = opts.timer;
159
- var errorFn = function (err) {
160
- var _a, _b;return reject((_b = (_a = _this.errFn) === null || _a === void 0 ? void 0 : _a.call(_this, err)) !== null && _b !== void 0 ? _b : err);
161
- };
162
- signal.addEventListener('abort', errorFn);
187
+ var signal = opts.signal;
188
+ signal.addEventListener('abort', function () {
189
+ return _this.errorFn(reject);
190
+ });
163
191
  fetch(url, opts).then(function (response) {
164
192
  if (response.status >= 200 && response.status < 300) {
165
193
  return _this.getDataByType(opts.type, response);
166
194
  }
167
- return errorFn(response.statusText);
195
+ return _this.errorFn(reject);
168
196
  }).then(function (res) {
169
197
  var _a, _b;return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, res)) !== null && _b !== void 0 ? _b : res);
170
- }).catch(errorFn).finally(function () {
171
- return clearTimeout(timer);
198
+ }).catch(_this.errorFn(reject)).finally(function () {
199
+ return _this.clearTimer(opts);
172
200
  });
173
201
  return promise;
174
202
  };
175
- _this.http = function (url, opts) {
203
+ _this.http = function (_url, _opts) {
176
204
  var _a = defer(),
177
205
  promise = _a.promise,
178
206
  resolve = _a.resolve,
179
207
  reject = _a.reject;
208
+ var params = _this.initHttpParams(_url, _opts);
209
+ var signal = params.signal;
210
+ var req = request(params, function (response) {
211
+ if (response.statusCode >= 200 && response.statusCode < 300) {
212
+ var data_1 = "";
213
+ response.setEncoding('utf8');
214
+ response.on('data', function (chunk) {
215
+ return data_1 += chunk;
216
+ });
217
+ return response.on("end", function () {
218
+ var _a, _b;return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, data_1)) !== null && _b !== void 0 ? _b : data_1);
219
+ });
220
+ }
221
+ return _this.errorFn(reject)(response === null || response === void 0 ? void 0 : response.statusMessage);
222
+ });
223
+ signal.addEventListener('abort', function () {
224
+ return _this.errorFn(reject)(req.destroy(new Error('request timeout')));
225
+ });
226
+ req.on('error', _this.errorFn(reject));
227
+ req.end();
180
228
  return promise;
181
229
  };
182
230
  _this.GET = function (url, query, _, opts) {
@@ -203,15 +251,27 @@ var Request = function (_super) {
203
251
  }
204
252
  return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
205
253
  };
206
- _this.OPTION = function (url, query, body, opts) {
254
+ _this.OPTIONS = function (url, query, body, opts) {
207
255
  if (query === void 0) {
208
256
  query = {};
209
257
  }
210
- return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
258
+ return _this.request(url, __assign({ query: query, method: "OPTIONS", body: body }, opts));
259
+ };
260
+ _this.HEAD = function (url, query, body, opts) {
261
+ if (query === void 0) {
262
+ query = {};
263
+ }
264
+ return _this.request(url, __assign({ query: query, method: "HEAD", body: body }, opts));
265
+ };
266
+ _this.PATCH = function (url, query, body, opts) {
267
+ if (query === void 0) {
268
+ query = {};
269
+ }
270
+ return _this.request(url, __assign({ query: query, method: "PATCH", body: body }, opts));
211
271
  };
212
272
  _this.request = _this.requestType();
213
273
  return _this;
214
274
  }
215
275
  return Request;
216
- }(RequestBase);
276
+ }(RequestInit);
217
277
  export { Request };
@@ -16,7 +16,7 @@ export declare type IUrlSplit = (url: string) => IObject<any>;
16
16
  export declare type IUrlJoin = (url: string, query: object) => string;
17
17
  export declare type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
18
18
  export declare type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
19
- export declare type IGetValue = <T>(object: IObject<T> | IObject<T>[IKey], key: string, defaultValue?: any) => IObject<T>[IKey];
19
+ export declare type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U;
20
20
  export declare type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>;
21
21
  export declare type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U;
22
22
  export declare type IEnumInversion = (target: IObject<string>) => IObject<string>;
@@ -48,18 +48,22 @@ export declare type IStopDefault = (e: Event) => void;
48
48
  export declare type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
49
49
  export declare type IDispatchEvent = <T extends Document>(ele: T, data: any) => void;
50
50
  export declare type IRequestParams<T> = T | IObject<any> | null;
51
+ export declare type IUrl = string;
52
+ export declare type IEnv = 'Window' | 'Node';
51
53
  export declare type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
52
54
  export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION";
53
55
  export declare type IRequestBody = IRequestParams<BodyInit>;
54
56
  export declare type IRequestHeaders = IRequestParams<HeadersInit>;
55
- export declare type IRequestFn = (url: string, params: IObject<any>, body: IRequestBody, opts: IRequestOptions) => Promise<any>;
57
+ export declare type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
58
+ export declare type IRequestFn = (url?: IUrl, params?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>;
56
59
  export declare type IRequestOptions = {
57
60
  method?: IRequestMethods;
58
61
  query?: IRequestParams<IObject<any>>;
59
62
  body?: IRequestBody;
60
63
  headers?: IRequestHeaders;
61
- abort?: AbortController;
64
+ controller?: AbortController;
62
65
  timeout?: number;
66
+ timer?: number | unknown | null;
63
67
  [key: string]: any;
64
68
  };
65
69
  export declare type IInterceptors = {
@@ -69,21 +73,30 @@ export declare type IInterceptors = {
69
73
  get errFn(): Function;
70
74
  };
71
75
  export declare type IRequestBase = {
72
- origin: string;
76
+ readonly origin: string;
77
+ chackUrl: (url: IUrl) => boolean;
78
+ envDesc: () => IEnv;
79
+ errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R;
80
+ clearTimer: (opts: IRequestOptions) => void;
81
+ initAbort: <T = IRequestOptions>(opts: T) => T;
82
+ requestType: () => IRequestBaseFn;
73
83
  fixOrigin: (fixStr: string) => string;
74
- envDesc: () => "Window" | "Node";
75
- chackUrl: (url: string) => boolean;
76
- fetch: (url: string, opts: IRequestOptions) => Promise<any>;
77
- http: (url: string, opts: IRequestOptions) => Promise<any>;
78
- initFetchParams: (url: string, opts: IRequestOptions) => any;
84
+ fetch: IRequestBaseFn;
85
+ http: IRequestBaseFn;
79
86
  getDataByType: (type: IDataType, response: Response) => Promise<any>;
80
87
  };
88
+ export declare type IRequestInit = {
89
+ initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
90
+ initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
91
+ initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
92
+ };
81
93
  export declare type IRequest = {
82
- request: Function;
83
94
  GET: IRequestFn;
84
95
  POST: IRequestFn;
85
96
  DELETE: IRequestFn;
86
97
  PUT: IRequestFn;
87
- OPTION: IRequestFn;
98
+ OPTIONS: IRequestFn;
99
+ HEAD: IRequestFn;
100
+ PATCH: IRequestFn;
88
101
  } & IRequestBase;
89
102
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-lib-js",
3
- "version": "1.0.5",
3
+ "version": "1.0.8",
4
4
  "description": "JavaScript工具函数,封装的一些常用的js函数",
5
5
  "main": "./dist/common/index.js",
6
6
  "types": "./dist/common/index.d.ts",
@@ -10,10 +10,11 @@
10
10
  "require": "./dist/common/index.js"
11
11
  },
12
12
  "scripts": {
13
- "debug": "start cmd /k pnpm run build:hot & pnpm run node:hot",
14
- "node:hot": "nodemon ./dist/common/index.js",
13
+ "debug:esm": "start cmd /k pnpm run build:hot:esm",
14
+ "debug:node": "start cmd /k pnpm run build:hot:node & pnpm run node:hot",
15
+ "node:hot": "nodemon ./example/server.js",
15
16
  "build:hot:esm": "tsc -p tsconfig.es.json -w",
16
- "build:hot": "tsc -p tsconfig.json -w",
17
+ "build:hot:node": "tsc -p tsconfig.json -w",
17
18
  "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
18
19
  "tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
19
20
  "publish": "pnpm run build && pnpm publish",
@@ -25,13 +26,18 @@
25
26
  "type": "git",
26
27
  "url": "https://gitee.com/DieHunter/utils-lib-js.git"
27
28
  },
28
- "keywords": ["utils", "tools", "lib"],
29
+ "keywords": [
30
+ "utils",
31
+ "tools",
32
+ "lib"
33
+ ],
29
34
  "author": "",
30
35
  "license": "ISC",
31
36
  "dependencies": {
32
37
  "event-message-center": "^1.0.16"
33
38
  },
34
39
  "devDependencies": {
40
+ "@types/node": "^18.7.1",
35
41
  "babel-cli": "^6.26.0"
36
42
  }
37
43
  }
package/index.html DELETED
@@ -1,37 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
- <title>Document</title>
8
- </head>
9
- <body>
10
- <script type="module">
11
- import { Request } from "./dist/esm/request.js";
12
- const resource = new Request("http://127.0.0.1:1024");
13
- resource
14
- .use("request", (params) => {
15
- return params;
16
- })
17
- .use("response", (params) => {
18
- return params;
19
- })
20
- .use("error", (error) => {
21
- console.log(error)
22
- return error;
23
- });
24
- resource
25
- .GET(
26
- "/getList",
27
- {
28
- name: "hello",
29
- age: 22,
30
- },
31
- {},
32
- {}
33
- )
34
- .then((res) => console.log(res));
35
- </script>
36
- </body>
37
- </html>