Skip to content

Commit

Permalink
Fix custom targeting ID (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
zapo authored Jul 16, 2024
1 parent 4a8e756 commit 47cc7f4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @type {import('jest').Config} */
const config = {
testEnvironment: "jsdom",
setupFiles: ["<rootDir>/setup-jest.js"],
};

module.exports = config;
12 changes: 12 additions & 0 deletions lib/core/network.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { buildRequest } from "./network";
import { default as buildInfo } from "../build.json";

describe("buildRequest", () => {
test("preserves path query string", () => {
const dcn = { cookies: true, host: "host", insecure: false, site: "site" };
const req = { method: "GET" };
const request = buildRequest("/path?query=string", dcn, req);

expect(request.url).toBe(`https://host/site/path?query=string&osdk=web-${buildInfo.version}&cookies=yes`);
});
});
15 changes: 5 additions & 10 deletions lib/core/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@ function buildRequest(path: string, config: Required<OptableConfig>, init?: Requ

const proto = insecure ? "http" : "https";
const url = new URL(`${site}${path}`, `${proto}://${host}`);
url.searchParams.set("osdk", `web-${buildInfo.version}`);

if (cookies) {
url.search = new URLSearchParams({
cookies: "yes",
osdk: `web-${buildInfo.version}`,
}).toString();
url.searchParams.set("cookies", "yes");
} else {
const ls = new LocalStorage(config);
const pass = ls.getPassport();
url.search = new URLSearchParams({
cookies: "no",
passport: pass ? pass : "",
osdk: `web-${buildInfo.version}`,
}).toString();
url.searchParams.set("cookies", "no");
url.searchParams.set("pass", pass ? pass : "");
}

const requestInit: RequestInit = { ...init };
Expand Down Expand Up @@ -56,5 +51,5 @@ async function fetch<T>(path: string, config: Required<OptableConfig>, init?: Re
return data;
}

export { fetch };
export { fetch, buildRequest };
export default fetch;
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"jest-environment-jsdom": "^29.7.0",
"typescript": "^5.2.2",
"webpack": "^5.76.0",
"webpack-cli": "^4.7.2"
"webpack-cli": "^4.7.2",
"whatwg-fetch": "^3.6.20"
},
"dependencies": {
"@babel/runtime": "^7.12.5",
Expand Down
1 change: 1 addition & 0 deletions setup-jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "whatwg-fetch";

0 comments on commit 47cc7f4

Please sign in to comment.