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:
parent
b15949f8fa
commit
29a8650375
3 changed files with 36 additions and 13 deletions
24
.changeset/six-icons-pump.md
Normal file
24
.changeset/six-icons-pump.md
Normal 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;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
```
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
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 { type MaybePromise, hasContentType } from '../utils.js';
|
||||||
import {
|
import {
|
||||||
ActionError,
|
ActionError,
|
||||||
|
@ -13,7 +13,8 @@ export * from './shared.js';
|
||||||
|
|
||||||
export { z } from 'zod';
|
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 Accept = 'form' | 'json';
|
||||||
export type InputSchema<T extends Accept> = T extends 'form'
|
export type InputSchema<T extends Accept> = T extends 'form'
|
||||||
|
@ -21,8 +22,8 @@ export type InputSchema<T extends Accept> = T extends 'form'
|
||||||
: z.ZodType;
|
: z.ZodType;
|
||||||
|
|
||||||
type Handler<TInputSchema, TOutput> = TInputSchema extends z.ZodType
|
type Handler<TInputSchema, TOutput> = TInputSchema extends z.ZodType
|
||||||
? (input: z.infer<TInputSchema>) => MaybePromise<TOutput>
|
? (input: z.infer<TInputSchema>, context: ActionAPIContext) => MaybePromise<TOutput>
|
||||||
: (input?: any) => MaybePromise<TOutput>;
|
: (input: any, context: ActionAPIContext) => MaybePromise<TOutput>;
|
||||||
|
|
||||||
export type ActionClient<
|
export type ActionClient<
|
||||||
TOutput,
|
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));
|
const parsed = await inputSchema.safeParseAsync(formDataToObject(unparsedInput, inputSchema));
|
||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
throw new ActionInputError(parsed.error.issues);
|
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);
|
const parsed = await inputSchema.safeParseAsync(unparsedInput);
|
||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
throw new ActionInputError(parsed.error.issues);
|
throw new ActionInputError(parsed.error.issues);
|
||||||
}
|
}
|
||||||
return await handler(parsed.data);
|
return await handler(parsed.data, getApiContext());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { defineAction, getApiContext, ActionError, z } from 'astro:actions';
|
import { defineAction, ActionError, z } from 'astro:actions';
|
||||||
|
|
||||||
export const server = {
|
export const server = {
|
||||||
subscribe: defineAction({
|
subscribe: defineAction({
|
||||||
|
@ -31,15 +31,13 @@ export const server = {
|
||||||
}),
|
}),
|
||||||
getUser: defineAction({
|
getUser: defineAction({
|
||||||
accept: 'form',
|
accept: 'form',
|
||||||
handler: async () => {
|
handler: async (_, { locals }) => {
|
||||||
const { locals } = getApiContext();
|
|
||||||
return locals.user;
|
return locals.user;
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
getUserOrThrow: defineAction({
|
getUserOrThrow: defineAction({
|
||||||
accept: 'form',
|
accept: 'form',
|
||||||
handler: async () => {
|
handler: async (_, { locals }) => {
|
||||||
const { locals } = getApiContext();
|
|
||||||
if (locals.user?.name !== 'admin') {
|
if (locals.user?.name !== 'admin') {
|
||||||
// Expected to throw
|
// Expected to throw
|
||||||
throw new ActionError({
|
throw new ActionError({
|
||||||
|
|
Loading…
Reference in a new issue