Podpora easter eggů
This commit is contained in:
@@ -25,11 +25,36 @@ async function request<TResponse>(
|
||||
try {
|
||||
const response = await fetch(getBaseUrl() + url, config);
|
||||
if (!response.ok) {
|
||||
// TODO tohle je blbě, jelikož automaticky očekáváme, že v případě chyby přijde vždy JSON, což není pravda
|
||||
const json = await response.json();
|
||||
// Vyhodíme samotnou hlášku z odpovědi, odchytí si jí errorHandler
|
||||
throw new Error(json.error);
|
||||
}
|
||||
return response.json() as TResponse;
|
||||
const contentType = response.headers.get("content-type");
|
||||
if (contentType && contentType.indexOf("application/json") !== -1) {
|
||||
return response.json() as TResponse;
|
||||
} else {
|
||||
return response.text() as TResponse;
|
||||
}
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function blobRequest(
|
||||
url: string,
|
||||
config: RequestInit = {}
|
||||
): Promise<Blob> {
|
||||
config.headers = config?.headers ? new Headers(config.headers) : new Headers();
|
||||
config.headers.set("Authorization", `Bearer ${getToken()}`);
|
||||
try {
|
||||
const response = await fetch(getBaseUrl() + url, config);
|
||||
if (!response.ok) {
|
||||
const json = await response.json();
|
||||
// Vyhodíme samotnou hlášku z odpovědi, odchytí si jí errorHandler
|
||||
throw new Error(json.error);
|
||||
}
|
||||
return response.blob()
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
@@ -37,6 +62,7 @@ async function request<TResponse>(
|
||||
|
||||
export const api = {
|
||||
get: <TResponse>(url: string) => request<TResponse>(url),
|
||||
blobGet: (url: string) => blobRequest(url),
|
||||
post: <TBody extends BodyInit, TResponse>(url: string, body: TBody) => request<TResponse>(url, { method: 'POST', body, headers: { 'Content-Type': 'application/json' } }),
|
||||
}
|
||||
|
||||
|
||||
12
client/src/api/EasterEggApi.ts
Normal file
12
client/src/api/EasterEggApi.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { EasterEgg } from "../types";
|
||||
import { api } from "./Api";
|
||||
|
||||
const EASTER_EGGS_API_PREFIX = '/api/easterEggs';
|
||||
|
||||
export const getEasterEgg = async (): Promise<EasterEgg | undefined> => {
|
||||
return await api.get(`${EASTER_EGGS_API_PREFIX}`);
|
||||
}
|
||||
|
||||
export const getImage = async (url: string) => {
|
||||
return await api.blobGet(`${EASTER_EGGS_API_PREFIX}/${url}`);
|
||||
}
|
||||
Reference in New Issue
Block a user