From 6fcae52edbb55caa4a557d89d98cb1f8182b4ea2 Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Thu, 29 Aug 2024 20:12:33 +0100 Subject: [PATCH] docs: document the changes to what types of body are acceptable --- docs/docs/@fetch-mock/core/configuration.md | 6 ------ docs/docs/@fetch-mock/core/route/response.md | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/docs/docs/@fetch-mock/core/configuration.md b/docs/docs/@fetch-mock/core/configuration.md index c1a6b563..96773735 100644 --- a/docs/docs/@fetch-mock/core/configuration.md +++ b/docs/docs/@fetch-mock/core/configuration.md @@ -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 - -`{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 `{Boolean}` default: `true` diff --git a/docs/docs/@fetch-mock/core/route/response.md b/docs/docs/@fetch-mock/core/route/response.md index 81bd4c15..44a05563 100644 --- a/docs/docs/@fetch-mock/core/route/response.md +++ b/docs/docs/@fetch-mock/core/route/response.md @@ -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 @@ -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