trigger.dev
Version:
A Command-Line Interface for Trigger.dev projects
53 lines • 1.83 kB
JavaScript
import { CLOUD_API_URL, CLOUD_WEB_URL } from "../consts.js";
import { readAuthConfigProfile } from "../utilities/configFiles.js";
const personalTokenPrefix = "tr_pat_";
const organizationTokenPrefix = "tr_oat_";
const apiKeyPrefix = "tr_";
export async function authenticateForDeploy({ accessToken, apiUrl, profile, silent, login, }) {
const authConfig = readAuthConfigProfile(profile);
const resolvedApiUrl = apiUrl ?? authConfig?.apiUrl ?? CLOUD_API_URL;
const isApiKey = !!accessToken &&
accessToken.startsWith(apiKeyPrefix) &&
!accessToken.startsWith(personalTokenPrefix) &&
!accessToken.startsWith(organizationTokenPrefix);
if (!isApiKey) {
return login({
embedded: true,
defaultApiUrl: resolvedApiUrl,
profile,
silent,
});
}
return {
ok: true,
profile,
dashboardUrl: dashboardUrlForApiUrl(resolvedApiUrl),
auth: {
apiUrl: resolvedApiUrl,
accessToken,
tokenType: "apiKey",
},
};
}
function dashboardUrlForApiUrl(apiUrl) {
if (apiUrl === CLOUD_API_URL) {
return CLOUD_WEB_URL;
}
try {
const url = new URL(apiUrl);
if (url.hostname.startsWith("api.") && url.hostname.endsWith(".trigger.dev")) {
url.hostname = url.hostname.slice(4);
return url.toString().replace(/\/$/, "");
}
}
catch {
throw new Error(`Invalid API URL "${apiUrl}". Check your TRIGGER_API_URL environment variable or --api-url flag.`);
}
return apiUrl;
}
export function userIdForDeploy(authorization) {
return authorization.auth.tokenType === "personal" && "userId" in authorization
? authorization.userId
: undefined;
}
//# sourceMappingURL=auth.js.map