This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/web/src/api/client.ts

36 lines
949 B
TypeScript
Raw Normal View History

import { NotFoundError, RequestError } from "./errors";
export const VERSION = "v2beta";
export class Client {
makeUrl(...url: string[]): string {
return `/api/${VERSION}/${url.join("/")}/`;
}
fetch<T>(...url: string[]): Promise<T> {
return fetch(this.makeUrl(...url))
.then((r) => {
if (r.status > 300) {
switch (r.status) {
case 404:
2020-11-26 22:35:59 +00:00
throw new NotFoundError(`URL ${this.makeUrl(...url)} not found`);
default:
throw new RequestError(r.statusText);
}
}
return r;
})
.then((r) => r.json())
.then((r) => <T>r);
}
}
export const DefaultClient = new Client();
export interface PBResponse {
count: number;
next: string;
previous: string;
results: Array<any>;
}