Skip to content

Commit

Permalink
fix: support custom posthog endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysSullivan committed Apr 18, 2024
1 parent 2448ad8 commit 309a2b8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions packages/posthog-generator/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import path from "node:path";

let posthogToken = process.env.POSTHOG_API_KEY;
let posthogProjectId = process.env.POSTHOG_PROJECT_ID;

let posthogEndpoint = process.env.POSTHOG_ENDPOINT || "app.posthog.com";
export async function generateTypeScriptFile(events: PostHogEvent[]) {
const eventMap = events.reduce(
(acc, event) => {
Expand Down Expand Up @@ -89,7 +89,7 @@ function fetchFromPosthog<T extends keyof PostHogEndpoints>(
} = {}
) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const url = `https://app.posthog.com/api/projects/${posthogProjectId!}/${endpoint}/?${toParams(
const url = `https:/${posthogEndpoint}//api/projects/${posthogProjectId!}/${endpoint}/?${toParams(
options.queryParams ?? {}
)}`;
return fetch(options.url ?? url, {
Expand Down Expand Up @@ -156,6 +156,12 @@ async function generate() {
})
).toString();
}
if (!process.env.POSTHOG_ENDPOINT) {
posthogEndpoint = (await text({
message: `No value for POSTHOG_ENDPOINT found in environment variables. Please enter your PostHog endpoint or leave blank to use ${posthogEndpoint}`,
defaultValue: posthogEndpoint
})).toString()
}
const data = await fetchAllEvents();
const events: PostHogEvent[] = [];
log.step(
Expand Down
4 changes: 2 additions & 2 deletions packages/posthog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ class PostHogQuery<
smoothing_intervals: options.smoothing_intervals,
};
const encodedQueryString = toParams(reqData);
const url = `https://app.posthog.com/api/projects/${this.config.projectId}/insights/trend/?${encodedQueryString}`;
const url = `${this.config.url}/insights/trend/?${encodedQueryString}`;
const fetchResponse = await fetch(url, {
headers: {
Authorization: `Bearer ${this.config.apiKey}`,
Expand Down Expand Up @@ -633,7 +633,7 @@ export class PostHog<const Events extends _ExtendOnlyEventMap> {
}
const url =
opts?.url ??
process.env.POSTHOG_URL ??
process.env.POSTHOG_ENDPOINT ??
`https://app.posthog.com/api/projects/${projectId}/`;
this.config = {
apiKey,
Expand Down

0 comments on commit 309a2b8

Please sign in to comment.