-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmod.ts
More file actions
93 lines (83 loc) · 2.66 KB
/
Copy pathmod.ts
File metadata and controls
93 lines (83 loc) · 2.66 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// deno-lint-ignore-file no-explicit-any
import { Api, ApiConfig, HttpResponse } from "./openapi/client.ts";
import { createRateLimitFetch } from "./rate_limit_fetch.ts";
import type { RateLimitFetchConfig } from "./rate_limit_fetch.ts";
export interface LagoClientConfig extends ApiConfig {
/**
* Rate limit retry configuration
*/
rateLimitRetry?: RateLimitFetchConfig;
}
export const Client = (apiKey: string, apiConfig?: LagoClientConfig) => {
const { rateLimitRetry, ...restConfig } = apiConfig ?? {};
// Create rate-limit-aware fetch if configured
const customFetch = rateLimitRetry
? createRateLimitFetch(
(restConfig?.customFetch ?? globalThis.fetch) as typeof fetch,
rateLimitRetry,
)
: restConfig?.customFetch;
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",
},
...restConfig,
...(customFetch && { customFetch }),
});
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);
}
// Rate limit exports
export { LagoRateLimitError } from "./rate_limit_error.ts";
export {
parseRateLimitHeaders,
parseRateLimitInfo,
type RateLimitHeaders,
type RateLimitInfo,
rateLimitUsagePct,
} from "./rate_limit_headers.ts";
export {
createRateLimitFetch,
type RateLimitFetchConfig,
type RateLimitInfoCallback,
} from "./rate_limit_fetch.ts";
export {
DEFAULT_RATE_LIMIT_THRESHOLDS,
loggingRateLimitObserver,
type LoggingRateLimitObserverOptions,
} from "./logging_rate_limit_observer.ts";
export * from "./openapi/client.ts";
// Webhook payload types (see webhook_types.ts and openapi/webhooks.ts)
export type {
LagoWebhookPayload,
LagoWebhookPayloads,
LagoWebhookType,
WebhookOf,
} from "./webhook_types.ts";