Last active
August 30, 2024 20:54
-
-
Save dadamssg/28d30173b528489328b2048288426496 to your computer and use it in GitHub Desktop.
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
// 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