Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Last active August 30, 2024 20:54
Show Gist options
  • Save dadamssg/28d30173b528489328b2048288426496 to your computer and use it in GitHub Desktop.
Save dadamssg/28d30173b528489328b2048288426496 to your computer and use it in GitHub Desktop.
// app/routes/app/playwright.ts
import type { ActionFunctionArgs } from '@remix-run/node'
import { server } from '~/mocks/node'
import { z } from 'zod'
import { http, HttpResponse } from 'msw'
export async function action({ request }: ActionFunctionArgs) {
if (process.env.NODE_ENV === 'production') {
throw new Error('No mocking in prod, silly.')
}
const data = await request.json()
const schema = z.object({
method: z.enum(['get', 'post', 'put', 'delete']),
url: z.string(),
response: z.any(),
once: z.boolean().optional(),
})
const reqData = schema.parse(data)
server.use(
http[reqData.method](
reqData.url,
() => {
return HttpResponse.json(reqData.response)
},
{ once: reqData.once },
),
)
return {
success: true,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment