keq 2.6.10 → 2.7.1
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 +19 -0
- package/README.md +1 -0
- package/dist/esm/src/core.d.ts +2 -3
- package/dist/esm/src/core.js +6 -3
- package/dist/esm/src/create-request.js +0 -1
- package/dist/esm/src/middlewares/fetch-arguments-middleware.js +1 -13
- package/dist/esm/src/types/keq-context-request.d.ts +3 -3
- package/dist/esm/src/types/keq-request.d.ts +1 -5
- package/dist/esm/src/util/compile-url.d.ts +1 -0
- package/dist/esm/src/util/{compile-pathname-template.js → compile-url.js} +6 -1
- package/dist/umd/src/core.d.ts +2 -3
- package/dist/umd/src/core.js +8 -5
- package/dist/umd/src/create-request.js +3 -4
- package/dist/umd/src/middlewares/fetch-arguments-middleware.js +2 -14
- package/dist/umd/src/types/keq-context-request.d.ts +3 -3
- package/dist/umd/src/types/keq-request.d.ts +1 -5
- package/dist/umd/src/util/compile-url.d.ts +1 -0
- package/dist/umd/src/util/{compile-pathname-template.js → compile-url.js} +7 -2
- package/package.json +2 -4
- package/dist/esm/src/util/compile-pathname-template.d.ts +0 -1
- package/dist/umd/src/util/compile-pathname-template.d.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
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.7.1](https://github.com/keq-request/keq/compare/v2.7.0...v2.7.1) (2024-07-20)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Performance Improvements
|
|
9
|
+
|
|
10
|
+
* remove whatwg-url because mosts browsers had support ([6877d79](https://github.com/keq-request/keq/commit/6877d79e95721ea76adcf54013c1909b0dec98a3))
|
|
11
|
+
|
|
12
|
+
## [2.7.0](https://github.com/keq-request/keq/compare/v2.6.10...v2.7.0) (2024-07-05)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* add ctx.request.__url__ that return a url merged routeParams ([896c029](https://github.com/keq-request/keq/commit/896c0293e8a0bd0243cd81b780ea2f9e09cbfeea))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* the default parameter of KeqRequest ([d7939d2](https://github.com/keq-request/keq/commit/d7939d2f2e1f79e719db3c663658c05586be10cf))
|
|
23
|
+
|
|
5
24
|
## [2.6.10](https://github.com/keq-request/keq/compare/v2.6.9...v2.6.10) (2024-06-27)
|
|
6
25
|
|
|
7
26
|
|
package/README.md
CHANGED
|
@@ -508,6 +508,7 @@ Keq's context object has many parameters. The following lists all the built-in c
|
|
|
508
508
|
| :------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
509
509
|
| `context.request` | Includes request options for [Fetch API][Fetch MDN]. |
|
|
510
510
|
| `context.request.url` | [URL][URL MDN] Class |
|
|
511
|
+
| `context.request.__url__` | Readonly [URL][URL MDN] Class that merged routeParams |
|
|
511
512
|
| `context.request.method` | One of 'get', 'post', 'put', 'patch', 'head', 'delete'. |
|
|
512
513
|
| `context.request.body` | Object, Array, string or undefined. |
|
|
513
514
|
| `context.request.headers` | The [`Headers`][Headers MDN] Object. |
|
package/dist/esm/src/core.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { URL } from 'whatwg-url';
|
|
2
1
|
import type { KeqContextOptions } from './types/keq-context.js';
|
|
3
2
|
import type { KeqMiddleware } from './types/keq-middleware.js';
|
|
4
3
|
import type { KeqInit } from './types/keq-init.js';
|
|
@@ -9,13 +8,13 @@ import type { KeqContextRequest } from './types/keq-context-request.js';
|
|
|
9
8
|
*/
|
|
10
9
|
export declare class Core<OUTPUT> {
|
|
11
10
|
private requestPromise?;
|
|
12
|
-
protected requestContext: Omit<KeqContextRequest, 'abort'>;
|
|
11
|
+
protected requestContext: Omit<KeqContextRequest, 'abort' | '__url__'>;
|
|
13
12
|
protected __listeners__: KeqListeners;
|
|
14
13
|
protected __global__: Record<string, any>;
|
|
15
14
|
protected __prepend_middlewares__: KeqMiddleware[];
|
|
16
15
|
protected __append_middlewares__: KeqMiddleware[];
|
|
17
16
|
protected __options__: KeqContextOptions;
|
|
18
|
-
constructor(url:
|
|
17
|
+
constructor(url: URL, init: KeqInit, global?: Record<string, any>);
|
|
19
18
|
prependMiddlewares(...middlewares: KeqMiddleware[]): this;
|
|
20
19
|
appendMiddlewares(...middlewares: KeqMiddleware[]): this;
|
|
21
20
|
on<K extends keyof KeqEvents>(event: K, listener: (data: KeqEvents[K]) => void): this;
|
package/dist/esm/src/core.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import mitt from 'mitt';
|
|
2
|
-
import { URL } from 'whatwg-url';
|
|
3
2
|
import { Exception } from './exception/exception.js';
|
|
4
3
|
import { cloneBody } from './util/clone-body.js';
|
|
5
4
|
import { ABORT_PROPERTY, NEXT_INVOKED_PROPERTY, OUTPUT_PROPERTY } from './constant.js';
|
|
6
5
|
import { composeMiddleware } from './util/compose-middleware.js';
|
|
7
6
|
import { shallowClone } from './util/shallow-clone.js';
|
|
7
|
+
import { compileUrl } from './util/compile-url.js';
|
|
8
8
|
/**
|
|
9
9
|
* @description Keq 核心 API,发送请求必要的原子化的API
|
|
10
10
|
*/
|
|
@@ -49,10 +49,13 @@ export class Core {
|
|
|
49
49
|
headers.append(key, value);
|
|
50
50
|
}
|
|
51
51
|
const requestContext = {
|
|
52
|
-
method: this.requestContext.method,
|
|
53
52
|
url: new URL(this.requestContext.url.href),
|
|
54
|
-
headers,
|
|
55
53
|
routeParams: shallowClone(this.requestContext.routeParams),
|
|
54
|
+
get __url__() {
|
|
55
|
+
return compileUrl(this.url, this.routeParams);
|
|
56
|
+
},
|
|
57
|
+
method: this.requestContext.method,
|
|
58
|
+
headers,
|
|
56
59
|
body: cloneBody(this.requestContext.body),
|
|
57
60
|
cache: this.requestContext.cache,
|
|
58
61
|
credentials: this.requestContext.credentials,
|
|
@@ -1,18 +1,6 @@
|
|
|
1
|
-
import { URL } from 'whatwg-url';
|
|
2
1
|
import { Exception } from '../exception/exception.js';
|
|
3
|
-
import { compilePathnameTemplate } from '../util/compile-pathname-template.js';
|
|
4
2
|
import { ABORT_PROPERTY } from '../constant.js';
|
|
5
3
|
import { isBuffer } from "../is/is-buffer.js";
|
|
6
|
-
function compileUrl(obj, routeParams) {
|
|
7
|
-
const url = new URL(typeof obj === 'string' ? obj : obj.href);
|
|
8
|
-
try {
|
|
9
|
-
url.pathname = compilePathnameTemplate(url.pathname, routeParams);
|
|
10
|
-
}
|
|
11
|
-
catch (e) {
|
|
12
|
-
throw new Exception(`Cannot compile the params in ${url.pathname}, Because ${e?.message}.`);
|
|
13
|
-
}
|
|
14
|
-
return url.href;
|
|
15
|
-
}
|
|
16
4
|
function inferContentTypeByBody(body) {
|
|
17
5
|
if (!body)
|
|
18
6
|
return 'text/plain';
|
|
@@ -78,7 +66,7 @@ function compileBody(ctx) {
|
|
|
78
66
|
export function fetchArgumentsMiddleware() {
|
|
79
67
|
return async function fetchArgumentsMiddleware(ctx, next) {
|
|
80
68
|
const request = ctx.request;
|
|
81
|
-
const url =
|
|
69
|
+
const url = ctx.request.__url__.href;
|
|
82
70
|
if (!request.headers.has('Content-Type') && request.body) {
|
|
83
71
|
request.headers.set('Content-Type', inferContentTypeByBody(ctx.request.body));
|
|
84
72
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { URL } from 'whatwg-url';
|
|
2
1
|
export type KeqContextRequestMethod = 'get' | 'post' | 'put' | 'delete' | 'head' | 'patch';
|
|
3
2
|
export type KeqContextRequestBody = object | Array<any> | string | undefined;
|
|
4
3
|
export interface KeqContextRequest {
|
|
5
|
-
url: URL
|
|
4
|
+
url: URL;
|
|
5
|
+
routeParams: Record<string, string>;
|
|
6
|
+
readonly __url__: Readonly<URL>;
|
|
6
7
|
method: KeqContextRequestMethod;
|
|
7
8
|
headers: Headers;
|
|
8
|
-
routeParams: Record<string, string>;
|
|
9
9
|
body: KeqContextRequestBody;
|
|
10
10
|
cache?: RequestCache;
|
|
11
11
|
credentials?: RequestCredentials;
|
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
import { FlattenOperations, KeqOperation, KeqOperations } from './keq-operation.js';
|
|
2
|
-
import type { URL } from 'whatwg-url';
|
|
3
2
|
import type { KeqRouter } from "../router/keq-router.js";
|
|
4
3
|
import type { Keq } from "../keq.js";
|
|
5
4
|
import type { KeqMiddleware } from './keq-middleware.js';
|
|
6
5
|
import type { KeqInit } from './keq-init.js';
|
|
7
|
-
type GlobalURL = globalThis.URL;
|
|
8
6
|
interface KeqRequestFn<OPERATIONS extends Record<string, KeqOperation>> {
|
|
9
7
|
<Path extends keyof OPERATIONS>(url: Path): Keq<OPERATIONS[Path]['responseBody'], OPERATIONS[Path]>;
|
|
10
8
|
<OUTPUT = any>(url: string): Keq<OUTPUT>;
|
|
11
9
|
<OUTPUT = any>(url: URL): Keq<OUTPUT>;
|
|
12
|
-
<OUTPUT = any>(url: GlobalURL): Keq<OUTPUT>;
|
|
13
10
|
}
|
|
14
11
|
type PickKeqOperationsProperty<T extends KeqOperations, P extends keyof T, X extends keyof KeqOperation> = T extends {
|
|
15
12
|
[Path in keyof T as Path extends P ? Path : never]: {
|
|
16
13
|
[Method in keyof T[Path] as T[Path][Method] extends KeqOperation ? Method : never]: infer R;
|
|
17
14
|
};
|
|
18
15
|
} ? R extends KeqOperation ? R[X] : never : never;
|
|
19
|
-
export interface KeqRequest<OPERATIONS extends KeqOperations> {
|
|
16
|
+
export interface KeqRequest<OPERATIONS extends KeqOperations = KeqOperations> {
|
|
20
17
|
<Path extends keyof OPERATIONS>(url: Path, init: Omit<KeqInit, 'global'>): Keq<Exclude<PickKeqOperationsProperty<OPERATIONS, Path, 'responseBody'>, undefined>>;
|
|
21
18
|
<OUTPUT = any>(url: string, init: Omit<KeqInit, 'global'>): Keq<OUTPUT>;
|
|
22
19
|
<OUTPUT = any>(url: URL, init: Omit<KeqInit, 'global'>): Keq<OUTPUT>;
|
|
23
|
-
<OUTPUT = any>(url: GlobalURL, init: Omit<KeqInit, 'global'>): Keq<OUTPUT>;
|
|
24
20
|
baseOrigin: (baseOrigin: string) => void;
|
|
25
21
|
get: KeqRequestFn<FlattenOperations<OPERATIONS, 'get'>>;
|
|
26
22
|
post: KeqRequestFn<FlattenOperations<OPERATIONS, 'post'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function compileUrl(obj: string | URL, routeParams: Record<string, string | number>): URL;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
function compilePathnameTemplate(template, params) {
|
|
2
2
|
return template
|
|
3
3
|
.replace(/(^|\/)(?::([^/]+)|{([^/]+)}|%7B([^/]+)%7D)(?=$|\/)/g, (_, prefix, group1, group2, group3) => {
|
|
4
4
|
if (group1 && params[group1]) {
|
|
@@ -13,3 +13,8 @@ export function compilePathnameTemplate(template, params) {
|
|
|
13
13
|
return _;
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
+
export function compileUrl(obj, routeParams) {
|
|
17
|
+
const url = new URL(typeof obj === 'string' ? obj : obj.href);
|
|
18
|
+
url.pathname = compilePathnameTemplate(url.pathname, routeParams);
|
|
19
|
+
return url;
|
|
20
|
+
}
|
package/dist/umd/src/core.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { URL } from 'whatwg-url';
|
|
2
1
|
import type { KeqContextOptions } from './types/keq-context.js';
|
|
3
2
|
import type { KeqMiddleware } from './types/keq-middleware.js';
|
|
4
3
|
import type { KeqInit } from './types/keq-init.js';
|
|
@@ -9,13 +8,13 @@ import type { KeqContextRequest } from './types/keq-context-request.js';
|
|
|
9
8
|
*/
|
|
10
9
|
export declare class Core<OUTPUT> {
|
|
11
10
|
private requestPromise?;
|
|
12
|
-
protected requestContext: Omit<KeqContextRequest, 'abort'>;
|
|
11
|
+
protected requestContext: Omit<KeqContextRequest, 'abort' | '__url__'>;
|
|
13
12
|
protected __listeners__: KeqListeners;
|
|
14
13
|
protected __global__: Record<string, any>;
|
|
15
14
|
protected __prepend_middlewares__: KeqMiddleware[];
|
|
16
15
|
protected __append_middlewares__: KeqMiddleware[];
|
|
17
16
|
protected __options__: KeqContextOptions;
|
|
18
|
-
constructor(url:
|
|
17
|
+
constructor(url: URL, init: KeqInit, global?: Record<string, any>);
|
|
19
18
|
prependMiddlewares(...middlewares: KeqMiddleware[]): this;
|
|
20
19
|
appendMiddlewares(...middlewares: KeqMiddleware[]): this;
|
|
21
20
|
on<K extends keyof KeqEvents>(event: K, listener: (data: KeqEvents[K]) => void): this;
|
package/dist/umd/src/core.js
CHANGED
|
@@ -7,19 +7,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
7
7
|
if (v !== undefined) module.exports = v;
|
|
8
8
|
}
|
|
9
9
|
else if (typeof define === "function" && define.amd) {
|
|
10
|
-
define(["require", "exports", "mitt", "
|
|
10
|
+
define(["require", "exports", "mitt", "./exception/exception.js", "./util/clone-body.js", "./constant.js", "./util/compose-middleware.js", "./util/shallow-clone.js", "./util/compile-url.js"], factory);
|
|
11
11
|
}
|
|
12
12
|
})(function (require, exports) {
|
|
13
13
|
"use strict";
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.Core = void 0;
|
|
16
16
|
const mitt_1 = __importDefault(require("mitt"));
|
|
17
|
-
const whatwg_url_1 = require("whatwg-url");
|
|
18
17
|
const exception_js_1 = require("./exception/exception.js");
|
|
19
18
|
const clone_body_js_1 = require("./util/clone-body.js");
|
|
20
19
|
const constant_js_1 = require("./constant.js");
|
|
21
20
|
const compose_middleware_js_1 = require("./util/compose-middleware.js");
|
|
22
21
|
const shallow_clone_js_1 = require("./util/shallow-clone.js");
|
|
22
|
+
const compile_url_js_1 = require("./util/compile-url.js");
|
|
23
23
|
/**
|
|
24
24
|
* @description Keq 核心 API,发送请求必要的原子化的API
|
|
25
25
|
*/
|
|
@@ -42,7 +42,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
42
42
|
routeParams: {},
|
|
43
43
|
body: undefined,
|
|
44
44
|
...init,
|
|
45
|
-
url: new
|
|
45
|
+
url: new URL(url.href),
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
48
|
prependMiddlewares(...middlewares) {
|
|
@@ -64,10 +64,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
64
64
|
headers.append(key, value);
|
|
65
65
|
}
|
|
66
66
|
const requestContext = {
|
|
67
|
+
url: new URL(this.requestContext.url.href),
|
|
68
|
+
routeParams: (0, shallow_clone_js_1.shallowClone)(this.requestContext.routeParams),
|
|
69
|
+
get __url__() {
|
|
70
|
+
return (0, compile_url_js_1.compileUrl)(this.url, this.routeParams);
|
|
71
|
+
},
|
|
67
72
|
method: this.requestContext.method,
|
|
68
|
-
url: new whatwg_url_1.URL(this.requestContext.url.href),
|
|
69
73
|
headers,
|
|
70
|
-
routeParams: (0, shallow_clone_js_1.shallowClone)(this.requestContext.routeParams),
|
|
71
74
|
body: (0, clone_body_js_1.cloneBody)(this.requestContext.body),
|
|
72
75
|
cache: this.requestContext.cache,
|
|
73
76
|
credentials: this.requestContext.credentials,
|
|
@@ -4,14 +4,13 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports", "
|
|
7
|
+
define(["require", "exports", "./is/is-browser.js", "./keq.js", "./middlewares/abort-flow-control-middleware.js", "./middlewares/fetch-arguments-middleware.js", "./middlewares/fetch-middleware.js", "./middlewares/proxy-response-middleware.js", "./middlewares/retry-middleware.js", "./middlewares/serial-flow-control-middleware.js", "./router/keq-router.js", "./middlewares/timeout-middleware.js"], factory);
|
|
8
8
|
}
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.createRequest = void 0;
|
|
13
13
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
14
|
-
const whatwg_url_1 = require("whatwg-url");
|
|
15
14
|
const is_browser_js_1 = require("./is/is-browser.js");
|
|
16
15
|
const keq_js_1 = require("./keq.js");
|
|
17
16
|
const abort_flow_control_middleware_js_1 = require("./middlewares/abort-flow-control-middleware.js");
|
|
@@ -49,9 +48,9 @@
|
|
|
49
48
|
const global = {};
|
|
50
49
|
const formatUrl = (url) => {
|
|
51
50
|
if (typeof url === 'string') {
|
|
52
|
-
return new
|
|
51
|
+
return new URL(url, baseOrigin);
|
|
53
52
|
}
|
|
54
|
-
return new
|
|
53
|
+
return new URL(url.href);
|
|
55
54
|
};
|
|
56
55
|
const router = new keq_router_js_1.KeqRouter(prependMiddlewares);
|
|
57
56
|
const request = function (url, init) {
|
|
@@ -4,27 +4,15 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports", "
|
|
7
|
+
define(["require", "exports", "../exception/exception.js", "../constant.js", "../is/is-buffer.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 whatwg_url_1 = require("whatwg-url");
|
|
14
13
|
const exception_js_1 = require("../exception/exception.js");
|
|
15
|
-
const compile_pathname_template_js_1 = require("../util/compile-pathname-template.js");
|
|
16
14
|
const constant_js_1 = require("../constant.js");
|
|
17
15
|
const is_buffer_js_1 = require("../is/is-buffer.js");
|
|
18
|
-
function compileUrl(obj, routeParams) {
|
|
19
|
-
const url = new whatwg_url_1.URL(typeof obj === 'string' ? obj : obj.href);
|
|
20
|
-
try {
|
|
21
|
-
url.pathname = (0, compile_pathname_template_js_1.compilePathnameTemplate)(url.pathname, routeParams);
|
|
22
|
-
}
|
|
23
|
-
catch (e) {
|
|
24
|
-
throw new exception_js_1.Exception(`Cannot compile the params in ${url.pathname}, Because ${e?.message}.`);
|
|
25
|
-
}
|
|
26
|
-
return url.href;
|
|
27
|
-
}
|
|
28
16
|
function inferContentTypeByBody(body) {
|
|
29
17
|
if (!body)
|
|
30
18
|
return 'text/plain';
|
|
@@ -90,7 +78,7 @@
|
|
|
90
78
|
function fetchArgumentsMiddleware() {
|
|
91
79
|
return async function fetchArgumentsMiddleware(ctx, next) {
|
|
92
80
|
const request = ctx.request;
|
|
93
|
-
const url =
|
|
81
|
+
const url = ctx.request.__url__.href;
|
|
94
82
|
if (!request.headers.has('Content-Type') && request.body) {
|
|
95
83
|
request.headers.set('Content-Type', inferContentTypeByBody(ctx.request.body));
|
|
96
84
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { URL } from 'whatwg-url';
|
|
2
1
|
export type KeqContextRequestMethod = 'get' | 'post' | 'put' | 'delete' | 'head' | 'patch';
|
|
3
2
|
export type KeqContextRequestBody = object | Array<any> | string | undefined;
|
|
4
3
|
export interface KeqContextRequest {
|
|
5
|
-
url: URL
|
|
4
|
+
url: URL;
|
|
5
|
+
routeParams: Record<string, string>;
|
|
6
|
+
readonly __url__: Readonly<URL>;
|
|
6
7
|
method: KeqContextRequestMethod;
|
|
7
8
|
headers: Headers;
|
|
8
|
-
routeParams: Record<string, string>;
|
|
9
9
|
body: KeqContextRequestBody;
|
|
10
10
|
cache?: RequestCache;
|
|
11
11
|
credentials?: RequestCredentials;
|
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
import { FlattenOperations, KeqOperation, KeqOperations } from './keq-operation.js';
|
|
2
|
-
import type { URL } from 'whatwg-url';
|
|
3
2
|
import type { KeqRouter } from "../router/keq-router.js";
|
|
4
3
|
import type { Keq } from "../keq.js";
|
|
5
4
|
import type { KeqMiddleware } from './keq-middleware.js';
|
|
6
5
|
import type { KeqInit } from './keq-init.js';
|
|
7
|
-
type GlobalURL = globalThis.URL;
|
|
8
6
|
interface KeqRequestFn<OPERATIONS extends Record<string, KeqOperation>> {
|
|
9
7
|
<Path extends keyof OPERATIONS>(url: Path): Keq<OPERATIONS[Path]['responseBody'], OPERATIONS[Path]>;
|
|
10
8
|
<OUTPUT = any>(url: string): Keq<OUTPUT>;
|
|
11
9
|
<OUTPUT = any>(url: URL): Keq<OUTPUT>;
|
|
12
|
-
<OUTPUT = any>(url: GlobalURL): Keq<OUTPUT>;
|
|
13
10
|
}
|
|
14
11
|
type PickKeqOperationsProperty<T extends KeqOperations, P extends keyof T, X extends keyof KeqOperation> = T extends {
|
|
15
12
|
[Path in keyof T as Path extends P ? Path : never]: {
|
|
16
13
|
[Method in keyof T[Path] as T[Path][Method] extends KeqOperation ? Method : never]: infer R;
|
|
17
14
|
};
|
|
18
15
|
} ? R extends KeqOperation ? R[X] : never : never;
|
|
19
|
-
export interface KeqRequest<OPERATIONS extends KeqOperations> {
|
|
16
|
+
export interface KeqRequest<OPERATIONS extends KeqOperations = KeqOperations> {
|
|
20
17
|
<Path extends keyof OPERATIONS>(url: Path, init: Omit<KeqInit, 'global'>): Keq<Exclude<PickKeqOperationsProperty<OPERATIONS, Path, 'responseBody'>, undefined>>;
|
|
21
18
|
<OUTPUT = any>(url: string, init: Omit<KeqInit, 'global'>): Keq<OUTPUT>;
|
|
22
19
|
<OUTPUT = any>(url: URL, init: Omit<KeqInit, 'global'>): Keq<OUTPUT>;
|
|
23
|
-
<OUTPUT = any>(url: GlobalURL, init: Omit<KeqInit, 'global'>): Keq<OUTPUT>;
|
|
24
20
|
baseOrigin: (baseOrigin: string) => void;
|
|
25
21
|
get: KeqRequestFn<FlattenOperations<OPERATIONS, 'get'>>;
|
|
26
22
|
post: KeqRequestFn<FlattenOperations<OPERATIONS, 'post'>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function compileUrl(obj: string | URL, routeParams: Record<string, string | number>): URL;
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.compileUrl = void 0;
|
|
13
13
|
function compilePathnameTemplate(template, params) {
|
|
14
14
|
return template
|
|
15
15
|
.replace(/(^|\/)(?::([^/]+)|{([^/]+)}|%7B([^/]+)%7D)(?=$|\/)/g, (_, prefix, group1, group2, group3) => {
|
|
@@ -25,5 +25,10 @@
|
|
|
25
25
|
return _;
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
function compileUrl(obj, routeParams) {
|
|
29
|
+
const url = new URL(typeof obj === 'string' ? obj : obj.href);
|
|
30
|
+
url.pathname = compilePathnameTemplate(url.pathname, routeParams);
|
|
31
|
+
return url;
|
|
32
|
+
}
|
|
33
|
+
exports.compileUrl = compileUrl;
|
|
29
34
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keq",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.1",
|
|
4
4
|
"description": "Request API write by Typescript for flexibility, readability, and a low learning curve.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"request",
|
|
@@ -40,12 +40,10 @@
|
|
|
40
40
|
"test": "jest"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@types/whatwg-url": "^11.0.5",
|
|
44
43
|
"fastq": "^1.17.1",
|
|
45
44
|
"minimatch": "^9.0.4",
|
|
46
45
|
"mitt": "^3.0.1",
|
|
47
|
-
"ts-custom-error": "^3.3.1"
|
|
48
|
-
"whatwg-url": "^14.0.0"
|
|
46
|
+
"ts-custom-error": "^3.3.1"
|
|
49
47
|
},
|
|
50
48
|
"devDependencies": {
|
|
51
49
|
"@buka/eslint-config": "^1.6.4",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function compilePathnameTemplate(template: string, params: Record<string, string | number>): string;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function compilePathnameTemplate(template: string, params: Record<string, string | number>): string;
|