keq 1.8.5 → 1.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,13 @@
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
+ ### [1.8.6](https://www.github.com/keq-request/keq/compare/v1.8.5...v1.8.6) (2022-05-26)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * cannot send multiple files ([ec70e66](https://www.github.com/keq-request/keq/commit/ec70e6641b93888257cc69e0c6c426dced801117))
11
+
5
12
  ### [1.8.5](https://www.github.com/keq-request/keq/compare/v1.8.4...v1.8.5) (2022-05-01)
6
13
 
7
14
 
package/es/src/keq.d.ts CHANGED
@@ -28,7 +28,7 @@ export declare class Keq<T> {
28
28
  * Http Basic Authentication
29
29
  */
30
30
  auth(username: string, password: string): Keq<T>;
31
- private appendFormDate;
31
+ private mergeToBody;
32
32
  private setType;
33
33
  /**
34
34
  * set request body
package/es/src/keq.js CHANGED
@@ -11,7 +11,7 @@ import { clone } from "./util/clone";
11
11
  import { Exception, FileExpectedException, OverwriteArrayBodyException, UnknownContentTypeException, } from "./exception";
12
12
  import { sleep, inferContentTypeByBody, fixContentType, getBoundaryByContentType, parseFormData, serializeBody, } from "./util";
13
13
  import { isBlob, isBrowser, isFormData } from './util/is';
14
- import { FormData, Response, Headers, Blob, fetch, btoa, } from "./polyfill";
14
+ import { Response, Headers, File, fetch, btoa, } from "./polyfill";
15
15
  import { matchHost, matchMiddleware, compose } from './middleware';
16
16
  export class Keq {
17
17
  constructor(urlObj, method, middlewares) {
@@ -58,22 +58,19 @@ export class Keq {
58
58
  this.headers.set('Authorization', `Basic ${btoa(`${username}:${password}`)}`);
59
59
  return this;
60
60
  }
61
- appendFormDate(formData) {
61
+ mergeToBody(key, value) {
62
62
  if (!this.body)
63
63
  this.body = {};
64
64
  const body = this.body;
65
- formData.forEach((value, key) => {
66
- if (key in body && Array.isArray(body[key])) {
67
- body[key].push(value);
68
- }
69
- else if (key in body && !Array.isArray(body[key])) {
70
- body[key] = [body[key]];
71
- body[key].push(value);
72
- }
73
- else {
74
- body[key] = value;
75
- }
76
- });
65
+ if (key in body && Array.isArray(body[key])) {
66
+ body[key].push(value);
67
+ }
68
+ else if (key in body && !Array.isArray(body[key])) {
69
+ body[key] = [body[key], value];
70
+ }
71
+ else {
72
+ body[key] = value;
73
+ }
77
74
  }
78
75
  setType(contentType) {
79
76
  if (!this.headers.has('Content-Type'))
@@ -90,7 +87,10 @@ export class Keq {
90
87
  if (isFormData(value)) {
91
88
  if (Array.isArray(this.body))
92
89
  throw new OverwriteArrayBodyException();
93
- this.appendFormDate(value);
90
+ const entries = value.entries();
91
+ for (const [key, value] of entries) {
92
+ this.mergeToBody(key, value);
93
+ }
94
94
  this.setType('form-data');
95
95
  }
96
96
  else if (typeof value === 'object') {
@@ -115,19 +115,17 @@ export class Keq {
115
115
  if (Array.isArray(this.body)) {
116
116
  throw new Exception('Cannot merge or overwrite body. Because it has been set as an array. ');
117
117
  }
118
- const formData = new FormData();
119
118
  if (typeof arg1 === 'object') {
120
119
  for (const key in arg1) {
121
- formData.append(key, arg1[key]);
120
+ this.mergeToBody(key, arg1[key]);
122
121
  }
123
122
  }
124
123
  else if (arg2) {
125
- formData.append(arg1, arg2);
124
+ this.mergeToBody(arg1, arg2);
126
125
  }
127
126
  else {
128
127
  throw new Exception('Need value');
129
128
  }
130
- this.appendFormDate(formData);
131
129
  this.setType('form-data');
132
130
  return this;
133
131
  }
@@ -138,14 +136,12 @@ export class Keq {
138
136
  this.body = {};
139
137
  if (!(isBlob(file) || file instanceof Buffer))
140
138
  throw new FileExpectedException();
141
- const formData = new FormData();
142
139
  if (isBlob(file)) {
143
- formData.set(key, file, arg3);
140
+ this.mergeToBody(key, new File([file], arg3));
144
141
  }
145
142
  else {
146
- formData.set(key, new Blob([file]), arg3);
143
+ this.mergeToBody(key, new File([file], arg3));
147
144
  }
148
- this.appendFormDate(formData);
149
145
  this.setType('form-data');
150
146
  return this;
151
147
  }
@@ -1,12 +1,16 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
1
2
  import { isBlob } from './is';
2
3
  import deepClone from 'clone';
3
4
  import fromEntries from 'object.fromentries';
4
5
  export function clone(obj) {
5
- if (typeof obj === 'object' && !Array.isArray(obj)) {
6
+ if (Array.isArray(obj)) {
7
+ return obj.map(item => isBlob(item) ? item : clone(item));
8
+ }
9
+ else if (typeof obj === 'object' && !Array.isArray(obj)) {
6
10
  const entries = Object.entries(obj)
7
11
  .map(([key, value]) => ([
8
12
  key,
9
- isBlob(value) ? value : deepClone(value),
13
+ isBlob(value) ? value : clone(value),
10
14
  ]));
11
15
  return fromEntries(entries);
12
16
  }
@@ -1 +1 @@
1
- export declare function isFormData(object: any): boolean;
1
+ export declare function isFormData(object: any): object is FormData;
package/lib/src/keq.d.ts CHANGED
@@ -28,7 +28,7 @@ export declare class Keq<T> {
28
28
  * Http Basic Authentication
29
29
  */
30
30
  auth(username: string, password: string): Keq<T>;
31
- private appendFormDate;
31
+ private mergeToBody;
32
32
  private setType;
33
33
  /**
34
34
  * set request body
package/lib/src/keq.js CHANGED
@@ -70,22 +70,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
70
70
  this.headers.set('Authorization', `Basic ${(0, polyfill_1.btoa)(`${username}:${password}`)}`);
71
71
  return this;
72
72
  }
73
- appendFormDate(formData) {
73
+ mergeToBody(key, value) {
74
74
  if (!this.body)
75
75
  this.body = {};
76
76
  const body = this.body;
77
- formData.forEach((value, key) => {
78
- if (key in body && Array.isArray(body[key])) {
79
- body[key].push(value);
80
- }
81
- else if (key in body && !Array.isArray(body[key])) {
82
- body[key] = [body[key]];
83
- body[key].push(value);
84
- }
85
- else {
86
- body[key] = value;
87
- }
88
- });
77
+ if (key in body && Array.isArray(body[key])) {
78
+ body[key].push(value);
79
+ }
80
+ else if (key in body && !Array.isArray(body[key])) {
81
+ body[key] = [body[key], value];
82
+ }
83
+ else {
84
+ body[key] = value;
85
+ }
89
86
  }
90
87
  setType(contentType) {
91
88
  if (!this.headers.has('Content-Type'))
@@ -102,7 +99,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
102
99
  if ((0, is_1.isFormData)(value)) {
103
100
  if (Array.isArray(this.body))
104
101
  throw new exception_1.OverwriteArrayBodyException();
105
- this.appendFormDate(value);
102
+ const entries = value.entries();
103
+ for (const [key, value] of entries) {
104
+ this.mergeToBody(key, value);
105
+ }
106
106
  this.setType('form-data');
107
107
  }
108
108
  else if (typeof value === 'object') {
@@ -127,19 +127,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
127
127
  if (Array.isArray(this.body)) {
128
128
  throw new exception_1.Exception('Cannot merge or overwrite body. Because it has been set as an array. ');
129
129
  }
130
- const formData = new polyfill_1.FormData();
131
130
  if (typeof arg1 === 'object') {
132
131
  for (const key in arg1) {
133
- formData.append(key, arg1[key]);
132
+ this.mergeToBody(key, arg1[key]);
134
133
  }
135
134
  }
136
135
  else if (arg2) {
137
- formData.append(arg1, arg2);
136
+ this.mergeToBody(arg1, arg2);
138
137
  }
139
138
  else {
140
139
  throw new exception_1.Exception('Need value');
141
140
  }
142
- this.appendFormDate(formData);
143
141
  this.setType('form-data');
144
142
  return this;
145
143
  }
@@ -150,14 +148,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
150
148
  this.body = {};
151
149
  if (!((0, is_1.isBlob)(file) || file instanceof Buffer))
152
150
  throw new exception_1.FileExpectedException();
153
- const formData = new polyfill_1.FormData();
154
151
  if ((0, is_1.isBlob)(file)) {
155
- formData.set(key, file, arg3);
152
+ this.mergeToBody(key, new polyfill_1.File([file], arg3));
156
153
  }
157
154
  else {
158
- formData.set(key, new polyfill_1.Blob([file]), arg3);
155
+ this.mergeToBody(key, new polyfill_1.File([file], arg3));
159
156
  }
160
- this.appendFormDate(formData);
161
157
  this.setType('form-data');
162
158
  return this;
163
159
  }
@@ -13,15 +13,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  "use strict";
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.clone = void 0;
16
+ /* eslint-disable @typescript-eslint/no-unsafe-return */
16
17
  const is_1 = require("./is");
17
18
  const clone_1 = __importDefault(require("clone"));
18
19
  const object_fromentries_1 = __importDefault(require("object.fromentries"));
19
20
  function clone(obj) {
20
- if (typeof obj === 'object' && !Array.isArray(obj)) {
21
+ if (Array.isArray(obj)) {
22
+ return obj.map(item => (0, is_1.isBlob)(item) ? item : clone(item));
23
+ }
24
+ else if (typeof obj === 'object' && !Array.isArray(obj)) {
21
25
  const entries = Object.entries(obj)
22
26
  .map(([key, value]) => ([
23
27
  key,
24
- (0, is_1.isBlob)(value) ? value : (0, clone_1.default)(value),
28
+ (0, is_1.isBlob)(value) ? value : clone(value),
25
29
  ]));
26
30
  return (0, object_fromentries_1.default)(entries);
27
31
  }
@@ -1 +1 @@
1
- export declare function isFormData(object: any): boolean;
1
+ export declare function isFormData(object: any): object is FormData;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "keq",
3
3
  "main": "lib/index.js",
4
4
  "module": "es/index.js",
5
- "version": "1.8.5",
5
+ "version": "1.8.6",
6
6
  "license": "MIT",
7
7
  "types": "lib/index.d.ts",
8
8
  "scripts": {