feat(frontend): add apiFetch wrapper for automatic auth token injection
Centralizes auth token management into a reusable apiFetch() helper and getAuthHeaders() utility, eliminating inline Bearer token logic across all frontend files. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { authStore } from '$lib/stores/auth.svelte';
|
||||
import { SvelteDate } from 'svelte/reactivity';
|
||||
import { apiFetch } from '$lib/utils/api';
|
||||
import { CalendarDate } from '@internationalized/date';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import { sanitizeText } from '$lib/utils/toast-safe';
|
||||
@@ -222,11 +222,9 @@
|
||||
async function fetchBlockers() {
|
||||
loading = true;
|
||||
try {
|
||||
const response = await fetch('/api/admin/time-blockers', {
|
||||
method: 'GET',
|
||||
const response = await apiFetch('/api/admin/time-blockers', {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
@@ -247,11 +245,9 @@
|
||||
async function fetchDefaultHours() {
|
||||
hoursLoading = true;
|
||||
try {
|
||||
const response = await fetch('/api/scheduling/default-hours', {
|
||||
method: 'GET',
|
||||
const response = await apiFetch('/api/scheduling/default-hours', {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
@@ -312,13 +308,11 @@
|
||||
checkingOverlap = true;
|
||||
try {
|
||||
const dateParam = newStartDate;
|
||||
const response = await fetch(
|
||||
const response = await apiFetch(
|
||||
`/api/admin/bookings/by-date-range?start=${encodeURIComponent(dateParam)}&end=${encodeURIComponent(dateParam)}`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -409,11 +403,10 @@
|
||||
if (overlappingBookings.length > 0) {
|
||||
for (const booking of overlappingBookings) {
|
||||
try {
|
||||
await fetch('/api/admin/time-blockers', {
|
||||
await apiFetch('/api/admin/time-blockers', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
start_time: startIso,
|
||||
@@ -428,11 +421,10 @@
|
||||
}
|
||||
|
||||
// Create the actual time blocker
|
||||
const response = await fetch('/api/admin/time-blockers', {
|
||||
const response = await apiFetch('/api/admin/time-blockers', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
start_time: startIso,
|
||||
@@ -464,11 +456,8 @@
|
||||
const loadingToast = toast.loading('Deleting time blocker...');
|
||||
|
||||
try {
|
||||
const response = await fetch(`/api/admin/time-blockers/${blockerToDelete.id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: `Bearer ${authStore.currentToken}`
|
||||
}
|
||||
const response = await apiFetch(`/api/admin/time-blockers/${blockerToDelete.id}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
|
||||
if (response.ok || response.status === 204) {
|
||||
|
||||
Reference in New Issue
Block a user