Skip to content

Commit 7793d0b

Browse files
committed
fix: validation
1 parent 3284772 commit 7793d0b

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/clients/local/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Agent } from "undici";
44
import { z } from "zod/v4";
55

66
import {
7-
createValidationInterceptor,
7+
attachValidationInterceptor,
88
type ValidationConfig,
99
} from "@/utils/axios/interceptors/validation";
1010
import { encode } from "@/utils/base64";
@@ -46,7 +46,7 @@ export class LocalApiClient {
4646

4747
constructor(options: LocalApiClientOptions) {
4848
this.options = localApiClientOptionsSchema.parse(options);
49-
createValidationInterceptor(this.axios);
49+
attachValidationInterceptor(this.axios);
5050
}
5151

5252
private getServerUrl() {

src/clients/remote/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "@/helpers/servers";
1111
import { zv } from "@/schemas/valorant";
1212
import {
13-
createValidationInterceptor,
13+
attachValidationInterceptor,
1414
type ValidationConfig,
1515
} from "@/utils/axios/interceptors/validation";
1616
import { encode } from "@/utils/base64";
@@ -57,7 +57,7 @@ export class RemoteApiClient {
5757

5858
constructor(options: RemoteApiClientOptions) {
5959
this.options = remoteApiClientOptionsSchema.parse(options);
60-
createValidationInterceptor(this.axios);
60+
attachValidationInterceptor(this.axios);
6161
}
6262

6363
get puuid() {

src/utils/axios/interceptors/validation.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,26 @@ export type ValidationConfig<TSchema extends StandardSchemaV1> = {
99
[SCHEMA_KEY]?: TSchema;
1010
};
1111

12-
export function createValidationInterceptor(axiosInstance: AxiosInstance) {
13-
return axiosInstance.interceptors.response.use(async response => {
14-
if (
15-
SCHEMA_KEY in response.config &&
16-
isStandardSchema(response.config[SCHEMA_KEY])
17-
) {
18-
validate(response.config[SCHEMA_KEY], response.data);
19-
}
12+
export function attachValidationInterceptor(axiosInstance: AxiosInstance) {
13+
const responseInterceptorId = axiosInstance.interceptors.response.use(
14+
async response => {
15+
if (
16+
SCHEMA_KEY in response.config &&
17+
isStandardSchema(response.config[SCHEMA_KEY])
18+
) {
19+
response.data = await validate(
20+
response.config[SCHEMA_KEY],
21+
response.data,
22+
);
23+
}
2024

21-
return response;
22-
});
25+
return response;
26+
},
27+
);
28+
29+
return {
30+
eject() {
31+
axiosInstance.interceptors.response.eject(responseInterceptorId);
32+
},
33+
};
2334
}

0 commit comments

Comments
 (0)