javascript / expert
Snippet
Composable Server Action Wrappers for Global State Audit
Apply higher-order functions to Server Actions to implement cross-cutting concerns like telemetry, logging, or authorization. This keeps the core business logic clean while ensuring consistent monitoring across all mutations.
snippet.js
1
2
3
4
5
6
7
8
9
10
const withAudit = (action) => async (formData) => {const startTime = Date.now();const result = await action(formData);console.log(`Action executed in ${Date.now() - startTime}ms`);return result;};export const updateProfile = withAudit(async (data) => {// Core logic here});
nextjs
Breakdown
1
withAudit = (action) => async (formData)
A higher-order function that returns a new decorated async function.
2
await action(formData)
Executes the original server action logic within the wrapper.
3
startTime = Date.now()
Captures the timestamp to measure execution performance.