keq 1.7.1 → 1.8.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.
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.0](https://www.github.com/keq-request/keq/compare/v1.7.3...v1.8.0) (2022-03-22)
6
+
7
+
8
+ ### Features
9
+
10
+ * support custom request instance ([2c05dec](https://www.github.com/keq-request/keq/commit/2c05dec6d779861112507b7365822ecff175dbc3))
11
+
12
+ ### [1.7.3](https://www.github.com/keq-request/keq/compare/v1.7.2...v1.7.3) (2022-03-08)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * unable to send formdate request with file ([a4ed415](https://www.github.com/keq-request/keq/commit/a4ed41560aacb67a49d8159b453ddf69bce56799))
18
+
19
+ ### [1.7.2](https://www.github.com/keq-request/keq/compare/v1.7.1...v1.7.2) (2022-02-25)
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * calling response.json() in middleware will cause garbled chinese characters ([40ffe02](https://www.github.com/keq-request/keq/commit/40ffe0238628c106a06eafe349e6d33b856fbf8f))
25
+ * throw error when not set url origin ([9040396](https://www.github.com/keq-request/keq/commit/9040396ba23edc1642abf27a203b39efe53fb130))
26
+
5
27
  ### [1.7.1](https://www.github.com/keq-request/keq/compare/v1.7.0...v1.7.1) (2022-02-24)
6
28
 
7
29
 
package/README.md CHANGED
@@ -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://example.com')
458
+ ```
459
+
460
+ > The gloabl request instance is created by `request.create()` too.
443
461
 
444
462
  <!-- usage -->
445
463
 
@@ -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://example.com')
442
+ ```
443
+
444
+ > 全局的`request`实例也是调用`request.create()`方法创建的。
445
+ > 因此,自定义的`request`实例也具备全局`request`的所有特性。
446
+
447
+
428
448
 
429
449
  ## 更多信息
430
450
 
package/es/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.7.1",
5
+ "version": "1.8.0",
6
6
  "license": "MIT",
7
7
  "types": "lib/index.d.ts",
8
8
  "scripts": {
@@ -64,6 +64,7 @@
64
64
  "formidable": "^2.0.1",
65
65
  "mime-types": "^2.1.27",
66
66
  "nanoid": "^3.1.10",
67
+ "object.fromentries": "^2.0.5",
67
68
  "path-to-regexp": "^6.2.0",
68
69
  "picomatch": "^2.2.2",
69
70
  "querystring": "^0.2.0",
@@ -1,5 +1,6 @@
1
1
  import { URL } from 'whatwg-url';
2
2
  export declare class KeqURL extends URL {
3
+ constructor(url: string, base?: string | URL);
3
4
  params: {};
4
5
  get query(): Record<string, any>;
5
6
  set query(value: Record<string, any>);
package/es/src/keq-url.js CHANGED
@@ -2,9 +2,10 @@ import { URL } from 'whatwg-url';
2
2
  import { compile } from 'path-to-regexp';
3
3
  import { Exception } from "./exception";
4
4
  import * as R from 'ramda';
5
+ import { isBrowser } from "./util";
5
6
  export class KeqURL extends URL {
6
- constructor() {
7
- super(...arguments);
7
+ constructor(url, base = isBrowser() ? window.location.origin : 'http://localhost') {
8
+ super(url, base);
8
9
  this.params = {};
9
10
  }
10
11
  get query() {
@@ -60,14 +61,13 @@ export class KeqURL extends URL {
60
61
  }
61
62
  }
62
63
  toPath() {
63
- var _a;
64
64
  const uri = new URL(this.href);
65
65
  try {
66
66
  const toPath = compile(uri.pathname, { encode: encodeURIComponent });
67
67
  uri.pathname = toPath(this.params);
68
68
  }
69
69
  catch (e) {
70
- throw new Exception(`Cannot compile the params in ${uri.pathname}, Because ${(_a = e) === null || _a === void 0 ? void 0 : _a.message}.`);
70
+ throw new Exception(`Cannot compile the params in ${uri.pathname}, Because ${e === null || e === void 0 ? void 0 : e.message}.`);
71
71
  }
72
72
  return uri.href;
73
73
  }
package/es/src/keq.js CHANGED
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import fetch, { Headers } from 'cross-fetch';
11
- import * as clone from 'clone';
11
+ import { clone } from "./util/clone";
12
12
  import { Exception, FileExpectedException, OverwriteArrayBodyException, UnknowContentTypeException, } from "./exception";
13
13
  import { isFormData, isFile, isBrowser, encodeBase64, sleep, inferContentTypeByBody, fixContentType, getBoundaryByContentType, parseFormData, serializeBody, } from "./util";
14
14
  import { matchHost, matchMiddleware, compose } from "./middleware";
@@ -255,7 +255,7 @@ export class Keq {
255
255
  return () => __awaiter(this, void 0, void 0, function* () {
256
256
  if (!cache)
257
257
  cache = yield target.arrayBuffer();
258
- const res = new Response(cache, { headers: target.headers });
258
+ const res = new Response(new TextDecoder().decode(cache), { headers: target.headers });
259
259
  res.formData = resFromData.bind(res);
260
260
  return yield res[property]();
261
261
  });
package/es/src/mount.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import { Mounter } from "./types";
2
+ /**
3
+ * NOTE: Not work in NodeJS
4
+ * Unable to get the host and port of the service
5
+ */
2
6
  export declare function location(): Mounter;
3
7
  /**
4
8
  * @param matcher glob or regexp
package/es/src/mount.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Exception } from "./exception";
2
2
  import * as picomatch from 'picomatch';
3
+ import { isBrowser } from "./util";
3
4
  function createMounter(matcher) {
4
5
  const mounter = ctx => matcher(ctx);
5
6
  mounter.pathname = arg => compose(mounter, pathname(arg));
@@ -8,8 +9,20 @@ function createMounter(matcher) {
8
9
  mounter.module = arg => compose(mounter, module(arg));
9
10
  return mounter;
10
11
  }
12
+ /**
13
+ * NOTE: Not work in NodeJS
14
+ * Unable to get the host and port of the service
15
+ */
11
16
  export function location() {
12
- return createMounter((ctx) => !ctx.request.url.host);
17
+ return createMounter((ctx) => {
18
+ if (!ctx.request.url.host)
19
+ return true;
20
+ if (isBrowser()) {
21
+ if (ctx.request.url.host === window.location.host)
22
+ return true;
23
+ }
24
+ return false;
25
+ });
13
26
  }
14
27
  /**
15
28
  * @param matcher glob or regexp
@@ -1,2 +1 @@
1
- import { RequestCreator } from "./types";
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 { Keq } from "./keq";
2
- import * as clone from 'clone';
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();
@@ -14,7 +14,7 @@ export interface Context {
14
14
  res?: Response;
15
15
  /** the result get by user */
16
16
  output: any;
17
- options: Required<Options>;
17
+ options: Required<Pick<Options, 'fetchAPI'>> & Options;
18
18
  /** delegate res */
19
19
  response?: Response;
20
20
  /** delegate request */
@@ -17,6 +17,7 @@ export interface RequestCreator {
17
17
  put: CreateReqeustPromiseF;
18
18
  patch: CreateReqeustPromiseF;
19
19
  head: CreateReqeustPromiseF;
20
+ create(): RequestCreator;
20
21
  use(middleware: Middleware): RequestCreator;
21
22
  use(matcher: string, middleware: Middleware): RequestCreator;
22
23
  use(matcher: MiddlewareMatcher, middleware: Middleware): RequestCreator;
@@ -0,0 +1 @@
1
+ export declare function clone<T>(obj: T): T;
@@ -0,0 +1,14 @@
1
+ import * as deepClone from 'clone';
2
+ import { isBlob } from "./is";
3
+ import * as fromEntries from 'object.fromentries';
4
+ export function clone(obj) {
5
+ if (typeof obj === 'object' && !Array.isArray(obj)) {
6
+ const entries = Object.entries(obj)
7
+ .map(([key, value]) => ([
8
+ key,
9
+ isBlob(value) ? value : deepClone(value),
10
+ ]));
11
+ return fromEntries(entries);
12
+ }
13
+ return deepClone(obj);
14
+ }
@@ -0,0 +1,2 @@
1
+ import { RequestCreator } from "../types";
2
+ export declare function createRequest(): RequestCreator;
@@ -0,0 +1,48 @@
1
+ import { Keq } from "../keq";
2
+ import * as clone from 'clone';
3
+ import { matchMiddleware, matchHost } from "../middleware";
4
+ import { KeqURL } from "../keq-url";
5
+ const defaultOptions = {};
6
+ export function createRequest() {
7
+ const middlewares = [];
8
+ const request = function (params, options = defaultOptions) {
9
+ const request = new Keq(new KeqURL(params.url), 'get', middlewares);
10
+ return request.options(options);
11
+ };
12
+ request.get = function (href) {
13
+ const request = new Keq(new KeqURL(href), 'get', clone(middlewares));
14
+ return request;
15
+ };
16
+ request.put = function (href) {
17
+ const request = new Keq(new KeqURL(href), 'put', middlewares);
18
+ return request;
19
+ };
20
+ request.delete = function (href) {
21
+ const request = new Keq(new KeqURL(href), 'delete', middlewares);
22
+ return request;
23
+ };
24
+ request.del = request.delete;
25
+ request.post = function (href) {
26
+ const request = new Keq(new KeqURL(href), 'post', middlewares);
27
+ return request;
28
+ };
29
+ request.head = function (href) {
30
+ const request = new Keq(new KeqURL(href), 'head', middlewares);
31
+ return request;
32
+ };
33
+ request.patch = function (href) {
34
+ const request = new Keq(new KeqURL(href), 'patch', middlewares);
35
+ return request;
36
+ };
37
+ request.use = function use(m, middleware) {
38
+ if (!middleware)
39
+ middlewares.push(m);
40
+ else if (typeof m === 'string')
41
+ middlewares.push(matchMiddleware(matchHost(m), middleware));
42
+ else
43
+ middlewares.push(matchMiddleware(m, middleware));
44
+ return request;
45
+ };
46
+ request.create = createRequest;
47
+ return request;
48
+ }
package/lib/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
2
  if (k2 === undefined) k2 = k;
3
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
4
8
  }) : (function(o, m, k, k2) {
5
9
  if (k2 === undefined) k2 = k;
6
10
  o[k2] = m[k];
package/lib/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.7.1",
5
+ "version": "1.8.0",
6
6
  "license": "MIT",
7
7
  "types": "lib/index.d.ts",
8
8
  "scripts": {
@@ -64,6 +64,7 @@
64
64
  "formidable": "^2.0.1",
65
65
  "mime-types": "^2.1.27",
66
66
  "nanoid": "^3.1.10",
67
+ "object.fromentries": "^2.0.5",
67
68
  "path-to-regexp": "^6.2.0",
68
69
  "picomatch": "^2.2.2",
69
70
  "querystring": "^0.2.0",
@@ -1,6 +1,10 @@
1
1
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
2
  if (k2 === undefined) k2 = k;
3
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
4
8
  }) : (function(o, m, k, k2) {
5
9
  if (k2 === undefined) k2 = k;
6
10
  o[k2] = m[k];
package/lib/src/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
2
  if (k2 === undefined) k2 = k;
3
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
4
8
  }) : (function(o, m, k, k2) {
5
9
  if (k2 === undefined) k2 = k;
6
10
  o[k2] = m[k];
@@ -1,5 +1,6 @@
1
1
  import { URL } from 'whatwg-url';
2
2
  export declare class KeqURL extends URL {
3
+ constructor(url: string, base?: string | URL);
3
4
  params: {};
4
5
  get query(): Record<string, any>;
5
6
  set query(value: Record<string, any>);
@@ -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", "whatwg-url", "path-to-regexp", "./exception", "ramda"], factory);
7
+ define(["require", "exports", "whatwg-url", "path-to-regexp", "./exception", "ramda", "./util"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
@@ -14,9 +14,10 @@
14
14
  const path_to_regexp_1 = require("path-to-regexp");
15
15
  const exception_1 = require("./exception");
16
16
  const R = require("ramda");
17
+ const util_1 = require("./util");
17
18
  class KeqURL extends whatwg_url_1.URL {
18
- constructor() {
19
- super(...arguments);
19
+ constructor(url, base = (0, util_1.isBrowser)() ? window.location.origin : 'http://localhost') {
20
+ super(url, base);
20
21
  this.params = {};
21
22
  }
22
23
  get query() {
@@ -72,14 +73,13 @@
72
73
  }
73
74
  }
74
75
  toPath() {
75
- var _a;
76
76
  const uri = new whatwg_url_1.URL(this.href);
77
77
  try {
78
78
  const toPath = (0, path_to_regexp_1.compile)(uri.pathname, { encode: encodeURIComponent });
79
79
  uri.pathname = toPath(this.params);
80
80
  }
81
81
  catch (e) {
82
- throw new exception_1.Exception(`Cannot compile the params in ${uri.pathname}, Because ${(_a = e) === null || _a === void 0 ? void 0 : _a.message}.`);
82
+ throw new exception_1.Exception(`Cannot compile the params in ${uri.pathname}, Because ${e === null || e === void 0 ? void 0 : e.message}.`);
83
83
  }
84
84
  return uri.href;
85
85
  }
package/lib/src/keq.js CHANGED
@@ -13,14 +13,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  if (v !== undefined) module.exports = v;
14
14
  }
15
15
  else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "cross-fetch", "clone", "./exception", "./util", "./middleware", "./polyfill"], factory);
16
+ define(["require", "exports", "cross-fetch", "./util/clone", "./exception", "./util", "./middleware", "./polyfill"], factory);
17
17
  }
18
18
  })(function (require, exports) {
19
19
  "use strict";
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  exports.Keq = void 0;
22
22
  const cross_fetch_1 = require("cross-fetch");
23
- const clone = require("clone");
23
+ const clone_1 = require("./util/clone");
24
24
  const exception_1 = require("./exception");
25
25
  const util_1 = require("./util");
26
26
  const middleware_1 = require("./middleware");
@@ -267,7 +267,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
267
267
  return () => __awaiter(this, void 0, void 0, function* () {
268
268
  if (!cache)
269
269
  cache = yield target.arrayBuffer();
270
- const res = new polyfill_1.Response(cache, { headers: target.headers });
270
+ const res = new polyfill_1.Response(new TextDecoder().decode(cache), { headers: target.headers });
271
271
  res.formData = resFromData.bind(res);
272
272
  return yield res[property]();
273
273
  });
@@ -314,14 +314,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
314
314
  urlObj.params = value.params;
315
315
  },
316
316
  headers,
317
- body: clone(this.body),
317
+ body: (0, clone_1.clone)(this.body),
318
318
  options: {},
319
319
  };
320
320
  if (this.opts.redirect)
321
321
  request.options.redirect = this.opts.redirect;
322
322
  const ctx = {
323
323
  request,
324
- options: clone(Object.assign(Object.assign({}, this.opts), { fetchAPI: this.opts.fetchAPI || cross_fetch_1.default })),
324
+ options: (0, clone_1.clone)(Object.assign(Object.assign({}, this.opts), { fetchAPI: this.opts.fetchAPI || cross_fetch_1.default })),
325
325
  output: undefined,
326
326
  get url() {
327
327
  return this.request.url;
@@ -1,4 +1,8 @@
1
1
  import { Mounter } from "./types";
2
+ /**
3
+ * NOTE: Not work in NodeJS
4
+ * Unable to get the host and port of the service
5
+ */
2
6
  export declare function location(): Mounter;
3
7
  /**
4
8
  * @param matcher glob or regexp
package/lib/src/mount.js CHANGED
@@ -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", "picomatch"], factory);
7
+ define(["require", "exports", "./exception", "picomatch", "./util"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
@@ -12,6 +12,7 @@
12
12
  exports.module = exports.host = exports.pathname = exports.location = void 0;
13
13
  const exception_1 = require("./exception");
14
14
  const picomatch = require("picomatch");
15
+ const util_1 = require("./util");
15
16
  function createMounter(matcher) {
16
17
  const mounter = ctx => matcher(ctx);
17
18
  mounter.pathname = arg => compose(mounter, pathname(arg));
@@ -20,8 +21,20 @@
20
21
  mounter.module = arg => compose(mounter, module(arg));
21
22
  return mounter;
22
23
  }
24
+ /**
25
+ * NOTE: Not work in NodeJS
26
+ * Unable to get the host and port of the service
27
+ */
23
28
  function location() {
24
- return createMounter((ctx) => !ctx.request.url.host);
29
+ return createMounter((ctx) => {
30
+ if (!ctx.request.url.host)
31
+ return true;
32
+ if ((0, util_1.isBrowser)()) {
33
+ if (ctx.request.url.host === window.location.host)
34
+ return true;
35
+ }
36
+ return false;
37
+ });
25
38
  }
26
39
  exports.location = location;
27
40
  /**
@@ -1,2 +1 @@
1
- import { RequestCreator } from "./types";
2
- export declare const request: RequestCreator;
1
+ export declare const request: import("./types").RequestCreator;
@@ -4,55 +4,12 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./keq", "clone", "./middleware", "./keq-url"], factory);
7
+ define(["require", "exports", "./util/create-request"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.request = void 0;
13
- const keq_1 = require("./keq");
14
- const clone = require("clone");
15
- const middleware_1 = require("./middleware");
16
- const keq_url_1 = require("./keq-url");
17
- const defaultOptions = {};
18
- const middlewares = [];
19
- const request = function (params, options = defaultOptions) {
20
- const request = new keq_1.Keq(new keq_url_1.KeqURL(params.url), 'get', middlewares);
21
- return request.options(options);
22
- };
23
- exports.request = request;
24
- exports.request.get = function (href) {
25
- const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'get', clone(middlewares));
26
- return request;
27
- };
28
- exports.request.put = function (href) {
29
- const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'put', middlewares);
30
- return request;
31
- };
32
- exports.request.delete = function (href) {
33
- const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'delete', middlewares);
34
- return request;
35
- };
36
- exports.request.del = exports.request.delete;
37
- exports.request.post = function (href) {
38
- const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'post', middlewares);
39
- return request;
40
- };
41
- exports.request.head = function (href) {
42
- const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'head', middlewares);
43
- return request;
44
- };
45
- exports.request.patch = function (href) {
46
- const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'patch', middlewares);
47
- return request;
48
- };
49
- exports.request.use = function use(m, middleware) {
50
- if (!middleware)
51
- middlewares.push(m);
52
- else if (typeof m === 'string')
53
- middlewares.push((0, middleware_1.matchMiddleware)((0, middleware_1.matchHost)(m), middleware));
54
- else
55
- middlewares.push((0, middleware_1.matchMiddleware)(m, middleware));
56
- return exports.request;
57
- };
13
+ const create_request_1 = require("./util/create-request");
14
+ exports.request = (0, create_request_1.createRequest)();
58
15
  });
@@ -14,7 +14,7 @@ export interface Context {
14
14
  res?: Response;
15
15
  /** the result get by user */
16
16
  output: any;
17
- options: Required<Options>;
17
+ options: Required<Pick<Options, 'fetchAPI'>> & Options;
18
18
  /** delegate res */
19
19
  response?: Response;
20
20
  /** delegate request */
@@ -1,6 +1,10 @@
1
1
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
2
  if (k2 === undefined) k2 = k;
3
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
4
8
  }) : (function(o, m, k, k2) {
5
9
  if (k2 === undefined) k2 = k;
6
10
  o[k2] = m[k];
@@ -17,6 +17,7 @@ export interface RequestCreator {
17
17
  put: CreateReqeustPromiseF;
18
18
  patch: CreateReqeustPromiseF;
19
19
  head: CreateReqeustPromiseF;
20
+ create(): RequestCreator;
20
21
  use(middleware: Middleware): RequestCreator;
21
22
  use(matcher: string, middleware: Middleware): RequestCreator;
22
23
  use(matcher: MiddlewareMatcher, middleware: Middleware): RequestCreator;
@@ -0,0 +1 @@
1
+ export declare function clone<T>(obj: T): T;
@@ -0,0 +1,28 @@
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", "clone", "./is", "object.fromentries"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.clone = void 0;
13
+ const deepClone = require("clone");
14
+ const is_1 = require("./is");
15
+ const fromEntries = require("object.fromentries");
16
+ function clone(obj) {
17
+ if (typeof obj === 'object' && !Array.isArray(obj)) {
18
+ const entries = Object.entries(obj)
19
+ .map(([key, value]) => ([
20
+ key,
21
+ (0, is_1.isBlob)(value) ? value : deepClone(value),
22
+ ]));
23
+ return fromEntries(entries);
24
+ }
25
+ return deepClone(obj);
26
+ }
27
+ exports.clone = clone;
28
+ });
@@ -0,0 +1,2 @@
1
+ import { RequestCreator } from "../types";
2
+ export declare function createRequest(): RequestCreator;
@@ -0,0 +1,62 @@
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", "../keq", "clone", "../middleware", "../keq-url"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createRequest = void 0;
13
+ const keq_1 = require("../keq");
14
+ const clone = require("clone");
15
+ const middleware_1 = require("../middleware");
16
+ const keq_url_1 = require("../keq-url");
17
+ const defaultOptions = {};
18
+ function createRequest() {
19
+ const middlewares = [];
20
+ const request = function (params, options = defaultOptions) {
21
+ const request = new keq_1.Keq(new keq_url_1.KeqURL(params.url), 'get', middlewares);
22
+ return request.options(options);
23
+ };
24
+ request.get = function (href) {
25
+ const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'get', clone(middlewares));
26
+ return request;
27
+ };
28
+ request.put = function (href) {
29
+ const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'put', middlewares);
30
+ return request;
31
+ };
32
+ request.delete = function (href) {
33
+ const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'delete', middlewares);
34
+ return request;
35
+ };
36
+ request.del = request.delete;
37
+ request.post = function (href) {
38
+ const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'post', middlewares);
39
+ return request;
40
+ };
41
+ request.head = function (href) {
42
+ const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'head', middlewares);
43
+ return request;
44
+ };
45
+ request.patch = function (href) {
46
+ const request = new keq_1.Keq(new keq_url_1.KeqURL(href), 'patch', middlewares);
47
+ return request;
48
+ };
49
+ request.use = function use(m, middleware) {
50
+ if (!middleware)
51
+ middlewares.push(m);
52
+ else if (typeof m === 'string')
53
+ middlewares.push((0, middleware_1.matchMiddleware)((0, middleware_1.matchHost)(m), middleware));
54
+ else
55
+ middlewares.push((0, middleware_1.matchMiddleware)(m, middleware));
56
+ return request;
57
+ };
58
+ request.create = createRequest;
59
+ return request;
60
+ }
61
+ exports.createRequest = createRequest;
62
+ });
@@ -1,6 +1,10 @@
1
1
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
2
  if (k2 === undefined) k2 = k;
3
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
4
8
  }) : (function(o, m, k, k2) {
5
9
  if (k2 === undefined) k2 = k;
6
10
  o[k2] = m[k];
@@ -1,6 +1,10 @@
1
1
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
2
  if (k2 === undefined) k2 = k;
3
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
4
8
  }) : (function(o, m, k, k2) {
5
9
  if (k2 === undefined) k2 = k;
6
10
  o[k2] = m[k];
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.7.1",
5
+ "version": "1.8.0",
6
6
  "license": "MIT",
7
7
  "types": "lib/index.d.ts",
8
8
  "scripts": {
@@ -64,6 +64,7 @@
64
64
  "formidable": "^2.0.1",
65
65
  "mime-types": "^2.1.27",
66
66
  "nanoid": "^3.1.10",
67
+ "object.fromentries": "^2.0.5",
67
68
  "path-to-regexp": "^6.2.0",
68
69
  "picomatch": "^2.2.2",
69
70
  "querystring": "^0.2.0",