Skip to content

Commit

Permalink
docs: document the changes to what types of body are acceptable
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Aug 29, 2024
1 parent 4b11fc4 commit 6fcae52
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
6 changes: 0 additions & 6 deletions docs/docs/@fetch-mock/core/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ fetchMock.config.sendAsJson = false;

Options marked with a `` can also be overridden for individual calls to `.mock(matcher, response, options)` by setting as properties on the `options` parameter

### sendAsJson<sup>†</sup>

`{Boolean}` default: `true`

Always convert objects passed to `.mock()` to JSON strings before building reponses. Can be useful to set to `false` globally if e.g. dealing with a lot of `ArrayBuffer`s. When `true` the `Content-Type: application/json` header will also be set on each response.

### includeContentLength<sup>†</sup>

`{Boolean}` default: `true`
Expand Down
18 changes: 14 additions & 4 deletions docs/docs/@fetch-mock/core/route/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ If the object _only_ contains properties from among those listed below it is use

### body

`{String|Object}`
`{String|Object|BodyInit}`

Set the `Response` body, e.g. `"Server responded ok"`, `{ token: 'abcdef' }`. See the `Object` section of the docs below for behaviour when passed an `Object`.
Set the `Response` body. This could be

- a string e.g. `"Server responded ok"`, `{ token: 'abcdef' }`.
- an object literal (see the `Object` section of the docs below).
- Anything else that satisfies the specification for the [body parameter of new Response()](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body). This currently allows instances of Blob, ArrayBuffer, TypedArray, DataView, FormData, ReadableStream, URLSearchParams, and String.

### status

Expand Down Expand Up @@ -62,9 +66,15 @@ Forces `fetch` to return a `Promise` rejected with the value of `throws` e.g. `n

## Object

`{Object|ArrayBuffer|...`
`{Object}`

Any object literal that does not match the schema for a response config will be converted to a `JSON` string and set as the response `body`.

The `Content-Type: application/json` header will also be set on each response. To send JSON responses that do not set this header (e.g. to mock a poorly configured server) manually convert the object to a string first e.g.

If the `sendAsJson` option is set to `true`, any object that does not match the schema for a response config will be converted to a `JSON` string and set as the response `body`. Otherwise, the object will be set as the response `body` (useful for `ArrayBuffer`s etc.)
```js
fetchMock.route('http://a.com', JSON.stringify({ prop: 'value' }));
```

## Promise

Expand Down

0 comments on commit 6fcae52

Please sign in to comment.