-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move getAction to separate file for bundling
- Loading branch information
1 parent
392e383
commit ef2b409
Showing
4 changed files
with
30 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |