generated from rakenduste-programmeerimine-2024/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add API client and token utilities for secure data fetching (#27)
- Loading branch information
1 parent
e16c97c
commit 6dbb36e
Showing
2 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { getAuthToken } from "@/utils/api/token-utils" | ||
|
||
export async function fetchMultipleResources<T>(url: string): Promise<T[]> { | ||
const token = await getAuthToken() | ||
const response = await fetch(url, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new Error(`Error fetching resources: ${response.statusText}`) | ||
} | ||
|
||
return response.json() | ||
} | ||
|
||
export async function fetchByForeignKey<T>( | ||
url: string, | ||
foreignKey: string, | ||
value: string, | ||
): Promise<T[]> { | ||
const query = new URLSearchParams({ [foreignKey]: value }).toString() | ||
const fullUrl = `${url}?${query}` | ||
|
||
return fetchMultipleResources<T>(fullUrl) | ||
} | ||
|
||
export async function fetchResource<T>(url: string): Promise<T> { | ||
const token = await getAuthToken() | ||
console.log(token) | ||
console.log(url) | ||
const response = await fetch(url, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new Error(`Error fetching resource: ${response.statusText}`) | ||
} | ||
|
||
return response.json() | ||
} | ||
|
||
export async function createResource<T, U>(url: string, data: U): Promise<T> { | ||
const token = await getAuthToken() | ||
const response = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new Error(`Error creating resource: ${response.statusText}`) | ||
} | ||
|
||
return response.json() | ||
} | ||
|
||
export async function updateResource<T, U>(url: string, data: U): Promise<T> { | ||
const token = await getAuthToken() | ||
const response = await fetch(url, { | ||
method: "PUT", | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(data), | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new Error(`Error updating resource: ${response.statusText}`) | ||
} | ||
|
||
return response.json() | ||
} | ||
|
||
export async function deleteResource(url: string): Promise<void> { | ||
const token = await getAuthToken() | ||
const response = await fetch(url, { | ||
method: "DELETE", | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
}) | ||
|
||
if (!response.ok) { | ||
throw new Error(`Error deleting resource: ${response.statusText}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createClient } from "@/utils/supabase/client" | ||
|
||
export async function getAuthToken(): Promise<string> { | ||
const supabase = createClient() | ||
const { data, error } = await supabase.auth.getSession() | ||
|
||
if (error) { | ||
console.error("Error fetching session:", error) | ||
throw new Error("Failed to retrieve authentication session.") | ||
} | ||
|
||
const token = data.session?.access_token | ||
|
||
if (!token) { | ||
throw new Error("No authentication token available. Please log in.") | ||
} | ||
|
||
return token | ||
} |