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

Switch from ajv to io-ts for config validation; validate configs on save in the API/dashboard; start work on creating io-ts schemas for all plugins

This commit is contained in:
Dragory 2019-07-11 12:23:57 +03:00
parent b230a73a6f
commit da114c0e60
14 changed files with 256 additions and 41 deletions

View file

@ -3,6 +3,19 @@ const apiUrl = process.env.API_URL;
type QueryParamObject = { [key: string]: string | null };
export class ApiError extends Error {
protected body: object;
protected status: number;
protected res: Response;
constructor(message: string, body: object, status: number, res: Response) {
super(message);
this.body = body;
this.status = status;
this.res = res;
}
}
function buildQueryString(params: QueryParamObject) {
if (Object.keys(params).length === 0) return "";
@ -15,7 +28,14 @@ function buildQueryString(params: QueryParamObject) {
}
export function request(resource, fetchOpts: RequestInit = {}) {
return fetch(`${apiUrl}/${resource}`, fetchOpts).then(res => res.json());
return fetch(`${apiUrl}/${resource}`, fetchOpts).then(async res => {
if (!res.ok) {
const body = await res.json();
throw new ApiError(res.statusText, body, res.status, res);
}
return res.json();
});
}
export function get(resource: string, params: QueryParamObject = {}) {
@ -32,7 +52,7 @@ export function post(resource: string, params: QueryParamObject = {}) {
const headers: Record<string, string> = RootStore.state.auth.apiKey
? { "X-Api-Key": RootStore.state.auth.apiKey }
: {};
return request(resource + buildQueryString(params), {
return request(resource, {
method: "POST",
body: JSON.stringify(params),
headers: {