web: add update method

This commit is contained in:
Jens Langhammer 2021-01-16 19:08:20 +01:00
parent f959212692
commit 36e8b1004c
1 changed files with 31 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import { getCookie } from "../utils";
import { NotFoundError, RequestError } from "./Error"; import { NotFoundError, RequestError } from "./Error";
export const VERSION = "v2beta"; export const VERSION = "v2beta";
@ -35,6 +36,36 @@ export class Client {
.then((r) => r.json()) .then((r) => r.json())
.then((r) => <T>r); .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(); export const DefaultClient = new Client();