@modern-js/create-request 2.15.0 → 2.17.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/cjs/browser.js +27 -43
- package/dist/cjs/handleRes.js +11 -29
- package/dist/cjs/node.js +36 -48
- package/dist/cjs/types.js +4 -15
- package/dist/esm/browser.js +249 -234
- package/dist/esm/handleRes.js +181 -173
- package/dist/esm/node.js +131 -123
- package/dist/esm/types.js +1 -1
- package/dist/esm-node/browser.js +5 -7
- package/dist/esm-node/handleRes.js +5 -7
- package/dist/esm-node/node.js +10 -8
- package/dist/esm-node/types.js +1 -0
- package/package.json +10 -6
package/dist/cjs/browser.js
CHANGED
|
@@ -1,42 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
8
6
|
for (var name in all)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var browser_exports = {};
|
|
29
|
-
__export(browser_exports, {
|
|
7
|
+
Object.defineProperty(target, name, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: all[name]
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
_export(exports, {
|
|
30
13
|
configure: () => configure,
|
|
31
14
|
createRequest: () => createRequest
|
|
32
15
|
});
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
16
|
+
const _pathtoregexp = require("path-to-regexp");
|
|
17
|
+
const _querystring = /* @__PURE__ */ _interop_require_default(require("query-string"));
|
|
18
|
+
const _handleRes = require("./handleRes");
|
|
19
|
+
function _interop_require_default(obj) {
|
|
20
|
+
return obj && obj.__esModule ? obj : {
|
|
21
|
+
default: obj
|
|
22
|
+
};
|
|
23
|
+
}
|
|
37
24
|
let realRequest;
|
|
38
25
|
let realAllowedHeaders;
|
|
39
|
-
const originFetch = (...params) => fetch(...params).then(
|
|
26
|
+
const originFetch = (...params) => fetch(...params).then(_handleRes.handleRes);
|
|
40
27
|
const configure = (options) => {
|
|
41
28
|
const { request, interceptor, allowedHeaders } = options;
|
|
42
29
|
realRequest = request || originFetch;
|
|
@@ -47,10 +34,12 @@ const configure = (options) => {
|
|
|
47
34
|
realAllowedHeaders = allowedHeaders;
|
|
48
35
|
}
|
|
49
36
|
};
|
|
50
|
-
const createRequest = (path, method, port, httpMethodDecider = "functionName",
|
|
51
|
-
const getFinalPath = (0,
|
|
37
|
+
const createRequest = (path, method, port, httpMethodDecider = "functionName", fetch1 = originFetch) => {
|
|
38
|
+
const getFinalPath = (0, _pathtoregexp.compile)(path, {
|
|
39
|
+
encode: encodeURIComponent
|
|
40
|
+
});
|
|
52
41
|
const keys = [];
|
|
53
|
-
(0,
|
|
42
|
+
(0, _pathtoregexp.pathToRegexp)(path, keys);
|
|
54
43
|
const sender = async (...args) => {
|
|
55
44
|
const fetcher = realRequest || originFetch;
|
|
56
45
|
let body;
|
|
@@ -79,7 +68,7 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
79
68
|
});
|
|
80
69
|
}
|
|
81
70
|
const finalPath = getFinalPath(payload.params);
|
|
82
|
-
finalURL = payload.query ? `${finalPath}?${
|
|
71
|
+
finalURL = payload.query ? `${finalPath}?${_querystring.default.stringify(payload.query)}` : finalPath;
|
|
83
72
|
headers = payload.headers || {};
|
|
84
73
|
body = payload.data && typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.body;
|
|
85
74
|
if (payload.data) {
|
|
@@ -96,7 +85,7 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
96
85
|
// @ts-expect-error
|
|
97
86
|
// eslint-disable-next-line node/prefer-global/url-search-params,node/no-unsupported-features/node-builtins
|
|
98
87
|
!(payload.formUrlencoded instanceof URLSearchParams)) {
|
|
99
|
-
body =
|
|
88
|
+
body = _querystring.default.stringify(payload.formUrlencoded);
|
|
100
89
|
} else {
|
|
101
90
|
body = payload.formUrlencoded;
|
|
102
91
|
}
|
|
@@ -114,8 +103,3 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
114
103
|
};
|
|
115
104
|
return sender;
|
|
116
105
|
};
|
|
117
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
118
|
-
0 && (module.exports = {
|
|
119
|
-
configure,
|
|
120
|
-
createRequest
|
|
121
|
-
});
|
package/dist/cjs/handleRes.js
CHANGED
|
@@ -1,25 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var handleRes_exports = {};
|
|
19
|
-
__export(handleRes_exports, {
|
|
20
|
-
handleRes: () => handleRes
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "handleRes", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: () => handleRes
|
|
21
8
|
});
|
|
22
|
-
module.exports = __toCommonJS(handleRes_exports);
|
|
23
9
|
const handleRes = async (res) => {
|
|
24
10
|
const contentType = res.headers.get("content-type");
|
|
25
11
|
if (!res.ok) {
|
|
@@ -27,21 +13,17 @@ const handleRes = async (res) => {
|
|
|
27
13
|
res.data = data;
|
|
28
14
|
throw res;
|
|
29
15
|
}
|
|
30
|
-
if ((contentType
|
|
16
|
+
if ((contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/json")) || (contentType === null || contentType === void 0 ? void 0 : contentType.includes("text/json"))) {
|
|
31
17
|
return res.json();
|
|
32
18
|
}
|
|
33
|
-
if ((contentType
|
|
19
|
+
if ((contentType === null || contentType === void 0 ? void 0 : contentType.includes("text/html")) || (contentType === null || contentType === void 0 ? void 0 : contentType.includes("text/plain"))) {
|
|
34
20
|
return res.text();
|
|
35
21
|
}
|
|
36
|
-
if (((contentType
|
|
22
|
+
if (((contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/x-www-form-urlencoded")) || (contentType === null || contentType === void 0 ? void 0 : contentType.includes("multipart/form-data"))) && res instanceof Response) {
|
|
37
23
|
return res.formData();
|
|
38
24
|
}
|
|
39
|
-
if (contentType
|
|
25
|
+
if (contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/octet-stream")) {
|
|
40
26
|
return res.arrayBuffer();
|
|
41
27
|
}
|
|
42
28
|
return res.text();
|
|
43
29
|
};
|
|
44
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
-
0 && (module.exports = {
|
|
46
|
-
handleRes
|
|
47
|
-
});
|
package/dist/cjs/node.js
CHANGED
|
@@ -1,60 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
8
6
|
for (var name in all)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var node_exports = {};
|
|
29
|
-
__export(node_exports, {
|
|
7
|
+
Object.defineProperty(target, name, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: all[name]
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
_export(exports, {
|
|
30
13
|
configure: () => configure,
|
|
31
14
|
createRequest: () => createRequest
|
|
32
15
|
});
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
16
|
+
const _nodefetch = /* @__PURE__ */ _interop_require_default(require("node-fetch"));
|
|
17
|
+
const _pathtoregexp = require("path-to-regexp");
|
|
18
|
+
const _ssr = require("@modern-js/utils/ssr");
|
|
19
|
+
const _querystring = /* @__PURE__ */ _interop_require_default(require("query-string"));
|
|
20
|
+
const _handleRes = require("./handleRes");
|
|
21
|
+
function _interop_require_default(obj) {
|
|
22
|
+
return obj && obj.__esModule ? obj : {
|
|
23
|
+
default: obj
|
|
24
|
+
};
|
|
25
|
+
}
|
|
39
26
|
let realRequest;
|
|
40
27
|
let realAllowedHeaders = [];
|
|
41
|
-
const originFetch = (...params) => (0,
|
|
28
|
+
const originFetch = (...params) => (0, _nodefetch.default)(...params).then(_handleRes.handleRes);
|
|
42
29
|
const configure = (options) => {
|
|
43
30
|
const { request, interceptor, allowedHeaders } = options;
|
|
44
31
|
realRequest = request || originFetch;
|
|
45
32
|
if (interceptor && !request) {
|
|
46
|
-
realRequest = interceptor(
|
|
33
|
+
realRequest = interceptor(_nodefetch.default);
|
|
47
34
|
}
|
|
48
35
|
if (Array.isArray(allowedHeaders)) {
|
|
49
36
|
realAllowedHeaders = allowedHeaders;
|
|
50
37
|
}
|
|
51
38
|
};
|
|
52
|
-
const createRequest = (path, method, port, httpMethodDecider = "functionName", fetch =
|
|
53
|
-
const getFinalPath = (0,
|
|
39
|
+
const createRequest = (path, method, port, httpMethodDecider = "functionName", fetch = _nodefetch.default) => {
|
|
40
|
+
const getFinalPath = (0, _pathtoregexp.compile)(path, {
|
|
41
|
+
encode: encodeURIComponent
|
|
42
|
+
});
|
|
54
43
|
const keys = [];
|
|
55
|
-
(0,
|
|
44
|
+
(0, _pathtoregexp.pathToRegexp)(path, keys);
|
|
56
45
|
const sender = (...args) => {
|
|
57
|
-
const webRequestHeaders = (0,
|
|
46
|
+
const webRequestHeaders = (0, _ssr.useHeaders)();
|
|
58
47
|
let body;
|
|
59
48
|
let headers;
|
|
60
49
|
let url;
|
|
@@ -79,7 +68,7 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
79
68
|
});
|
|
80
69
|
}
|
|
81
70
|
const plainPath = getFinalPath(payload.params);
|
|
82
|
-
const finalPath = payload.query ? `${plainPath}?${
|
|
71
|
+
const finalPath = payload.query ? `${plainPath}?${_querystring.default.stringify(payload.query)}` : plainPath;
|
|
83
72
|
headers = payload.headers || {};
|
|
84
73
|
for (const key of realAllowedHeaders) {
|
|
85
74
|
if (typeof webRequestHeaders[key] !== "undefined") {
|
|
@@ -97,7 +86,7 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
97
86
|
} else if (payload.formUrlencoded) {
|
|
98
87
|
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
99
88
|
if (typeof payload.formUrlencoded === "object") {
|
|
100
|
-
body =
|
|
89
|
+
body = _querystring.default.stringify(payload.formUrlencoded);
|
|
101
90
|
} else {
|
|
102
91
|
body = payload.formUrlencoded;
|
|
103
92
|
}
|
|
@@ -112,12 +101,11 @@ const createRequest = (path, method, port, httpMethodDecider = "functionName", f
|
|
|
112
101
|
body = void 0;
|
|
113
102
|
}
|
|
114
103
|
headers.accept = `application/json,*/*;q=0.8`;
|
|
115
|
-
return fetcher(url, {
|
|
104
|
+
return fetcher(url, {
|
|
105
|
+
method,
|
|
106
|
+
body,
|
|
107
|
+
headers
|
|
108
|
+
});
|
|
116
109
|
};
|
|
117
110
|
return sender;
|
|
118
111
|
};
|
|
119
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
120
|
-
0 && (module.exports = {
|
|
121
|
-
configure,
|
|
122
|
-
createRequest
|
|
123
|
-
});
|
package/dist/cjs/types.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __copyProps = (to, from, except, desc) => {
|
|
6
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
-
for (let key of __getOwnPropNames(from))
|
|
8
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
-
}
|
|
11
|
-
return to;
|
|
12
|
-
};
|
|
13
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
-
var types_exports = {};
|
|
15
|
-
module.exports = __toCommonJS(types_exports);
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|