UNPKG

@heroku/applink

Version:

Applink SDK for Heroku Apps.

50 lines (49 loc) 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveAddonConfigByAttachmentOrColor = resolveAddonConfigByAttachmentOrColor; exports.resolveAddonConfigByUrl = resolveAddonConfigByUrl; function resolveAddonConfigByAttachmentOrColor(attachmentOrColor) { const appUuid = process.env.HEROKU_APP_ID; if (!appUuid) { throw Error(`Heroku Applink app UUID not found`); } const addon = process.env.HEROKU_APPLINK_ADDON_NAME || "HEROKU_APPLINK"; let apiUrl; let token; apiUrl = process.env[`${attachmentOrColor.toUpperCase()}_API_URL`]; token = process.env[`${attachmentOrColor.toUpperCase()}_TOKEN`]; if (!apiUrl || !token) { apiUrl = process.env[`${addon}_${attachmentOrColor.toUpperCase()}_API_URL`]; token = process.env[`${addon}_${attachmentOrColor.toUpperCase()}_TOKEN`]; } if (!apiUrl || !token) { throw Error(`Heroku Applink config not found under attachment or color ${attachmentOrColor}`); } return { apiUrl, token, appUuid, }; } function resolveAddonConfigByUrl(url) { const appUuid = process.env.HEROKU_APP_ID; if (!appUuid) { throw Error(`Heroku Applink app UUID not found`); } const envVarEntries = Object.entries(process.env); const matchingApiUrlEntry = envVarEntries.find(([key, value]) => key.endsWith("_API_URL") && value.toLowerCase() === url.toLowerCase()); if (!matchingApiUrlEntry) { throw Error(`Heroku Applink config not found for API URL: ${url}`); } const [envVarName] = matchingApiUrlEntry; const prefix = envVarName.slice(0, -"_API_URL".length); const token = process.env[`${prefix}_TOKEN`]; if (!token) { throw Error(`Heroku Applink token not found for API URL: ${url}`); } return { apiUrl: matchingApiUrlEntry[1], token, appUuid, }; }