Skip to content

fix: ensure x-app-uuid is present when using token #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated `getAuthorization` to use the correct API URL.
- Rename `getConnection(name: string)` -> `getAuthorization(developerName: string, attachmentNameOrColorUrl = "HEROKU_APPLINK")`, accepting a new attachmentNameOrColorOrUrl to use a specific Applink addon's config.
- Remove node-fetch in favor of native fetch, add `HTTPResponseError`

## Unreleased
- Add `X-App-UUID` header, `heroku-applink-node-sdk` UA.
1 change: 1 addition & 0 deletions src/add-ons/heroku-applink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function getAuthorization(
method: "GET",
headers: {
Authorization: `Bearer ${config.token}`,
"X-App-UUID": config.appUuid,
"Content-Type": "application/json",
},
retry: {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/addon-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

type AppUuid = string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why use a branded type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a Go habit of declaring "this thing really is a UUID instead of literally any string"


interface AddonConfig {
apiUrl: string;
token: string;
appUuid: AppUuid;
}

export function resolveAddonConfigByAttachmentOrColor(
attachmentOrColor: string
): AddonConfig {
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;
Expand All @@ -37,10 +46,17 @@ export function resolveAddonConfigByAttachmentOrColor(
return {
apiUrl,
token,
appUuid,
};
}

export function resolveAddonConfigByUrl(url: string): AddonConfig {
const appUuid = process.env.HEROKU_APP_ID;

if (!appUuid) {
throw Error(`Heroku Applink app UUID not found`);
}

// Find the environment variable ending with _API_URL that matches the given URL
const envVarEntries = Object.entries(process.env);
const matchingApiUrlEntry = envVarEntries.find(
Expand All @@ -65,5 +81,6 @@ export function resolveAddonConfigByUrl(url: string): AddonConfig {
return {
apiUrl: matchingApiUrlEntry[1],
token,
appUuid,
};
}
17 changes: 16 additions & 1 deletion src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import fs from "fs";
import path from "path";

/** Error thrown by the SDK when receiving non-2xx responses on HTTP requests. */
export class HTTPResponseError extends Error {
response: any;
Expand All @@ -19,7 +22,19 @@ export class HTTPResponseError extends Error {
*/
export class HttpRequestUtil {
async request(url: string, opts: any, json = true) {
const response = await fetch(url, opts);
const pjson = fs.readFileSync(
path.join(__dirname, "..", "package.json"),
"utf8"
);
const pkg = JSON.parse(pjson);

const defaultOpts = {
headers: {
"User-Agent": `heroku-applink-node-sdk/${pkg.version}`,
},
};

const response = await fetch(url, { ...defaultOpts, ...opts });

if (!response.ok) {
throw new HTTPResponseError(response);
Expand Down
1 change: 1 addition & 0 deletions test/add-ons/heroku-applink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe("getAuthorization", () => {
process.env.HEROKU_APPLINK_PURPLE_API_URL =
"https://applink.staging.herokudev.com/addons/536c15d8-c2c1-47f7-a582-76f5aae385e0";
process.env.HEROKU_APPLINK_PURPLE_TOKEN = "purple-token";
process.env.HEROKU_APP_ID = "d52a726b-11a4-47a1-a4b6-2e18a771c2ac";
httpRequestStub = sinon.stub(HttpRequestUtil.prototype, "request");
});

Expand Down
18 changes: 18 additions & 0 deletions test/utils/addon-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe("resolveAddonConfigByAttachmentOrColor", () => {

beforeEach(() => {
originalEnv = { ...process.env };
process.env.HEROKU_APP_ID = "d52a726b-11a4-47a1-a4b6-2e18a771c2ac";
});

afterEach(() => {
Expand Down Expand Up @@ -81,13 +82,22 @@ describe("resolveAddonConfigByAttachmentOrColor", () => {
"Heroku Applink config not found under attachment or color HEROKU_APPLINK"
);
});

it("throws if HEROKU_APP_ID is not set", () => {
delete process.env.HEROKU_APP_ID;

expect(() => resolveAddonConfigByAttachmentOrColor(ATTACHMENT)).to.throw(
"Heroku Applink app UUID not found"
);
});
});

describe("resolveAddonConfigByUrl", () => {
let originalEnv: NodeJS.ProcessEnv;

beforeEach(() => {
originalEnv = { ...process.env };
process.env.HEROKU_APP_ID = "d52a726b-11a4-47a1-a4b6-2e18a771c2ac";
});

afterEach(() => {
Expand Down Expand Up @@ -131,4 +141,12 @@ describe("resolveAddonConfigByUrl", () => {
`Heroku Applink token not found for API URL: ${testUrl}`
);
});

it("throws if HEROKU_APP_ID is not set", () => {
delete process.env.HEROKU_APP_ID;

expect(() => resolveAddonConfigByUrl("https://api.example.com")).to.throw(
"Heroku Applink app UUID not found"
);
});
});
Loading