Skip to content

Commit

Permalink
feat: add API client and token utilities for secure data fetching (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Citronnelle committed Jan 14, 2025
1 parent e16c97c commit 6dbb36e
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
97 changes: 97 additions & 0 deletions utils/api/api-client.ts
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}`)
}
}
19 changes: 19 additions & 0 deletions utils/api/token-utils.ts
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
}

0 comments on commit 6dbb36e

Please sign in to comment.