keq 2.8.5 → 2.8.6

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/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [2.8.6](https://github.com/keq-request/keq/compare/v2.8.5...v2.8.6) (2024-10-25)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * cannot send blob body ([ba7a14d](https://github.com/keq-request/keq/commit/ba7a14d10090aded2525789f0ef543564ce05434))
11
+ * cannot send readableStream ([1b16142](https://github.com/keq-request/keq/commit/1b16142e84f08a7b5c300ba69f5c0036fdb1c9ef))
12
+
5
13
  ## [2.8.5](https://github.com/keq-request/keq/compare/v2.8.4...v2.8.5) (2024-10-25)
6
14
 
7
15
 
@@ -0,0 +1 @@
1
+ export declare function isArrayBuffer(body: any): body is ArrayBuffer;
@@ -0,0 +1,3 @@
1
+ export function isArrayBuffer(body) {
2
+ return body instanceof ArrayBuffer;
3
+ }
@@ -0,0 +1 @@
1
+ export declare function isReadableStream(body: any): body is ReadableStream;
@@ -0,0 +1,3 @@
1
+ export function isReadableStream(body) {
2
+ return body instanceof ReadableStream;
3
+ }
@@ -12,6 +12,8 @@ import { mergeKeqRequestBody } from './util/merge-keq-request-body.js';
12
12
  import { base64Encode } from './util/base64.js';
13
13
  import { fixContentType } from './util/fix-content-type.js';
14
14
  import { isValidHeaderValue } from './util/is-valid-header-value.js';
15
+ import { isReadableStream } from './is/is-readable-stream.js';
16
+ import { isArrayBuffer } from './is/is-array-buffer.js';
15
17
  /**
16
18
  * @description Keq 扩展 API,人性化的常用的API
17
19
  */
@@ -124,6 +126,9 @@ export class Keq extends Core {
124
126
  else if (isFormData(value)) {
125
127
  this.setTypeIfEmpty('form-data');
126
128
  }
129
+ else if (isBlob(value) || isReadableStream(value) || isArrayBuffer(value)) {
130
+ // don't set content-type
131
+ }
127
132
  else if (typeof value === 'object') {
128
133
  this.setTypeIfEmpty('json');
129
134
  }
@@ -1,6 +1,9 @@
1
1
  import { Exception } from '../exception/exception.js';
2
2
  import { ABORT_PROPERTY } from '../constant.js';
3
3
  import { isBuffer } from "../is/is-buffer.js";
4
+ import { isBlob } from "../is/is-blob.js";
5
+ import { isArrayBuffer } from "../is/is-array-buffer.js";
6
+ import { isReadableStream } from "../is/is-readable-stream.js";
4
7
  function inferContentTypeByBody(body) {
5
8
  if (!body)
6
9
  return 'text/plain';
@@ -12,12 +15,29 @@ function compileBody(ctx) {
12
15
  const request = ctx.request;
13
16
  const body = request.body;
14
17
  const contentType = request.headers.get('Content-Type');
15
- if (contentType === 'application/json' && body) {
18
+ if (body === undefined)
19
+ return;
20
+ if (body === null)
21
+ return 'null';
22
+ if (typeof body === 'string')
23
+ return body;
24
+ if (typeof body === 'number')
25
+ return String(body);
26
+ if (isBuffer(body))
27
+ return body;
28
+ if (isBlob(body))
29
+ return body;
30
+ if (isArrayBuffer(body))
31
+ return body;
32
+ if (isReadableStream(body))
33
+ return body;
34
+ if (contentType === 'application/json') {
16
35
  return typeof body === 'object' ? JSON.stringify(body) : body;
17
36
  }
18
- else if (contentType === 'application/x-www-form-urlencoded' && body) {
19
- if (Array.isArray(body))
20
- return;
37
+ if (contentType === 'application/x-www-form-urlencoded') {
38
+ if (Array.isArray(body)) {
39
+ throw new Exception('application/x-www-form-urlencoded cannot send array');
40
+ }
21
41
  const params = new URLSearchParams();
22
42
  Object.entries(body).map(([key, value]) => {
23
43
  if (Array.isArray(value)) {
@@ -31,13 +51,10 @@ function compileBody(ctx) {
31
51
  });
32
52
  return params;
33
53
  }
34
- else if (contentType === 'multipart/form-data') {
35
- if (Array.isArray(ctx.request.body)) {
54
+ if (contentType === 'multipart/form-data') {
55
+ if (Array.isArray(body)) {
36
56
  throw new Exception('FormData cannot send array');
37
57
  }
38
- if (!ctx.request.body)
39
- return;
40
- const body = ctx.request.body;
41
58
  const form = new FormData();
42
59
  for (const [key, value] of Object.entries(body)) {
43
60
  if (Array.isArray(value)) {
@@ -52,16 +69,8 @@ function compileBody(ctx) {
52
69
  request.headers.delete('content-type');
53
70
  return form;
54
71
  }
55
- if (isBuffer(body))
56
- return body;
57
- if (body === undefined)
58
- return body;
59
- if (body === null)
60
- return 'null';
61
- if (typeof body === 'string')
62
- return body;
63
- if (typeof body === 'number')
64
- return String(body);
72
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
+ return body;
65
74
  }
66
75
  export function fetchArgumentsMiddleware() {
67
76
  return async function fetchArgumentsMiddleware(ctx, next) {
@@ -1,6 +1,10 @@
1
+ import { isBuffer } from "../is/is-buffer.js";
1
2
  import { Exception } from '../exception/exception.js';
2
3
  import { isFormData } from '../is/is-form-data.js';
3
4
  import { isUrlSearchParams } from '../is/is-url-search-params.js';
5
+ import { isArrayBuffer } from "../is/is-array-buffer.js";
6
+ import { isBlob } from "../is/is-blob.js";
7
+ import { isReadableStream } from "../is/is-readable-stream.js";
4
8
  export function mergeKeqRequestBody(left, right) {
5
9
  if (right === undefined)
6
10
  return left;
@@ -9,6 +13,14 @@ export function mergeKeqRequestBody(left, right) {
9
13
  }
10
14
  if (left === null ||
11
15
  right === null ||
16
+ isBuffer(right) ||
17
+ isArrayBuffer(right) ||
18
+ isBlob(right) ||
19
+ isReadableStream(right) ||
20
+ isBuffer(left) ||
21
+ isArrayBuffer(left) ||
22
+ isBlob(left) ||
23
+ isReadableStream(left) ||
12
24
  Array.isArray(left) ||
13
25
  Array.isArray(right) ||
14
26
  (typeof left !== 'object' && left !== undefined) ||
@@ -0,0 +1 @@
1
+ export declare function isArrayBuffer(body: any): body is ArrayBuffer;
@@ -0,0 +1,17 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.isArrayBuffer = void 0;
13
+ function isArrayBuffer(body) {
14
+ return body instanceof ArrayBuffer;
15
+ }
16
+ exports.isArrayBuffer = isArrayBuffer;
17
+ });
@@ -0,0 +1 @@
1
+ export declare function isReadableStream(body: any): body is ReadableStream;
@@ -0,0 +1,17 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.isReadableStream = void 0;
13
+ function isReadableStream(body) {
14
+ return body instanceof ReadableStream;
15
+ }
16
+ exports.isReadableStream = isReadableStream;
17
+ });
@@ -4,7 +4,7 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./core.js", "./exception/exception.js", "./exception/invalid-arguments.exception.js", "./is/is-blob.js", "./is/is-file.js", "./is/is-form-data.js", "./is/is-headers.js", "./is/is-buffer.js", "./is/is-url-search-params.js", "./util/merge-keq-request-body.js", "./util/base64.js", "./util/fix-content-type.js", "./util/is-valid-header-value.js"], factory);
7
+ define(["require", "exports", "./core.js", "./exception/exception.js", "./exception/invalid-arguments.exception.js", "./is/is-blob.js", "./is/is-file.js", "./is/is-form-data.js", "./is/is-headers.js", "./is/is-buffer.js", "./is/is-url-search-params.js", "./util/merge-keq-request-body.js", "./util/base64.js", "./util/fix-content-type.js", "./util/is-valid-header-value.js", "./is/is-readable-stream.js", "./is/is-array-buffer.js"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
@@ -24,6 +24,8 @@
24
24
  const base64_js_1 = require("./util/base64.js");
25
25
  const fix_content_type_js_1 = require("./util/fix-content-type.js");
26
26
  const is_valid_header_value_js_1 = require("./util/is-valid-header-value.js");
27
+ const is_readable_stream_js_1 = require("./is/is-readable-stream.js");
28
+ const is_array_buffer_js_1 = require("./is/is-array-buffer.js");
27
29
  /**
28
30
  * @description Keq 扩展 API,人性化的常用的API
29
31
  */
@@ -136,6 +138,9 @@
136
138
  else if ((0, is_form_data_js_1.isFormData)(value)) {
137
139
  this.setTypeIfEmpty('form-data');
138
140
  }
141
+ else if ((0, is_blob_js_1.isBlob)(value) || (0, is_readable_stream_js_1.isReadableStream)(value) || (0, is_array_buffer_js_1.isArrayBuffer)(value)) {
142
+ // don't set content-type
143
+ }
139
144
  else if (typeof value === 'object') {
140
145
  this.setTypeIfEmpty('json');
141
146
  }
@@ -4,7 +4,7 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "../exception/exception.js", "../constant.js", "../is/is-buffer.js"], factory);
7
+ define(["require", "exports", "../exception/exception.js", "../constant.js", "../is/is-buffer.js", "../is/is-blob.js", "../is/is-array-buffer.js", "../is/is-readable-stream.js"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
@@ -13,6 +13,9 @@
13
13
  const exception_js_1 = require("../exception/exception.js");
14
14
  const constant_js_1 = require("../constant.js");
15
15
  const is_buffer_js_1 = require("../is/is-buffer.js");
16
+ const is_blob_js_1 = require("../is/is-blob.js");
17
+ const is_array_buffer_js_1 = require("../is/is-array-buffer.js");
18
+ const is_readable_stream_js_1 = require("../is/is-readable-stream.js");
16
19
  function inferContentTypeByBody(body) {
17
20
  if (!body)
18
21
  return 'text/plain';
@@ -24,12 +27,29 @@
24
27
  const request = ctx.request;
25
28
  const body = request.body;
26
29
  const contentType = request.headers.get('Content-Type');
27
- if (contentType === 'application/json' && body) {
30
+ if (body === undefined)
31
+ return;
32
+ if (body === null)
33
+ return 'null';
34
+ if (typeof body === 'string')
35
+ return body;
36
+ if (typeof body === 'number')
37
+ return String(body);
38
+ if ((0, is_buffer_js_1.isBuffer)(body))
39
+ return body;
40
+ if ((0, is_blob_js_1.isBlob)(body))
41
+ return body;
42
+ if ((0, is_array_buffer_js_1.isArrayBuffer)(body))
43
+ return body;
44
+ if ((0, is_readable_stream_js_1.isReadableStream)(body))
45
+ return body;
46
+ if (contentType === 'application/json') {
28
47
  return typeof body === 'object' ? JSON.stringify(body) : body;
29
48
  }
30
- else if (contentType === 'application/x-www-form-urlencoded' && body) {
31
- if (Array.isArray(body))
32
- return;
49
+ if (contentType === 'application/x-www-form-urlencoded') {
50
+ if (Array.isArray(body)) {
51
+ throw new exception_js_1.Exception('application/x-www-form-urlencoded cannot send array');
52
+ }
33
53
  const params = new URLSearchParams();
34
54
  Object.entries(body).map(([key, value]) => {
35
55
  if (Array.isArray(value)) {
@@ -43,13 +63,10 @@
43
63
  });
44
64
  return params;
45
65
  }
46
- else if (contentType === 'multipart/form-data') {
47
- if (Array.isArray(ctx.request.body)) {
66
+ if (contentType === 'multipart/form-data') {
67
+ if (Array.isArray(body)) {
48
68
  throw new exception_js_1.Exception('FormData cannot send array');
49
69
  }
50
- if (!ctx.request.body)
51
- return;
52
- const body = ctx.request.body;
53
70
  const form = new FormData();
54
71
  for (const [key, value] of Object.entries(body)) {
55
72
  if (Array.isArray(value)) {
@@ -64,16 +81,8 @@
64
81
  request.headers.delete('content-type');
65
82
  return form;
66
83
  }
67
- if ((0, is_buffer_js_1.isBuffer)(body))
68
- return body;
69
- if (body === undefined)
70
- return body;
71
- if (body === null)
72
- return 'null';
73
- if (typeof body === 'string')
74
- return body;
75
- if (typeof body === 'number')
76
- return String(body);
84
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
+ return body;
77
86
  }
78
87
  function fetchArgumentsMiddleware() {
79
88
  return async function fetchArgumentsMiddleware(ctx, next) {
@@ -4,15 +4,19 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "../exception/exception.js", "../is/is-form-data.js", "../is/is-url-search-params.js"], factory);
7
+ define(["require", "exports", "../is/is-buffer.js", "../exception/exception.js", "../is/is-form-data.js", "../is/is-url-search-params.js", "../is/is-array-buffer.js", "../is/is-blob.js", "../is/is-readable-stream.js"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.mergeKeqRequestBody = void 0;
13
+ const is_buffer_js_1 = require("../is/is-buffer.js");
13
14
  const exception_js_1 = require("../exception/exception.js");
14
15
  const is_form_data_js_1 = require("../is/is-form-data.js");
15
16
  const is_url_search_params_js_1 = require("../is/is-url-search-params.js");
17
+ const is_array_buffer_js_1 = require("../is/is-array-buffer.js");
18
+ const is_blob_js_1 = require("../is/is-blob.js");
19
+ const is_readable_stream_js_1 = require("../is/is-readable-stream.js");
16
20
  function mergeKeqRequestBody(left, right) {
17
21
  if (right === undefined)
18
22
  return left;
@@ -21,6 +25,14 @@
21
25
  }
22
26
  if (left === null ||
23
27
  right === null ||
28
+ (0, is_buffer_js_1.isBuffer)(right) ||
29
+ (0, is_array_buffer_js_1.isArrayBuffer)(right) ||
30
+ (0, is_blob_js_1.isBlob)(right) ||
31
+ (0, is_readable_stream_js_1.isReadableStream)(right) ||
32
+ (0, is_buffer_js_1.isBuffer)(left) ||
33
+ (0, is_array_buffer_js_1.isArrayBuffer)(left) ||
34
+ (0, is_blob_js_1.isBlob)(left) ||
35
+ (0, is_readable_stream_js_1.isReadableStream)(left) ||
24
36
  Array.isArray(left) ||
25
37
  Array.isArray(right) ||
26
38
  (typeof left !== 'object' && left !== undefined) ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keq",
3
- "version": "2.8.5",
3
+ "version": "2.8.6",
4
4
  "description": "Request API write by Typescript for flexibility, readability, and a low learning curve.",
5
5
  "keywords": [
6
6
  "request",
@@ -1,4 +0,0 @@
1
- import { Exception } from './exception.js';
2
- export declare class OverwriteArrayBodyException extends Exception {
3
- constructor();
4
- }
@@ -1,6 +0,0 @@
1
- import { Exception } from './exception.js';
2
- export class OverwriteArrayBodyException extends Exception {
3
- constructor() {
4
- super('Cannot merge or overwrite body, because it has been set as an array. Please use .body(arr) instead');
5
- }
6
- }
@@ -1,4 +0,0 @@
1
- import { Exception } from './exception.js';
2
- export declare class OverwriteArrayBodyException extends Exception {
3
- constructor();
4
- }
@@ -1,20 +0,0 @@
1
- (function (factory) {
2
- if (typeof module === "object" && typeof module.exports === "object") {
3
- var v = factory(require, exports);
4
- if (v !== undefined) module.exports = v;
5
- }
6
- else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./exception.js"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.OverwriteArrayBodyException = void 0;
13
- const exception_js_1 = require("./exception.js");
14
- class OverwriteArrayBodyException extends exception_js_1.Exception {
15
- constructor() {
16
- super('Cannot merge or overwrite body, because it has been set as an array. Please use .body(arr) instead');
17
- }
18
- }
19
- exports.OverwriteArrayBodyException = OverwriteArrayBodyException;
20
- });