Skip to content

Commit

Permalink
fix url construction
Browse files Browse the repository at this point in the history
  • Loading branch information
schehata committed Sep 19, 2022
1 parent 9b5ca91 commit 3690940
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions __tests__/noEnvVars.test.ts
Expand Up @@ -3,6 +3,7 @@
*/

// clear Axiom env vars
process.env.AXIOM_URL = '';
process.env.AXIOM_INGEST_ENDPOINT = '';
process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT = '';
import { NextWebVitalsMetric } from 'next/app';
Expand Down
20 changes: 16 additions & 4 deletions __tests__/shared.test.ts
@@ -1,10 +1,22 @@
import { getIngestURL, EndpointType } from '../src/shared';

test('reading ingest endpoint', () => {
process.env.AXIOM_INGEST_ENDPOINT = 'https://axiom.co/api/test';
test('reading vercel ingest endpoint', () => {
process.env.AXIOM_URL = '';
process.env.AXIOM_INGEST_ENDPOINT = 'https://axiom.co/api/v1/integrations/vercel';
let url = getIngestURL(EndpointType.webVitals);
expect(url).toEqual('https://axiom.co/api/test?type=web-vitals');
expect(url).toEqual('https://axiom.co/api/v1/integrations/vercel?type=web-vitals');

url = getIngestURL(EndpointType.logs);
expect(url).toEqual('https://axiom.co/api/test?type=logs');
expect(url).toEqual('https://axiom.co/api/v1/integrations/vercel?type=logs');
});

test('reading axiom ingest endpoint', () => {
process.env.AXIOM_INGEST_ENDPOINT = '';
process.env.AXIOM_URL = 'https://test.axiom.co';
process.env.AXIOM_DATASET = 'test';
let url = getIngestURL(EndpointType.webVitals);
expect(url).toEqual('https://test.axiom.co/api/v1/datasets/test/ingest');

url = getIngestURL(EndpointType.logs);
expect(url).toEqual('https://test.axiom.co/api/v1/datasets/test/ingest');
});
11 changes: 2 additions & 9 deletions src/shared.ts
@@ -1,12 +1,9 @@
export const proxyPath = '/_axiom';
const TOKEN = process.env.AXIOM_TOKEN;
const DATASET = process.env.AXIOM_DATASET;
const AXIOM_URL = process.env.AXIOM_URL;

function detectEnvironmentConfiguration() {
const isVercel = process.env.NEXT_PUBLIC_VERCEL_ENV ? true : false;
const nodeEnv = process.env.NODE_ENV;
const datasetName = process.env.AXIOM_DATASET || null;

const baseConfig = {
isBrowser: typeof window !== 'undefined',
Expand All @@ -15,7 +12,6 @@ function detectEnvironmentConfiguration() {
isNoPrettyPrint: process.env.AXIOM_NO_PRETTY_PRINT == 'true' ? true : false,
isVercel,
token: TOKEN,
dataset: DATASET,
};

if (isVercel) {
Expand All @@ -32,7 +28,6 @@ function detectEnvironmentConfiguration() {
...baseConfig,
region: '',
environment: nodeEnv || 'dev',
dataset: datasetName,
provider: 'self-hosted',
};
}
Expand All @@ -47,20 +42,18 @@ export enum EndpointType {
export const getIngestURL = function (t: EndpointType) {
const vercelEndpoint = process.env.NEXT_PUBLIC_AXIOM_INGEST_ENDPOINT || process.env.AXIOM_INGEST_ENDPOINT;
if (vercelEndpoint) {
const ingestEndpoint = `${vercelEndpoint}/api/v1/integrations/vercel/ingest`;
const url = new URL(ingestEndpoint);
const url = new URL(vercelEndpoint);
// attach type query param based on passed EndpointType
url.searchParams.set('type', t.toString());
return url.toString();
}

const ingestEndpoint = `${AXIOM_URL}/api/v1/datasets/${config.dataset}/ingest`;
const ingestEndpoint = `${process.env.AXIOM_URL}/api/v1/datasets/${process.env.AXIOM_DATASET}/ingest`;
if (!ingestEndpoint) {
return '';
}

const url = new URL(ingestEndpoint);
url.searchParams.set('type', t.toString());
return url.toString();
};

Expand Down

0 comments on commit 3690940

Please sign in to comment.