20 lines
521 B
TypeScript
20 lines
521 B
TypeScript
|
|
import { apiEndpoint, apiKey } from './store';
|
|
import { get } from 'svelte/store';
|
|
|
|
export const apiRequest = async (method: string, endpoint: string, body?: string) => {
|
|
const myHeaders = new Headers();
|
|
myHeaders.append('X-API-Key', get(apiKey));
|
|
|
|
const myInit: RequestInit = {
|
|
method,
|
|
headers: myHeaders,
|
|
mode: 'cors',
|
|
cache: 'default',
|
|
body,
|
|
};
|
|
|
|
const myRequest = new Request(`${get(apiEndpoint)}/${endpoint}`);
|
|
return await fetch(myRequest, myInit);
|
|
}
|