mastra
Version:
cli for mastra
110 lines (108 loc) • 3.89 kB
JavaScript
import createClient from 'openapi-fetch';
// src/commands/auth/client.ts
var MASTRA_PLATFORM_API_URL = process.env.MASTRA_PLATFORM_API_URL || "https://platform.mastra.ai";
function deriveGatewayUrl() {
if (process.env.MASTRA_GATEWAY_URL) return process.env.MASTRA_GATEWAY_URL;
if (MASTRA_PLATFORM_API_URL.includes("staging")) return "https://gateway-api.staging.mastra.ai/v1";
return "https://gateway-api.mastra.ai/v1";
}
deriveGatewayUrl();
function deriveStudioUrl() {
if (process.env.MASTRA_STUDIO_URL) return process.env.MASTRA_STUDIO_URL;
if (MASTRA_PLATFORM_API_URL.includes("staging")) return "https://studio.staging.mastra.ai";
return "https://studio.mastra.ai";
}
var MASTRA_STUDIO_URL = deriveStudioUrl();
var SESSION_EXPIRED_MESSAGE = "Session expired. Run: mastra auth login";
function throwApiError(message, status, detail) {
if (status === 401) {
throw new Error(SESSION_EXPIRED_MESSAGE);
}
if (detail) {
throw new Error(detail);
}
throw new Error(`${message}: ${status}`);
}
function extractApiErrorDetail(error) {
if (!error || typeof error !== "object") return void 0;
const o = error;
if (typeof o.detail === "string" && o.detail.trim()) return o.detail;
if (typeof o.message === "string" && o.message.trim()) return o.message;
return void 0;
}
var _currentToken = null;
var _refreshInFlight = null;
function setCurrentAuth(token, orgId) {
_currentToken = token;
}
async function authenticatedFetch(input, init) {
const clonedRequest = input instanceof Request ? input.clone() : null;
if (!_currentToken) {
const authHeader = (input instanceof Request ? input.headers.get("Authorization") : null) ?? (init?.headers instanceof Headers ? init.headers.get("Authorization") : typeof init?.headers === "object" && init.headers && !Array.isArray(init.headers) ? init.headers["Authorization"] : null);
if (authHeader?.startsWith("Bearer ")) {
_currentToken = authHeader.slice(7);
}
}
const response = await fetch(input, init);
if (response.status !== 401 || !_currentToken) {
return response;
}
if (!_refreshInFlight) {
_refreshInFlight = (async () => {
try {
const { tryRefreshToken, loadCredentials } = await import('./credentials-EGXLGJFI.js');
const creds = await loadCredentials();
if (!creds) throw new Error("No credentials");
const newToken2 = await tryRefreshToken(creds);
if (!newToken2) throw new Error("Refresh failed");
_currentToken = newToken2;
return newToken2;
} finally {
_refreshInFlight = null;
}
})();
}
let newToken;
try {
newToken = await _refreshInFlight;
} catch {
return response;
}
if (clonedRequest) {
const retryHeaders2 = new Headers(clonedRequest.headers);
retryHeaders2.set("Authorization", `Bearer ${newToken}`);
return fetch(new Request(clonedRequest, { headers: retryHeaders2 }));
}
const retryHeaders = new Headers(init?.headers);
retryHeaders.set("Authorization", `Bearer ${newToken}`);
return fetch(input, { ...init, headers: retryHeaders });
}
function createApiClient(token, orgId) {
setCurrentAuth(token);
const headers = {
Authorization: `Bearer ${token}`
};
if (orgId) {
headers["x-organization-id"] = orgId;
}
return createClient({
baseUrl: MASTRA_PLATFORM_API_URL,
headers,
fetch: authenticatedFetch
});
}
function authHeaders(token, orgId) {
const headers = {
Authorization: `Bearer ${token}`
};
if (orgId) {
headers["x-organization-id"] = orgId;
}
return headers;
}
function platformFetch(input, init) {
return authenticatedFetch(input, init);
}
export { MASTRA_PLATFORM_API_URL, MASTRA_STUDIO_URL, authHeaders, createApiClient, extractApiErrorDetail, platformFetch, throwApiError };
//# sourceMappingURL=chunk-L2SGSIJI.js.map
//# sourceMappingURL=chunk-L2SGSIJI.js.map