web: add update method
This commit is contained in:
		| @ -1,3 +1,4 @@ | ||||
| import { getCookie } from "../utils"; | ||||
| import { NotFoundError, RequestError } from "./Error"; | ||||
|  | ||||
| export const VERSION = "v2beta"; | ||||
| @ -35,6 +36,36 @@ export class Client { | ||||
|             .then((r) => r.json()) | ||||
|             .then((r) => <T>r); | ||||
|     } | ||||
|  | ||||
|     update<T>(url: string[], body: T, query?: QueryArguments): Promise<T> { | ||||
|         const finalUrl = this.makeUrl(url, query); | ||||
|         const csrftoken = getCookie("authentik_csrf"); | ||||
|         const request = new Request(finalUrl, { | ||||
|             headers: { | ||||
|                 "Accept": "application/json", | ||||
|                 "Content-Type": "application/json", | ||||
|                 "X-CSRFToken": csrftoken, | ||||
|                 }, | ||||
|         }); | ||||
|         return fetch(request, { | ||||
|             method: "PATCH", | ||||
|             mode: "same-origin", | ||||
|             body: JSON.stringify(body), | ||||
|         }) | ||||
|             .then((r) => { | ||||
|                 if (r.status > 300) { | ||||
|                     switch (r.status) { | ||||
|                     case 404: | ||||
|                         throw new NotFoundError(`URL ${finalUrl} not found`); | ||||
|                     default: | ||||
|                         throw new RequestError(r.statusText); | ||||
|                     } | ||||
|                 } | ||||
|                 return r; | ||||
|             }) | ||||
|             .then((r) => r.json()) | ||||
|             .then((r) => <T>r); | ||||
|     } | ||||
| } | ||||
|  | ||||
| export const DefaultClient = new Client(); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer