-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.ts
45 lines (40 loc) · 1.35 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// deno-lint-ignore-file no-explicit-any
import { Api, ApiConfig, HttpResponse } from "./openapi/client.ts";
export const Client = (apiKey: string, apiConfig?: ApiConfig) => {
const api = new Api({
securityWorker: (apiKey) =>
apiKey ? { headers: { Authorization: `Bearer ${apiKey}` } } : {},
// Cloudflare Workers doesn't support some options like credentials so need to override default
baseApiParams: {
redirect: "follow",
},
...apiConfig,
});
api.setSecurityData(apiKey);
return api;
};
type ExtractLagoError<E> = E extends (
...args: any
) => Promise<HttpResponse<infer T, infer P>> ? P
: never;
// https://github.com/remix-run/remix/blob/ef26f7671a9619966a6cfa3c39e196d44fbf32cf/packages/remix-server-runtime/responses.ts#L60
function isResponse(value: any): value is Response {
return (
value != null &&
typeof value.status === "number" &&
typeof value.statusText === "string" &&
typeof value.headers === "object" &&
typeof value.body !== "undefined"
);
}
export async function getLagoError<T>(error: any) {
if (isResponse(error)) {
if (!error.bodyUsed) {
const errorJson = await error.json() as ExtractLagoError<T>;
return errorJson;
}
return (error as HttpResponse<any, any>).error as ExtractLagoError<T>;
}
throw new Error(error);
}
export * from "./openapi/client.ts";