web: allow setting of querystring arguments with API Client, update table

This commit is contained in:
Jens Langhammer
2020-11-29 12:01:06 +01:00
parent 7f821c484c
commit 66b3635648
10 changed files with 87 additions and 64 deletions

View File

@ -13,6 +13,6 @@ export class Application {
policies?: string[];
static get(slug: string): Promise<Application> {
return DefaultClient.fetch<Application>("core", "applications", slug);
return DefaultClient.fetch<Application>(["core", "applications", slug]);
}
}

View File

@ -3,17 +3,25 @@ import { NotFoundError, RequestError } from "./errors";
export const VERSION = "v2beta";
export class Client {
makeUrl(...url: string[]): string {
return `/api/${VERSION}/${url.join("/")}/`;
makeUrl(url: string[], query?: { [key: string]: string }): string {
let builtUrl = `/api/${VERSION}/${url.join("/")}/`;
if (query) {
let queryString = Object.keys(query)
.map((k) => encodeURIComponent(k) + "=" + encodeURIComponent(query[k]))
.join("&");
builtUrl += `?${queryString}`;
}
return builtUrl;
}
fetch<T>(...url: string[]): Promise<T> {
return fetch(this.makeUrl(...url))
fetch<T>(url: string[], query?: { [key: string]: string }): Promise<T> {
const finalUrl = this.makeUrl(url, query);
return fetch(finalUrl)
.then((r) => {
if (r.status > 300) {
switch (r.status) {
case 404:
throw new NotFoundError(`URL ${this.makeUrl(...url)} not found`);
throw new NotFoundError(`URL ${finalUrl} not found`);
default:
throw new RequestError(r.statusText);
}

View File

@ -5,6 +5,6 @@ export class Config {
branding_title?: string;
static get(): Promise<Config> {
return DefaultClient.fetch<Config>("root", "config");
return DefaultClient.fetch<Config>(["root", "config"]);
}
}

View File

@ -5,7 +5,7 @@ interface TokenResponse {
}
export function tokenByIdentifier(identifier: string): Promise<string> {
return DefaultClient.fetch<TokenResponse>("core", "tokens", identifier, "view_key").then(
return DefaultClient.fetch<TokenResponse>(["core", "tokens", identifier, "view_key"]).then(
(r) => r.key
);
}

View File

@ -10,6 +10,6 @@ export class User {
avatar?: string;
static me(): Promise<User> {
return DefaultClient.fetch<User>("core", "users", "me");
return DefaultClient.fetch<User>(["core", "users", "me"]);
}
}