From 36e8b1004c6f28e34b22c9b7b93ecb2fc8f9773d Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 16 Jan 2021 19:08:20 +0100 Subject: [PATCH] web: add update method --- web/src/api/Client.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/web/src/api/Client.ts b/web/src/api/Client.ts index 8288867b3..20fa6abaf 100644 --- a/web/src/api/Client.ts +++ b/web/src/api/Client.ts @@ -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) => r); } + + update(url: string[], body: T, query?: QueryArguments): Promise { + 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) => r); + } } export const DefaultClient = new Client();