# Mocks API

Every export of kitr/mocks, with its signature.

Every export of `kitr/mocks`, read from the published type definitions. The [guide](/packages/mocks) shows them in use.

## kitr/mocks

Re-exports `msw` unchanged, plus the following.

### kitrErrorResponse

```ts
function kitrErrorResponse(error: unknown): HttpResponse<import("msw").JsonBodyType>;
```

Turns anything thrown into the JSON response a kitr server sends for it: a `KitrError` keeps its
code and status, anything else becomes `INTERNAL` with status 500.

### mockRoutes

```ts
function mockRoutes(table: RouteDescriptor[], baseUrl: string, resolvers: Record<string, Resolver | unknown>): RequestHandler[];
```

MSW handlers from a kitr route table (`routes.toJSON()`) or an OpenAPI-ish list.
Unhandled routes get a 501 so a missing mock is loud, not silent.

```ts
const handlers = mockRoutes(routeTable, 'http://localhost:3000', {
  'projects.index': () => [{ id: '1', name: 'Alpha' }],
  'projects.show': ({ params }) => ({ id: params.id, name: 'Alpha' }),
})
```

### mockValidationError

```ts
function mockValidationError(method: 'post' | 'put' | 'patch', url: string, fieldErrors: Record<string, string[]>): import("msw").HttpHandler;
```

A handler that throws a kitr validation error, to test form error mapping.

### setupBrowserMocks

```ts
function setupBrowserMocks(handlers: RequestHandler[], options?: {
  quiet?: boolean;
}): Promise<import("msw/browser").SetupWorker>;
```

Starts MSW's service worker in the browser for development: `await setupBrowserMocks(handlers)`.
Needs `mockServiceWorker.js` in the public folder. Pass `quiet: false` to see MSW's console messages.

### setupNodeMocks

```ts
function setupNodeMocks(handlers: RequestHandler[]): Promise<import("msw/node").SetupServer>;
```

Starts MSW in Node for tests: `const mocks = await setupNodeMocks(handlers)`. Requests no handler
matches go to the real network. Returns MSW's server, so `use`, `resetHandlers` and `close` work.

### RouteDescriptor

```ts
interface RouteDescriptor {
  /** The route name that `mockRoutes` resolvers are keyed by, such as `orders.show`. */
  name: string;
  /** The HTTP method, in any case: `GET`, `post`. */
  method: string;
  /** The path pattern with `:params`, such as `/api/orders/:id`. */
  path: string;
}
```

One route of a kitr route table, as `routes.toJSON()` and `kitr api:sync` describe it.

<!-- GENERATED by scripts/api-docs.mjs from the type definitions. Do not edit. -->
