keq 2.3.2 → 2.3.4

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,32 @@
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.3.4](https://github.com/keq-request/keq/compare/v2.3.3...v2.3.4) (2024-05-14)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * avoid repeatedly defining response causing errors to be throw ([fc4d0ab](https://github.com/keq-request/keq/commit/fc4d0abf47a35e894fb31f9284e4fa3947c99383))
11
+ * missing typescript definition ([fff9046](https://github.com/keq-request/keq/commit/fff9046a5d10d264d4aeef918aaeb986056abd67))
12
+
13
+
14
+ ### Performance Improvements
15
+
16
+ * baseOrigin(origin) return this ([d50773c](https://github.com/keq-request/keq/commit/d50773c00eeaf87850c1ee621dc75d920d6897ba))
17
+ * use http://127.0.0.1 as baseOrigin in nodejs ([22f0202](https://github.com/keq-request/keq/commit/22f0202c21c83d128f86f9c0270b124018ce515d))
18
+
19
+ ## [2.3.3](https://github.com/keq-request/keq/compare/v2.3.2...v2.3.3) (2024-05-10)
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * the error thrown when the request timeout is not DOMException ([7c84d8f](https://github.com/keq-request/keq/commit/7c84d8fe40e26713d478d443fbdb9a9d71d99ba9))
25
+
26
+
27
+ ### Performance Improvements
28
+
29
+ * upgrade dependencies ([090e244](https://github.com/keq-request/keq/commit/090e24449f055e185dde1d3972c8f5e9c637fb20))
30
+
5
31
  ## [2.3.2](https://github.com/keq-request/keq/compare/v2.3.1...v2.3.2) (2024-04-23)
6
32
 
7
33
 
package/README.md CHANGED
@@ -556,6 +556,11 @@ const body = await customRequest.get("http://test.com");
556
556
 
557
557
  > The gloabl request instance is created by `request.create()` too.
558
558
 
559
+ option | description
560
+ :----------------|:---------------
561
+ initMiddlewares | `fetch`, `retry`, `flowController` are all implemented by middleware. you can customize the init middlewares to change behavior.
562
+ baseOrigin | When sending a request without an `origin`, `origin` will set to `window.location.origin` in the browser and `"http://127.0.0.1"` in NodeJS.
563
+
559
564
  <!-- usage -->
560
565
 
561
566
  <!-- addition -->
@@ -71,7 +71,7 @@ export class Core {
71
71
  };
72
72
  const middleware = composeMiddleware([...this.__prepend_middlewares__, ...this.__append_middlewares__]);
73
73
  // eslint-disable-next-line @typescript-eslint/no-empty-function
74
- await middleware(ctx, async () => { });
74
+ await middleware(ctx, async function emptyNext() { });
75
75
  const output = ctx[OUTPUT_PROPERTY];
76
76
  if (ctx.options.resolveWithFullResponse || ctx.options.resolveWith === 'response') {
77
77
  return ctx.response;
@@ -12,8 +12,14 @@ import { KeqRouter } from './router/keq-router.js';
12
12
  import { timeoutMiddleware } from './middlewares/timeout-middleware.js';
13
13
  export function createRequest(options) {
14
14
  let baseOrigin = options?.baseOrigin;
15
- if (isBrowser() && !baseOrigin)
16
- baseOrigin = location.origin;
15
+ if (!baseOrigin) {
16
+ if (isBrowser()) {
17
+ baseOrigin = location.origin;
18
+ }
19
+ else {
20
+ baseOrigin = 'http://127.0.0.1';
21
+ }
22
+ }
17
23
  const appendMiddlewares = options?.initMiddlewares ? [...options.initMiddlewares] : [
18
24
  serialFlowControlMiddleware(),
19
25
  abortFlowControlMiddleware(),
@@ -44,6 +50,7 @@ export function createRequest(options) {
44
50
  };
45
51
  request.baseOrigin = (origin) => {
46
52
  baseOrigin = origin;
53
+ return request;
47
54
  };
48
55
  request.useRouter = function useRouter() {
49
56
  return router;
@@ -1,5 +1,5 @@
1
1
  export function abortFlowControlMiddleware() {
2
- return async (ctx, next) => {
2
+ return async function abortFlowControlMiddleware(ctx, next) {
3
3
  if (!ctx.options.flowControl || ctx.options.flowControl.mode !== 'abort') {
4
4
  await next();
5
5
  return;
@@ -15,7 +15,7 @@ export function abortFlowControlMiddleware() {
15
15
  ctx.global.abortFlowControl = {};
16
16
  if (ctx.global.abortFlowControl[key]) {
17
17
  const abortController = ctx.global.abortFlowControl[key];
18
- abortController.abort('abort flow control');
18
+ abortController.abort(new DOMException('The previous request was not completed, so keq flowControl abort this request.', 'AbortError'));
19
19
  }
20
20
  const abortController = new AbortController();
21
21
  ctx.global.abortFlowControl[key] = abortController;
@@ -64,7 +64,7 @@ function compileBody(ctx) {
64
64
  }
65
65
  }
66
66
  export function fetchArgumentsMiddleware() {
67
- return async (ctx, next) => {
67
+ return async function fetchArgumentsMiddleware(ctx, next) {
68
68
  const request = ctx.request;
69
69
  const url = compileUrl(request.url, request.routeParams);
70
70
  if (!request.headers.has('Content-Type') && request.body) {
@@ -3,7 +3,7 @@ import { Exception } from "../exception/exception";
3
3
  * Send Request
4
4
  */
5
5
  export function fetchMiddleware() {
6
- return async (ctx) => {
6
+ return async function fetchMiddleware(ctx) {
7
7
  const fetchArguments = ctx.fetchArguments;
8
8
  if (!fetchArguments) {
9
9
  throw new Exception('fetchArguments is required');
@@ -1,11 +1,11 @@
1
1
  export function proxyResponseMiddleware() {
2
- return async (ctx, next) => {
2
+ return async function proxyResponseMiddleware(ctx, next) {
3
3
  await next();
4
4
  const res = ctx.res;
5
- if (res) {
5
+ if (res && !('response' in ctx)) {
6
6
  Object.defineProperty(ctx, 'response', {
7
7
  get() {
8
- return res.clone();
8
+ return ctx.res?.clone();
9
9
  },
10
10
  });
11
11
  }
@@ -2,7 +2,7 @@ function sleep(ms) {
2
2
  return new Promise((resolve) => setTimeout(resolve, ms));
3
3
  }
4
4
  export function retryMiddleware() {
5
- return async (ctx, next) => {
5
+ return async function retryMiddleware(ctx, next) {
6
6
  const retryTimes = (Number(ctx.options.retryTimes) || 0) + 1;
7
7
  const retryDelay = ctx.options.retryDelay || 10;
8
8
  const retryOn = ctx.options.retryOn;
@@ -1,6 +1,6 @@
1
1
  import * as fastq from 'fastq';
2
2
  export function serialFlowControlMiddleware() {
3
- return async (ctx, next) => {
3
+ return async function serialFlowControlMiddleware(ctx, next) {
4
4
  if (!ctx.options.flowControl || ctx.options.flowControl.mode !== 'serial') {
5
5
  await next();
6
6
  return;
@@ -1,5 +1,5 @@
1
1
  export function timeoutMiddleware() {
2
- return async (ctx, next) => {
2
+ return async function timeoutMiddleware(ctx, next) {
3
3
  if (!ctx.options.timeout || ctx.options.timeout.millisecond <= 0) {
4
4
  await next();
5
5
  return;
@@ -11,9 +11,11 @@ export function timeoutMiddleware() {
11
11
  }
12
12
  const timeoutSignal = new AbortController();
13
13
  ctx.request.signal = timeoutSignal.signal;
14
+ const millisecond = ctx.options.timeout.millisecond;
14
15
  setTimeout(() => {
15
- timeoutSignal.abort('timeout');
16
- }, ctx.options.timeout.millisecond);
16
+ const err = new DOMException(`keq request timeout(${millisecond}ms)`, 'AbortError');
17
+ timeoutSignal.abort(err);
18
+ }, millisecond);
17
19
  await next();
18
20
  };
19
21
  }
@@ -83,7 +83,7 @@
83
83
  };
84
84
  const middleware = (0, compose_middleware_1.composeMiddleware)([...this.__prepend_middlewares__, ...this.__append_middlewares__]);
85
85
  // eslint-disable-next-line @typescript-eslint/no-empty-function
86
- await middleware(ctx, async () => { });
86
+ await middleware(ctx, async function emptyNext() { });
87
87
  const output = ctx[constant_1.OUTPUT_PROPERTY];
88
88
  if (ctx.options.resolveWithFullResponse || ctx.options.resolveWith === 'response') {
89
89
  return ctx.response;
@@ -24,8 +24,14 @@
24
24
  const timeout_middleware_js_1 = require("./middlewares/timeout-middleware.js");
25
25
  function createRequest(options) {
26
26
  let baseOrigin = options?.baseOrigin;
27
- if ((0, is_browser_1.isBrowser)() && !baseOrigin)
28
- baseOrigin = location.origin;
27
+ if (!baseOrigin) {
28
+ if ((0, is_browser_1.isBrowser)()) {
29
+ baseOrigin = location.origin;
30
+ }
31
+ else {
32
+ baseOrigin = 'http://127.0.0.1';
33
+ }
34
+ }
29
35
  const appendMiddlewares = options?.initMiddlewares ? [...options.initMiddlewares] : [
30
36
  (0, serial_flow_control_middleware_js_1.serialFlowControlMiddleware)(),
31
37
  (0, abort_flow_control_middleware_js_1.abortFlowControlMiddleware)(),
@@ -56,6 +62,7 @@
56
62
  };
57
63
  request.baseOrigin = (origin) => {
58
64
  baseOrigin = origin;
65
+ return request;
59
66
  };
60
67
  request.useRouter = function useRouter() {
61
68
  return router;
@@ -11,7 +11,7 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.abortFlowControlMiddleware = void 0;
13
13
  function abortFlowControlMiddleware() {
14
- return async (ctx, next) => {
14
+ return async function abortFlowControlMiddleware(ctx, next) {
15
15
  if (!ctx.options.flowControl || ctx.options.flowControl.mode !== 'abort') {
16
16
  await next();
17
17
  return;
@@ -27,7 +27,7 @@
27
27
  ctx.global.abortFlowControl = {};
28
28
  if (ctx.global.abortFlowControl[key]) {
29
29
  const abortController = ctx.global.abortFlowControl[key];
30
- abortController.abort('abort flow control');
30
+ abortController.abort(new DOMException('The previous request was not completed, so keq flowControl abort this request.', 'AbortError'));
31
31
  }
32
32
  const abortController = new AbortController();
33
33
  ctx.global.abortFlowControl[key] = abortController;
@@ -76,7 +76,7 @@
76
76
  }
77
77
  }
78
78
  function fetchArgumentsMiddleware() {
79
- return async (ctx, next) => {
79
+ return async function fetchArgumentsMiddleware(ctx, next) {
80
80
  const request = ctx.request;
81
81
  const url = compileUrl(request.url, request.routeParams);
82
82
  if (!request.headers.has('Content-Type') && request.body) {
@@ -15,7 +15,7 @@
15
15
  * Send Request
16
16
  */
17
17
  function fetchMiddleware() {
18
- return async (ctx) => {
18
+ return async function fetchMiddleware(ctx) {
19
19
  const fetchArguments = ctx.fetchArguments;
20
20
  if (!fetchArguments) {
21
21
  throw new exception_1.Exception('fetchArguments is required');
@@ -11,13 +11,13 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.proxyResponseMiddleware = void 0;
13
13
  function proxyResponseMiddleware() {
14
- return async (ctx, next) => {
14
+ return async function proxyResponseMiddleware(ctx, next) {
15
15
  await next();
16
16
  const res = ctx.res;
17
- if (res) {
17
+ if (res && !('response' in ctx)) {
18
18
  Object.defineProperty(ctx, 'response', {
19
19
  get() {
20
- return res.clone();
20
+ return ctx.res?.clone();
21
21
  },
22
22
  });
23
23
  }
@@ -14,7 +14,7 @@
14
14
  return new Promise((resolve) => setTimeout(resolve, ms));
15
15
  }
16
16
  function retryMiddleware() {
17
- return async (ctx, next) => {
17
+ return async function retryMiddleware(ctx, next) {
18
18
  const retryTimes = (Number(ctx.options.retryTimes) || 0) + 1;
19
19
  const retryDelay = ctx.options.retryDelay || 10;
20
20
  const retryOn = ctx.options.retryOn;
@@ -35,7 +35,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
35
35
  exports.serialFlowControlMiddleware = void 0;
36
36
  const fastq = __importStar(require("fastq"));
37
37
  function serialFlowControlMiddleware() {
38
- return async (ctx, next) => {
38
+ return async function serialFlowControlMiddleware(ctx, next) {
39
39
  if (!ctx.options.flowControl || ctx.options.flowControl.mode !== 'serial') {
40
40
  await next();
41
41
  return;
@@ -11,7 +11,7 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.timeoutMiddleware = void 0;
13
13
  function timeoutMiddleware() {
14
- return async (ctx, next) => {
14
+ return async function timeoutMiddleware(ctx, next) {
15
15
  if (!ctx.options.timeout || ctx.options.timeout.millisecond <= 0) {
16
16
  await next();
17
17
  return;
@@ -23,9 +23,11 @@
23
23
  }
24
24
  const timeoutSignal = new AbortController();
25
25
  ctx.request.signal = timeoutSignal.signal;
26
+ const millisecond = ctx.options.timeout.millisecond;
26
27
  setTimeout(() => {
27
- timeoutSignal.abort('timeout');
28
- }, ctx.options.timeout.millisecond);
28
+ const err = new DOMException(`keq request timeout(${millisecond}ms)`, 'AbortError');
29
+ timeoutSignal.abort(err);
30
+ }, millisecond);
29
31
  await next();
30
32
  };
31
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keq",
3
- "version": "2.3.2",
3
+ "version": "2.3.4",
4
4
  "description": "Request API write by Typescript for flexibility, readability, and a low learning curve.",
5
5
  "keywords": [
6
6
  "request",
@@ -40,25 +40,25 @@
40
40
  "test": "jest"
41
41
  },
42
42
  "dependencies": {
43
+ "@types/whatwg-url": "^11.0.4",
43
44
  "clone": "^2.1.2",
44
45
  "fastq": "^1.17.1",
45
- "minimatch": "^9.0.3",
46
- "object.fromentries": "^2.0.7",
46
+ "minimatch": "^9.0.4",
47
+ "object.fromentries": "^2.0.8",
47
48
  "ts-custom-error": "^3.3.1",
48
49
  "whatwg-url": "^14.0.0"
49
50
  },
50
51
  "devDependencies": {
51
- "@buka/eslint-config": "^1.6.0",
52
- "@commitlint/cli": "^18.6.1",
53
- "@commitlint/config-conventional": "^18.6.2",
52
+ "@buka/eslint-config": "^1.6.4",
53
+ "@commitlint/cli": "^19.3.0",
54
+ "@commitlint/config-conventional": "^19.2.2",
54
55
  "@jest/globals": "^29.7.0",
55
- "@rushstack/eslint-patch": "^1.7.2",
56
+ "@rushstack/eslint-patch": "^1.10.2",
56
57
  "@types/clone": "^2.1.4",
57
58
  "@types/minimatch": "^5.1.2",
58
- "@types/node": "^20.11.20",
59
- "@types/whatwg-url": "^11.0.4",
60
- "@typescript-eslint/eslint-plugin": "^7.0.2",
61
- "@typescript-eslint/parser": "^7.0.2",
59
+ "@types/node": "^20.12.7",
60
+ "@typescript-eslint/eslint-plugin": "^7.7.1",
61
+ "@typescript-eslint/parser": "^7.7.1",
62
62
  "eslint": "^8.57.0",
63
63
  "husky": "^9.0.11",
64
64
  "is-ci": "^3.0.1",
@@ -68,7 +68,7 @@
68
68
  "ts-jest": "^29.1.2",
69
69
  "ts-node": "^10.9.2",
70
70
  "ts-patch": "^3.1.2",
71
- "typescript": "^5.3.3",
71
+ "typescript": "^5.4.5",
72
72
  "typescript-transform-paths": "^3.4.7"
73
73
  },
74
74
  "packageManager": "pnpm@8.6.4",