Skip to content

Commit

Permalink
add supoort for npm in user agent header (#32)
Browse files Browse the repository at this point in the history
* add supoort for npm in user agent header
  • Loading branch information
tomas-zijdemans-vipps authored Jan 19, 2024
1 parent add4b27 commit d6c7bc0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/base_client_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ export const getHeaders = (
* @returns The user agent string.
*/
export const getUserAgent = (): string => {
const metaUrl = import.meta.url;
const metaUrl = import.meta.url || undefined;
// If the sdk is loaded using require, import.meta.url will be undefined
if (!metaUrl) {
return "Vipps/Deno SDK/npm-require";
}
const userAgent = createSDKUserAgent(metaUrl);
return userAgent;
};
Expand All @@ -165,9 +169,9 @@ export const createSDKUserAgent = (metaUrl: string): string => {
// Extract the module version from the URL
const sdkVersion = url.pathname.split("@")[1].split("/")[0];
userAgent += sdkVersion;
} // Or if the module was loaded from a local file
else if (url.protocol === "file:") {
userAgent += "local";
} // Or if the module was loaded from npm
else if (url.pathname.includes("node_modules")) {
userAgent += "npm-module";
} // Otherwise, we don't know where the module was loaded from
else {
userAgent += "unknown";
Expand Down
6 changes: 3 additions & 3 deletions tests/base_client_helper_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ Deno.test("createUserAgent - Should return the correct user agent string when lo
assertEquals(actualUserAgent, expectedUserAgent);
});

Deno.test("createUserAgent - Should return the correct user agent string when loaded locally", () => {
const expectedUserAgent = "Vipps/Deno SDK/local";
Deno.test("createUserAgent - Should return the correct user agent string when loaded from node_modules", () => {
const expectedUserAgent = "Vipps/Deno SDK/npm-module";
const actualUserAgent = createSDKUserAgent(
"file:///Users/foo/bar/deno-sdk/src/mod.ts",
"file:///Users/foo/bar/node_modules/mod.ts",
);

assertEquals(actualUserAgent, expectedUserAgent);
Expand Down

0 comments on commit d6c7bc0

Please sign in to comment.