@modern-js/create-request 1.2.11 → 1.3.0-alpha.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.
@@ -1,5 +1,11 @@
1
- const handleRes = res => {
2
- const contentType = res.headers.get('content-type');
1
+ const handleRes = async res => {
2
+ const contentType = res.headers.get('content-type'); // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
3
+
4
+ if (!res.ok) {
5
+ const data = await res.json();
6
+ res.data = data;
7
+ throw res;
8
+ }
3
9
 
4
10
  if (contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json')) {
5
11
  return res.json();
@@ -5,8 +5,14 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.handleRes = void 0;
7
7
 
8
- const handleRes = res => {
9
- const contentType = res.headers.get('content-type');
8
+ const handleRes = async res => {
9
+ const contentType = res.headers.get('content-type'); // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
10
+
11
+ if (!res.ok) {
12
+ const data = await res.json();
13
+ res.data = data;
14
+ throw res;
15
+ }
10
16
 
11
17
  if (contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json')) {
12
18
  return res.json();
@@ -1,23 +1,74 @@
1
- var handleRes = function handleRes(res) {
2
- var contentType = res.headers.get('content-type');
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
3
 
4
- if (contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json')) {
5
- return res.json();
6
- }
4
+ var handleRes = /*#__PURE__*/function () {
5
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(res) {
6
+ var contentType, data;
7
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
8
+ while (1) {
9
+ switch (_context.prev = _context.next) {
10
+ case 0:
11
+ contentType = res.headers.get('content-type'); // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
7
12
 
8
- if (contentType !== null && contentType !== void 0 && contentType.includes('text/html') || contentType !== null && contentType !== void 0 && contentType.includes('text/plain')) {
9
- return res.text();
10
- }
13
+ if (res.ok) {
14
+ _context.next = 7;
15
+ break;
16
+ }
11
17
 
12
- if ((contentType !== null && contentType !== void 0 && contentType.includes('application/x-www-form-urlencoded') || contentType !== null && contentType !== void 0 && contentType.includes('multipart/form-data')) && res instanceof Response) {
13
- return res.formData();
14
- }
18
+ _context.next = 4;
19
+ return res.json();
15
20
 
16
- if (contentType !== null && contentType !== void 0 && contentType.includes('application/octet-stream')) {
17
- return res.arrayBuffer();
18
- }
21
+ case 4:
22
+ data = _context.sent;
23
+ res.data = data;
24
+ throw res;
19
25
 
20
- return res.text();
21
- };
26
+ case 7:
27
+ if (!(contentType !== null && contentType !== void 0 && contentType.includes('application/json') || contentType !== null && contentType !== void 0 && contentType.includes('text/json'))) {
28
+ _context.next = 9;
29
+ break;
30
+ }
31
+
32
+ return _context.abrupt("return", res.json());
33
+
34
+ case 9:
35
+ if (!(contentType !== null && contentType !== void 0 && contentType.includes('text/html') || contentType !== null && contentType !== void 0 && contentType.includes('text/plain'))) {
36
+ _context.next = 11;
37
+ break;
38
+ }
39
+
40
+ return _context.abrupt("return", res.text());
41
+
42
+ case 11:
43
+ if (!((contentType !== null && contentType !== void 0 && contentType.includes('application/x-www-form-urlencoded') || contentType !== null && contentType !== void 0 && contentType.includes('multipart/form-data')) && res instanceof Response)) {
44
+ _context.next = 13;
45
+ break;
46
+ }
47
+
48
+ return _context.abrupt("return", res.formData());
49
+
50
+ case 13:
51
+ if (!(contentType !== null && contentType !== void 0 && contentType.includes('application/octet-stream'))) {
52
+ _context.next = 15;
53
+ break;
54
+ }
55
+
56
+ return _context.abrupt("return", res.arrayBuffer());
57
+
58
+ case 15:
59
+ return _context.abrupt("return", res.text());
60
+
61
+ case 16:
62
+ case "end":
63
+ return _context.stop();
64
+ }
65
+ }
66
+ }, _callee);
67
+ }));
68
+
69
+ return function handleRes(_x) {
70
+ return _ref.apply(this, arguments);
71
+ };
72
+ }();
22
73
 
23
74
  export { handleRes };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.11",
14
+ "version": "1.3.0-alpha.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -63,8 +63,8 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "@babel/runtime": "^7.18.0",
66
- "@modern-js/plugin-ssr": "^1.4.5",
67
- "@modern-js/utils": "^1.7.6",
66
+ "@modern-js/plugin-ssr": "^1.4.6-alpha.0",
67
+ "@modern-js/utils": "^1.7.9-alpha.0",
68
68
  "node-fetch": "^2.6.1",
69
69
  "path-to-regexp": "^6.2.0",
70
70
  "query-string": "^7.1.1"