Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { Response } from "./Response"
//...(this.key ? { authorization: `Bearer ${this.key}` } : undefined),
export class Client<Error = never> {
onError?: (request: Request, response: Response) => Promise<boolean>
toError?: (request: Request, response: Response) => Promise<Error>
onUnauthorized?: (connection: Client<Error>) => Promise<boolean>
authorization?: Authorization
private f: Fetch
constructor(
public url?: string,
public key?: string,
options?: Partial<Pick<Client<Error>, "onError" | "onUnauthorized" | "authorization">> & {
options?: Partial<Pick<Client<Error>, "onError" | "toError" | "onUnauthorized" | "authorization">> & {
process?: Middleware<any, Body, BodyInit, any>
} & { fetch: GlobalFetch }
) {
Expand All @@ -39,10 +40,12 @@ export class Client<Error = never> {
private async fetch<R>(path: string, method: Method, body?: any, header?: Request.Header): Promise<R | Error> {
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<R>(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<R>(path, method, body, header)
: await this.toError?.(request, response)) || ((await response.body) as R | Error)
)
}
async get<R>(path: string, header?: Request.Header): Promise<R | Error> {
return await this.fetch<R>(path, "GET", undefined, header)
Expand Down