UNPKG

next-axiom

Version:

Send WebVitals from your Next.js project to Axiom.

73 lines 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const shared_1 = require("../shared"); const config_1 = require("../config"); // This is the generic config class for all platforms that doesn't have a special // implementation (e.g: vercel, netlify). All config classes extends this one. class GenericConfig { proxyPath = '/_axiom'; shouldSendEdgeReport = false; token = process.env.NEXT_PUBLIC_AXIOM_TOKEN || process.env.AXIOM_TOKEN; dataset = process.env.NEXT_PUBLIC_AXIOM_DATASET || process.env.AXIOM_DATASET; environment = process.env.NODE_ENV; axiomUrl = process.env.NEXT_PUBLIC_AXIOM_URL || process.env.AXIOM_URL || 'https://api.axiom.co'; region = process.env.REGION || undefined; customEndpoint = process.env.NEXT_PUBLIC_AXIOM_CUSTOM_ENDPOINT; isEnvVarsSet() { return !!(this.axiomUrl && this.dataset && this.token) || !!this.customEndpoint; } getIngestURL(_) { return `${this.axiomUrl}/v1/datasets/${this.dataset}/ingest`; } getLogsEndpoint() { if (config_1.isBrowser && this.customEndpoint) { return this.customEndpoint; } return config_1.isBrowser ? `${this.proxyPath}/logs` : this.getIngestURL(shared_1.EndpointType.logs); } getWebVitalsEndpoint() { if (config_1.isBrowser && this.customEndpoint) { return this.customEndpoint; } return config_1.isBrowser ? `${this.proxyPath}/web-vitals` : this.getIngestURL(shared_1.EndpointType.webVitals); } wrapWebVitalsObject(metrics) { return metrics.map(m => ({ webVital: m, _time: new Date().getTime(), platform: { environment: this.environment, source: 'web-vital', }, source: 'web-vital' })); } injectPlatformMetadata(logEvent, source) { let key = "platform"; if (config_1.isVercel) { key = "vercel"; } logEvent.source = source; logEvent[key] = { environment: this.environment, region: this.region, source: source, }; if (config_1.isVercel) { logEvent[key].region = process.env.VERCEL_REGION; logEvent[key].deploymentId = process.env.VERCEL_DEPLOYMENT_ID; logEvent[key].deploymentUrl = process.env.NEXT_PUBLIC_VERCEL_URL; logEvent[key].project = process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL; logEvent.git = { commit: process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA, repo: process.env.NEXT_PUBLIC_VERCEL_GIT_REPO_SLUG, ref: process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF, }; } } getHeaderOrDefault(req, headerName, defaultValue) { return req.headers[headerName] ? req.headers[headerName] : defaultValue; } } exports.default = GenericConfig; //# sourceMappingURL=generic.js.map