diff --git a/.changeset/six-icons-pump.md b/.changeset/six-icons-pump.md new file mode 100644 index 0000000000..c3246c3443 --- /dev/null +++ b/.changeset/six-icons-pump.md @@ -0,0 +1,24 @@ +--- +"astro": patch +--- + +Deprecate the `getApiContext()` function. API Context can now be accessed from the second parameter to your Action `handler()`: + +```diff +// src/actions/index.ts +import { + defineAction, + z, +- getApiContext, +} from 'astro:actions'; + +export const server = { + login: defineAction({ + input: z.object({ id: z.string }), ++ handler(input, context) { + const user = context.locals.auth(input.id); + return user; + } + }), +} +``` diff --git a/packages/astro/src/actions/runtime/virtual/server.ts b/packages/astro/src/actions/runtime/virtual/server.ts index 087b00cbb3..be870b957c 100644 --- a/packages/astro/src/actions/runtime/virtual/server.ts +++ b/packages/astro/src/actions/runtime/virtual/server.ts @@ -1,5 +1,5 @@ import { z } from 'zod'; -import { getApiContext } from '../store.js'; +import { getApiContext as _getApiContext, type ActionAPIContext } from '../store.js'; import { type MaybePromise, hasContentType } from '../utils.js'; import { ActionError, @@ -13,7 +13,8 @@ export * from './shared.js'; export { z } from 'zod'; -export { getApiContext } from '../store.js'; +/** @deprecated Access context from the second `handler()` parameter. */ +export const getApiContext = _getApiContext; export type Accept = 'form' | 'json'; export type InputSchema = T extends 'form' @@ -21,8 +22,8 @@ export type InputSchema = T extends 'form' : z.ZodType; type Handler = TInputSchema extends z.ZodType - ? (input: z.infer) => MaybePromise - : (input?: any) => MaybePromise; + ? (input: z.infer, context: ActionAPIContext) => MaybePromise + : (input: any, context: ActionAPIContext) => MaybePromise; export type ActionClient< TOutput, @@ -88,13 +89,13 @@ function getFormServerHandler> }); } - if (!(inputSchema instanceof z.ZodObject)) return await handler(unparsedInput); + if (!(inputSchema instanceof z.ZodObject)) return await handler(unparsedInput, getApiContext()); const parsed = await inputSchema.safeParseAsync(formDataToObject(unparsedInput, inputSchema)); if (!parsed.success) { throw new ActionInputError(parsed.error.issues); } - return await handler(parsed.data); + return await handler(parsed.data, getApiContext()); }; } @@ -112,12 +113,12 @@ function getJsonServerHandler> }); } - if (!inputSchema) return await handler(unparsedInput); + if (!inputSchema) return await handler(unparsedInput, getApiContext()); const parsed = await inputSchema.safeParseAsync(unparsedInput); if (!parsed.success) { throw new ActionInputError(parsed.error.issues); } - return await handler(parsed.data); + return await handler(parsed.data, getApiContext()); }; } diff --git a/packages/astro/test/fixtures/actions/src/actions/index.ts b/packages/astro/test/fixtures/actions/src/actions/index.ts index c196c1ae3e..7429006cdb 100644 --- a/packages/astro/test/fixtures/actions/src/actions/index.ts +++ b/packages/astro/test/fixtures/actions/src/actions/index.ts @@ -1,4 +1,4 @@ -import { defineAction, getApiContext, ActionError, z } from 'astro:actions'; +import { defineAction, ActionError, z } from 'astro:actions'; export const server = { subscribe: defineAction({ @@ -31,15 +31,13 @@ export const server = { }), getUser: defineAction({ accept: 'form', - handler: async () => { - const { locals } = getApiContext(); + handler: async (_, { locals }) => { return locals.user; } }), getUserOrThrow: defineAction({ accept: 'form', - handler: async () => { - const { locals } = getApiContext(); + handler: async (_, { locals }) => { if (locals.user?.name !== 'admin') { // Expected to throw throw new ActionError({