@mastra/core
Version:
115 lines (114 loc) • 4.44 kB
JavaScript
import { a as RegisteredLogger } from "../logger-B_aQzjbm.js";
import { t as MastraBase } from "../base-BeUQ6mLP.js";
import { i as MastraError, n as ErrorDomain, t as ErrorCategory } from "../error-MjDSls8S.js";
import { a as isAuthHttpHandler, c as isSSOProvider, i as hasAuthInit, l as isSessionProvider, n as MastraAuthProvider, o as isCredentialsProvider, r as SimpleAuth, s as isOrganizationsProvider, t as CompositeAuth, u as isUserProvider } from "../provider-SdHMYtrR.js";
//#region src/server/request-types.ts
function getRequestHeader(request, name) {
if (request instanceof Request) return request.headers.get(name);
return request.raw?.headers.get(name) ?? request.headers?.get(name) ?? request.header(name) ?? null;
}
function getWebRequest(request) {
if (request instanceof Request) return request;
return request.raw instanceof Request ? request.raw : void 0;
}
//#endregion
//#region src/server/base.ts
/**
* Base class for server adapters that provides app storage and retrieval.
*
* This class extends MastraBase to get logging capabilities and provides
* a framework-agnostic way to store and retrieve the server app instance
* (e.g., Hono, Express).
*
* Server adapters (like MastraServer from @mastra/hono or @mastra/express) extend this
* base class to inherit the app storage functionality while adding their
* framework-specific route registration and middleware handling.
*
* @template TApp - The type of the server app (e.g., Hono, Express Application)
*
* @example
* ```typescript
* // After server creation, the app is accessible via Mastra
* const app = mastra.getServerApp<Hono>();
* const response = await app.fetch(new Request('http://localhost/health'));
* ```
*/
var MastraServerBase = class extends MastraBase {
#app;
constructor({ app, name }) {
super({
component: RegisteredLogger.SERVER,
name: name ?? "Server"
});
this.#app = app;
}
/**
* Get the app instance.
*
* Returns the server app that was passed to the constructor. This allows users
* to access the underlying server framework's app for direct operations
* like calling routes via app.fetch() (Hono) or using the app for testing.
*
* @template T - The expected type of the app (defaults to TApp)
* @returns The app instance cast to T. Callers are responsible for ensuring T matches the actual app type.
*
* @example
* ```typescript
* const app = adapter.getApp<Hono>();
* const response = await app.fetch(new Request('http://localhost/api/agents'));
* ```
*/
getApp() {
return this.#app;
}
/**
* Protected getter for subclasses to access the app.
* This allows subclasses to use `this.app` naturally.
*/
get app() {
return this.#app;
}
};
//#endregion
//#region src/server/index.ts
function validateOptions(path, options) {
if (options.method === void 0) throw new MastraError({
id: "MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS",
text: `Invalid options for route "${path}", missing "method" property`,
domain: ErrorDomain.MASTRA_SERVER,
category: ErrorCategory.USER
});
if (options.handler === void 0 && options.createHandler === void 0) throw new MastraError({
id: "MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS",
text: `Invalid options for route "${path}", you must define a "handler" or "createHandler" property`,
domain: ErrorDomain.MASTRA_SERVER,
category: ErrorCategory.USER
});
if (options.handler !== void 0 && options.createHandler !== void 0) throw new MastraError({
id: "MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS",
text: `Invalid options for route "${path}", you can only define one of the following properties: "handler" or "createHandler"`,
domain: ErrorDomain.MASTRA_SERVER,
category: ErrorCategory.USER
});
}
function registerApiRoute(path, options) {
validateOptions(path, options);
return {
path,
method: options.method,
handler: options.handler,
createHandler: options.createHandler,
openapi: options.openapi,
middleware: options.middleware,
cors: options.cors,
requiresAuth: options.requiresAuth,
requiresPermission: options.requiresPermission,
fga: options.fga
};
}
function defineAuth(config) {
return config;
}
//#endregion
export { CompositeAuth, MastraAuthProvider, MastraServerBase, SimpleAuth, defineAuth, getRequestHeader, getWebRequest, hasAuthInit, isAuthHttpHandler, isCredentialsProvider, isOrganizationsProvider, isSSOProvider, isSessionProvider, isUserProvider, registerApiRoute };
//# sourceMappingURL=index.js.map