Skip to content

Commit

Permalink
refactor: move getAction to separate file for bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 1, 2024
1 parent 392e383 commit ef2b409
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/astro/src/actions/runtime/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
} from '../../core/errors/errors-data.js';
import { AstroError } from '../../core/errors/errors.js';
import { defineMiddleware } from '../../core/middleware/index.js';
import { formContentTypes, getAction, hasContentType } from './utils.js';
import { formContentTypes, hasContentType } from './utils.js';
import {
type SafeResult,
type SerializedActionResult,
serializeActionResult,
} from './virtual/shared.js';
import { getAction } from './virtual/get-action.js';

export type Locals = {
_actionsInternal: {
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/actions/runtime/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { APIRoute } from '../../@types/astro.js';
import { formContentTypes, getAction, hasContentType } from './utils.js';
import { formContentTypes, hasContentType } from './utils.js';
import { getAction } from './virtual/get-action.js';
import { serializeActionResult } from './virtual/shared.js';

export const POST: APIRoute = async (context) => {
Expand Down
26 changes: 0 additions & 26 deletions packages/astro/src/actions/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { ZodType } from 'zod';
import type { APIContext } from '../../@types/astro.js';
import type { ActionAccept, ActionClient } from './virtual/server.js';

export const formContentTypes = ['application/x-www-form-urlencoded', 'multipart/form-data'];

Expand All @@ -15,30 +13,6 @@ export function hasContentType(contentType: string, expected: string[]) {
export type ActionAPIContext = Omit<APIContext, 'getActionResult' | 'callAction' | 'props'>;
export type MaybePromise<T> = T | Promise<T>;

/**
* Get server-side action based on the route path.
* Imports from the virtual module `astro:internal-actions`, which maps to
* the user's `src/actions/index.ts` file at build-time.
*/
export async function getAction(
path: string
): Promise<ActionClient<unknown, ActionAccept, ZodType> | undefined> {
const pathKeys = path.replace('/_actions/', '').split('.');
// @ts-expect-error virtual module
let { server: actionLookup } = await import('astro:internal-actions');

for (const key of pathKeys) {
if (!(key in actionLookup)) {
return undefined;
}
actionLookup = actionLookup[key];
}
if (typeof actionLookup !== 'function') {
return undefined;
}
return actionLookup;
}

/**
* Used to preserve the input schema type in the error object.
* This allows for type inference on the `fields` property
Expand Down
26 changes: 26 additions & 0 deletions packages/astro/src/actions/runtime/virtual/get-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ZodType } from 'zod';
import type { ActionAccept, ActionClient } from './server.js';

/**
* Get server-side action based on the route path.
* Imports from the virtual module `astro:internal-actions`, which maps to
* the user's `src/actions/index.ts` file at build-time.
*/
export async function getAction(
path: string
): Promise<ActionClient<unknown, ActionAccept, ZodType> | undefined> {
const pathKeys = path.replace('/_actions/', '').split('.');
// @ts-expect-error virtual module
let { server: actionLookup } = await import('astro:internal-actions');

for (const key of pathKeys) {
if (!(key in actionLookup)) {
return undefined;
}
actionLookup = actionLookup[key];
}
if (typeof actionLookup !== 'function') {
return undefined;
}
return actionLookup;
}

0 comments on commit ef2b409

Please sign in to comment.