diff --git a/Client.ts b/Client.ts index 65cfb5d..41b70cf 100644 --- a/Client.ts +++ b/Client.ts @@ -8,13 +8,14 @@ import { Response } from "./Response" //...(this.key ? { authorization: `Bearer ${this.key}` } : undefined), export class Client { onError?: (request: Request, response: Response) => Promise + toError?: (request: Request, response: Response) => Promise onUnauthorized?: (connection: Client) => Promise authorization?: Authorization private f: Fetch constructor( public url?: string, public key?: string, - options?: Partial, "onError" | "onUnauthorized" | "authorization">> & { + options?: Partial, "onError" | "toError" | "onUnauthorized" | "authorization">> & { process?: Middleware } & { fetch: GlobalFetch } ) { @@ -39,10 +40,12 @@ export class Client { private async fetch(path: string, method: Method, body?: any, header?: Request.Header): Promise { const request = Request.create({ url: `${this.url ?? ""}${path}`, method, header, body }) const response = await this.f(request).catch(error => Response.create({ status: 601, body: error })) - return (response.status == 401 && this.onUnauthorized && (await this.onUnauthorized(this))) || - (response.status >= 300 && this.onError && (await this.onError(request, response))) - ? await this.fetch(path, method, body, header) - : ((await response.body) as R | Error) + return ( + ((response.status == 401 && (await this.onUnauthorized?.(this))) || + (response.status >= 300 && (await this.onError?.(request, response))) + ? await this.fetch(path, method, body, header) + : await this.toError?.(request, response)) || ((await response.body) as R | Error) + ) } async get(path: string, header?: Request.Header): Promise { return await this.fetch(path, "GET", undefined, header)