UNPKG

@mastra/core

Version:
103 lines (99 loc) 3.43 kB
export { CompositeAuth, MastraAuthProvider, SimpleAuth } from '../chunk-PETV6YNX.js'; import { MastraBase } from '../chunk-77VL4DNS.js'; import { RegisteredLogger } from '../chunk-O274XICP.js'; import { MastraError, ErrorCategory, ErrorDomain } from '../chunk-M7RBQNFP.js'; // 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; } // src/server/base.ts 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; } }; // 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; } export { MastraServerBase, defineAuth, getRequestHeader, getWebRequest, registerApiRoute }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map