3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

dashboard: auth fixes, guild listing, config editing

This commit is contained in:
Dragory 2019-06-23 03:40:53 +03:00
parent 1dae3019c4
commit 7bda2b1763
14 changed files with 200 additions and 42 deletions

View file

@ -1,3 +1,4 @@
import { RootStore } from "./store";
const apiUrl = process.env.API_URL;
type QueryParamObject = { [key: string]: string | null };
@ -13,27 +14,14 @@ function buildQueryString(params: QueryParamObject) {
);
}
let apiKey = null;
export function setApiKey(newKey) {
apiKey = newKey;
}
export function hasApiKey() {
return apiKey != null;
}
export function resetApiKey() {
apiKey = null;
}
export function request(resource, fetchOpts: RequestInit = {}) {
return fetch(`${apiUrl}/${resource}`, fetchOpts)
.then(res => res.json())
.catch(err => {
console.log(err);
});
return fetch(`${apiUrl}/${resource}`, fetchOpts).then(res => res.json());
}
export function get(resource: string, params: QueryParamObject = {}) {
const headers: Record<string, string> = apiKey ? { "X-Api-Key": (apiKey as unknown) as string } : {};
const headers: Record<string, string> = RootStore.state.auth.apiKey
? { "X-Api-Key": RootStore.state.auth.apiKey }
: {};
return request(resource + buildQueryString(params), {
method: "GET",
headers,
@ -41,7 +29,9 @@ export function get(resource: string, params: QueryParamObject = {}) {
}
export function post(resource: string, params: QueryParamObject = {}) {
const headers: Record<string, string> = apiKey ? { "X-Api-Key": (apiKey as unknown) as string } : {};
const headers: Record<string, string> = RootStore.state.auth.apiKey
? { "X-Api-Key": RootStore.state.auth.apiKey }
: {};
return request(resource + buildQueryString(params), {
method: "POST",
body: JSON.stringify(params),