@mastra/core
Version:
130 lines (129 loc) • 4.96 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_logger = require("../logger-BPclhj7J.cjs");
const require_base = require("../base-B6soWsYg.cjs");
const require_error = require("../error-B-e62x-A.cjs");
const require_provider = require("../provider-BT2YASPQ.cjs");
//#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 require_base.MastraBase {
#app;
constructor({ app, name }) {
super({
component: require_logger.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 require_error.MastraError({
id: "MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS",
text: `Invalid options for route "${path}", missing "method" property`,
domain: require_error.ErrorDomain.MASTRA_SERVER,
category: require_error.ErrorCategory.USER
});
if (options.handler === void 0 && options.createHandler === void 0) throw new require_error.MastraError({
id: "MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS",
text: `Invalid options for route "${path}", you must define a "handler" or "createHandler" property`,
domain: require_error.ErrorDomain.MASTRA_SERVER,
category: require_error.ErrorCategory.USER
});
if (options.handler !== void 0 && options.createHandler !== void 0) throw new require_error.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: require_error.ErrorDomain.MASTRA_SERVER,
category: require_error.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
exports.CompositeAuth = require_provider.CompositeAuth;
exports.MastraAuthProvider = require_provider.MastraAuthProvider;
exports.MastraServerBase = MastraServerBase;
exports.SimpleAuth = require_provider.SimpleAuth;
exports.defineAuth = defineAuth;
exports.getRequestHeader = getRequestHeader;
exports.getWebRequest = getWebRequest;
exports.hasAuthInit = require_provider.hasAuthInit;
exports.isAuthHttpHandler = require_provider.isAuthHttpHandler;
exports.isCredentialsProvider = require_provider.isCredentialsProvider;
exports.isOrganizationsProvider = require_provider.isOrganizationsProvider;
exports.isSSOProvider = require_provider.isSSOProvider;
exports.isSessionProvider = require_provider.isSessionProvider;
exports.isUserProvider = require_provider.isUserProvider;
exports.registerApiRoute = registerApiRoute;
//# sourceMappingURL=index.cjs.map