@inertiapixel/nextjs-auth
Version:
Authentication system for Next.js. Supports credentials and social login, JWT token management, and lifecycle hooks — designed to integrate with nodejs-auth for full-stack MERN apps.
61 lines (60 loc) • 1.87 kB
JavaScript
// src/utils/ApiClient.ts
export class ApiClient {
async post(url, body, headers = {}) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify(body),
credentials: 'include',
});
if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage || 'Request failed');
}
return response.json();
}
async get(url, headers = {}) {
const response = await fetch(url, {
method: 'GET',
headers,
credentials: 'include',
});
if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage || 'Request failed');
}
return response.json();
}
async put(url, body, headers = {}) {
const response = await fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify(body),
credentials: 'include',
});
if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage || 'Request failed');
}
return response.json();
}
async delete(url, headers = {}) {
const response = await fetch(url, {
method: 'DELETE',
headers,
credentials: 'include',
});
if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage || 'Request failed');
}
return response.json();
}
}
export const apiClient = new ApiClient();