Skip to content

Commit 8351abf

Browse files
committed
feat(api-client): Move URL to config in interceptor
1 parent 8cde960 commit 8351abf

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

packages/api-client/src/APIClient.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
BasicRequestOptions,
44
ApiClientConfig,
55
Interceptors,
6-
RequestInitWithMethod,
6+
RequestInitWithMethodAndURL,
77
RequestOptions,
88
} from './types.js';
99

@@ -42,7 +42,7 @@ export class APIClient {
4242
}
4343

4444
async request(endpoint: string, options: RequestOptions): Promise<Response> {
45-
const url = new URL(endpoint, this.baseUrl);
45+
let url = new URL(endpoint, this.baseUrl);
4646

4747
if (options.params) {
4848
for (const [key, param] of Object.entries(options.params)) {
@@ -52,8 +52,9 @@ export class APIClient {
5252
}
5353
}
5454

55-
let requestOptions: RequestInitWithMethod = {
55+
let requestOptions: RequestInitWithMethodAndURL = {
5656
method: options.method.toUpperCase(),
57+
url,
5758
...this.config,
5859
};
5960

@@ -87,10 +88,12 @@ export class APIClient {
8788

8889
if (this.interceptors.request.length > 0) {
8990
for (const interceptor of this.interceptors.request) {
90-
requestOptions = await interceptor(url, requestOptions);
91+
requestOptions = {...requestOptions, ...(await interceptor({...requestOptions, url}))};
9192
}
9293
}
9394

95+
url = requestOptions.url;
96+
9497
const response = await fetch(url, requestOptions);
9598
if (!response.ok) {
9699
throw new Error(`Request failed with status code ${response.status}`);

packages/api-client/src/types.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ export interface Interceptors {
3535
response: ResponseInterceptor[];
3636
}
3737

38-
export type RequestInitWithMethod = Required<Pick<RequestInit, 'method'>> & Omit<RequestInit, 'method'>;
38+
export type RequestInitWithMethodAndURL = Required<Pick<RequestInit, 'method'>> &
39+
Omit<RequestInit, 'method'> & {
40+
url: URL;
41+
};
3942

4043
export type RequestInterceptor = (
41-
url: URL,
42-
options: RequestInitWithMethod
43-
) => RequestInitWithMethod | Promise<RequestInitWithMethod>;
44+
options: RequestInitWithMethodAndURL
45+
) => RequestInitWithMethodAndURL | Promise<RequestInitWithMethodAndURL>;
4446

4547
export type ResponseInterceptor = (response: Response) => void | Promise<void>;

0 commit comments

Comments
 (0)