keq 1.7.3 → 1.8.2
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 +22 -0
- package/README.md +25 -7
- package/ava.config.mjs +5 -0
- package/doc/zh-cn/readme.md +26 -6
- package/es/index.d.ts +1 -4
- package/es/index.js +1 -5
- package/es/src/exception/file-expected.exception.d.ts +1 -1
- package/es/src/exception/file-expected.exception.js +1 -1
- package/es/src/exception/index.d.ts +4 -4
- package/es/src/exception/index.js +4 -4
- package/es/src/exception/overwrite-array-body.exception.d.ts +1 -1
- package/es/src/exception/overwrite-array-body.exception.js +1 -1
- package/es/src/exception/unknown-content-type.exception.d.ts +4 -0
- package/es/src/exception/unknown-content-type.exception.js +6 -0
- package/es/src/index.d.ts +6 -6
- package/es/src/index.js +6 -6
- package/es/src/keq-url.js +5 -5
- package/es/src/keq.d.ts +5 -6
- package/es/src/keq.js +23 -22
- package/es/src/mount.js +4 -4
- package/es/src/polyfill.d.ts +7 -6
- package/es/src/polyfill.js +10 -3
- package/es/src/request.d.ts +1 -2
- package/es/src/request.js +2 -44
- package/es/src/types/context.d.ts +4 -4
- package/es/src/types/index.d.ts +10 -10
- package/es/src/types/index.js +10 -10
- package/es/src/types/middleware.d.ts +1 -1
- package/es/src/types/mounter.d.ts +1 -1
- package/es/src/types/options.d.ts +2 -8
- package/es/src/types/request-creator.d.ts +4 -3
- package/es/src/types/serialize-body-fn.d.ts +2 -2
- package/es/src/util/clone.js +1 -1
- package/es/src/util/create-request.d.ts +2 -0
- package/es/src/util/create-request.js +48 -0
- package/es/src/util/get-boundary-by-content-type.js +1 -1
- package/es/src/util/index.d.ts +7 -8
- package/es/src/util/index.js +7 -8
- package/es/src/util/is/index.d.ts +11 -11
- package/es/src/util/is/index.js +11 -11
- package/es/src/util/is/is-blob.d.ts +1 -1
- package/es/src/util/is/is-blob.js +3 -3
- package/es/src/util/is/is-browser.d.ts +1 -1
- package/es/src/util/is/is-browser.js +4 -3
- package/es/src/util/is/is-file.d.ts +1 -1
- package/es/src/util/is/is-file.js +5 -4
- package/es/src/util/is/is-read-stream.js +1 -1
- package/es/src/util/is/is-readable.js +2 -1
- package/es/src/util/is/is-stream.js +3 -3
- package/es/src/util/is/is-whatwg-readable.js +2 -2
- package/es/src/util/parse-form-data.js +11 -5
- package/es/src/util/serialize-body.d.ts +2 -2
- package/es/src/util/serialize-body.js +17 -5
- package/index.ts +0 -4
- package/lib/index.d.ts +1 -4
- package/lib/index.js +1 -5
- package/lib/src/exception/file-expected.exception.d.ts +1 -1
- package/lib/src/exception/index.d.ts +4 -4
- package/lib/src/exception/index.js +2 -2
- package/lib/src/exception/overwrite-array-body.exception.d.ts +1 -1
- package/lib/src/exception/unknown-content-type.exception.d.ts +4 -0
- package/lib/src/exception/{unknow-content-type.exception.js → unknown-content-type.exception.js} +3 -3
- package/lib/src/index.d.ts +6 -6
- package/lib/src/keq-url.js +4 -4
- package/lib/src/keq.d.ts +5 -6
- package/lib/src/keq.js +26 -25
- package/lib/src/mount.js +4 -4
- package/lib/src/polyfill.d.ts +7 -6
- package/lib/src/polyfill.js +12 -8
- package/lib/src/request.d.ts +1 -2
- package/lib/src/request.js +3 -46
- package/lib/src/types/context.d.ts +4 -4
- package/lib/src/types/index.d.ts +10 -10
- package/lib/src/types/middleware.d.ts +1 -1
- package/lib/src/types/mounter.d.ts +1 -1
- package/lib/src/types/options.d.ts +2 -8
- package/lib/src/types/request-creator.d.ts +4 -3
- package/lib/src/types/serialize-body-fn.d.ts +2 -2
- package/lib/src/util/clone.js +2 -2
- package/lib/src/util/create-request.d.ts +2 -0
- package/lib/src/util/create-request.js +62 -0
- package/lib/src/util/get-boundary-by-content-type.js +1 -1
- package/lib/src/util/index.d.ts +7 -8
- package/lib/src/util/index.js +2 -3
- package/lib/src/util/is/index.d.ts +11 -11
- package/lib/src/util/is/is-blob.d.ts +1 -1
- package/lib/src/util/is/is-browser.d.ts +1 -1
- package/lib/src/util/is/is-browser.js +4 -4
- package/lib/src/util/is/is-file.d.ts +1 -1
- package/lib/src/util/is/is-file.js +6 -5
- package/lib/src/util/is/is-readable.js +4 -3
- package/lib/src/util/parse-form-data.js +11 -5
- package/lib/src/util/serialize-body.d.ts +2 -2
- package/lib/src/util/serialize-body.js +18 -6
- package/package.json +25 -26
- package/es/images/logo.svg +0 -1
- package/es/package.json +0 -93
- package/es/src/exception/file-expected.exception.ts +0 -8
- package/es/src/exception/overwrite-array-body.exception.ts +0 -8
- package/es/src/exception/unknow-content-type.exception.d.ts +0 -4
- package/es/src/exception/unknow-content-type.exception.js +0 -6
- package/es/src/exception/unknow-content-type.exception.ts +0 -8
- package/es/src/form-data-node/index.d.ts +0 -246
- package/es/src/form-data-node/index.js +0 -462
- package/es/src/form-data-node/util/boundary.d.ts +0 -5
- package/es/src/form-data-node/util/boundary.js +0 -7
- package/es/src/form-data-node/util/file.d.ts +0 -16
- package/es/src/form-data-node/util/file.js +0 -75
- package/es/src/form-data-node/util/get-length.d.ts +0 -11
- package/es/src/form-data-node/util/get-length.js +0 -43
- package/es/src/form-data-node/util/get-stream-iterator.d.ts +0 -9
- package/es/src/form-data-node/util/get-stream-iterator.js +0 -16
- package/es/src/form-data-node/util/get-type.d.ts +0 -11
- package/es/src/form-data-node/util/get-type.js +0 -27
- package/es/src/form-data-node/util/read-only.d.ts +0 -2
- package/es/src/form-data-node/util/read-only.js +0 -10
- package/es/src/form-data-node/util/readable-stream-iterator.d.ts +0 -9
- package/es/src/form-data-node/util/readable-stream-iterator.js +0 -31
- package/es/src/form-data-node/util/to-file.d.ts +0 -6
- package/es/src/form-data-node/util/to-file.js +0 -24
- package/es/src/util/encode-base64.d.ts +0 -1
- package/es/src/util/encode-base64.js +0 -4
- package/lib/images/logo.svg +0 -1
- package/lib/package.json +0 -93
- package/lib/src/exception/file-expected.exception.ts +0 -8
- package/lib/src/exception/overwrite-array-body.exception.ts +0 -8
- package/lib/src/exception/unknow-content-type.exception.d.ts +0 -4
- package/lib/src/exception/unknow-content-type.exception.ts +0 -8
- package/lib/src/form-data-node/index.d.ts +0 -246
- package/lib/src/form-data-node/index.js +0 -474
- package/lib/src/form-data-node/util/boundary.d.ts +0 -5
- package/lib/src/form-data-node/util/boundary.js +0 -19
- package/lib/src/form-data-node/util/file.d.ts +0 -16
- package/lib/src/form-data-node/util/file.js +0 -87
- package/lib/src/form-data-node/util/get-length.d.ts +0 -11
- package/lib/src/form-data-node/util/get-length.js +0 -55
- package/lib/src/form-data-node/util/get-stream-iterator.d.ts +0 -9
- package/lib/src/form-data-node/util/get-stream-iterator.js +0 -28
- package/lib/src/form-data-node/util/get-type.d.ts +0 -11
- package/lib/src/form-data-node/util/get-type.js +0 -39
- package/lib/src/form-data-node/util/read-only.d.ts +0 -2
- package/lib/src/form-data-node/util/read-only.js +0 -22
- package/lib/src/form-data-node/util/readable-stream-iterator.d.ts +0 -9
- package/lib/src/form-data-node/util/readable-stream-iterator.js +0 -43
- package/lib/src/form-data-node/util/to-file.d.ts +0 -6
- package/lib/src/form-data-node/util/to-file.js +0 -36
- package/lib/src/util/encode-base64.d.ts +0 -1
- package/lib/src/util/encode-base64.js +0 -18
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,28 @@
|
|
|
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.2](https://www.github.com/keq-request/keq/compare/v1.8.1...v1.8.2) (2022-04-26)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* cannot find keq main file ([41908fa](https://www.github.com/keq-request/keq/commit/41908fa47eeb8f54fc018d445c5ef85ffc5ca383))
|
|
11
|
+
|
|
12
|
+
### [1.8.1](https://www.github.com/keq-request/keq/compare/v1.8.0...v1.8.1) (2022-04-25)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* cannot compile by vite ([2727867](https://www.github.com/keq-request/keq/commit/27278676f924428e2598b634577ed8b63a1e935b))
|
|
18
|
+
* wrong return of response.blob() ([ca5a5b5](https://www.github.com/keq-request/keq/commit/ca5a5b5bd64bdde6a476d7ce5e86b9833cd2a43b))
|
|
19
|
+
|
|
20
|
+
## [1.8.0](https://www.github.com/keq-request/keq/compare/v1.7.3...v1.8.0) (2022-03-22)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Features
|
|
24
|
+
|
|
25
|
+
* support custom request instance ([2c05dec](https://www.github.com/keq-request/keq/commit/2c05dec6d779861112507b7365822ecff175dbc3))
|
|
26
|
+
|
|
5
27
|
### [1.7.3](https://www.github.com/keq-request/keq/compare/v1.7.2...v1.7.3) (2022-03-08)
|
|
6
28
|
|
|
7
29
|
|
package/README.md
CHANGED
|
@@ -91,7 +91,7 @@ Add option `resolveWithFullResponse` to get the origin [`Response`][Response MD
|
|
|
91
91
|
import { request } from 'keq'
|
|
92
92
|
|
|
93
93
|
const response = await request
|
|
94
|
-
.get('http://
|
|
94
|
+
.get('http://test.com')
|
|
95
95
|
.option('resolveWithFullResponse')
|
|
96
96
|
|
|
97
97
|
const body = await response.json()
|
|
@@ -311,7 +311,7 @@ No retry by default, invoke `.retry(times[, initialTime[, callback]])` to set r
|
|
|
311
311
|
import { request } from 'keq'
|
|
312
312
|
|
|
313
313
|
await request
|
|
314
|
-
.get('http://
|
|
314
|
+
.get('http://test.com')
|
|
315
315
|
.retry(2, 1000, () => {})
|
|
316
316
|
```
|
|
317
317
|
|
|
@@ -324,7 +324,7 @@ Invoke `.option()` add options.
|
|
|
324
324
|
import { request } from 'keq'
|
|
325
325
|
|
|
326
326
|
await request
|
|
327
|
-
.get('http://
|
|
327
|
+
.get('http://test.com')
|
|
328
328
|
.option('resolveWithFullResponse')
|
|
329
329
|
.option('middlewareOption', 'value')
|
|
330
330
|
```
|
|
@@ -335,7 +335,7 @@ Or as a single object:
|
|
|
335
335
|
import { request } from 'keq'
|
|
336
336
|
|
|
337
337
|
await request
|
|
338
|
-
.get('http://
|
|
338
|
+
.get('http://test.com')
|
|
339
339
|
.options({
|
|
340
340
|
resolveWithFullResponse: true,
|
|
341
341
|
middlewareOption: 'value',
|
|
@@ -390,7 +390,7 @@ request
|
|
|
390
390
|
|
|
391
391
|
// Request Middleware
|
|
392
392
|
await request
|
|
393
|
-
.get('http://
|
|
393
|
+
.get('http://test.com')
|
|
394
394
|
.use(middleware)
|
|
395
395
|
```
|
|
396
396
|
|
|
@@ -416,7 +416,7 @@ Keq's context object has many parameters. The following lists all the built-in c
|
|
|
416
416
|
**Property** | **Type**
|
|
417
417
|
:-----------------------------|:------------------------------------
|
|
418
418
|
`context.request` | Includes request options for Fetch API.
|
|
419
|
-
`context.request.url` | The return type of `url.parse('http://
|
|
419
|
+
`context.request.url` | The return type of `url.parse('http://test.com', true)`.
|
|
420
420
|
`context.request.method` | One of 'get', 'post', 'put', 'patch', 'head', 'delete'.
|
|
421
421
|
`context.request.body` | Object, Array, Stream, Blob or undefined.
|
|
422
422
|
`context.request.headers` | The [`Headers`][Headers MDN] Object.
|
|
@@ -440,6 +440,24 @@ This is the utils used to mount middleware.
|
|
|
440
440
|
`mount.pathname(matcher: string \| Regexp)` | Mount to the pathname that match the `matcher`. `string` can be [`glob`](https://www.npmjs.com/package/picomatch).
|
|
441
441
|
`mount.host(host: string)` | Mount to the Host.
|
|
442
442
|
|
|
443
|
+
### Create Request
|
|
444
|
+
|
|
445
|
+
If you want to create a request instance, you can invoke `request.create()`:
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
```typescript
|
|
449
|
+
import { request } from 'keq'
|
|
450
|
+
|
|
451
|
+
const customRequest = request.create()
|
|
452
|
+
|
|
453
|
+
// Middleware only takes effect on customRequests
|
|
454
|
+
customRequest.use(/** some middleware */)
|
|
455
|
+
|
|
456
|
+
const body = await customRequest
|
|
457
|
+
.get('http://test.com')
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
> The gloabl request instance is created by `request.create()` too.
|
|
443
461
|
|
|
444
462
|
<!-- usage -->
|
|
445
463
|
|
|
@@ -459,7 +477,7 @@ The difference between the two is that when called multiple times.
|
|
|
459
477
|
```javascript
|
|
460
478
|
import { request } from 'keq'
|
|
461
479
|
|
|
462
|
-
const keq = request.get('http://
|
|
480
|
+
const keq = request.get('http://test.com')
|
|
463
481
|
|
|
464
482
|
keq.then(onfulfilled, onrejected)
|
|
465
483
|
// Won't send request, and will use the last request result.
|
package/ava.config.mjs
ADDED
package/doc/zh-cn/readme.md
CHANGED
|
@@ -90,7 +90,7 @@ await request.del('https://example.com/search')
|
|
|
90
90
|
import { request } from 'keq'
|
|
91
91
|
|
|
92
92
|
const response = await request
|
|
93
|
-
.get('http://
|
|
93
|
+
.get('http://test.com')
|
|
94
94
|
.option('resolveWithFullResponse')
|
|
95
95
|
|
|
96
96
|
const body = await response.json()
|
|
@@ -301,7 +301,7 @@ await request
|
|
|
301
301
|
import { request } from 'keq'
|
|
302
302
|
|
|
303
303
|
await request
|
|
304
|
-
.get('http://
|
|
304
|
+
.get('http://test.com')
|
|
305
305
|
.option('resolveWithFullResponse')
|
|
306
306
|
.option('middlewareOption', 'value')
|
|
307
307
|
```
|
|
@@ -312,7 +312,7 @@ await request
|
|
|
312
312
|
import { request } from 'keq'
|
|
313
313
|
|
|
314
314
|
await request
|
|
315
|
-
.get('http://
|
|
315
|
+
.get('http://test.com')
|
|
316
316
|
.options({
|
|
317
317
|
resolveWithFullResponse: true,
|
|
318
318
|
middlewareOption: 'value',
|
|
@@ -353,7 +353,7 @@ request
|
|
|
353
353
|
|
|
354
354
|
// Request Middleware
|
|
355
355
|
await request
|
|
356
|
-
.get('http://
|
|
356
|
+
.get('http://test.com')
|
|
357
357
|
.use(middleware)
|
|
358
358
|
```
|
|
359
359
|
|
|
@@ -379,7 +379,7 @@ Keq的上下文对象有许多可以修改和使用的参数,下面列出了
|
|
|
379
379
|
**Property** | **Type**
|
|
380
380
|
:-----------------------------|:------------------------------------
|
|
381
381
|
`context.request` | 发送fetch请求的参数。
|
|
382
|
-
`context.request.url` | 请求地址,是一个`URL`对象,通过`url.parse('http://
|
|
382
|
+
`context.request.url` | 请求地址,是一个`URL`对象,通过`url.parse('http://test.com', true)`创建。
|
|
383
383
|
`context.request.method` | Request method。
|
|
384
384
|
`context.request.body` | 请求体,根据发送请求的不同,其结构可能是:`Object`(json/form-data/x-www-form-urlencoded), Array(json), Stream(NodeJS发送二进制文件), Blob(浏览器中发送二进制文件), undefined(未携带请求体)中的任意一种。
|
|
385
385
|
`context.request.headers` | 请求头[`Headers`][Headers MDN]对象。
|
|
@@ -409,7 +409,7 @@ Keq的上下文对象有许多可以修改和使用的参数,下面列出了
|
|
|
409
409
|
```javascript
|
|
410
410
|
import { request } from 'keq'
|
|
411
411
|
|
|
412
|
-
const keq = request.get('http://
|
|
412
|
+
const keq = request.get('http://test.com')
|
|
413
413
|
|
|
414
414
|
keq.then(onfulfilled, onrejected)
|
|
415
415
|
// Won't send request, and will use the last request result.
|
|
@@ -425,6 +425,26 @@ keq.end()
|
|
|
425
425
|
|
|
426
426
|
更重要的是,在`NodeJS`里面,`node-fetch`库没有实现`Response`的`.formData()`方法,在`NodeJS`环境中,`Keq`在克隆`ctx.res`后会自动添加`.formData()`实现。
|
|
427
427
|
|
|
428
|
+
### 创建可自定义的请求实例
|
|
429
|
+
|
|
430
|
+
如果你打算自己创建一个请求实例,而不使用全局的`request`实例,可以调用`request.create()`方法:
|
|
431
|
+
|
|
432
|
+
```typescript
|
|
433
|
+
import { request } from 'keq'
|
|
434
|
+
|
|
435
|
+
const customRequest = request.create()
|
|
436
|
+
|
|
437
|
+
// Middleware only takes effect on customRequests
|
|
438
|
+
customRequest.use(/** some middleware */)
|
|
439
|
+
|
|
440
|
+
const body = await customRequest
|
|
441
|
+
.get('http://test.com')
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
> 全局的`request`实例也是调用`request.create()`方法创建的。
|
|
445
|
+
> 因此,自定义的`request`实例也具备全局`request`的所有特性。
|
|
446
|
+
|
|
447
|
+
|
|
428
448
|
|
|
429
449
|
## 更多信息
|
|
430
450
|
|
package/es/index.d.ts
CHANGED
package/es/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from './exception';
|
|
2
|
+
export * from './file-expected.exception';
|
|
3
|
+
export * from './overwrite-array-body.exception';
|
|
4
|
+
export * from './unknown-content-type.exception';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from './exception';
|
|
2
|
+
export * from './file-expected.exception';
|
|
3
|
+
export * from './overwrite-array-body.exception';
|
|
4
|
+
export * from './unknown-content-type.exception';
|
package/es/src/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export { compose } from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
import * as mount from
|
|
1
|
+
export * from './request';
|
|
2
|
+
export * from './types';
|
|
3
|
+
export { compose } from './middleware';
|
|
4
|
+
export * from './keq';
|
|
5
|
+
export * from './polyfill';
|
|
6
|
+
import * as mount from './mount';
|
|
7
7
|
export { mount };
|
package/es/src/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export { compose } from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
import * as mount from
|
|
1
|
+
export * from './request';
|
|
2
|
+
export * from './types';
|
|
3
|
+
export { compose } from './middleware';
|
|
4
|
+
export * from './keq';
|
|
5
|
+
export * from './polyfill';
|
|
6
|
+
import * as mount from './mount';
|
|
7
7
|
export { mount };
|
package/es/src/keq-url.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { URL } from 'whatwg-url';
|
|
2
2
|
import { compile } from 'path-to-regexp';
|
|
3
|
-
import { Exception } from
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
3
|
+
import { Exception } from './exception';
|
|
4
|
+
import { isBrowser } from './util';
|
|
5
|
+
import { clone } from './util/clone';
|
|
6
6
|
export class KeqURL extends URL {
|
|
7
|
-
constructor(url, base = isBrowser
|
|
7
|
+
constructor(url, base = isBrowser ? window.location.origin : 'http://localhost') {
|
|
8
8
|
super(url, base);
|
|
9
9
|
this.params = {};
|
|
10
10
|
}
|
|
@@ -73,7 +73,7 @@ export class KeqURL extends URL {
|
|
|
73
73
|
}
|
|
74
74
|
clone() {
|
|
75
75
|
const uri = new KeqURL(this.href);
|
|
76
|
-
uri.params =
|
|
76
|
+
uri.params = clone(this.params);
|
|
77
77
|
return uri;
|
|
78
78
|
}
|
|
79
79
|
}
|
package/es/src/keq.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { SerializeBodyFn, RequestMethod, Options, BuildInOptions, OptionsWithFullResponse, OptionsWithoutFullResponse, Middleware, MiddlewareMatcher, ShorthandContentType,
|
|
3
|
-
import { KeqURL } from
|
|
4
|
-
import { Stream } from 'stream';
|
|
2
|
+
import { SerializeBodyFn, RequestMethod, Options, BuildInOptions, OptionsWithFullResponse, OptionsWithoutFullResponse, Middleware, MiddlewareMatcher, ShorthandContentType, RetryCallback } from "./types";
|
|
3
|
+
import { KeqURL } from './keq-url';
|
|
5
4
|
export declare class Keq<T> {
|
|
6
5
|
private requestPromise?;
|
|
7
6
|
private urlObj;
|
|
@@ -38,9 +37,9 @@ export declare class Keq<T> {
|
|
|
38
37
|
send(value: FormData | Record<string, any> | any[] | string): Keq<T>;
|
|
39
38
|
field(arg1: string, value: string): Keq<T>;
|
|
40
39
|
field(arg1: Record<string, string>): Keq<T>;
|
|
41
|
-
attach(key: string, file: Blob | File | Buffer
|
|
42
|
-
attach(key: string, file: Blob | File | Buffer
|
|
43
|
-
attach(key: string, file: Blob | File | Buffer
|
|
40
|
+
attach(key: string, file: Blob | File | Buffer): Keq<T>;
|
|
41
|
+
attach(key: string, file: Blob | File | Buffer, filename: string): Keq<T>;
|
|
42
|
+
attach(key: string, file: Blob | File | Buffer): Keq<T>;
|
|
44
43
|
serialize(fn: SerializeBodyFn): Keq<T>;
|
|
45
44
|
use(middleware: Middleware): Keq<T>;
|
|
46
45
|
use(host: string, middleware: Middleware): Keq<T>;
|
package/es/src/keq.js
CHANGED
|
@@ -7,12 +7,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import fetch, { Headers } from 'cross-fetch';
|
|
11
10
|
import { clone } from "./util/clone";
|
|
12
|
-
import { Exception, FileExpectedException, OverwriteArrayBodyException,
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import { FormData, Response } from "./polyfill";
|
|
11
|
+
import { Exception, FileExpectedException, OverwriteArrayBodyException, UnknownContentTypeException, } from "./exception";
|
|
12
|
+
import { sleep, inferContentTypeByBody, fixContentType, getBoundaryByContentType, parseFormData, serializeBody, } from "./util";
|
|
13
|
+
import { isBlob, isBrowser, isFormData } from './util/is';
|
|
14
|
+
import { FormData, Response, Headers, Blob, fetch, btoa, } from "./polyfill";
|
|
15
|
+
import { matchHost, matchMiddleware, compose } from './middleware';
|
|
16
16
|
export class Keq {
|
|
17
17
|
constructor(urlObj, method, middlewares) {
|
|
18
18
|
this.headers = new Headers();
|
|
@@ -47,7 +47,7 @@ export class Keq {
|
|
|
47
47
|
type(contentType) {
|
|
48
48
|
const type = fixContentType(contentType);
|
|
49
49
|
if (!type)
|
|
50
|
-
throw new
|
|
50
|
+
throw new UnknownContentTypeException();
|
|
51
51
|
this.headers.set('Content-Type', type);
|
|
52
52
|
return this;
|
|
53
53
|
}
|
|
@@ -55,7 +55,7 @@ export class Keq {
|
|
|
55
55
|
* Http Basic Authentication
|
|
56
56
|
*/
|
|
57
57
|
auth(username, password) {
|
|
58
|
-
this.headers.set('Authorization', `Basic ${
|
|
58
|
+
this.headers.set('Authorization', `Basic ${btoa(`${username}:${password}`)}`);
|
|
59
59
|
return this;
|
|
60
60
|
}
|
|
61
61
|
appendFormDate(formData) {
|
|
@@ -136,13 +136,15 @@ export class Keq {
|
|
|
136
136
|
throw new OverwriteArrayBodyException();
|
|
137
137
|
if (!this.body)
|
|
138
138
|
this.body = {};
|
|
139
|
-
if (!
|
|
139
|
+
if (!(isBlob(file) || file instanceof Buffer))
|
|
140
140
|
throw new FileExpectedException();
|
|
141
141
|
const formData = new FormData();
|
|
142
|
-
if (
|
|
143
|
-
formData.set(key, file, arg3.filename);
|
|
144
|
-
else
|
|
142
|
+
if (isBlob(file)) {
|
|
145
143
|
formData.set(key, file, arg3);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
formData.set(key, new Blob([file]), arg3);
|
|
147
|
+
}
|
|
146
148
|
this.appendFormDate(formData);
|
|
147
149
|
this.setType('form-data');
|
|
148
150
|
return this;
|
|
@@ -214,7 +216,6 @@ export class Keq {
|
|
|
214
216
|
return this;
|
|
215
217
|
}
|
|
216
218
|
fetch(ctx) {
|
|
217
|
-
var _a;
|
|
218
219
|
return __awaiter(this, void 0, void 0, function* () {
|
|
219
220
|
const uri = ctx.request.url.toPath();
|
|
220
221
|
const fetchOptions = Object.assign({ method: ctx.request.method.toUpperCase(), headers: ctx.request.headers, body: this.serializeBodyFn(ctx.request.body, ctx) }, ctx.request.options);
|
|
@@ -224,13 +225,6 @@ export class Keq {
|
|
|
224
225
|
if (ctx.options.highWaterMark) {
|
|
225
226
|
fetchOptions['highWaterMark'] = ctx.options.highWaterMark;
|
|
226
227
|
}
|
|
227
|
-
/**
|
|
228
|
-
* Content-Type should be removed when it is 'multipart/form-data
|
|
229
|
-
* FetchAPI will auth set it and add boundary
|
|
230
|
-
*/
|
|
231
|
-
if (((_a = fetchOptions.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.toLocaleLowerCase()) === 'multipart/form-data') {
|
|
232
|
-
fetchOptions.headers.delete('content-type');
|
|
233
|
-
}
|
|
234
228
|
const res = yield ctx.options.fetchAPI(uri, fetchOptions);
|
|
235
229
|
function resFromData() {
|
|
236
230
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -242,20 +236,27 @@ export class Keq {
|
|
|
242
236
|
return parseFormData(str, boundary);
|
|
243
237
|
});
|
|
244
238
|
}
|
|
245
|
-
if (!isBrowser
|
|
239
|
+
if (!isBrowser) {
|
|
246
240
|
// node-fetch does not implement Response.formData()
|
|
247
241
|
res.formData = resFromData.bind(res);
|
|
248
242
|
}
|
|
249
243
|
let cache;
|
|
250
244
|
ctx.res = new Proxy(res, {
|
|
251
245
|
get(target, property) {
|
|
252
|
-
if (
|
|
246
|
+
if (typeof property !== 'string' ||
|
|
247
|
+
(property !== 'json' &&
|
|
248
|
+
property !== 'text' &&
|
|
249
|
+
property !== 'formData' &&
|
|
250
|
+
property !== 'blob')) {
|
|
253
251
|
return target[property];
|
|
254
252
|
}
|
|
255
253
|
return () => __awaiter(this, void 0, void 0, function* () {
|
|
254
|
+
if (isBrowser)
|
|
255
|
+
return target.clone()[property]();
|
|
256
256
|
if (!cache)
|
|
257
257
|
cache = yield target.arrayBuffer();
|
|
258
|
-
const
|
|
258
|
+
const buf = Buffer.from(cache);
|
|
259
|
+
const res = new Response(buf, { headers: target.headers });
|
|
259
260
|
res.formData = resFromData.bind(res);
|
|
260
261
|
return yield res[property]();
|
|
261
262
|
});
|
package/es/src/mount.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Exception } from "./exception";
|
|
2
|
-
import * as
|
|
3
|
-
import { isBrowser } from
|
|
2
|
+
import * as minimatch from 'minimatch';
|
|
3
|
+
import { isBrowser } from './util';
|
|
4
4
|
function createMounter(matcher) {
|
|
5
5
|
const mounter = ctx => matcher(ctx);
|
|
6
6
|
mounter.pathname = arg => compose(mounter, pathname(arg));
|
|
@@ -17,7 +17,7 @@ export function location() {
|
|
|
17
17
|
return createMounter((ctx) => {
|
|
18
18
|
if (!ctx.request.url.host)
|
|
19
19
|
return true;
|
|
20
|
-
if (isBrowser
|
|
20
|
+
if (isBrowser) {
|
|
21
21
|
if (ctx.request.url.host === window.location.host)
|
|
22
22
|
return true;
|
|
23
23
|
}
|
|
@@ -31,7 +31,7 @@ export function pathname(matcher) {
|
|
|
31
31
|
return createMounter(ctx => {
|
|
32
32
|
const pathname = ctx.url.pathname || '/';
|
|
33
33
|
if (typeof matcher === 'string')
|
|
34
|
-
return
|
|
34
|
+
return minimatch(pathname, matcher);
|
|
35
35
|
return matcher.test(pathname);
|
|
36
36
|
});
|
|
37
37
|
}
|
package/es/src/polyfill.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export declare const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export declare const FormData: typeof global.FormData;
|
|
2
|
+
export declare const fetch: typeof global.fetch;
|
|
3
|
+
export declare const Headers: typeof global.Headers;
|
|
4
|
+
export declare const Response: typeof global.Response;
|
|
5
|
+
export declare const Blob: typeof global.Blob;
|
|
6
|
+
export declare const File: typeof global.File;
|
|
7
|
+
export declare const btoa: typeof global.btoa;
|
package/es/src/polyfill.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
1
3
|
import { isBrowser } from "./util";
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export const
|
|
4
|
+
export const FormData = isBrowser ? window.FormData : require('formdata-node').FormData;
|
|
5
|
+
export const fetch = isBrowser ? window.fetch.bind(window) : require('node-fetch').default;
|
|
6
|
+
// export const fetch: typeof global.fetch = cFetch
|
|
7
|
+
export const Headers = isBrowser ? window.Headers : require('node-fetch').default.Headers;
|
|
8
|
+
export const Response = isBrowser ? window.Response : require('node-fetch').Response;
|
|
9
|
+
export const Blob = isBrowser ? window.Blob : require('formdata-node').Blob;
|
|
10
|
+
export const File = isBrowser ? window.File : require('formdata-node').File;
|
|
11
|
+
export const btoa = isBrowser ? window.btoa : (str) => Buffer.from(str).toString('base64');
|
package/es/src/request.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const request: RequestCreator;
|
|
1
|
+
export declare const request: import("./types").RequestCreator;
|
package/es/src/request.js
CHANGED
|
@@ -1,44 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { matchMiddleware, matchHost } from "./middleware";
|
|
4
|
-
import { KeqURL } from "./keq-url";
|
|
5
|
-
const defaultOptions = {};
|
|
6
|
-
const middlewares = [];
|
|
7
|
-
export const request = function (params, options = defaultOptions) {
|
|
8
|
-
const request = new Keq(new KeqURL(params.url), 'get', middlewares);
|
|
9
|
-
return request.options(options);
|
|
10
|
-
};
|
|
11
|
-
request.get = function (href) {
|
|
12
|
-
const request = new Keq(new KeqURL(href), 'get', clone(middlewares));
|
|
13
|
-
return request;
|
|
14
|
-
};
|
|
15
|
-
request.put = function (href) {
|
|
16
|
-
const request = new Keq(new KeqURL(href), 'put', middlewares);
|
|
17
|
-
return request;
|
|
18
|
-
};
|
|
19
|
-
request.delete = function (href) {
|
|
20
|
-
const request = new Keq(new KeqURL(href), 'delete', middlewares);
|
|
21
|
-
return request;
|
|
22
|
-
};
|
|
23
|
-
request.del = request.delete;
|
|
24
|
-
request.post = function (href) {
|
|
25
|
-
const request = new Keq(new KeqURL(href), 'post', middlewares);
|
|
26
|
-
return request;
|
|
27
|
-
};
|
|
28
|
-
request.head = function (href) {
|
|
29
|
-
const request = new Keq(new KeqURL(href), 'head', middlewares);
|
|
30
|
-
return request;
|
|
31
|
-
};
|
|
32
|
-
request.patch = function (href) {
|
|
33
|
-
const request = new Keq(new KeqURL(href), 'patch', middlewares);
|
|
34
|
-
return request;
|
|
35
|
-
};
|
|
36
|
-
request.use = function use(m, middleware) {
|
|
37
|
-
if (!middleware)
|
|
38
|
-
middlewares.push(m);
|
|
39
|
-
else if (typeof m === 'string')
|
|
40
|
-
middlewares.push(matchMiddleware(matchHost(m), middleware));
|
|
41
|
-
else
|
|
42
|
-
middlewares.push(matchMiddleware(m, middleware));
|
|
43
|
-
return request;
|
|
44
|
-
};
|
|
1
|
+
import { createRequest } from './util/create-request';
|
|
2
|
+
export const request = createRequest();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { KeqURL } from
|
|
2
|
-
import { KeqBody } from
|
|
3
|
-
import { RequestMethod } from
|
|
4
|
-
import { Options } from
|
|
1
|
+
import { KeqURL } from '../keq-url';
|
|
2
|
+
import { KeqBody } from './keq-body';
|
|
3
|
+
import { RequestMethod } from './request-method';
|
|
4
|
+
import { Options } from './options';
|
|
5
5
|
export interface RequestContext {
|
|
6
6
|
url: KeqURL;
|
|
7
7
|
method: RequestMethod;
|
package/es/src/types/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
1
|
+
export * from './keq-body';
|
|
2
|
+
export * from './request-method';
|
|
3
|
+
export * from './request-creator';
|
|
4
|
+
export * from './serialize-body-fn';
|
|
5
|
+
export * from './context';
|
|
6
|
+
export * from './options';
|
|
7
|
+
export * from './middleware';
|
|
8
|
+
export * from './shorthand-content-type';
|
|
9
|
+
export * from './mounter';
|
|
10
|
+
export * from './retry-callback';
|
package/es/src/types/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
1
|
+
export * from './keq-body';
|
|
2
|
+
export * from './request-method';
|
|
3
|
+
export * from './request-creator';
|
|
4
|
+
export * from './serialize-body-fn';
|
|
5
|
+
export * from './context';
|
|
6
|
+
export * from './options';
|
|
7
|
+
export * from './middleware';
|
|
8
|
+
export * from './shorthand-content-type';
|
|
9
|
+
export * from './mounter';
|
|
10
|
+
export * from './retry-callback';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Context } from
|
|
1
|
+
import { Context } from './context';
|
|
2
2
|
export declare type InnerMiddleware = () => Promise<void>;
|
|
3
3
|
export declare type Middleware = (ctx: Context, next: InnerMiddleware) => Promise<void>;
|
|
4
4
|
export declare type MiddlewareMatcher = (ctx: Context) => boolean;
|