mirror of
https://github.com/versia-pub/api.git
synced 2026-01-26 12:26:02 +01:00
feat(client): ✨ Add global error handler
This commit is contained in:
parent
218af68dcc
commit
f998264300
|
|
@ -81,6 +81,9 @@ export class BaseClient {
|
||||||
constructor(
|
constructor(
|
||||||
protected baseUrl: URL,
|
protected baseUrl: URL,
|
||||||
private accessToken?: string,
|
private accessToken?: string,
|
||||||
|
public globalCatch: (error: ResponseError) => void = () => {
|
||||||
|
// Do nothing by default
|
||||||
|
},
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
get url(): URL {
|
get url(): URL {
|
||||||
|
|
@ -159,9 +162,12 @@ export class BaseClient {
|
||||||
path: string,
|
path: string,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(
|
return this.request<ReturnType>(
|
||||||
this.constructRequest(path, "GET", undefined, extra),
|
this.constructRequest(path, "GET", undefined, extra),
|
||||||
);
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public post<ReturnType>(
|
public post<ReturnType>(
|
||||||
|
|
@ -169,7 +175,12 @@ export class BaseClient {
|
||||||
body?: object,
|
body?: object,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(this.constructRequest(path, "POST", body, extra));
|
return this.request<ReturnType>(
|
||||||
|
this.constructRequest(path, "POST", body, extra),
|
||||||
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public postForm<ReturnType>(
|
public postForm<ReturnType>(
|
||||||
|
|
@ -177,14 +188,17 @@ export class BaseClient {
|
||||||
body: FormData | ConvertibleObject,
|
body: FormData | ConvertibleObject,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(
|
return this.request<ReturnType>(
|
||||||
this.constructRequest(
|
this.constructRequest(
|
||||||
path,
|
path,
|
||||||
"POST",
|
"POST",
|
||||||
body instanceof FormData ? body : objectToFormData(body),
|
body instanceof FormData ? body : objectToFormData(body),
|
||||||
extra,
|
extra,
|
||||||
),
|
),
|
||||||
);
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public put<ReturnType>(
|
public put<ReturnType>(
|
||||||
|
|
@ -192,7 +206,12 @@ export class BaseClient {
|
||||||
body?: object,
|
body?: object,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(this.constructRequest(path, "PUT", body, extra));
|
return this.request<ReturnType>(
|
||||||
|
this.constructRequest(path, "PUT", body, extra),
|
||||||
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public putForm<ReturnType>(
|
public putForm<ReturnType>(
|
||||||
|
|
@ -200,14 +219,17 @@ export class BaseClient {
|
||||||
body: FormData | ConvertibleObject,
|
body: FormData | ConvertibleObject,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(
|
return this.request<ReturnType>(
|
||||||
this.constructRequest(
|
this.constructRequest(
|
||||||
path,
|
path,
|
||||||
"PUT",
|
"PUT",
|
||||||
body instanceof FormData ? body : objectToFormData(body),
|
body instanceof FormData ? body : objectToFormData(body),
|
||||||
extra,
|
extra,
|
||||||
),
|
),
|
||||||
);
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public patch<ReturnType>(
|
public patch<ReturnType>(
|
||||||
|
|
@ -215,7 +237,12 @@ export class BaseClient {
|
||||||
body?: object,
|
body?: object,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(this.constructRequest(path, "PATCH", body, extra));
|
return this.request<ReturnType>(
|
||||||
|
this.constructRequest(path, "PATCH", body, extra),
|
||||||
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public patchForm<ReturnType>(
|
public patchForm<ReturnType>(
|
||||||
|
|
@ -223,14 +250,17 @@ export class BaseClient {
|
||||||
body: FormData | ConvertibleObject,
|
body: FormData | ConvertibleObject,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(
|
return this.request<ReturnType>(
|
||||||
this.constructRequest(
|
this.constructRequest(
|
||||||
path,
|
path,
|
||||||
"PATCH",
|
"PATCH",
|
||||||
body instanceof FormData ? body : objectToFormData(body),
|
body instanceof FormData ? body : objectToFormData(body),
|
||||||
extra,
|
extra,
|
||||||
),
|
),
|
||||||
);
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public delete<ReturnType>(
|
public delete<ReturnType>(
|
||||||
|
|
@ -238,7 +268,12 @@ export class BaseClient {
|
||||||
body?: object,
|
body?: object,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(this.constructRequest(path, "DELETE", body, extra));
|
return this.request<ReturnType>(
|
||||||
|
this.constructRequest(path, "DELETE", body, extra),
|
||||||
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public deleteForm<ReturnType>(
|
public deleteForm<ReturnType>(
|
||||||
|
|
@ -246,13 +281,16 @@ export class BaseClient {
|
||||||
body: FormData | ConvertibleObject,
|
body: FormData | ConvertibleObject,
|
||||||
extra?: RequestInit,
|
extra?: RequestInit,
|
||||||
): Promise<Output<ReturnType>> {
|
): Promise<Output<ReturnType>> {
|
||||||
return this.request(
|
return this.request<ReturnType>(
|
||||||
this.constructRequest(
|
this.constructRequest(
|
||||||
path,
|
path,
|
||||||
"DELETE",
|
"DELETE",
|
||||||
body instanceof FormData ? body : objectToFormData(body),
|
body instanceof FormData ? body : objectToFormData(body),
|
||||||
extra,
|
extra,
|
||||||
),
|
),
|
||||||
);
|
).catch((e) => {
|
||||||
|
this.globalCatch(e);
|
||||||
|
throw e;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue