124 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			124 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { goto } from '$app/navigation';
 | |
| import { userStore } from 'routes/UserStore.svelte';
 | |
| import { notificationStore } from './NotificationsStore.svelte';
 | |
| 
 | |
| const API = '/api';
 | |
| 
 | |
| export async function get(url: string) {
 | |
| 	const headers = new Headers();
 | |
| 	//headers.append('content-type', 'application/json');
 | |
| 	headers.append('response-type', 'application/json');
 | |
| 	if (userStore.user) {
 | |
| 		headers.append('token', userStore.user.token);
 | |
| 	}
 | |
| 
 | |
| 	const r = await fetch(`${API}/${url}`, {
 | |
| 		method: 'GET',
 | |
| 		headers: headers
 | |
| 	});
 | |
| 
 | |
| 	if (r.status === 401) {
 | |
| 		userStore.user = undefined;
 | |
| 		goto('/login');
 | |
| 	} else if (r.status !== 200) {
 | |
| 		throw r;
 | |
| 	}
 | |
| 
 | |
| 	return r.json();
 | |
| }
 | |
| 
 | |
| export async function post(url: string, body: any) {
 | |
| 	const headers = new Headers();
 | |
| 	headers.append('content-type', 'application/json');
 | |
| 	if (userStore.user) {
 | |
| 		headers.append('token', userStore.user.token);
 | |
| 	}
 | |
| 
 | |
| 	const r = await fetch(`${API}/${url}`, {
 | |
| 		method: 'POST',
 | |
| 		headers: headers,
 | |
| 		body: JSON.stringify(body)
 | |
| 	});
 | |
| 
 | |
| 	if (r.status === 401) {
 | |
| 		userStore.user = undefined;
 | |
| 		goto('/login');
 | |
| 		throw r;
 | |
| 	} else if (r.status !== 200) {
 | |
| 		throw r;
 | |
| 	}
 | |
| 
 | |
| 	return r.json();
 | |
| }
 | |
| 
 | |
| export async function rdelete(url: string, body: any) {
 | |
| 	const headers = new Headers();
 | |
| 	headers.append('content-type', 'application/json');
 | |
| 	if (userStore.user) {
 | |
| 		headers.append('token', userStore.user.token);
 | |
| 	}
 | |
| 
 | |
| 	const r = await fetch(`${API}/${url}`, {
 | |
| 		method: 'DELETE',
 | |
| 		headers: headers,
 | |
| 		body: JSON.stringify(body)
 | |
| 	});
 | |
| 
 | |
| 	if (r.status === 401) {
 | |
| 		userStore.user = undefined;
 | |
| 		goto('/login');
 | |
| 	} else if (r.status !== 200) {
 | |
| 		throw r;
 | |
| 	}
 | |
| 
 | |
| 	return r.json();
 | |
| }
 | |
| 
 | |
| export async function postFormData(url: string, body: FormData) {
 | |
| 	const headers = new Headers();
 | |
| 	//headers.append('content-type', 'multipart/form-data');
 | |
| 	headers.append('response-type', 'application/json');
 | |
| 	if (userStore.user) {
 | |
| 		headers.append('token', userStore.user.token);
 | |
| 	}
 | |
| 
 | |
| 	const r = await fetch(`${API}/${url}`, {
 | |
| 		method: 'POST',
 | |
| 		headers: headers,
 | |
| 		body: body
 | |
| 	});
 | |
| 
 | |
| 	if (r.status == 401) {
 | |
| 		userStore.user = undefined;
 | |
| 		goto('/login');
 | |
| 		throw new Error('Redirect');
 | |
| 	}
 | |
| 
 | |
| 	if (r.status !== 200) {
 | |
| 		throw r;
 | |
| 	}
 | |
| 
 | |
| 	return r.json();
 | |
| }
 | |
| 
 | |
| export async function showMessage(
 | |
| 	e: any,
 | |
| 	messages: any = notificationStore,
 | |
| 	message = 'Could not complete request'
 | |
| ): Promise<boolean> {
 | |
| 	if (e == null) {
 | |
| 		return false;
 | |
| 	} else if (e instanceof Response) {
 | |
| 		try {
 | |
| 			messages.display(await e.json());
 | |
| 		} catch (ex) {
 | |
| 			showMessage(ex, messages, message);
 | |
| 		}
 | |
| 		return true;
 | |
| 	} else {
 | |
| 		console.error(e);
 | |
| 		messages.display(message);
 | |
| 		return true;
 | |
| 	}
 | |
| }
 |