@ffflorian/api-client 2.1.6 → 2.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/README.md +1 -1
- package/dist/APIClient.d.ts +2 -5
- package/dist/APIClient.js +4 -2
- package/dist/types.d.ts +10 -4
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# api-client [](https://www.gnu.org/licenses/gpl-3.0) [](https://www.npmjs.com/package/@ffflorian/api-client)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A simple API client using fetch ([Node.js](https://nodejs.org/api/globals.html#fetch) / [Web](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)).
|
|
4
4
|
|
|
5
5
|
## Prerequisites
|
|
6
6
|
|
package/dist/APIClient.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import type { APIResponse, BasicRequestOptions, ApiClientConfig,
|
|
1
|
+
import type { APIResponse, BasicRequestOptions, ApiClientConfig, Interceptors, RequestOptions } from './types.js';
|
|
2
2
|
export declare class APIClient {
|
|
3
3
|
private baseUrl;
|
|
4
4
|
private config?;
|
|
5
|
-
interceptors:
|
|
6
|
-
request: RequestInterceptor[];
|
|
7
|
-
response: ResponseInterceptor[];
|
|
8
|
-
};
|
|
5
|
+
interceptors: Interceptors;
|
|
9
6
|
constructor(baseUrl: string, config?: ApiClientConfig | undefined);
|
|
10
7
|
setBaseURL(baseUrl: string): void;
|
|
11
8
|
setConfig(config: Partial<RequestInit>): void;
|
package/dist/APIClient.js
CHANGED
|
@@ -29,7 +29,7 @@ export class APIClient {
|
|
|
29
29
|
}
|
|
30
30
|
async request(endpoint, options) {
|
|
31
31
|
var _a;
|
|
32
|
-
|
|
32
|
+
let url = new URL(endpoint, this.baseUrl);
|
|
33
33
|
if (options.params) {
|
|
34
34
|
for (const [key, param] of Object.entries(options.params)) {
|
|
35
35
|
if (param !== null && param !== undefined) {
|
|
@@ -39,6 +39,7 @@ export class APIClient {
|
|
|
39
39
|
}
|
|
40
40
|
let requestOptions = {
|
|
41
41
|
method: options.method.toUpperCase(),
|
|
42
|
+
url,
|
|
42
43
|
...this.config,
|
|
43
44
|
};
|
|
44
45
|
if (options.headers) {
|
|
@@ -67,9 +68,10 @@ export class APIClient {
|
|
|
67
68
|
}
|
|
68
69
|
if (this.interceptors.request.length > 0) {
|
|
69
70
|
for (const interceptor of this.interceptors.request) {
|
|
70
|
-
requestOptions = await interceptor(
|
|
71
|
+
requestOptions = { ...requestOptions, ...(await interceptor({ ...requestOptions, url })) };
|
|
71
72
|
}
|
|
72
73
|
}
|
|
74
|
+
url = requestOptions.url;
|
|
73
75
|
const response = await fetch(url, requestOptions);
|
|
74
76
|
if (!response.ok) {
|
|
75
77
|
throw new Error(`Request failed with status code ${response.status}`);
|
package/dist/types.d.ts
CHANGED
|
@@ -11,12 +11,18 @@ export type ApiClientConfig = Partial<Omit<RequestInit, 'method'>> & {
|
|
|
11
11
|
username: string;
|
|
12
12
|
};
|
|
13
13
|
};
|
|
14
|
-
export type BasicRequestOptions = Omit<RequestOptions, 'method'>;
|
|
15
|
-
export type RequestInitWithMethod = Required<Pick<RequestInit, 'method'>> & Omit<RequestInit, 'method'>;
|
|
16
|
-
export type RequestInterceptor = (url: URL, options: RequestInitWithMethod) => RequestInitWithMethod | Promise<RequestInitWithMethod>;
|
|
17
|
-
export type ResponseInterceptor = (response: Response) => void | Promise<void>;
|
|
18
14
|
export interface APIResponse<T> {
|
|
19
15
|
data: T;
|
|
20
16
|
headers: Headers;
|
|
21
17
|
status: number;
|
|
22
18
|
}
|
|
19
|
+
export type BasicRequestOptions = Omit<RequestOptions, 'method'>;
|
|
20
|
+
export interface Interceptors {
|
|
21
|
+
request: RequestInterceptor[];
|
|
22
|
+
response: ResponseInterceptor[];
|
|
23
|
+
}
|
|
24
|
+
export type RequestInitWithMethodAndURL = Required<Pick<RequestInit, 'method'>> & Omit<RequestInit, 'method'> & {
|
|
25
|
+
url: URL;
|
|
26
|
+
};
|
|
27
|
+
export type RequestInterceptor = (options: RequestInitWithMethodAndURL) => RequestInitWithMethodAndURL | Promise<RequestInitWithMethodAndURL>;
|
|
28
|
+
export type ResponseInterceptor = (response: Response) => void | Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Florian Imdahl <git@ffflorian.de>",
|
|
3
|
-
"description": "
|
|
3
|
+
"description": "A simple API client using fetch",
|
|
4
4
|
"devDependencies": {
|
|
5
5
|
"rimraf": "6.1.2",
|
|
6
6
|
"typescript": "5.9.3"
|
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
"keywords": [
|
|
16
16
|
"api-client",
|
|
17
17
|
"fetch",
|
|
18
|
-
"cli",
|
|
19
18
|
"typescript"
|
|
20
19
|
],
|
|
21
20
|
"license": "GPL-3.0",
|
|
@@ -29,5 +28,5 @@
|
|
|
29
28
|
"test": "exit 0"
|
|
30
29
|
},
|
|
31
30
|
"type": "module",
|
|
32
|
-
"version": "2.
|
|
31
|
+
"version": "2.2.0"
|
|
33
32
|
}
|