keq 2.8.4 → 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.
Files changed (26) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/esm/src/is/is-array-buffer.d.ts +1 -0
  3. package/dist/esm/src/is/is-array-buffer.js +3 -0
  4. package/dist/esm/src/is/is-readable-stream.d.ts +1 -0
  5. package/dist/esm/src/is/is-readable-stream.js +3 -0
  6. package/dist/esm/src/keq.js +10 -5
  7. package/dist/esm/src/middlewares/fetch-arguments-middleware.js +28 -19
  8. package/dist/esm/src/types/keq-context-request.d.ts +1 -1
  9. package/dist/esm/src/util/merge-keq-request-body.d.ts +2 -0
  10. package/dist/esm/src/util/{assign-keq-request-body.js → merge-keq-request-body.js} +25 -16
  11. package/dist/umd/src/is/is-array-buffer.d.ts +1 -0
  12. package/dist/umd/src/is/is-array-buffer.js +17 -0
  13. package/dist/umd/src/is/is-readable-stream.d.ts +1 -0
  14. package/dist/umd/src/is/is-readable-stream.js +17 -0
  15. package/dist/umd/src/keq.js +11 -6
  16. package/dist/umd/src/middlewares/fetch-arguments-middleware.js +29 -20
  17. package/dist/umd/src/types/keq-context-request.d.ts +1 -1
  18. package/dist/umd/src/util/merge-keq-request-body.d.ts +2 -0
  19. package/dist/umd/src/util/{assign-keq-request-body.js → merge-keq-request-body.js} +28 -19
  20. package/package.json +6 -6
  21. package/dist/esm/src/exception/overwrite-array-body.exception.d.ts +0 -4
  22. package/dist/esm/src/exception/overwrite-array-body.exception.js +0 -6
  23. package/dist/esm/src/util/assign-keq-request-body.d.ts +0 -2
  24. package/dist/umd/src/exception/overwrite-array-body.exception.d.ts +0 -4
  25. package/dist/umd/src/exception/overwrite-array-body.exception.js +0 -20
  26. package/dist/umd/src/util/assign-keq-request-body.d.ts +0 -2
