Skip to content

Commit

Permalink
feat(client): ✨ Add global error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed Jun 19, 2024
1 parent 218af68 commit f998264
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions client/lysand/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ export class BaseClient {
constructor(
protected baseUrl: URL,
private accessToken?: string,
public globalCatch: (error: ResponseError) => void = () => {
// Do nothing by default
},
) {}

get url(): URL {
Expand Down Expand Up @@ -159,100 +162,135 @@ export class BaseClient {
path: string,
extra?: RequestInit,
): Promise<Output<ReturnType>> {
return this.request(
return this.request<ReturnType>(
this.constructRequest(path, "GET", undefined, extra),
);
).catch((e) => {
this.globalCatch(e);
throw e;
});
}

public post<ReturnType>(
path: string,
body?: object,
extra?: RequestInit,
): 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>(
path: string,
body: FormData | ConvertibleObject,
extra?: RequestInit,
): Promise<Output<ReturnType>> {
return this.request(
return this.request<ReturnType>(
this.constructRequest(
path,
"POST",
body instanceof FormData ? body : objectToFormData(body),
extra,
),
);
).catch((e) => {
this.globalCatch(e);
throw e;
});
}

public put<ReturnType>(
path: string,
body?: object,
extra?: RequestInit,
): 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>(
path: string,
body: FormData | ConvertibleObject,
extra?: RequestInit,
): Promise<Output<ReturnType>> {
return this.request(
return this.request<ReturnType>(
this.constructRequest(
path,
"PUT",
body instanceof FormData ? body : objectToFormData(body),
extra,
),
);
).catch((e) => {
this.globalCatch(e);
throw e;
});
}

public patch<ReturnType>(
path: string,
body?: object,
extra?: RequestInit,
): 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>(
path: string,
body: FormData | ConvertibleObject,
extra?: RequestInit,
): Promise<Output<ReturnType>> {
return this.request(
return this.request<ReturnType>(
this.constructRequest(
path,
"PATCH",
body instanceof FormData ? body : objectToFormData(body),
extra,
),
);
).catch((e) => {
this.globalCatch(e);
throw e;
});
}

public delete<ReturnType>(
path: string,
body?: object,
extra?: RequestInit,
): 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>(
path: string,
body: FormData | ConvertibleObject,
extra?: RequestInit,
): Promise<Output<ReturnType>> {
return this.request(
return this.request<ReturnType>(
this.constructRequest(
path,
"DELETE",
body instanceof FormData ? body : objectToFormData(body),
extra,
),
);
).catch((e) => {
this.globalCatch(e);
throw e;
});
}
}

0 comments on commit f998264

Please sign in to comment.