MCPcopy
hub / github.com/PostHog/posthog / mocksToHandlers

Function mocksToHandlers

frontend/src/mocks/utils.ts:12–33  ·  view source on GitHub ↗
(mocks: Mocks)

Source from the content-addressed store, hash-verified

10export type Mocks = Partial<Record<keyof typeof rest, Record<string, MockSignature>>>
11
12export const mocksToHandlers = (mocks: Mocks): ReturnType<typeof rest['get']>[] => {
13 const response: ReturnType<typeof rest['get']>[] = []
14 Object.entries(mocks).map(([method, mockHandlers]) => {
15 Object.entries(mockHandlers).map(([path, handler]) => {
16 const pathWithoutTrailingSlash = path.replace(/\/$/, '')
17 response.push(
18 (rest[method] as typeof rest['get'])(pathWithoutTrailingSlash, async (req, res, ctx) => {
19 if (typeof handler === 'function') {
20 const responseArray = await handler(req, res, ctx)
21 if (responseArray.length === 2 && typeof responseArray[0] === 'number') {
22 return res(ctx.status(responseArray[0]), ctx.json(responseArray[1] ?? null))
23 }
24 return res(...responseArray)
25 } else {
26 return res(ctx.json(handler ?? null))
27 }
28 })
29 )
30 })
31 })
32 return response
33}

Callers 3

useStorybookMocksFunction · 0.90
useMocksFunction · 0.90
handlers.tsFile · 0.90

Calls 2

jsonMethod · 0.80
handlerFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…