-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.ts
More file actions
107 lines (87 loc) · 3.11 KB
/
index.ts
File metadata and controls
107 lines (87 loc) · 3.11 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { ApiClient } from "../apiClient/index.js";
import { getGlobal, registerGlobal, unregisterGlobal } from "../utils/globals.js";
import { getEnvVar } from "../utils/getEnv.js";
import { ApiClientConfiguration } from "./types.js";
const API_NAME = "api-client";
export class ApiClientMissingError extends Error {
constructor(message: string) {
super(message);
this.name = "ApiClientMissingError";
}
}
export class APIClientManagerAPI {
private static _instance?: APIClientManagerAPI;
private constructor() {}
public static getInstance(): APIClientManagerAPI {
if (!this._instance) {
this._instance = new APIClientManagerAPI();
}
return this._instance;
}
public disable() {
unregisterGlobal(API_NAME);
}
get baseURL(): string | undefined {
const config = this.#getConfig();
return config?.baseURL ?? getEnvVar("TRIGGER_API_URL") ?? "https://api.trigger.dev";
}
get accessToken(): string | undefined {
const config = this.#getConfig();
return (
config?.secretKey ??
config?.accessToken ??
getEnvVar("TRIGGER_SECRET_KEY") ??
getEnvVar("TRIGGER_ACCESS_TOKEN")
);
}
get branchName(): string | undefined {
const config = this.#getConfig();
const value =
config?.previewBranch ??
getEnvVar("TRIGGER_PREVIEW_BRANCH") ??
getEnvVar("VERCEL_GIT_COMMIT_REF") ??
undefined;
return value ? value : undefined;
}
get client(): ApiClient | undefined {
if (!this.baseURL || !this.accessToken) {
return undefined;
}
return new ApiClient(this.baseURL, this.accessToken, this.branchName);
}
clientOrThrow(): ApiClient {
if (!this.baseURL || !this.accessToken) {
throw new ApiClientMissingError(this.apiClientMissingError());
}
return new ApiClient(this.baseURL, this.accessToken, this.branchName);
}
runWithConfig<R extends (...args: any[]) => Promise<any>>(
config: ApiClientConfiguration,
fn: R
): Promise<ReturnType<R>> {
const originalConfig = this.#getConfig();
const $config = { ...originalConfig, ...config };
registerGlobal(API_NAME, $config, true);
return fn().finally(() => {
registerGlobal(API_NAME, originalConfig, true);
});
}
public setGlobalAPIClientConfiguration(config: ApiClientConfiguration): boolean {
return registerGlobal(API_NAME, config);
}
#getConfig(): ApiClientConfiguration | undefined {
return getGlobal(API_NAME);
}
apiClientMissingError() {
const hasBaseUrl = !!this.baseURL;
const hasAccessToken = !!this.accessToken;
if (!hasBaseUrl && !hasAccessToken) {
return `You need to set the TRIGGER_API_URL and TRIGGER_SECRET_KEY environment variables. See https://trigger.dev/docs/management/overview#authentication`;
} else if (!hasBaseUrl) {
return `You need to set the TRIGGER_API_URL environment variable. See https://trigger.dev/docs/management/overview#authentication`;
} else if (!hasAccessToken) {
return `You need to set the TRIGGER_SECRET_KEY environment variable. See https://trigger.dev/docs/management/overview#authentication`;
}
return `Unknown error`;
}
}