Skip to content

Commit e917fbd

Browse files
fix(mobile): normalize Expo fetch URLs
Generated-By: PostHog Code Task-Id: 11daf8d3-7830-436a-ab8f-99123586070d
1 parent c296439 commit e917fbd

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

apps/mobile/src/lib/posthogApiClient.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,29 @@ describe("createPostHogApiClient", () => {
9797
teamId: 123,
9898
options: {
9999
appVersion: "1.2.3",
100-
fetch: mocks.expoFetch,
101100
githubConnectFrom: "posthog_mobile",
102101
userAgent: "posthog/mobile.hog.dev; version: 1.2.3",
103102
},
104103
});
105104
});
106105

106+
it("converts URL inputs before calling Expo fetch", async () => {
107+
const { createPostHogApiClient } = await import("./posthogApiClient");
108+
createPostHogApiClient();
109+
const mobileFetch = mocks.instances[0]?.options.fetch as typeof fetch;
110+
const init = { method: "GET" };
111+
112+
await mobileFetch(
113+
new URL("https://us.posthog.com/api/projects/2/tasks/"),
114+
init,
115+
);
116+
117+
expect(mocks.expoFetch).toHaveBeenCalledWith(
118+
"https://us.posthog.com/api/projects/2/tasks/",
119+
init,
120+
);
121+
});
122+
107123
it("falls back to the Expo config version", async () => {
108124
mocks.expoApplication.nativeApplicationVersion = null;
109125
mocks.expoConstants.expoConfig = { version: "4.5.6" };

apps/mobile/src/lib/posthogApiClient.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ const MOBILE_GITHUB_CONNECT_FROM = "posthog_mobile";
1111
let posthogApiClient: PostHogAPIClient | null = null;
1212
let posthogApiHost: string | null = null;
1313

14+
const mobileFetch: FetchImplementation = (input, init) =>
15+
fetch(
16+
typeof input === "string"
17+
? input
18+
: input instanceof URL
19+
? input.toString()
20+
: input.url,
21+
init,
22+
);
23+
1424
function getAppVersion(): string {
1525
return (
1626
Application.nativeApplicationVersion ??
@@ -63,7 +73,7 @@ export function createPostHogApiClient(): PostHogAPIClient {
6373
projectId,
6474
{
6575
appVersion,
66-
fetch: fetch as FetchImplementation,
76+
fetch: mobileFetch,
6777
githubConnectFrom: MOBILE_GITHUB_CONNECT_FROM,
6878
userAgent: `posthog/mobile.hog.dev; version: ${appVersion}`,
6979
},

0 commit comments

Comments
 (0)