create-mastra 1.30.0 → 1.31.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api-BBNA526i-rJQIUzno.js +2 -0
- package/dist/api-GOhAsGMy-C0KZztSZ.js +9 -0
- package/dist/client-Bdk2AMV6-BY9vpw2e.js +504 -0
- package/dist/credentials-B4M5VuZS-BRW9qqvO.js +2 -0
- package/dist/credentials-CdxijtI9-DfSUvZVu.js +279 -0
- package/dist/index.js +16686 -13475
- package/package.json +8 -16
- package/dist/credentials-B4M5VuZS-BDWk6njZ.js +0 -28
- package/dist/credentials-B4M5VuZS-BDWk6njZ.js.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { a as throwApiError, r as createApiClient } from "./client-Bdk2AMV6-BY9vpw2e.js";
|
|
2
|
+
//#region ../cli/dist/api-GOhAsGMy.js
|
|
3
|
+
async function fetchOrgs(token) {
|
|
4
|
+
const { data, error, response } = await createApiClient(token).GET("/v1/auth/orgs");
|
|
5
|
+
if (error) throwApiError("Failed to fetch orgs", response.status, error.detail);
|
|
6
|
+
return data.organizations;
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { fetchOrgs as t };
|
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
//#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/openapi-fetch/0.17.0/74b9406f656bb94957700cf63ed04c1745023b47201693eac93641318b84ba31/node_modules/openapi-fetch/dist/index.mjs
|
|
2
|
+
const PATH_PARAM_RE = /\{[^{}]+\}/g;
|
|
3
|
+
const supportsRequestInitExt = () => {
|
|
4
|
+
return typeof process === "object" && Number.parseInt(process?.versions?.node?.substring(0, 2)) >= 18 && process.versions.undici;
|
|
5
|
+
};
|
|
6
|
+
function randomID() {
|
|
7
|
+
return Math.random().toString(36).slice(2, 11);
|
|
8
|
+
}
|
|
9
|
+
function createClient(clientOptions) {
|
|
10
|
+
let { baseUrl = "", Request: CustomRequest = globalThis.Request, fetch: baseFetch = globalThis.fetch, querySerializer: globalQuerySerializer, bodySerializer: globalBodySerializer, pathSerializer: globalPathSerializer, headers: baseHeaders, requestInitExt = void 0, ...baseOptions } = { ...clientOptions };
|
|
11
|
+
requestInitExt = supportsRequestInitExt() ? requestInitExt : void 0;
|
|
12
|
+
baseUrl = removeTrailingSlash(baseUrl);
|
|
13
|
+
const globalMiddlewares = [];
|
|
14
|
+
async function coreFetch(schemaPath, fetchOptions) {
|
|
15
|
+
const { baseUrl: localBaseUrl, fetch = baseFetch, Request = CustomRequest, headers, params = {}, parseAs = "json", querySerializer: requestQuerySerializer, bodySerializer = globalBodySerializer ?? defaultBodySerializer, pathSerializer: requestPathSerializer, body, middleware: requestMiddlewares = [], ...init } = fetchOptions || {};
|
|
16
|
+
let finalBaseUrl = baseUrl;
|
|
17
|
+
if (localBaseUrl) finalBaseUrl = removeTrailingSlash(localBaseUrl) ?? baseUrl;
|
|
18
|
+
let querySerializer = typeof globalQuerySerializer === "function" ? globalQuerySerializer : createQuerySerializer(globalQuerySerializer);
|
|
19
|
+
if (requestQuerySerializer) querySerializer = typeof requestQuerySerializer === "function" ? requestQuerySerializer : createQuerySerializer({
|
|
20
|
+
...typeof globalQuerySerializer === "object" ? globalQuerySerializer : {},
|
|
21
|
+
...requestQuerySerializer
|
|
22
|
+
});
|
|
23
|
+
const pathSerializer = requestPathSerializer || globalPathSerializer || defaultPathSerializer;
|
|
24
|
+
const serializedBody = body === void 0 ? void 0 : bodySerializer(body, mergeHeaders(baseHeaders, headers, params.header));
|
|
25
|
+
const finalHeaders = mergeHeaders(serializedBody === void 0 || serializedBody instanceof FormData ? {} : { "Content-Type": "application/json" }, baseHeaders, headers, params.header);
|
|
26
|
+
const finalMiddlewares = [...globalMiddlewares, ...requestMiddlewares];
|
|
27
|
+
const requestInit = {
|
|
28
|
+
redirect: "follow",
|
|
29
|
+
...baseOptions,
|
|
30
|
+
...init,
|
|
31
|
+
body: serializedBody,
|
|
32
|
+
headers: finalHeaders
|
|
33
|
+
};
|
|
34
|
+
let id;
|
|
35
|
+
let options;
|
|
36
|
+
let request = new Request(createFinalURL(schemaPath, {
|
|
37
|
+
baseUrl: finalBaseUrl,
|
|
38
|
+
params,
|
|
39
|
+
querySerializer,
|
|
40
|
+
pathSerializer
|
|
41
|
+
}), requestInit);
|
|
42
|
+
let response;
|
|
43
|
+
for (const key in init) if (!(key in request)) request[key] = init[key];
|
|
44
|
+
if (finalMiddlewares.length) {
|
|
45
|
+
id = randomID();
|
|
46
|
+
options = Object.freeze({
|
|
47
|
+
baseUrl: finalBaseUrl,
|
|
48
|
+
fetch,
|
|
49
|
+
parseAs,
|
|
50
|
+
querySerializer,
|
|
51
|
+
bodySerializer,
|
|
52
|
+
pathSerializer
|
|
53
|
+
});
|
|
54
|
+
for (const m of finalMiddlewares) if (m && typeof m === "object" && typeof m.onRequest === "function") {
|
|
55
|
+
const result = await m.onRequest({
|
|
56
|
+
request,
|
|
57
|
+
schemaPath,
|
|
58
|
+
params,
|
|
59
|
+
options,
|
|
60
|
+
id
|
|
61
|
+
});
|
|
62
|
+
if (result) if (result instanceof Request) request = result;
|
|
63
|
+
else if (result instanceof Response) {
|
|
64
|
+
response = result;
|
|
65
|
+
break;
|
|
66
|
+
} else throw new Error("onRequest: must return new Request() or Response() when modifying the request");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (!response) {
|
|
70
|
+
try {
|
|
71
|
+
response = await fetch(request, requestInitExt);
|
|
72
|
+
} catch (error2) {
|
|
73
|
+
let errorAfterMiddleware = error2;
|
|
74
|
+
if (finalMiddlewares.length) for (let i = finalMiddlewares.length - 1; i >= 0; i--) {
|
|
75
|
+
const m = finalMiddlewares[i];
|
|
76
|
+
if (m && typeof m === "object" && typeof m.onError === "function") {
|
|
77
|
+
const result = await m.onError({
|
|
78
|
+
request,
|
|
79
|
+
error: errorAfterMiddleware,
|
|
80
|
+
schemaPath,
|
|
81
|
+
params,
|
|
82
|
+
options,
|
|
83
|
+
id
|
|
84
|
+
});
|
|
85
|
+
if (result) {
|
|
86
|
+
if (result instanceof Response) {
|
|
87
|
+
errorAfterMiddleware = void 0;
|
|
88
|
+
response = result;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
if (result instanceof Error) {
|
|
92
|
+
errorAfterMiddleware = result;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
throw new Error("onError: must return new Response() or instance of Error");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (errorAfterMiddleware) throw errorAfterMiddleware;
|
|
100
|
+
}
|
|
101
|
+
if (finalMiddlewares.length) for (let i = finalMiddlewares.length - 1; i >= 0; i--) {
|
|
102
|
+
const m = finalMiddlewares[i];
|
|
103
|
+
if (m && typeof m === "object" && typeof m.onResponse === "function") {
|
|
104
|
+
const result = await m.onResponse({
|
|
105
|
+
request,
|
|
106
|
+
response,
|
|
107
|
+
schemaPath,
|
|
108
|
+
params,
|
|
109
|
+
options,
|
|
110
|
+
id
|
|
111
|
+
});
|
|
112
|
+
if (result) {
|
|
113
|
+
if (!(result instanceof Response)) throw new Error("onResponse: must return new Response() when modifying the response");
|
|
114
|
+
response = result;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const contentLength = response.headers.get("Content-Length");
|
|
120
|
+
if (response.status === 204 || request.method === "HEAD" || contentLength === "0" && !response.headers.get("Transfer-Encoding")?.includes("chunked")) return response.ok ? {
|
|
121
|
+
data: void 0,
|
|
122
|
+
response
|
|
123
|
+
} : {
|
|
124
|
+
error: void 0,
|
|
125
|
+
response
|
|
126
|
+
};
|
|
127
|
+
if (response.ok) {
|
|
128
|
+
const getResponseData = async () => {
|
|
129
|
+
if (parseAs === "stream") return response.body;
|
|
130
|
+
if (parseAs === "json" && !contentLength) {
|
|
131
|
+
const raw = await response.text();
|
|
132
|
+
return raw ? JSON.parse(raw) : void 0;
|
|
133
|
+
}
|
|
134
|
+
return await response[parseAs]();
|
|
135
|
+
};
|
|
136
|
+
return {
|
|
137
|
+
data: await getResponseData(),
|
|
138
|
+
response
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
let error = await response.text();
|
|
142
|
+
try {
|
|
143
|
+
error = JSON.parse(error);
|
|
144
|
+
} catch {}
|
|
145
|
+
return {
|
|
146
|
+
error,
|
|
147
|
+
response
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
request(method, url, init) {
|
|
152
|
+
return coreFetch(url, {
|
|
153
|
+
...init,
|
|
154
|
+
method: method.toUpperCase()
|
|
155
|
+
});
|
|
156
|
+
},
|
|
157
|
+
/** Call a GET endpoint */
|
|
158
|
+
GET(url, init) {
|
|
159
|
+
return coreFetch(url, {
|
|
160
|
+
...init,
|
|
161
|
+
method: "GET"
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
/** Call a PUT endpoint */
|
|
165
|
+
PUT(url, init) {
|
|
166
|
+
return coreFetch(url, {
|
|
167
|
+
...init,
|
|
168
|
+
method: "PUT"
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
/** Call a POST endpoint */
|
|
172
|
+
POST(url, init) {
|
|
173
|
+
return coreFetch(url, {
|
|
174
|
+
...init,
|
|
175
|
+
method: "POST"
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
/** Call a DELETE endpoint */
|
|
179
|
+
DELETE(url, init) {
|
|
180
|
+
return coreFetch(url, {
|
|
181
|
+
...init,
|
|
182
|
+
method: "DELETE"
|
|
183
|
+
});
|
|
184
|
+
},
|
|
185
|
+
/** Call a OPTIONS endpoint */
|
|
186
|
+
OPTIONS(url, init) {
|
|
187
|
+
return coreFetch(url, {
|
|
188
|
+
...init,
|
|
189
|
+
method: "OPTIONS"
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
/** Call a HEAD endpoint */
|
|
193
|
+
HEAD(url, init) {
|
|
194
|
+
return coreFetch(url, {
|
|
195
|
+
...init,
|
|
196
|
+
method: "HEAD"
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
/** Call a PATCH endpoint */
|
|
200
|
+
PATCH(url, init) {
|
|
201
|
+
return coreFetch(url, {
|
|
202
|
+
...init,
|
|
203
|
+
method: "PATCH"
|
|
204
|
+
});
|
|
205
|
+
},
|
|
206
|
+
/** Call a TRACE endpoint */
|
|
207
|
+
TRACE(url, init) {
|
|
208
|
+
return coreFetch(url, {
|
|
209
|
+
...init,
|
|
210
|
+
method: "TRACE"
|
|
211
|
+
});
|
|
212
|
+
},
|
|
213
|
+
/** Register middleware */
|
|
214
|
+
use(...middleware) {
|
|
215
|
+
for (const m of middleware) {
|
|
216
|
+
if (!m) continue;
|
|
217
|
+
if (typeof m !== "object" || !("onRequest" in m || "onResponse" in m || "onError" in m)) throw new Error("Middleware must be an object with one of `onRequest()`, `onResponse() or `onError()`");
|
|
218
|
+
globalMiddlewares.push(m);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
/** Unregister middleware */
|
|
222
|
+
eject(...middleware) {
|
|
223
|
+
for (const m of middleware) {
|
|
224
|
+
const i = globalMiddlewares.indexOf(m);
|
|
225
|
+
if (i !== -1) globalMiddlewares.splice(i, 1);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function serializePrimitiveParam(name, value, options) {
|
|
231
|
+
if (value === void 0 || value === null) return "";
|
|
232
|
+
if (typeof value === "object") throw new Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");
|
|
233
|
+
return `${name}=${options?.allowReserved === true ? value : encodeURIComponent(value)}`;
|
|
234
|
+
}
|
|
235
|
+
function serializeObjectParam(name, value, options) {
|
|
236
|
+
if (!value || typeof value !== "object") return "";
|
|
237
|
+
const values = [];
|
|
238
|
+
const joiner = {
|
|
239
|
+
simple: ",",
|
|
240
|
+
label: ".",
|
|
241
|
+
matrix: ";"
|
|
242
|
+
}[options.style] || "&";
|
|
243
|
+
if (options.style !== "deepObject" && options.explode === false) {
|
|
244
|
+
for (const k in value) values.push(k, options.allowReserved === true ? value[k] : encodeURIComponent(value[k]));
|
|
245
|
+
const final2 = values.join(",");
|
|
246
|
+
switch (options.style) {
|
|
247
|
+
case "form": return `${name}=${final2}`;
|
|
248
|
+
case "label": return `.${final2}`;
|
|
249
|
+
case "matrix": return `;${name}=${final2}`;
|
|
250
|
+
default: return final2;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
for (const k in value) {
|
|
254
|
+
const finalName = options.style === "deepObject" ? `${name}[${k}]` : k;
|
|
255
|
+
values.push(serializePrimitiveParam(finalName, value[k], options));
|
|
256
|
+
}
|
|
257
|
+
const final = values.join(joiner);
|
|
258
|
+
return options.style === "label" || options.style === "matrix" ? `${joiner}${final}` : final;
|
|
259
|
+
}
|
|
260
|
+
function serializeArrayParam(name, value, options) {
|
|
261
|
+
if (!Array.isArray(value)) return "";
|
|
262
|
+
if (options.explode === false) {
|
|
263
|
+
const joiner2 = {
|
|
264
|
+
form: ",",
|
|
265
|
+
spaceDelimited: "%20",
|
|
266
|
+
pipeDelimited: "|"
|
|
267
|
+
}[options.style] || ",";
|
|
268
|
+
const final = (options.allowReserved === true ? value : value.map((v) => encodeURIComponent(v))).join(joiner2);
|
|
269
|
+
switch (options.style) {
|
|
270
|
+
case "simple": return final;
|
|
271
|
+
case "label": return `.${final}`;
|
|
272
|
+
case "matrix": return `;${name}=${final}`;
|
|
273
|
+
default: return `${name}=${final}`;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const joiner = {
|
|
277
|
+
simple: ",",
|
|
278
|
+
label: ".",
|
|
279
|
+
matrix: ";"
|
|
280
|
+
}[options.style] || "&";
|
|
281
|
+
const values = [];
|
|
282
|
+
for (const v of value) if (options.style === "simple" || options.style === "label") values.push(options.allowReserved === true ? v : encodeURIComponent(v));
|
|
283
|
+
else values.push(serializePrimitiveParam(name, v, options));
|
|
284
|
+
return options.style === "label" || options.style === "matrix" ? `${joiner}${values.join(joiner)}` : values.join(joiner);
|
|
285
|
+
}
|
|
286
|
+
function createQuerySerializer(options) {
|
|
287
|
+
return function querySerializer(queryParams) {
|
|
288
|
+
const search = [];
|
|
289
|
+
if (queryParams && typeof queryParams === "object") for (const name in queryParams) {
|
|
290
|
+
const value = queryParams[name];
|
|
291
|
+
if (value === void 0 || value === null) continue;
|
|
292
|
+
if (Array.isArray(value)) {
|
|
293
|
+
if (value.length === 0) continue;
|
|
294
|
+
search.push(serializeArrayParam(name, value, {
|
|
295
|
+
style: "form",
|
|
296
|
+
explode: true,
|
|
297
|
+
...options?.array,
|
|
298
|
+
allowReserved: options?.allowReserved || false
|
|
299
|
+
}));
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (typeof value === "object") {
|
|
303
|
+
search.push(serializeObjectParam(name, value, {
|
|
304
|
+
style: "deepObject",
|
|
305
|
+
explode: true,
|
|
306
|
+
...options?.object,
|
|
307
|
+
allowReserved: options?.allowReserved || false
|
|
308
|
+
}));
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
search.push(serializePrimitiveParam(name, value, options));
|
|
312
|
+
}
|
|
313
|
+
return search.join("&");
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
function defaultPathSerializer(pathname, pathParams) {
|
|
317
|
+
let nextURL = pathname;
|
|
318
|
+
for (const match of pathname.match(PATH_PARAM_RE) ?? []) {
|
|
319
|
+
let name = match.substring(1, match.length - 1);
|
|
320
|
+
let explode = false;
|
|
321
|
+
let style = "simple";
|
|
322
|
+
if (name.endsWith("*")) {
|
|
323
|
+
explode = true;
|
|
324
|
+
name = name.substring(0, name.length - 1);
|
|
325
|
+
}
|
|
326
|
+
if (name.startsWith(".")) {
|
|
327
|
+
style = "label";
|
|
328
|
+
name = name.substring(1);
|
|
329
|
+
} else if (name.startsWith(";")) {
|
|
330
|
+
style = "matrix";
|
|
331
|
+
name = name.substring(1);
|
|
332
|
+
}
|
|
333
|
+
if (!pathParams || pathParams[name] === void 0 || pathParams[name] === null) continue;
|
|
334
|
+
const value = pathParams[name];
|
|
335
|
+
if (Array.isArray(value)) {
|
|
336
|
+
nextURL = nextURL.replace(match, serializeArrayParam(name, value, {
|
|
337
|
+
style,
|
|
338
|
+
explode
|
|
339
|
+
}));
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (typeof value === "object") {
|
|
343
|
+
nextURL = nextURL.replace(match, serializeObjectParam(name, value, {
|
|
344
|
+
style,
|
|
345
|
+
explode
|
|
346
|
+
}));
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (style === "matrix") {
|
|
350
|
+
nextURL = nextURL.replace(match, `;${serializePrimitiveParam(name, value)}`);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
nextURL = nextURL.replace(match, style === "label" ? `.${encodeURIComponent(value)}` : encodeURIComponent(value));
|
|
354
|
+
}
|
|
355
|
+
return nextURL;
|
|
356
|
+
}
|
|
357
|
+
function defaultBodySerializer(body, headers) {
|
|
358
|
+
if (body instanceof FormData) return body;
|
|
359
|
+
if (headers) {
|
|
360
|
+
if ((headers.get instanceof Function ? headers.get("Content-Type") ?? headers.get("content-type") : headers["Content-Type"] ?? headers["content-type"]) === "application/x-www-form-urlencoded") return new URLSearchParams(body).toString();
|
|
361
|
+
}
|
|
362
|
+
return JSON.stringify(body);
|
|
363
|
+
}
|
|
364
|
+
function createFinalURL(pathname, options) {
|
|
365
|
+
let finalURL = `${options.baseUrl}${pathname}`;
|
|
366
|
+
if (options.params?.path) finalURL = options.pathSerializer(finalURL, options.params.path);
|
|
367
|
+
let search = options.querySerializer(options.params.query ?? {});
|
|
368
|
+
if (search.startsWith("?")) search = search.substring(1);
|
|
369
|
+
if (search) finalURL += `?${search}`;
|
|
370
|
+
return finalURL;
|
|
371
|
+
}
|
|
372
|
+
function mergeHeaders(...allHeaders) {
|
|
373
|
+
const finalHeaders = new Headers();
|
|
374
|
+
for (const h of allHeaders) {
|
|
375
|
+
if (!h || typeof h !== "object") continue;
|
|
376
|
+
const iterator = h instanceof Headers ? h.entries() : Object.entries(h);
|
|
377
|
+
for (const [k, v] of iterator) if (v === null) finalHeaders.delete(k);
|
|
378
|
+
else if (Array.isArray(v)) for (const v2 of v) finalHeaders.append(k, v2);
|
|
379
|
+
else if (v !== void 0) finalHeaders.set(k, v);
|
|
380
|
+
}
|
|
381
|
+
return finalHeaders;
|
|
382
|
+
}
|
|
383
|
+
function removeTrailingSlash(url) {
|
|
384
|
+
if (url.endsWith("/")) return url.substring(0, url.length - 1);
|
|
385
|
+
return url;
|
|
386
|
+
}
|
|
387
|
+
//#endregion
|
|
388
|
+
//#region ../cli/dist/client-Bdk2AMV6.js
|
|
389
|
+
const MASTRA_PLATFORM_API_URL = process.env.MASTRA_PLATFORM_API_URL || "https://platform.mastra.ai";
|
|
390
|
+
/**
|
|
391
|
+
* Derive the gateway URL from the platform URL when not explicitly set.
|
|
392
|
+
* If the platform points at staging, the gateway defaults to staging too.
|
|
393
|
+
*/
|
|
394
|
+
function deriveGatewayUrl() {
|
|
395
|
+
if (process.env.MASTRA_GATEWAY_URL) return process.env.MASTRA_GATEWAY_URL;
|
|
396
|
+
if (MASTRA_PLATFORM_API_URL.includes("staging")) return "https://gateway-api.staging.mastra.ai/v1";
|
|
397
|
+
return "https://gateway-api.mastra.ai/v1";
|
|
398
|
+
}
|
|
399
|
+
deriveGatewayUrl();
|
|
400
|
+
/**
|
|
401
|
+
* Derive the studio URL from the platform URL when not explicitly set.
|
|
402
|
+
* If the platform points at staging, the studio defaults to staging too.
|
|
403
|
+
*/
|
|
404
|
+
function deriveStudioUrl() {
|
|
405
|
+
if (process.env.MASTRA_STUDIO_URL) return process.env.MASTRA_STUDIO_URL;
|
|
406
|
+
if (MASTRA_PLATFORM_API_URL.includes("staging")) return "https://studio.staging.mastra.ai";
|
|
407
|
+
return "https://studio.mastra.ai";
|
|
408
|
+
}
|
|
409
|
+
deriveStudioUrl();
|
|
410
|
+
const SESSION_EXPIRED_MESSAGE = "Session expired. Run: mastra auth login";
|
|
411
|
+
/**
|
|
412
|
+
* Throw a standardized error for API failures.
|
|
413
|
+
* - 401: "Session expired" (authentication failed)
|
|
414
|
+
* - Other: Show the server's error detail or fall back to status code
|
|
415
|
+
*/
|
|
416
|
+
function throwApiError(message, status, detail) {
|
|
417
|
+
if (status === 401) throw new Error(SESSION_EXPIRED_MESSAGE);
|
|
418
|
+
if (detail) throw new Error(detail);
|
|
419
|
+
throw new Error(`${message}: ${status}`);
|
|
420
|
+
}
|
|
421
|
+
let _currentToken = null;
|
|
422
|
+
let _refreshInFlight = null;
|
|
423
|
+
/**
|
|
424
|
+
* Set the current token/orgId used by authenticated fetch.
|
|
425
|
+
* Call this after login or getToken().
|
|
426
|
+
*/
|
|
427
|
+
function setCurrentAuth(token, orgId) {
|
|
428
|
+
_currentToken = token;
|
|
429
|
+
if (orgId !== void 0);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* A fetch wrapper that auto-refreshes the token on 401 and retries once.
|
|
433
|
+
* Used by both createApiClient (openapi-fetch) and raw fetch calls.
|
|
434
|
+
*/
|
|
435
|
+
async function authenticatedFetch(input, init) {
|
|
436
|
+
const clonedRequest = input instanceof Request ? input.clone() : null;
|
|
437
|
+
if (!_currentToken) {
|
|
438
|
+
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);
|
|
439
|
+
if (authHeader?.startsWith("Bearer ")) _currentToken = authHeader.slice(7);
|
|
440
|
+
}
|
|
441
|
+
const response = await fetch(input, init);
|
|
442
|
+
if (response.status !== 401 || !_currentToken) return response;
|
|
443
|
+
if (!_refreshInFlight) _refreshInFlight = (async () => {
|
|
444
|
+
try {
|
|
445
|
+
const { tryRefreshToken, loadCredentials } = await import("./credentials-B4M5VuZS-BRW9qqvO.js");
|
|
446
|
+
const creds = await loadCredentials();
|
|
447
|
+
if (!creds) throw new Error("No credentials");
|
|
448
|
+
const newToken = await tryRefreshToken(creds);
|
|
449
|
+
if (!newToken) throw new Error("Refresh failed");
|
|
450
|
+
_currentToken = newToken;
|
|
451
|
+
return newToken;
|
|
452
|
+
} finally {
|
|
453
|
+
_refreshInFlight = null;
|
|
454
|
+
}
|
|
455
|
+
})();
|
|
456
|
+
let newToken;
|
|
457
|
+
try {
|
|
458
|
+
newToken = await _refreshInFlight;
|
|
459
|
+
} catch {
|
|
460
|
+
return response;
|
|
461
|
+
}
|
|
462
|
+
if (clonedRequest) {
|
|
463
|
+
const retryHeaders = new Headers(clonedRequest.headers);
|
|
464
|
+
retryHeaders.set("Authorization", `Bearer ${newToken}`);
|
|
465
|
+
return fetch(new Request(clonedRequest, { headers: retryHeaders }));
|
|
466
|
+
}
|
|
467
|
+
const retryHeaders = new Headers(init?.headers);
|
|
468
|
+
retryHeaders.set("Authorization", `Bearer ${newToken}`);
|
|
469
|
+
return fetch(input, {
|
|
470
|
+
...init,
|
|
471
|
+
headers: retryHeaders
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Create a typed API client with Bearer token + org ID headers.
|
|
476
|
+
* Uses authenticatedFetch for automatic 401 retry.
|
|
477
|
+
*/
|
|
478
|
+
function createApiClient(token, orgId) {
|
|
479
|
+
setCurrentAuth(token, orgId);
|
|
480
|
+
const headers = { Authorization: `Bearer ${token}` };
|
|
481
|
+
if (orgId) headers["x-organization-id"] = orgId;
|
|
482
|
+
return createClient({
|
|
483
|
+
baseUrl: MASTRA_PLATFORM_API_URL,
|
|
484
|
+
headers,
|
|
485
|
+
fetch: authenticatedFetch
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Build auth headers for raw fetch calls (zip upload, SSE streaming, tokens).
|
|
490
|
+
*/
|
|
491
|
+
function authHeaders(token, orgId) {
|
|
492
|
+
const headers = { Authorization: `Bearer ${token}` };
|
|
493
|
+
if (orgId) headers["x-organization-id"] = orgId;
|
|
494
|
+
return headers;
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Make an authenticated fetch call that auto-refreshes on 401.
|
|
498
|
+
* Use this instead of raw fetch() for platform API calls.
|
|
499
|
+
*/
|
|
500
|
+
function platformFetch(input, init) {
|
|
501
|
+
return authenticatedFetch(input, init);
|
|
502
|
+
}
|
|
503
|
+
//#endregion
|
|
504
|
+
export { throwApiError as a, platformFetch as i, authHeaders as n, createApiClient as r, MASTRA_PLATFORM_API_URL as t };
|