@ffflorian/api-client 2.4.1 → 2.4.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/dist/APIClient.d.ts +6 -6
- package/dist/APIClient.js +50 -50
- package/dist/types.d.ts +13 -13
- package/package.json +3 -2
package/dist/APIClient.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import type { APIResponse, BasicRequestOptions,
|
|
1
|
+
import type { ApiClientConfig, APIResponse, BasicRequestOptions, Interceptors, RequestOptions } from './types.js';
|
|
2
2
|
export declare class APIClient {
|
|
3
3
|
private baseUrl;
|
|
4
4
|
private config?;
|
|
5
5
|
interceptors: Interceptors;
|
|
6
6
|
constructor(baseUrl: string, config?: ApiClientConfig | undefined);
|
|
7
|
-
setBaseURL(baseUrl: string): void;
|
|
8
|
-
setConfig(config: Partial<RequestInit>): void;
|
|
9
|
-
private formatData;
|
|
10
|
-
request(endpoint: string, options: RequestOptions): Promise<Response>;
|
|
11
7
|
delete<T = any>(endpoint: string, options?: BasicRequestOptions): Promise<APIResponse<T>>;
|
|
12
8
|
get<T = any>(endpoint: string, options?: BasicRequestOptions): Promise<APIResponse<T>>;
|
|
13
9
|
head<T = any>(endpoint: string, options?: BasicRequestOptions): Promise<APIResponse<T>>;
|
|
14
|
-
patch<T = any>(endpoint: string, data?: any, options?: BasicRequestOptions): Promise<APIResponse<T>>;
|
|
15
10
|
options<T = any>(endpoint: string, options?: BasicRequestOptions): Promise<APIResponse<T>>;
|
|
11
|
+
patch<T = any>(endpoint: string, data?: any, options?: BasicRequestOptions): Promise<APIResponse<T>>;
|
|
16
12
|
post<T = any>(endpoint: string, data?: any, options?: BasicRequestOptions): Promise<APIResponse<T>>;
|
|
17
13
|
put<T = any>(endpoint: string, data?: any, options?: BasicRequestOptions): Promise<APIResponse<T>>;
|
|
14
|
+
request(endpoint: string, options: RequestOptions): Promise<Response>;
|
|
15
|
+
setBaseURL(baseUrl: string): void;
|
|
16
|
+
setConfig(config: Partial<RequestInit>): void;
|
|
17
|
+
private formatData;
|
|
18
18
|
}
|
package/dist/APIClient.js
CHANGED
|
@@ -7,25 +7,40 @@ export class APIClient {
|
|
|
7
7
|
response: [],
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
this.
|
|
10
|
+
async delete(endpoint, options) {
|
|
11
|
+
const request = await this.request(endpoint, { ...options, method: 'DELETE' });
|
|
12
|
+
const requestData = await this.formatData(request, options);
|
|
13
|
+
return { data: requestData, headers: request.headers, status: request.status };
|
|
12
14
|
}
|
|
13
|
-
|
|
14
|
-
this.
|
|
15
|
+
async get(endpoint, options) {
|
|
16
|
+
const request = await this.request(endpoint, { ...options, method: 'GET' });
|
|
17
|
+
const requestData = await this.formatData(request, options);
|
|
18
|
+
return { data: requestData, headers: request.headers, status: request.status };
|
|
15
19
|
}
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
20
|
+
async head(endpoint, options) {
|
|
21
|
+
const request = await this.request(endpoint, { ...options, method: 'HEAD' });
|
|
22
|
+
const requestData = await this.formatData(request, options);
|
|
23
|
+
return { data: requestData, headers: request.headers, status: request.status };
|
|
24
|
+
}
|
|
25
|
+
async options(endpoint, options) {
|
|
26
|
+
const request = await this.request(endpoint, { ...options, method: 'OPTIONS' });
|
|
27
|
+
const requestData = await this.formatData(request, options);
|
|
28
|
+
return { data: requestData, headers: request.headers, status: request.status };
|
|
29
|
+
}
|
|
30
|
+
async patch(endpoint, data, options) {
|
|
31
|
+
const request = await this.request(endpoint, { ...options, data, method: 'PATCH' });
|
|
32
|
+
const requestData = await this.formatData(request, options);
|
|
33
|
+
return { data: requestData, headers: request.headers, status: request.status };
|
|
34
|
+
}
|
|
35
|
+
async post(endpoint, data, options) {
|
|
36
|
+
const request = await this.request(endpoint, { data, ...options, method: 'POST' });
|
|
37
|
+
const requestData = await this.formatData(request, options);
|
|
38
|
+
return { data: requestData, headers: request.headers, status: request.status };
|
|
39
|
+
}
|
|
40
|
+
async put(endpoint, data, options) {
|
|
41
|
+
const request = await this.request(endpoint, { data, ...options, method: 'PUT' });
|
|
42
|
+
const requestData = await this.formatData(request, options);
|
|
43
|
+
return { data: requestData, headers: request.headers, status: request.status };
|
|
29
44
|
}
|
|
30
45
|
async request(endpoint, options) {
|
|
31
46
|
var _a;
|
|
@@ -49,7 +64,7 @@ export class APIClient {
|
|
|
49
64
|
};
|
|
50
65
|
}
|
|
51
66
|
if ((_a = this.config) === null || _a === void 0 ? void 0 : _a.auth) {
|
|
52
|
-
const {
|
|
67
|
+
const { password, username } = this.config.auth;
|
|
53
68
|
const encoded = btoa(`${username}:${password}`);
|
|
54
69
|
requestOptions.headers = {
|
|
55
70
|
...requestOptions.headers,
|
|
@@ -95,39 +110,24 @@ export class APIClient {
|
|
|
95
110
|
}
|
|
96
111
|
return response;
|
|
97
112
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const requestData = await this.formatData(request, options);
|
|
101
|
-
return { data: requestData, headers: request.headers, status: request.status };
|
|
102
|
-
}
|
|
103
|
-
async get(endpoint, options) {
|
|
104
|
-
const request = await this.request(endpoint, { ...options, method: 'GET' });
|
|
105
|
-
const requestData = await this.formatData(request, options);
|
|
106
|
-
return { data: requestData, headers: request.headers, status: request.status };
|
|
107
|
-
}
|
|
108
|
-
async head(endpoint, options) {
|
|
109
|
-
const request = await this.request(endpoint, { ...options, method: 'HEAD' });
|
|
110
|
-
const requestData = await this.formatData(request, options);
|
|
111
|
-
return { data: requestData, headers: request.headers, status: request.status };
|
|
112
|
-
}
|
|
113
|
-
async patch(endpoint, data, options) {
|
|
114
|
-
const request = await this.request(endpoint, { ...options, data, method: 'PATCH' });
|
|
115
|
-
const requestData = await this.formatData(request, options);
|
|
116
|
-
return { data: requestData, headers: request.headers, status: request.status };
|
|
117
|
-
}
|
|
118
|
-
async options(endpoint, options) {
|
|
119
|
-
const request = await this.request(endpoint, { ...options, method: 'OPTIONS' });
|
|
120
|
-
const requestData = await this.formatData(request, options);
|
|
121
|
-
return { data: requestData, headers: request.headers, status: request.status };
|
|
113
|
+
setBaseURL(baseUrl) {
|
|
114
|
+
this.baseUrl = baseUrl;
|
|
122
115
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const requestData = await this.formatData(request, options);
|
|
126
|
-
return { data: requestData, headers: request.headers, status: request.status };
|
|
116
|
+
setConfig(config) {
|
|
117
|
+
this.config = config;
|
|
127
118
|
}
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
119
|
+
formatData(response, options) {
|
|
120
|
+
const responseType = (options === null || options === void 0 ? void 0 : options.responseType) || 'json';
|
|
121
|
+
switch (responseType) {
|
|
122
|
+
case 'arraybuffer':
|
|
123
|
+
return response.arrayBuffer();
|
|
124
|
+
case 'blob':
|
|
125
|
+
return response.blob();
|
|
126
|
+
case 'text':
|
|
127
|
+
return response.text();
|
|
128
|
+
case 'json':
|
|
129
|
+
default:
|
|
130
|
+
return response.json();
|
|
131
|
+
}
|
|
132
132
|
}
|
|
133
133
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
export
|
|
2
|
-
data?: any;
|
|
3
|
-
headers?: HeadersInit;
|
|
4
|
-
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options';
|
|
5
|
-
params?: object;
|
|
6
|
-
responseType?: 'arraybuffer' | 'json' | 'text' | 'blob';
|
|
7
|
-
}
|
|
8
|
-
export type ApiClientConfig = Partial<Omit<RequestInit, 'method'>> & {
|
|
1
|
+
export type ApiClientConfig = {
|
|
9
2
|
auth?: {
|
|
10
3
|
password: string;
|
|
11
4
|
username: string;
|
|
12
5
|
};
|
|
13
|
-
}
|
|
6
|
+
} & Partial<Omit<RequestInit, 'method'>>;
|
|
14
7
|
export interface APIResponse<T> {
|
|
15
8
|
data: T;
|
|
16
9
|
headers: Headers;
|
|
@@ -21,8 +14,15 @@ export interface Interceptors {
|
|
|
21
14
|
request: RequestInterceptor[];
|
|
22
15
|
response: ResponseInterceptor[];
|
|
23
16
|
}
|
|
24
|
-
export type RequestInitWithMethodAndURL =
|
|
17
|
+
export type RequestInitWithMethodAndURL = {
|
|
25
18
|
url: URL;
|
|
26
|
-
}
|
|
27
|
-
export type RequestInterceptor = (options: RequestInitWithMethodAndURL) => RequestInitWithMethodAndURL |
|
|
28
|
-
export
|
|
19
|
+
} & Omit<RequestInit, 'method'> & Required<Pick<RequestInit, 'method'>>;
|
|
20
|
+
export type RequestInterceptor = (options: RequestInitWithMethodAndURL) => Promise<RequestInitWithMethodAndURL> | RequestInitWithMethodAndURL;
|
|
21
|
+
export interface RequestOptions {
|
|
22
|
+
data?: any;
|
|
23
|
+
headers?: HeadersInit;
|
|
24
|
+
method: 'DELETE' | 'delete' | 'GET' | 'get' | 'HEAD' | 'head' | 'OPTIONS' | 'options' | 'PATCH' | 'patch' | 'POST' | 'post' | 'PUT' | 'put';
|
|
25
|
+
params?: object;
|
|
26
|
+
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
|
|
27
|
+
}
|
|
28
|
+
export type ResponseInterceptor = (response: Response) => Promise<void> | void;
|
package/package.json
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
"author": "Florian Imdahl <git@ffflorian.de>",
|
|
3
3
|
"description": "A simple API client using fetch",
|
|
4
4
|
"devDependencies": {
|
|
5
|
+
"@types/node": "~24",
|
|
5
6
|
"rimraf": "6.1.2",
|
|
6
7
|
"typescript": "5.9.3"
|
|
7
8
|
},
|
|
@@ -29,6 +30,6 @@
|
|
|
29
30
|
"test": "exit 0"
|
|
30
31
|
},
|
|
31
32
|
"type": "module",
|
|
32
|
-
"version": "2.4.
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"version": "2.4.2",
|
|
34
|
+
"gitHead": "59997e7c64b551213644945256b13ff3ba1ccfcd"
|
|
34
35
|
}
|