(mocks: Mocks)
| 10 | export type Mocks = Partial<Record<keyof typeof rest, Record<string, MockSignature>>> |
| 11 | |
| 12 | export 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 | } |
no test coverage detected
searching dependent graphs…