Files
back_end_oudelaa/oudelaa_dashboard/lib/api/response.ts
boutmoun123 1b753a419a
API Gateway CI / Build, lint, and test (push) Successful in 13m33s
API Gateway CI / Build API Gateway image (push) Successful in 5m43s
feat: sync marketplace dashboard and staging CI
2026-11-02 17:28:58 +03:00

22 lines
747 B
TypeScript

/** Unwrap only the backend's explicit envelope, not domain objects with a data field. */
export function unwrapApiResponse<T>(payload: unknown): T {
if (payload && typeof payload === "object" && "data" in payload && "meta" in payload) {
const meta = (payload as { meta?: { statusCode?: unknown } }).meta;
if (meta && typeof meta.statusCode === "number") {
return (payload as { data: T }).data;
}
}
return payload as T;
}
export class ApiError extends Error {
constructor(public readonly status: number, message: string) {
super(message);
this.name = "ApiError";
}
}
export function isHttpError(error: unknown, ...statuses: number[]) {
return error instanceof ApiError && statuses.includes(error.status);
}