package/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
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
+
13
+ ## [2.8.5](https://github.com/keq-request/keq/compare/v2.8.4...v2.8.5) (2024-10-25)
14
+
15
+
16
+ ### Performance Improvements
17
+
18
+ * support send binary body ([4f17198](https://github.com/keq-request/keq/commit/4f17198915e46bfb08a6900c2b784c5299164c2f))
19
+
5
20
  ## [2.8.4](https://github.com/keq-request/keq/compare/v2.8.3...v2.8.4) (2024-10-20)
6
21
 
7
22
 
@@ -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
+ }
@@ -8,10 +8,12 @@ import { isFormData } from './is/is-form-data.js';
8
8
  import { isHeaders } from './is/is-headers.js';
9
9
  import { isBuffer } from './is/is-buffer.js';
10
10
  import { isUrlSearchParams } from './is/is-url-search-params.js';
11
- import { assignKeqRequestBody } from './util/assign-keq-request-body.js';
11
+ 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
  */
@@ -117,13 +119,16 @@ export class Keq extends Core {
117
119
  void this.type(contentType);
118
120
  }
119
121
  send(value) {
120
- this.requestContext.body = assignKeqRequestBody(this.requestContext.body, value);
122
+ this.requestContext.body = mergeKeqRequestBody(this.requestContext.body, value);
121
123
  if (isUrlSearchParams(value)) {
122
124
  this.setTypeIfEmpty('form');
123
125
  }
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
  }
@@ -131,10 +136,10 @@ export class Keq extends Core {
131
136
  }
132
137
  field(arg1, arg2) {
133
138
  if (typeof arg1 === 'object') {
134
- this.requestContext.body = assignKeqRequestBody(this.requestContext.body, arg1);
139
+ this.requestContext.body = mergeKeqRequestBody(this.requestContext.body, arg1);
135
140
  }
136
141
  else if (arg2) {
137
- this.requestContext.body = assignKeqRequestBody(this.requestContext.body, { [arg1]: arg2 });
142
+ this.requestContext.body = mergeKeqRequestBody(this.requestContext.body, { [arg1]: arg2 });
138
143
  }
139
144
  else {
140
145
  throw new InvalidArgumentsExceptions();
@@ -160,7 +165,7 @@ export class Keq extends Core {
160
165
  else {
161
166
  throw new InvalidArgumentsExceptions();
162
167
  }
163
- this.requestContext.body = assignKeqRequestBody(this.requestContext.body, { [key]: file });
168
+ this.requestContext.body = mergeKeqRequestBody(this.requestContext.body, { [key]: file });
164
169
  this.setTypeIfEmpty('form-data');
165
170
  return this;
166
171
  }
@@ -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,5 +1,5 @@
1
1
  export type KeqContextRequestMethod = 'get' | 'post' | 'put' | 'delete' | 'head' | 'patch' | 'options';
2
- export type KeqContextRequestBody = object | Array<any> | string | undefined;
2
+ export type KeqContextRequestBody = BodyInit | object | Array<any> | undefined;
3
3
  export interface KeqContextRequest {
4
4
  url: URL;
5
5
  routeParams: Record<string, string>;
@@ -0,0 +1,2 @@
1
+ import type { KeqContextRequestBody } from '../types/keq-context-request.js';
2
+ export declare function mergeKeqRequestBody(left: KeqContextRequestBody, right: KeqContextRequestBody): KeqContextRequestBody;
@@ -1,23 +1,31 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
1
+ import { isBuffer } from "../is/is-buffer.js";
2
2
  import { Exception } from '../exception/exception.js';
3
- import { OverwriteArrayBodyException } from '../exception/overwrite-array-body.exception.js';
4
3
  import { isFormData } from '../is/is-form-data.js';
5
4
  import { isUrlSearchParams } from '../is/is-url-search-params.js';
6
- export function assignKeqRequestBody(left, right) {
7
- if (Array.isArray(left)) {
8
- throw new OverwriteArrayBodyException();
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";
8
+ export function mergeKeqRequestBody(left, right) {
9
+ if (right === undefined)
10
+ return left;
11
+ if (typeof right === 'number') {
12
+ throw new TypeError('Not support number type');
9
13
  }
10
- if (Array.isArray(right) && left) {
11
- throw new Exception(`Cannot overwrite body(${typeof right}) with array`);
12
- }
13
- if (typeof right === 'string' && left) {
14
- throw new Exception(`Cannot overwrite body(${typeof left}) with string`);
15
- }
16
- if (Array.isArray(right)) {
17
- return [...right];
18
- }
19
- if (typeof right === 'string') {
20
- return right;
14
+ if (left === null ||
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) ||
24
+ Array.isArray(left) ||
25
+ Array.isArray(right) ||
26
+ (typeof left !== 'object' && left !== undefined) ||
27
+ typeof right !== 'object') {
28
+ return Array.isArray(right) ? [...right] : right;
21
29
  }
22
30
  const result = left || {};
23
31
  if (isUrlSearchParams(right)) {
@@ -45,6 +53,7 @@ export function assignKeqRequestBody(left, right) {
45
53
  }
46
54
  }
47
55
  else if (typeof right === 'object') {
56
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
57
  for (const key in right) {
49
58
  result[key] = right[key];
50
59
  }
@@ -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/assign-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";
@@ -20,10 +20,12 @@
20
20
  const is_headers_js_1 = require("./is/is-headers.js");
21
21
  const is_buffer_js_1 = require("./is/is-buffer.js");
22
22
  const is_url_search_params_js_1 = require("./is/is-url-search-params.js");
23
- const assign_keq_request_body_js_1 = require("./util/assign-keq-request-body.js");
23
+ const merge_keq_request_body_js_1 = require("./util/merge-keq-request-body.js");
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
  */
@@ -129,13 +131,16 @@
129
131
  void this.type(contentType);
130
132
  }
131
133
  send(value) {
132
- this.requestContext.body = (0, assign_keq_request_body_js_1.assignKeqRequestBody)(this.requestContext.body, value);
134
+ this.requestContext.body = (0, merge_keq_request_body_js_1.mergeKeqRequestBody)(this.requestContext.body, value);
133
135
  if ((0, is_url_search_params_js_1.isUrlSearchParams)(value)) {
134
136
  this.setTypeIfEmpty('form');
135
137
  }
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
  }
@@ -143,10 +148,10 @@
143
148
  }
144
149
  field(arg1, arg2) {
145
150
  if (typeof arg1 === 'object') {
146
- this.requestContext.body = (0, assign_keq_request_body_js_1.assignKeqRequestBody)(this.requestContext.body, arg1);
151
+ this.requestContext.body = (0, merge_keq_request_body_js_1.mergeKeqRequestBody)(this.requestContext.body, arg1);
147
152
  }
148
153
  else if (arg2) {
149
- this.requestContext.body = (0, assign_keq_request_body_js_1.assignKeqRequestBody)(this.requestContext.body, { [arg1]: arg2 });
154
+ this.requestContext.body = (0, merge_keq_request_body_js_1.mergeKeqRequestBody)(this.requestContext.body, { [arg1]: arg2 });
150
155
  }
151
156
  else {
152
157
  throw new invalid_arguments_exception_js_1.InvalidArgumentsExceptions();
@@ -172,7 +177,7 @@
172
177
  else {
173
178
  throw new invalid_arguments_exception_js_1.InvalidArgumentsExceptions();
174
179
  }
175
- this.requestContext.body = (0, assign_keq_request_body_js_1.assignKeqRequestBody)(this.requestContext.body, { [key]: file });
180
+ this.requestContext.body = (0, merge_keq_request_body_js_1.mergeKeqRequestBody)(this.requestContext.body, { [key]: file });
176
181
  this.setTypeIfEmpty('form-data');
177
182
  return this;
178
183
  }
@@ -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) {
@@ -1,5 +1,5 @@
1
1
  export type KeqContextRequestMethod = 'get' | 'post' | 'put' | 'delete' | 'head' | 'patch' | 'options';
2
- export type KeqContextRequestBody = object | Array<any> | string | undefined;
2
+ export type KeqContextRequestBody = BodyInit | object | Array<any> | undefined;
3
3
  export interface KeqContextRequest {
4
4
  url: URL;
5
5
  routeParams: Record<string, string>;
@@ -0,0 +1,2 @@
1
+ import type { KeqContextRequestBody } from '../types/keq-context-request.js';
2
+ export declare function mergeKeqRequestBody(left: KeqContextRequestBody, right: KeqContextRequestBody): KeqContextRequestBody;
@@ -4,32 +4,40 @@
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", "../exception/overwrite-array-body.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
- exports.assignKeqRequestBody = void 0;
13
- /* eslint-disable @typescript-eslint/no-explicit-any */
12
+ exports.mergeKeqRequestBody = void 0;
13
+ const is_buffer_js_1 = require("../is/is-buffer.js");
14
14
  const exception_js_1 = require("../exception/exception.js");
15
- const overwrite_array_body_exception_js_1 = require("../exception/overwrite-array-body.exception.js");
16
15
  const is_form_data_js_1 = require("../is/is-form-data.js");
17
16
  const is_url_search_params_js_1 = require("../is/is-url-search-params.js");
18
- function assignKeqRequestBody(left, right) {
19
- if (Array.isArray(left)) {
20
- throw new overwrite_array_body_exception_js_1.OverwriteArrayBodyException();
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");
20
+ function mergeKeqRequestBody(left, right) {
21
+ if (right === undefined)
22
+ return left;
23
+ if (typeof right === 'number') {
24
+ throw new TypeError('Not support number type');
21
25
  }
22
- if (Array.isArray(right) && left) {
23
- throw new exception_js_1.Exception(`Cannot overwrite body(${typeof right}) with array`);
24
- }
25
- if (typeof right === 'string' && left) {
26
- throw new exception_js_1.Exception(`Cannot overwrite body(${typeof left}) with string`);
27
- }
28
- if (Array.isArray(right)) {
29
- return [...right];
30
- }
31
- if (typeof right === 'string') {
32
- return right;
26
+ if (left === null ||
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) ||
36
+ Array.isArray(left) ||
37
+ Array.isArray(right) ||
38
+ (typeof left !== 'object' && left !== undefined) ||
39
+ typeof right !== 'object') {
40
+ return Array.isArray(right) ? [...right] : right;
33
41
  }
34
42
  const result = left || {};
35
43
  if ((0, is_url_search_params_js_1.isUrlSearchParams)(right)) {
@@ -57,6 +65,7 @@
57
65
  }
58
66
  }
59
67
  else if (typeof right === 'object') {
68
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
69
  for (const key in right) {
61
70
  result[key] = right[key];
62
71
  }
@@ -66,5 +75,5 @@
66
75
  }
67
76
  return result;
68
77
  }
69
- exports.assignKeqRequestBody = assignKeqRequestBody;
78
+ exports.mergeKeqRequestBody = mergeKeqRequestBody;
70
79
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keq",
3
- "version": "2.8.4",
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",
@@ -49,14 +49,14 @@
49
49
  },
50
50
  "devDependencies": {
51
51
  "@buka/eslint-config": "^2.1.1",
52
- "@commitlint/cli": "^19.3.0",
53
- "@commitlint/config-conventional": "^19.2.2",
52
+ "@commitlint/cli": "^19.5.0",
53
+ "@commitlint/config-conventional": "^19.5.0",
54
54
  "@jest/globals": "^29.7.0",
55
55
  "@types/clone": "^2.1.4",
56
56
  "@types/minimatch": "^5.1.2",
57
57
  "@types/node": "^20.14.1",
58
- "eslint": "^9.10.0",
59
- "husky": "^9.0.11",
58
+ "eslint": "^9.13.0",
59
+ "husky": "^9.1.6",
60
60
  "is-ci": "^3.0.1",
61
61
  "jest": "^29.7.0",
62
62
  "jest-environment-jsdom": "^29.7.0",
@@ -66,7 +66,7 @@
66
66
  "ts-node": "^10.9.2",
67
67
  "ts-patch": "^3.2.0",
68
68
  "typescript": "5.4.5",
69
- "typescript-transform-paths": "^3.4.7"
69
+ "typescript-transform-paths": "^3.5.1"
70
70
  },
71
71
  "packageManager": "pnpm@9.12.2",
72
72
  "engines": {
@@ -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,2 +0,0 @@
1
- import type { KeqContextRequestBody } from '../types/keq-context-request.js';
2
- export declare function assignKeqRequestBody(left: KeqContextRequestBody, right: object | Array<any> | FormData | URLSearchParams | string): KeqContextRequestBody;
@@ -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
- });
@@ -1,2 +0,0 @@
1
- import type { KeqContextRequestBody } from '../types/keq-context-request.js';
2
- export declare function assignKeqRequestBody(left: KeqContextRequestBody, right: object | Array<any> | FormData | URLSearchParams | string): KeqContextRequestBody;