# openapi-msw

## 2.0.0

### Major Changes

- [#90](https://github.com/christoph-fricke/openapi-msw/pull/90) [`a7a4b25`](https://github.com/christoph-fricke/openapi-msw/commit/a7a4b2567018ba1e5765f7cf1c696aa5fe1c091a) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Removed CommonJS compilation and exports. OpenAPI-MSW now publishes ESM only. All actively maintained Node.js versions support requiring ESM, so `const { createOpenApiHttp } = require("openapi-msw");` will still work.

- [#94](https://github.com/christoph-fricke/openapi-msw/pull/94) [`72fac10`](https://github.com/christoph-fricke/openapi-msw/commit/72fac10de727884901daebf4b01a14b10676c47f) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Updated MSW peer dependency from v2.7.0 to v2.10.5. This is only a breaking change if you are not already using the latest version of MSW.

### Minor Changes

- [#91](https://github.com/christoph-fricke/openapi-msw/pull/91) [`905b985`](https://github.com/christoph-fricke/openapi-msw/commit/905b98506e7c254b2ab84ca1e64223c6a849a553) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added declaration maps to the build output. Together with packaged source files, this enables proper "Go To Definition" support in editors — jumping directly to the TypeScript source file instead of the declaration file.

## 1.3.0

### Minor Changes

- [#87](https://github.com/christoph-fricke/openapi-msw/pull/87) [`0a8a892`](https://github.com/christoph-fricke/openapi-msw/commit/0a8a89273adb808b2971aaf82f1f179162cd652f) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added a section to the README explaining how to use OpenAPI-MSW handlers with MSW, which provides clearer guidance for integration.

- [#88](https://github.com/christoph-fricke/openapi-msw/pull/88) [`e7fab35`](https://github.com/christoph-fricke/openapi-msw/commit/e7fab35911bc269d0b6dd7b178f1732a075e4d2b) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Changed the publishing flow to use [trusted publishing](https://docs.npmjs.com/trusted-publishers) via OIDC instead of a token-based approach.

- [#86](https://github.com/christoph-fricke/openapi-msw/pull/86) [`be6f731`](https://github.com/christoph-fricke/openapi-msw/commit/be6f73129429044186be996a2e759098950dc92d) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Updated TSDoc comments for `response` and `query` to show their code examples. Previously, VS Code did not display their `@example` blocks.

## 1.2.0

### Minor Changes

- [#78](https://github.com/christoph-fricke/openapi-msw/pull/78) [`482c028`](https://github.com/christoph-fricke/openapi-msw/commit/482c0282805a44013ada2ab95a08f75ae0bba479) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added utility types for creating type-safe functionality around OpenAPI-MSW. Special thanks to [@DrewHoo](https://github.com/DrewHoo) for suggesting and inspiring this change.

  ```typescript
  import {
    createOpenApiHttp,
    type PathsFor,
    type RequestBodyFor,
    type ResponseBodyFor,
  } from "openapi-msw";

  const http = createOpenApiHttp<paths>();

  // A union of all possible GET paths.
  type Paths = PathsFor<typeof http.get>;

  // The request body for POST /tasks.
  type RequestBody = RequestBodyFor<typeof http.post, "/tasks">;

  // The response body for GET /tasks.
  type ResponseBody = ResponseBodyFor<typeof http.get, "/tasks">;
  ```

## 1.1.0

### Minor Changes

- [#73](https://github.com/christoph-fricke/openapi-msw/pull/73) [`f81ae29`](https://github.com/christoph-fricke/openapi-msw/commit/f81ae2928233fb5d3dd22d2bb0d9123da8afc6ca) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added a `request.clone()` type override to continue returning type-safe `OpenApiRequest`s when called. With this, cloning the `request` in resolvers does not lose its type-safety on body parsing methods.

## 1.0.0

### Major Changes

- [#70](https://github.com/christoph-fricke/openapi-msw/pull/70) [`bc9a50f`](https://github.com/christoph-fricke/openapi-msw/commit/bc9a50ff869583a070a08be6e1de0868440adb48) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Updated MSW peer dependency from _v2.0.0_ to _v2.7.0_. This is only a breaking change if you are not already using the latest version of MSW.

- [#70](https://github.com/christoph-fricke/openapi-msw/pull/70) [`bc9a50f`](https://github.com/christoph-fricke/openapi-msw/commit/bc9a50ff869583a070a08be6e1de0868440adb48) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Renamed `HttpHandlerFactory` type to `OpenApiHttpRequestHandler`. This rename aligns its name with MSW's equivalent `HttpRequestHandler` type.

### Minor Changes

- [#68](https://github.com/christoph-fricke/openapi-msw/pull/68) [`33088be`](https://github.com/christoph-fricke/openapi-msw/commit/33088be804138e98647ffc0e9d85d71f2dfae6e8) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Removed dependency on _openapi-typescript-helpers_. We were depending on an older version without being able to easily update. With this refactoring, your projects should no longer resolve to multiple versions of _openapi-typescript-helpers_.

## 0.7.1

### Patch Changes

- [#63](https://github.com/christoph-fricke/openapi-msw/pull/63) [`b9f4bea`](https://github.com/christoph-fricke/openapi-msw/commit/b9f4bead7907eb7cd0d1c7458e6c89520c65414f) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Fixed type inference for extended JSON mime types, such as `application/problem+json`. Previously, APIs like `response(...).json` would be typed as `never` for such mime types. Now, they will be properly typed.

## 0.7.0

### Minor Changes

- [#58](https://github.com/christoph-fricke/openapi-msw/pull/58) [`f08acf1`](https://github.com/christoph-fricke/openapi-msw/commit/f08acf19a6e792ab36214bf8c1925447c2489704) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added "content-length" header for `response(...).empty()`. If no "content-length" header is provided in the response init, the "content-length" header is now set with the value "0". See #56 for more details.

## 0.6.1

### Patch Changes

- [#54](https://github.com/christoph-fricke/openapi-msw/pull/54) [`6793dcc`](https://github.com/christoph-fricke/openapi-msw/commit/6793dccff4641dedc266f8096ede373dc95fca8f) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Fixed type-exports for CommonJS refer to a non-existing file.

## 0.6.0

### Minor Changes

- [#50](https://github.com/christoph-fricke/openapi-msw/pull/50) [`37da681`](https://github.com/christoph-fricke/openapi-msw/commit/37da6814e65105cfc5c38067bdf32ba1c6208d8f) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added compilation and exports for CommonJS modules. This makes OpenAPI-MSW usable in projects that still use CommonJS as their module system.

- [#52](https://github.com/christoph-fricke/openapi-msw/pull/52) [`88ca9da`](https://github.com/christoph-fricke/openapi-msw/commit/88ca9da973ac0a9d25a3185e1cf05b88722c717d) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added enhanced typing for the `request` object. Now, `request.json()` and `request.text()` infer their return type from the given OpenAPI request-body content schema. Previously, only `request.json()` has been inferred without considering the content-type.

## 0.5.0

### Minor Changes

- [#41](https://github.com/christoph-fricke/openapi-msw/pull/41) [`fe70d20`](https://github.com/christoph-fricke/openapi-msw/commit/fe70d20494692df764188c35105cbf6be178d687) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added `response` helper to the resolver-info argument. It provides an granular type-safety when creating HTTP responses. Instead of being able to return any status code, `response` limits status codes, content types, and their response bodies to the combinations defined by the given OpenAPI spec.

  ```typescript
  /*
  Imagine this endpoint specification for the following example:
  
  /response-example:
    get:
      summary: Get Resource
      operationId: getResource
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Resource"
            text/plain:
              schema:
                type: string
                enum: ["Hello", "Goodbye"]
        204:
          description: NoContent
        "5XX":
          description: Error
          content:
            text/plain:
              schema:
                type: string
  */

  const handler = http.get("/response-example", ({ response }) => {
    // Error: Status Code 204 only allows empty responses
    const invalidRes = response(204).text("Hello");

    // Error: Status Code 200 only allows "Hello" as text
    const invalidRes = response(200).text("Some other string");

    // No Error: This combination is part of the defined OpenAPI spec
    const validRes = response(204).empty();

    // No Error: This combination is part of the defined OpenAPI spec
    const validRes = response(200).text("Hello");

    // Using a wildcard requires you to provide a matching status code for the response
    const validRes = response("5XX").text("Fatal Error", { status: 503 });
  });
  ```

## 0.4.0

### Minor Changes

- [#42](https://github.com/christoph-fricke/openapi-msw/pull/42) [`c466bbc`](https://github.com/christoph-fricke/openapi-msw/commit/c466bbcf4c27dea2e4c6928bf92369abf138fb47) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Changed response body types to be a union of all response bodies for all status codes and media types. This makes it possible to return responses for specified error codes without requiring a type cast. Imagine the following endpoint. Its response body is now typed as `StrictResponse<{ id: string, value: number } | string | null>`.

  ```yaml
  /resource:
    get:
      summary: Get Resource
      operationId: getResource
      responses:
        200:
          description: Success
          content:
            application/json:
              schema:
                type: object
                required: [id, value]
                properties:
                  id:
                    type: string
                  value:
                    type: integer
        202:
          description: Accepted
          content:
            text/plain:
              schema:
                type: string
        418:
          description: NoContent
  ```

### Patch Changes

- [#44](https://github.com/christoph-fricke/openapi-msw/pull/44) [`a9338b5`](https://github.com/christoph-fricke/openapi-msw/commit/a9338b5bcb289ceaab0e5538a4131995c10dd5f0) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Fixed endpoints with no specified query params allow any query key in the `query` helper methods. Now, providing any query key causes a type error.

## 0.3.0

### Minor Changes

- [#33](https://github.com/christoph-fricke/openapi-msw/pull/33) [`1f3958d`](https://github.com/christoph-fricke/openapi-msw/commit/1f3958dee1fce818b20c37bf486d6d73a0fcd1ea) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added `query` helper to resolver-info argument. It provides a type-safe wrapper around `URLSearchParams` for reading search parameters. As usual, the information about available parameters is inferred from your OpenAPI spec.

  ```typescript
  /*
  Imagine this endpoint specification for the following example:
  
  /query-example:
    get:
      summary: Query Example
      operationId: getQueryExample
      parameters:
        - name: filter
          in: query
          required: true
          schema:
            type: string
        - name: page
          in: query
          schema:
            type: number
        - name: sort
          in: query
          required: false
          schema:
            type: string
            enum: ["asc", "desc"]
        - name: sortBy
          in: query
          schema:
            type: array
            items:
              type: string
  */

  const handler = http.get("/query-example", ({ query }) => {
    const filter = query.get("filter"); // Typed as string
    const page = query.get("page"); // Typed as string | null since it is not required
    const sort = query.get("sort"); // Typed as "asc" | "desc" | null
    const sortBy = query.getAll("sortBy"); // Typed as string[]

    // Supported methods from URLSearchParams: get(), getAll(), has(), size
    if (query.has("sort", "asc")) {
      /* ... */
    }

    return HttpResponse.json({
      /* ... */
    });
  });
  ```

- [#35](https://github.com/christoph-fricke/openapi-msw/pull/35) [`07fa9b0`](https://github.com/christoph-fricke/openapi-msw/commit/07fa9b0822c441708c70d3e0698a6dbe7577f58c) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Restructured the library to add support for additional response resolver info. The enhanced `ResponseResolver` type and `ResponseResolverInfo` are available as exports.

## 0.2.2

### Patch Changes

- [#31](https://github.com/christoph-fricke/openapi-msw/pull/31) [`556dfca`](https://github.com/christoph-fricke/openapi-msw/commit/556dfca3a2c87eeec6f1f7acd2db63af52df2806) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Fixed a type mismatch between path fragment types and the values provided at runtime, which are always strings. Now all path-fragments are typed as string. If a fragment's schema is a string constrained by an enum, the resulting string literals are preserved. This fixes bug [#22](https://github.com/christoph-fricke/openapi-msw/issues/22).

  ```typescript
  const handler = http.get("/resource/{id}", ({ params }) => {
    // Previously calling "parseInt(...)" caused a type error
    // when the schema type for "id" is defined as number.
    const id = parseInt(params.id);

    return HttpResponse.json({ id });
  });
  ```

## 0.2.1

### Patch Changes

- [#27](https://github.com/christoph-fricke/openapi-msw/pull/27) [`232ae11`](https://github.com/christoph-fricke/openapi-msw/commit/232ae11b46bda40ec493b4eed6c270e4a9160a00) Thanks [@luchsamapparat](https://github.com/luchsamapparat)! - Fixed a compilation warning in projects using OpenAPI-MSW, which was caused by missing sources in source maps.

## 0.2.0

### Minor Changes

- [#24](https://github.com/christoph-fricke/openapi-msw/pull/24) [`bfd7a99`](https://github.com/christoph-fricke/openapi-msw/commit/bfd7a997c662c29bac8a91ea0952993c20dadee8) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added JSDoc comments to public API for improved DX.

### Patch Changes

- [#23](https://github.com/christoph-fricke/openapi-msw/pull/23) [`29ecb9c`](https://github.com/christoph-fricke/openapi-msw/commit/29ecb9cbccff09d042fe3e55552c906e22f6054c) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Fixed a small naming mistake in the "Getting Started" code example.

## 0.1.2

### Patch Changes

- [#17](https://github.com/christoph-fricke/openapi-msw/pull/17) [`2931f0c`](https://github.com/christoph-fricke/openapi-msw/commit/2931f0c37e5ca66378ec2a9596e07736b417a96b) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Fixed OpenAPI operations with no-content responses cannot return a response. Now they are required to return an empty response, i.e. `null` as response body.

  ```typescript
  const http = createOpenApiHttp<paths>();

  // Resolver function is required to return a `StrictResponse<null>` (empty response)
  // if the OpenAPI operation specifies `content?: never` for the response.
  const noContent = http.delete("/resource", ({ params }) => {
    return HttpResponse.json(null, { status: 204 });
  });
  ```

## 0.1.1

### Patch Changes

- [#12](https://github.com/christoph-fricke/openapi-msw/pull/12) [`96ce15c`](https://github.com/christoph-fricke/openapi-msw/commit/96ce15c5f81535fb1091143dab2dce671ba65836) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Add legacy entrypoint definitions (types, module) for tools and bundlers that do not understand package.json#exports fields.

## 0.1.0

### Minor Changes

- [#9](https://github.com/christoph-fricke/openapi-msw/pull/9) [`6364870`](https://github.com/christoph-fricke/openapi-msw/commit/636487083c131f582507b096318d114c97131630) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added installation and complete usage guide to the documentation.

- [#5](https://github.com/christoph-fricke/openapi-msw/pull/5) [`d15a0c2`](https://github.com/christoph-fricke/openapi-msw/commit/d15a0c2720f4d51415309f432cdc50aefb90f25f) Thanks [@christoph-fricke](https://github.com/christoph-fricke)! - Added `createOpenApiHttp(...)` to create a thin, type-safe wrapper around [MSW](https://mswjs.io/)'s `http` that uses [openapi-ts](https://openapi-ts.pages.dev/introduction/) `paths`:

  ```ts
  import type { paths } from "./openapi-ts-definitions";

  const http = createOpenApiHttp<paths>();

  // Define handlers with fully typed paths, path params, and request/response bodies
  const handler = http.get("/pets/{id}", () => {
    /* ... */
  });

  // Fallback to default http implementation
  const catchAll = http.untyped.all("*", () => {
    /* ... */
  });
  ```
