web: allow setting of querystring arguments with API Client, update table
This commit is contained in:
		| @ -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]); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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); | ||||
|                     } | ||||
|  | ||||
| @ -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"]); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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 | ||||
|     ); | ||||
| } | ||||
|  | ||||
| @ -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"]); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer