0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Actions: restore api context param (#11112)

* feat: expose APIContext from the second handler param

* refactor: use second param from test

* chore: changeset

* edit: minor -> patch

* edit: apiContext -> context

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

* refactor: apiContext -> context

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

* refactor: apiContext -> context

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
This commit is contained in:
Ben Holmes 2024-05-22 08:06:58 -04:00 committed by GitHub
parent b15949f8fa
commit 29a8650375
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 13 deletions

View file

@ -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;
}
}),
}
```

View file

@ -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 Accept> = T extends 'form'
@ -21,8 +22,8 @@ export type InputSchema<T extends Accept> = T extends 'form'
: z.ZodType;
type Handler<TInputSchema, TOutput> = TInputSchema extends z.ZodType
? (input: z.infer<TInputSchema>) => MaybePromise<TOutput>
: (input?: any) => MaybePromise<TOutput>;
? (input: z.infer<TInputSchema>, context: ActionAPIContext) => MaybePromise<TOutput>
: (input: any, context: ActionAPIContext) => MaybePromise<TOutput>;
export type ActionClient<
TOutput,
@ -88,13 +89,13 @@ function getFormServerHandler<TOutput, TInputSchema extends InputSchema<'form'>>
});
}
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<TOutput, TInputSchema extends InputSchema<'json'>>
});
}
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());
};
}

View file

@ -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({