@axiomhq/js
Version:
The official javascript bindings for the Axiom API
115 lines (110 loc) • 3.89 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var fetchClient = require('./fetchClient.cjs');
const Version = "AXIOM_VERSION";
const AxiomURL = "https://api.axiom.co";
/**
* Builds an ingest URL using edge path format: /v1/ingest/{dataset}
*/
function buildEdgeIngestUrl(baseUrl, dataset) {
try {
const parsed = new URL(baseUrl);
const path = parsed.pathname;
if (path === '' || path === '/') {
// Use edge path format
parsed.pathname = `/v1/ingest/${dataset}`;
return parsed.toString();
}
// URL has a custom path, use as-is (trim trailing slashes)
parsed.pathname = path.replace(/\/+$/, '');
return parsed.toString();
}
catch {
// If URL parsing fails, do simple string concatenation as fallback
const trimmed = baseUrl.replace(/\/+$/, '');
return `${trimmed}/v1/ingest/${dataset}`;
}
}
/**
* Builds an ingest URL using legacy path format: /v1/datasets/{dataset}/ingest
*/
function buildLegacyIngestUrl(baseUrl, dataset) {
try {
const parsed = new URL(baseUrl);
const path = parsed.pathname;
if (path === '' || path === '/') {
// Use legacy path format
parsed.pathname = `/v1/datasets/${dataset}/ingest`;
return parsed.toString();
}
// URL has a custom path, use as-is (trim trailing slashes)
parsed.pathname = path.replace(/\/+$/, '');
return parsed.toString();
}
catch {
// If URL parsing fails, do simple string concatenation as fallback
const trimmed = baseUrl.replace(/\/+$/, '');
return `${trimmed}/v1/datasets/${dataset}/ingest`;
}
}
/**
* Resolves the ingest endpoint URL based on the client options.
*
* Priority: edgeUrl > edge > url > default cloud endpoint
*
* Edge endpoints use: /v1/ingest/{dataset}
* Legacy endpoints use: /v1/datasets/{dataset}/ingest
*
* @param options - The client options
* @param dataset - The dataset name to ingest into
* @returns The full URL to use for ingestion
*/
function resolveIngestUrl(options, dataset) {
// If edgeUrl is set, use it (takes precedence over edge)
if (options.edgeUrl) {
return buildEdgeIngestUrl(options.edgeUrl, dataset);
}
// If edge domain is set, build edge URL
if (options.edge) {
return `https://${options.edge}/v1/ingest/${dataset}`;
}
// If url is set, use legacy path format
if (options.url) {
return buildLegacyIngestUrl(options.url, dataset);
}
// Default: use cloud endpoint with legacy path format
return `${AxiomURL}/v1/datasets/${dataset}/ingest`;
}
class HTTPClient {
client;
clientOptions;
constructor({ orgId = "", token, url, edge, edgeUrl, onError }) {
if (!token) {
console.warn("Missing Axiom token");
}
// Store options for use in ingest URL resolution
this.clientOptions = { orgId, token, url, edge, edgeUrl, onError };
// For the main API client, always use url or default (never edge options)
// edge/edgeUrl only affects ingest endpoints, not other API calls
const baseUrl = url ? url.replace(/\/+$/, '') : AxiomURL;
const headers = {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + token,
};
if (typeof window === "undefined") {
headers["User-Agent"] = "axiom-js/" + Version;
}
if (orgId) {
headers["X-Axiom-Org-Id"] = orgId;
}
this.client = new fetchClient.FetchClient({
baseUrl,
headers,
timeout: 20_000,
});
}
}
exports.default = HTTPClient;
exports.resolveIngestUrl = resolveIngestUrl;
//# sourceMappingURL=httpClient.cjs.map