108 lines
2.9 KiB
TypeScript
108 lines
2.9 KiB
TypeScript
|
|
export interface ApiErrorBody {
|
||
|
|
error: {
|
||
|
|
code: string;
|
||
|
|
message: string;
|
||
|
|
details?: Record<string, unknown>;
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export class ApiError extends Error {
|
||
|
|
readonly code: string;
|
||
|
|
readonly status: number;
|
||
|
|
readonly details?: Record<string, unknown>;
|
||
|
|
|
||
|
|
constructor(status: number, code: string, message: string, details?: Record<string, unknown>) {
|
||
|
|
super(message);
|
||
|
|
this.name = 'ApiError';
|
||
|
|
this.status = status;
|
||
|
|
this.code = code;
|
||
|
|
if (details !== undefined) {
|
||
|
|
this.details = details;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const API_PREFIX = '/api/v1';
|
||
|
|
|
||
|
|
export function isApiErrorPayload(value: unknown): value is ApiErrorBody['error'] {
|
||
|
|
if (typeof value !== 'object' || value === null) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
const candidate = value as { code?: unknown; message?: unknown };
|
||
|
|
return typeof candidate.code === 'string' && typeof candidate.message === 'string';
|
||
|
|
}
|
||
|
|
|
||
|
|
export function apiErrorCode(error: unknown): string | null {
|
||
|
|
if (error === null || typeof error !== 'object') {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
const payload = (error as { apiError?: unknown }).apiError;
|
||
|
|
return isApiErrorPayload(payload) ? payload.code : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface RequestOptions {
|
||
|
|
method?: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
||
|
|
body?: unknown;
|
||
|
|
signal?: AbortSignal;
|
||
|
|
headers?: Record<string, string>;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function parsePayload(text: string, contentType: string | null): unknown {
|
||
|
|
if (text.length === 0) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
if (contentType !== null && !contentType.includes('application/json')) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
return JSON.parse(text) as unknown;
|
||
|
|
} catch {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function request<T>(path: string, options: RequestOptions = {}): Promise<T> {
|
||
|
|
const init: RequestInit = {
|
||
|
|
method: options.method ?? 'GET',
|
||
|
|
credentials: 'same-origin',
|
||
|
|
headers: {
|
||
|
|
Accept: 'application/json',
|
||
|
|
...(options.body === undefined ? {} : { 'Content-Type': 'application/json' }),
|
||
|
|
...options.headers,
|
||
|
|
},
|
||
|
|
};
|
||
|
|
if (options.body !== undefined) {
|
||
|
|
init.body = JSON.stringify(options.body);
|
||
|
|
}
|
||
|
|
if (options.signal !== undefined) {
|
||
|
|
init.signal = options.signal;
|
||
|
|
}
|
||
|
|
|
||
|
|
const response = await fetch(`${API_PREFIX}${path}`, init);
|
||
|
|
const text = await response.text();
|
||
|
|
const payload = parsePayload(text, response.headers.get('Content-Type'));
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
const envelope =
|
||
|
|
typeof payload === 'object' && payload !== null
|
||
|
|
? (payload as { error?: unknown }).error
|
||
|
|
: undefined;
|
||
|
|
if (isApiErrorPayload(envelope)) {
|
||
|
|
const error = new ApiError(
|
||
|
|
response.status,
|
||
|
|
envelope.code,
|
||
|
|
envelope.message,
|
||
|
|
envelope.details,
|
||
|
|
);
|
||
|
|
Object.defineProperty(error, 'apiError', {
|
||
|
|
value: envelope,
|
||
|
|
enumerable: false,
|
||
|
|
writable: false,
|
||
|
|
});
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
throw new ApiError(response.status, 'internal.error', 'unexpected response');
|
||
|
|
}
|
||
|
|
return payload as T;
|
||
|
|
}
|