@mastra/core
Version:
1 lines • 2.87 kB
Source Map (JSON)
{"version":3,"file":"dynamic-argument-CUc1BBOS.cjs","names":["MASTRA_NORMALIZED_USER_KEY","callback"],"sources":["../src/utils/dynamic-argument.ts"],"sourcesContent":["import type { Mastra } from '../mastra';\nimport { MASTRA_NORMALIZED_USER_KEY } from '../request-context';\nimport type { RequestContext } from '../request-context';\nimport type { DynamicArgument, MastraUser } from '../types';\n\n/**\n * Read the authenticated end user from the request context.\n *\n * The server auth middleware stores the normalized user under\n * `MASTRA_NORMALIZED_USER_KEY` after an auth provider authenticates the\n * request. Returns `undefined` when the request is unauthenticated, no auth\n * provider is configured, or the stored value is malformed.\n */\nexport function getRequestUser(requestContext?: RequestContext<any>): MastraUser | undefined {\n const user = requestContext?.getRaw(MASTRA_NORMALIZED_USER_KEY);\n if (user && typeof user === 'object' && typeof (user as MastraUser).id === 'string') {\n return user as MastraUser;\n }\n return undefined;\n}\n\n/**\n * Arguments passed to a `DynamicArgument` callback when it is resolved.\n */\ninterface DynamicArgumentParams {\n requestContext: RequestContext<any>;\n mastra?: Mastra;\n user?: MastraUser;\n}\n\n/**\n * Resolve a `DynamicArgument<T>` to its value.\n *\n * Static values pass through unchanged. Callbacks are invoked with\n * `{ requestContext, mastra, user }`, where `user` is the authenticated end\n * user derived from the request context at invocation time (see\n * {@link getRequestUser}).\n */\n// The function-only overload comes first so that call sites which have\n// already narrowed the value to a callback infer `T` from the callback's\n// return type rather than treating the whole function as the static value.\nexport function resolveDynamicArgument<T>(\n value: (args: DynamicArgumentParams) => Promise<T> | T,\n args: { requestContext: RequestContext<any>; mastra?: Mastra },\n): Promise<T> | T;\nexport function resolveDynamicArgument<T>(\n value: DynamicArgument<T, any>,\n args: { requestContext: RequestContext<any>; mastra?: Mastra },\n): Promise<T> | T;\nexport function resolveDynamicArgument<T>(\n value: DynamicArgument<T, any>,\n { requestContext, mastra }: { requestContext: RequestContext<any>; mastra?: Mastra },\n): Promise<T> | T {\n if (typeof value !== 'function') {\n return value;\n }\n const callback = value as (args: DynamicArgumentParams) => Promise<T> | T;\n return callback({ requestContext, mastra, user: getRequestUser(requestContext) });\n}\n"],"mappings":";;;;;;;;;;AAaA,SAAgB,eAAe,gBAA8D;CAC3F,MAAM,OAAO,gBAAgB,OAAOA,wBAAAA,0BAA0B;CAC9D,IAAI,QAAQ,OAAO,SAAS,YAAY,OAAQ,KAAoB,OAAO,UACzE,OAAO;AAGX;AA8BA,SAAgB,uBACd,OACA,EAAE,gBAAgB,UACF;CAChB,IAAI,OAAO,UAAU,YACnB,OAAO;CAGT,OAAOC,MAAS;EAAE;EAAgB;EAAQ,MAAM,eAAe,cAAc;CAAE,CAAC;AAClF"}