UNPKG

@luxorai/sandbox

Version:

TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud

47 lines • 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateAuthToken = validateAuthToken; exports.resolveAuthToken = resolveAuthToken; exports.createAuthInterceptor = createAuthInterceptor; exports.setAuthHeaders = setAuthHeaders; exports.setSdkIdentityHeaders = setSdkIdentityHeaders; const client_metadata_js_1 = require("./client-metadata.js"); const constants_js_1 = require("./constants.js"); const env_js_1 = require("./env.js"); const errors_js_1 = require("./errors.js"); function validateAuthToken(token) { const trimmed = token.trim(); if (!trimmed) throw new errors_js_1.MissingAuthTokenError(); if (!constants_js_1.API_KEY_PREFIXES.some((prefix) => trimmed.startsWith(prefix))) throw new errors_js_1.InvalidAuthTokenError(); return trimmed; } /** apiKey is an alias for authToken; an explicit authToken wins, then the environment. */ function resolveAuthToken(authToken, apiKey) { const explicit = authToken?.trim() || ""; const alias = apiKey?.trim() || ""; return validateAuthToken(explicit || alias || (0, env_js_1.readEnv)(constants_js_1.AUTH_TOKEN_ENVS)); } function createAuthInterceptor(token) { const authToken = validateAuthToken(token); return (next) => async (req) => { setAuthHeaders(req.header, authToken); return next(req); }; } function setAuthHeaders(headers, token) { const authToken = validateAuthToken(token); headers.set("Authorization", `Bearer ${authToken}`); setSdkIdentityHeaders(headers); (0, client_metadata_js_1.setClientMetadataHeaders)(headers); } /** Leads the User-Agent with the SDK product, keeping any existing value. */ function setSdkIdentityHeaders(headers) { const existing = headers.get("User-Agent"); if (!existing) headers.set("User-Agent", constants_js_1.SDK_USER_AGENT); else if (!existing.startsWith(constants_js_1.SDK_USER_AGENT)) headers.set("User-Agent", `${constants_js_1.SDK_USER_AGENT} ${existing}`); } //# sourceMappingURL=auth.js.map