feat(web): scaffold Next.js 16 dashboard with design system and auth client (#82)
Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
This commit was merged in pull request #82.
This commit is contained in:
47
apps/web/src/lib/api.ts
Normal file
47
apps/web/src/lib/api.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
const GATEWAY_URL = process.env['NEXT_PUBLIC_GATEWAY_URL'] ?? 'http://localhost:4000';
|
||||
|
||||
export interface ApiRequestInit extends Omit<RequestInit, 'body'> {
|
||||
body?: unknown;
|
||||
}
|
||||
|
||||
export interface ApiError {
|
||||
statusCode: number;
|
||||
message: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch wrapper for the Mosaic gateway API.
|
||||
* Sends credentials (cookies) and JSON body automatically.
|
||||
*/
|
||||
export async function api<T>(path: string, init?: ApiRequestInit): Promise<T> {
|
||||
const { body, headers: customHeaders, ...rest } = init ?? {};
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
Accept: 'application/json',
|
||||
...(customHeaders as Record<string, string>),
|
||||
};
|
||||
|
||||
if (body !== undefined) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
const res = await fetch(`${GATEWAY_URL}${path}`, {
|
||||
credentials: 'include',
|
||||
...rest,
|
||||
headers,
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errorBody = (await res.json().catch(() => ({
|
||||
statusCode: res.status,
|
||||
message: res.statusText,
|
||||
}))) as ApiError;
|
||||
throw Object.assign(new Error(errorBody.message), {
|
||||
statusCode: errorBody.statusCode,
|
||||
});
|
||||
}
|
||||
|
||||
if (res.status === 204) return undefined as T;
|
||||
return (await res.json()) as T;
|
||||
}
|
||||
Reference in New Issue
Block a user