@x-requests/core 0.1.0 → 0.2.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 +9 -1
- package/README.md +29 -0
- package/dist/MiddlewareFetcher.d.ts +9 -0
- package/dist/MiddlewareFetcher.js +14 -0
- package/dist/XClient.d.ts +7 -2
- package/dist/XClient.js +19 -2
- package/dist/index.d.ts +1 -0
- package/dist/middleware.d.ts +4 -0
- package/dist/middleware.js +1 -0
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## @x-requests/core 0.2.0
|
|
4
|
+
|
|
5
|
+
- Middleware — `client.use()`, `client.onRequest()`, `client.onResponse()` and `config.middleware`.
|
|
6
|
+
|
|
7
|
+
## @x-requests/codegen 0.1.1
|
|
8
|
+
|
|
9
|
+
- Add README to the package.
|
|
10
|
+
|
|
11
|
+
## 0.1.0 (@x-requests/core, @x-requests/codegen)
|
|
4
12
|
|
|
5
13
|
- Initial public release of `@x-requests/core`: fluent request builder, retries with backoff,
|
|
6
14
|
timeouts, `.fallback()`, `.result()`, typed routes and typed errors.
|
package/README.md
CHANGED
|
@@ -222,6 +222,35 @@ interface Routes {
|
|
|
222
222
|
`result.error` is then `HttpError<404, {message: string}> | HttpError<422, {fields: string[]}> | RequestError`:
|
|
223
223
|
the server may always answer with an undeclared status, so the generic errors stay in the union.
|
|
224
224
|
|
|
225
|
+
### Middleware
|
|
226
|
+
|
|
227
|
+
A middleware wraps every attempt of a request (including retries) and can change the request,
|
|
228
|
+
the response, or skip the call altogether:
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
const off = client.use(async (req, next) => {
|
|
232
|
+
const started = Date.now();
|
|
233
|
+
const response = await next({ ...req, headers: { ...req.headers, Authorization: `Bearer ${token}` } });
|
|
234
|
+
|
|
235
|
+
console.log(req.url, Date.now() - started, 'ms');
|
|
236
|
+
|
|
237
|
+
return response;
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
client.onRequest(req => ({ ...req, headers: { ...req.headers, 'X-Trace': id } })); // before
|
|
241
|
+
client.onResponse(response => unwrap(response)); // after
|
|
242
|
+
|
|
243
|
+
off(); // remove a middleware
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Middlewares can also be passed up front: `xclient({ config: { middleware: [...] } })`.
|
|
247
|
+
|
|
248
|
+
- Order: the first registered middleware is the outermost one, so `onRequest` handlers run in
|
|
249
|
+
registration order and `onResponse` handlers in reverse.
|
|
250
|
+
- Middlewares added later apply to the requests made after that; each client has its own list.
|
|
251
|
+
- An error thrown by a middleware goes through the same rules as a fetcher error (`NetworkError`, retries).
|
|
252
|
+
- A middleware sees the plain request and the raw response (`unknown`), not the types of a route.
|
|
253
|
+
|
|
225
254
|
### Custom fetcher
|
|
226
255
|
|
|
227
256
|
Anything implementing the `Fetcher` interface can be used as the transport,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Middleware } from './middleware.js';
|
|
2
|
+
import type { Fetcher, FetcherRequestParams } from './types.js';
|
|
3
|
+
declare class MiddlewareFetcher implements Fetcher {
|
|
4
|
+
private readonly fetcher;
|
|
5
|
+
private readonly middlewares;
|
|
6
|
+
constructor(fetcher: Fetcher, middlewares: readonly Middleware[]);
|
|
7
|
+
request<T = unknown>(data: FetcherRequestParams): Promise<T>;
|
|
8
|
+
}
|
|
9
|
+
export default MiddlewareFetcher;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { compose } from './middleware.js';
|
|
2
|
+
class MiddlewareFetcher {
|
|
3
|
+
fetcher;
|
|
4
|
+
middlewares;
|
|
5
|
+
constructor(fetcher, middlewares) {
|
|
6
|
+
this.fetcher = fetcher;
|
|
7
|
+
this.middlewares = middlewares;
|
|
8
|
+
}
|
|
9
|
+
request(data) {
|
|
10
|
+
const run = compose(this.middlewares, _data => this.fetcher.request(_data));
|
|
11
|
+
return run(data);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default MiddlewareFetcher;
|
package/dist/XClient.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type { Config, Fetcher } from './types.js';
|
|
1
|
+
import type { Config, Fetcher, FetcherRequestParams } from './types.js';
|
|
2
2
|
import type { ErrorOf, InputOf, PathArgs, RegisteredRoutes, ResponseOf, RoutesConstraint, UrlOf } from './routes.js';
|
|
3
|
+
import type { Middleware } from './middleware.js';
|
|
3
4
|
import XRequest from './XRequest.js';
|
|
4
5
|
declare class XClient<R extends RoutesConstraint<R> = RegisteredRoutes> {
|
|
5
|
-
private readonly fetcher;
|
|
6
6
|
private readonly config?;
|
|
7
|
+
private readonly middlewares;
|
|
8
|
+
private readonly fetcher;
|
|
7
9
|
constructor(fetcher: Fetcher, config?: Config | undefined);
|
|
8
10
|
get<P extends UrlOf<R, 'get'> & string>(url: P, ...path: PathArgs<R, P, 'get'>): XRequest<ResponseOf<R, P, "get">, InputOf<R, P, "get">, ErrorOf<R, P, "get">>;
|
|
9
11
|
post<P extends UrlOf<R, 'post'> & string>(url: P, ...path: PathArgs<R, P, 'post'>): XRequest<ResponseOf<R, P, "post">, InputOf<R, P, "post">, ErrorOf<R, P, "post">>;
|
|
@@ -11,6 +13,9 @@ declare class XClient<R extends RoutesConstraint<R> = RegisteredRoutes> {
|
|
|
11
13
|
patch<P extends UrlOf<R, 'patch'> & string>(url: P, ...path: PathArgs<R, P, 'patch'>): XRequest<ResponseOf<R, P, "patch">, InputOf<R, P, "patch">, ErrorOf<R, P, "patch">>;
|
|
12
14
|
delete<P extends UrlOf<R, 'delete'> & string>(url: P, ...path: PathArgs<R, P, 'delete'>): XRequest<ResponseOf<R, P, "delete">, InputOf<R, P, "delete">, ErrorOf<R, P, "delete">>;
|
|
13
15
|
options<P extends UrlOf<R, 'options'> & string>(url: P, ...path: PathArgs<R, P, 'options'>): XRequest<ResponseOf<R, P, "options">, InputOf<R, P, "options">, ErrorOf<R, P, "options">>;
|
|
16
|
+
use(middleware: Middleware): () => void;
|
|
17
|
+
onRequest(handler: (data: FetcherRequestParams) => FetcherRequestParams | Promise<FetcherRequestParams>): () => void;
|
|
18
|
+
onResponse(handler: (response: unknown) => unknown): () => void;
|
|
14
19
|
private createXRequest;
|
|
15
20
|
}
|
|
16
21
|
export default XClient;
|
package/dist/XClient.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
import MiddlewareFetcher from './MiddlewareFetcher.js';
|
|
1
2
|
import XRequest from './XRequest.js';
|
|
2
3
|
class XClient {
|
|
3
|
-
fetcher;
|
|
4
4
|
config;
|
|
5
|
+
middlewares;
|
|
6
|
+
fetcher;
|
|
5
7
|
constructor(fetcher, config) {
|
|
6
|
-
this.fetcher = fetcher;
|
|
7
8
|
this.config = config;
|
|
9
|
+
this.middlewares = [...(config?.middleware ?? [])];
|
|
10
|
+
this.fetcher = new MiddlewareFetcher(fetcher, this.middlewares);
|
|
8
11
|
}
|
|
9
12
|
get(url, ...path) {
|
|
10
13
|
return this.createXRequest('get', url, path);
|
|
@@ -24,6 +27,20 @@ class XClient {
|
|
|
24
27
|
options(url, ...path) {
|
|
25
28
|
return this.createXRequest('options', url, path);
|
|
26
29
|
}
|
|
30
|
+
use(middleware) {
|
|
31
|
+
this.middlewares.push(middleware);
|
|
32
|
+
return () => {
|
|
33
|
+
const index = this.middlewares.indexOf(middleware);
|
|
34
|
+
if (index !== -1)
|
|
35
|
+
this.middlewares.splice(index, 1);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
onRequest(handler) {
|
|
39
|
+
return this.use(async (data, next) => next(await handler(data)));
|
|
40
|
+
}
|
|
41
|
+
onResponse(handler) {
|
|
42
|
+
return this.use(async (data, next) => handler(await next(data)));
|
|
43
|
+
}
|
|
27
44
|
createXRequest(method, url, args) {
|
|
28
45
|
const request = new XRequest(this.fetcher, method, url, this.config);
|
|
29
46
|
const [path] = args;
|
package/dist/index.d.ts
CHANGED
|
@@ -14,3 +14,4 @@ export type { Result } from './routes.js';
|
|
|
14
14
|
export type { FromOpenApi } from './openapi.js';
|
|
15
15
|
export type { RegisteredRoutes, RouteDef } from './routes.js';
|
|
16
16
|
export type { Config, Fetcher, FetcherRequestParams } from './types.js';
|
|
17
|
+
export type { Middleware, Next } from './middleware.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { FetcherRequestParams } from './types.js';
|
|
2
|
+
export type Next = (data: FetcherRequestParams) => Promise<unknown>;
|
|
3
|
+
export type Middleware = (data: FetcherRequestParams, next: Next) => Promise<unknown>;
|
|
4
|
+
export declare const compose: (middlewares: readonly Middleware[], last: Next) => Next;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const compose = (middlewares, last) => middlewares.reduceRight((next, middleware) => data => middleware(data, next), last);
|
package/dist/types.d.ts
CHANGED