-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfapi.ts
More file actions
61 lines (55 loc) · 2.46 KB
/
Copy pathfapi.ts
File metadata and controls
61 lines (55 loc) · 2.46 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
/**
* Instance + FAPI host resolution for `clerk api --fapi`.
*
* FAPI is the public API that clerk-js consumes. Its host is per-instance and
* derived from the instance's publishable key. The passthrough request itself
* lives in `lib/fapi.ts` (`fapiRequest`) alongside the other FAPI helpers.
*/
import { resolveAppContext, resolveFetchedApplicationInstance } from "../../lib/config.ts";
import { CliError, ERROR_CODE, throwUsageError, withApiContext } from "../../lib/errors.ts";
import { decodePublishableKey } from "../../lib/fapi.ts";
import { fetchApplication, type ApplicationInstance } from "../../lib/plapi.ts";
interface ResolveOptions {
app?: string;
instance?: string;
}
async function resolveInstance(options: ResolveOptions): Promise<ApplicationInstance> {
if (options.app) {
const app = await withApiContext(fetchApplication(options.app), "Failed to resolve instance");
const resolved = resolveFetchedApplicationInstance(options.app, app, options.instance);
if (!resolved.found) {
throw new CliError(`Instance ${resolved.instanceId} not found in application.`, {
code: ERROR_CODE.INSTANCE_NOT_FOUND,
docsUrl: "https://clerk.com/docs/guides/development/managing-environments",
});
}
return resolved.instance;
}
let ctx: Awaited<ReturnType<typeof resolveAppContext>>;
try {
ctx = await resolveAppContext({ app: options.app, instance: options.instance });
} catch (error) {
if (error instanceof CliError && error.code === ERROR_CODE.NOT_LINKED) {
throwUsageError(
"No instance found. Link a project with `clerk link`, or pass --app <app_id>.",
"https://clerk.com/docs/guides/development/managing-environments",
ERROR_CODE.NOT_LINKED,
);
}
throw error;
}
const app = await withApiContext(fetchApplication(ctx.appId), "Failed to resolve instance");
const resolved = resolveFetchedApplicationInstance(ctx.appId, app, ctx.instanceId);
if (!resolved.found) {
throw new CliError(`Instance ${ctx.instanceId} not found in application.`, {
code: ERROR_CODE.INSTANCE_NOT_FOUND,
docsUrl: "https://clerk.com/docs/guides/development/managing-environments",
});
}
return resolved.instance;
}
/** Resolve the instance's FAPI host from its publishable key. */
export async function resolveFapiHost(options: ResolveOptions): Promise<string> {
const instance = await resolveInstance(options);
return decodePublishableKey(instance.publishable_key).fapiHost;
}