- Published on
API Helper Functions
656 words4 min read
API Response Formatter
const successResponse = (res, data, message = 'Success', statusCode = 200) => {
return res.status(statusCode).json({
success: true,
message,
data,
})
}
const errorResponse = (res, message = 'Error', statusCode = 400) => {
return res.status(statusCode).json({
success: false,
message,
})
}
Fetch Wrapper with Error Handling
const apiCall = async (url, options = {}) => {
try {
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
...options.headers,
},
...options,
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
return await response.json()
} catch (error) {
console.error('API call failed:', error)
throw error
}
}
Async Fetch Hook (React)
import { useState, useEffect } from 'react'
function useApi(url, options = {}) {
const [data, setData] = useState(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
useEffect(() => {
const fetchData = async () => {
try {
setLoading(true)
const response = await fetch(url, options)
const json = await response.json()
setData(json)
} catch (err) {
setError(err)
} finally {
setLoading(false)
}
}
fetchData()
}, [url])
return { data, loading, error, refetch: () => fetchData() }
}