keq 2.1.0 → 2.1.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 CHANGED
@@ -2,6 +2,20 @@
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.1.2](https://github.com/keq-request/keq/compare/v2.1.1...v2.1.2) (2024-01-17)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * unable send request when parentheses exit in pathname ([24c81ff](https://github.com/keq-request/keq/commit/24c81ffdde71919163a09778aa15ae6106427d45))
11
+
12
+ ## [2.1.1](https://github.com/keq-request/keq/compare/v2.1.0...v2.1.1) (2024-01-09)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * should not throw error when query is undefined ([515fa20](https://github.com/keq-request/keq/commit/515fa2090a03eb60ed8e7d0e575ce44dd8bce12b))
18
+
5
19
  ## [2.1.0](https://github.com/keq-request/keq/compare/v2.0.8...v2.1.0) (2024-01-05)
6
20
 
7
21
 
package/README.md CHANGED
@@ -150,24 +150,20 @@ await request
150
150
  ### Request routing parameters
151
151
 
152
152
  The `.params()` method accepts key and value, which when used for the request with routing parameters.
153
- The follwing will produce the path `/search/keq`.
154
153
 
155
154
  ```javascript
156
155
  import { request } from "keq";
157
156
 
158
157
  await request
159
- .get("/search/:searchKey")
160
- .params("searchKey", "keq");
161
- ```
162
-
163
- Or as a single object:
164
-
165
- ```javascript
166
- import { request } from "keq";
167
-
168
- await request
169
- .get("/search/:searchKey")
170
- .params({ searchKey: "keq" });
158
+ // request to /users/jack/books/kafka
159
+ .get("/users/:userName/books/{bookName}")
160
+ .params("userName", 'jack');
161
+ .params("bookName", "kafka");
162
+ // or invoke with an object
163
+ .params({
164
+ "userName": "jack",
165
+ "bookName": "kafka"
166
+ })
171
167
  ```
172
168
 
173
169
  ### JSON Request
@@ -3,6 +3,7 @@ import { Core } from './core';
3
3
  import { KeqFlowControlMode, KeqFlowControlSignal } from './types/keq-flow-control.js';
4
4
  import { KeqMiddleware } from './types/keq-middleware';
5
5
  import { KeqBuildInOptions, KeqOptions, KeqOptionsWithFullResponse, KeqOptionsWithoutFullResponse } from './types/keq-options';
6
+ import { KeqQueryValue } from './types/keq-query-value.js';
6
7
  import { KeqRequestBody } from './types/keq-request-body';
7
8
  import { KeqRetryDelay } from './types/keq-retry-delay';
8
9
  import { KeqRetryOn } from './types/keq-retry-on';
@@ -29,8 +30,8 @@ export declare class Keq<T> extends Core<T> {
29
30
  /**
30
31
  * Set request query/searchParams
31
32
  */
32
- query(key: Record<string, string | number | string[] | number[] | undefined>): this;
33
- query(key: string, value: string | number | string[] | number[] | undefined): this;
33
+ query(key: Record<string, KeqQueryValue | KeqQueryValue[]>): this;
34
+ query(key: string, value: KeqQueryValue | KeqQueryValue[]): this;
34
35
  /**
35
36
  * Set request route params
36
37
  */
@@ -44,25 +44,32 @@ export class Keq extends Core {
44
44
  return this;
45
45
  }
46
46
  query(key, value) {
47
- if (typeof key === 'string' && Array.isArray(value)) {
48
- for (const item of value) {
49
- this.requestContext.url.searchParams.append(key, String(item));
50
- }
51
- }
52
- else if (typeof key === 'string' && typeof value === 'string') {
53
- this.requestContext.url.searchParams.append(key, value);
54
- }
55
- else if (typeof key === 'object') {
47
+ if (typeof key === 'object') {
56
48
  for (const [k, v] of Object.entries(key)) {
57
49
  if (v === undefined)
58
50
  continue;
59
51
  this.query(k, v);
60
52
  }
53
+ return this;
61
54
  }
62
- else {
63
- throw new Exception('please set query value');
55
+ if (typeof key === 'string') {
56
+ if (Array.isArray(value)) {
57
+ for (const item of value) {
58
+ this.query(key, item);
59
+ }
60
+ }
61
+ else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'bigint') {
62
+ this.requestContext.url.searchParams.append(key, String(value));
63
+ }
64
+ else if (value === 'undefined' || value === null) {
65
+ // skip
66
+ }
67
+ else {
68
+ console.warn(`query value type is invalid, key: ${key}`);
69
+ }
70
+ return this;
64
71
  }
65
- return this;
72
+ throw new TypeError('typeof key is invalid');
66
73
  }
67
74
  params(key, value) {
68
75
  if (typeof key === 'string') {
@@ -1,11 +1,10 @@
1
- import { compile } from 'path-to-regexp';
2
1
  import { URL } from 'whatwg-url';
3
2
  import { Exception } from "../exception/exception";
3
+ import { compilePathnameTemplate } from "../util/compile-pathname-template.js";
4
4
  function compileUrl(obj, routeParams) {
5
5
  const url = new URL(typeof obj === 'string' ? obj : obj.href);
6
6
  try {
7
- const toPath = compile(url.pathname, { encode: encodeURIComponent });
8
- url.pathname = toPath(routeParams);
7
+ url.pathname = compilePathnameTemplate(url.pathname, routeParams);
9
8
  }
10
9
  catch (e) {
11
10
  throw new Exception(`Cannot compile the params in ${url.pathname}, Because ${e?.message}.`);
@@ -0,0 +1 @@
1
+ export type KeqQueryValue = string | number | bigint | undefined;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare function compilePathnameTemplate(template: string, params: Record<string, string | number>): string;
@@ -0,0 +1,15 @@
1
+ export function compilePathnameTemplate(template, params) {
2
+ return template
3
+ .replace(/(^|\/)(?::([^/]+)|{([^/]+)}|%7B([^/]+)%7D)(?=$|\/)/g, (_, prefix, group1, group2, group3) => {
4
+ if (group1 && params[group1]) {
5
+ return `${prefix}${encodeURIComponent(params[group1])}`;
6
+ }
7
+ else if (group2 && params[group2]) {
8
+ return `${prefix}${encodeURIComponent(params[group2])}`;
9
+ }
10
+ else if (group3 && params[group3]) {
11
+ return `${prefix}${encodeURIComponent(params[group3])}`;
12
+ }
13
+ return _;
14
+ });
15
+ }
@@ -3,6 +3,7 @@ import { Core } from './core';
3
3
  import { KeqFlowControlMode, KeqFlowControlSignal } from './types/keq-flow-control.js';
4
4
  import { KeqMiddleware } from './types/keq-middleware';
5
5
  import { KeqBuildInOptions, KeqOptions, KeqOptionsWithFullResponse, KeqOptionsWithoutFullResponse } from './types/keq-options';
6
+ import { KeqQueryValue } from './types/keq-query-value.js';
6
7
  import { KeqRequestBody } from './types/keq-request-body';
7
8
  import { KeqRetryDelay } from './types/keq-retry-delay';
8
9
  import { KeqRetryOn } from './types/keq-retry-on';
@@ -29,8 +30,8 @@ export declare class Keq<T> extends Core<T> {
29
30
  /**
30
31
  * Set request query/searchParams
31
32
  */
32
- query(key: Record<string, string | number | string[] | number[] | undefined>): this;
33
- query(key: string, value: string | number | string[] | number[] | undefined): this;
33
+ query(key: Record<string, KeqQueryValue | KeqQueryValue[]>): this;
34
+ query(key: string, value: KeqQueryValue | KeqQueryValue[]): this;
34
35
  /**
35
36
  * Set request route params
36
37
  */
@@ -56,25 +56,32 @@
56
56
  return this;
57
57
  }
58
58
  query(key, value) {
59
- if (typeof key === 'string' && Array.isArray(value)) {
60
- for (const item of value) {
61
- this.requestContext.url.searchParams.append(key, String(item));
62
- }
63
- }
64
- else if (typeof key === 'string' && typeof value === 'string') {
65
- this.requestContext.url.searchParams.append(key, value);
66
- }
67
- else if (typeof key === 'object') {
59
+ if (typeof key === 'object') {
68
60
  for (const [k, v] of Object.entries(key)) {
69
61
  if (v === undefined)
70
62
  continue;
71
63
  this.query(k, v);
72
64
  }
65
+ return this;
73
66
  }
74
- else {
75
- throw new exception_1.Exception('please set query value');
67
+ if (typeof key === 'string') {
68
+ if (Array.isArray(value)) {
69
+ for (const item of value) {
70
+ this.query(key, item);
71
+ }
72
+ }
73
+ else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'bigint') {
74
+ this.requestContext.url.searchParams.append(key, String(value));
75
+ }
76
+ else if (value === 'undefined' || value === null) {
77
+ // skip
78
+ }
79
+ else {
80
+ console.warn(`query value type is invalid, key: ${key}`);
81
+ }
82
+ return this;
76
83
  }
77
- return this;
84
+ throw new TypeError('typeof key is invalid');
78
85
  }
79
86
  params(key, value) {
80
87
  if (typeof key === 'string') {
@@ -4,20 +4,19 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "path-to-regexp", "whatwg-url", "../exception/exception"], factory);
7
+ define(["require", "exports", "whatwg-url", "../exception/exception", "../util/compile-pathname-template.js"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.fetchArgumentsMiddleware = void 0;
13
- const path_to_regexp_1 = require("path-to-regexp");
14
13
  const whatwg_url_1 = require("whatwg-url");
15
14
  const exception_1 = require("../exception/exception");
15
+ const compile_pathname_template_js_1 = require("../util/compile-pathname-template.js");
16
16
  function compileUrl(obj, routeParams) {
17
17
  const url = new whatwg_url_1.URL(typeof obj === 'string' ? obj : obj.href);
18
18
  try {
19
- const toPath = (0, path_to_regexp_1.compile)(url.pathname, { encode: encodeURIComponent });
20
- url.pathname = toPath(routeParams);
19
+ url.pathname = (0, compile_pathname_template_js_1.compilePathnameTemplate)(url.pathname, routeParams);
21
20
  }
22
21
  catch (e) {
23
22
  throw new exception_1.Exception(`Cannot compile the params in ${url.pathname}, Because ${e?.message}.`);
@@ -0,0 +1 @@
1
+ export type KeqQueryValue = string | number | bigint | undefined;
@@ -0,0 +1,12 @@
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
+ });
@@ -0,0 +1 @@
1
+ export declare function compilePathnameTemplate(template: string, params: Record<string, string | number>): string;
@@ -0,0 +1,29 @@
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.compilePathnameTemplate = void 0;
13
+ function compilePathnameTemplate(template, params) {
14
+ return template
15
+ .replace(/(^|\/)(?::([^/]+)|{([^/]+)}|%7B([^/]+)%7D)(?=$|\/)/g, (_, prefix, group1, group2, group3) => {
16
+ if (group1 && params[group1]) {
17
+ return `${prefix}${encodeURIComponent(params[group1])}`;
18
+ }
19
+ else if (group2 && params[group2]) {
20
+ return `${prefix}${encodeURIComponent(params[group2])}`;
21
+ }
22
+ else if (group3 && params[group3]) {
23
+ return `${prefix}${encodeURIComponent(params[group3])}`;
24
+ }
25
+ return _;
26
+ });
27
+ }
28
+ exports.compilePathnameTemplate = compilePathnameTemplate;
29
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keq",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Request API write by Typescript for flexibility, readability, and a low learning curve.",
5
5
  "keywords": [
6
6
  "request",
@@ -44,7 +44,6 @@
44
44
  "fastq": "^1.16.0",
45
45
  "minimatch": "^9.0.3",
46
46
  "object.fromentries": "^2.0.7",
47
- "path-to-regexp": "^6.2.1",
48
47
  "ts-custom-error": "^3.3.1",
49
48
  "whatwg-url": "^14.0.0"
50
49
  },