From d06c1cba875a622e0037b5666fa0771f51e0c061 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Fri, 20 Jul 2018 13:05:48 +0200 Subject: [PATCH] Add support for setting `responseType` of xhr via m.request options --- docs/change-log.md | 1 + docs/request.md | 1 + request/request.js | 2 ++ request/tests/test-request.js | 14 ++++++++++++++ 4 files changed, 18 insertions(+) diff --git a/docs/change-log.md b/docs/change-log.md index e77780f8a..baa5d76fa 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -33,6 +33,7 @@ - API: Event handlers may also be objects with `handleEvent` methods ([#1939](https://github.com/MithrilJS/mithril.js/issues/1939)). - API: `m.route.link` accepts an optional `options` object ([#1930](https://github.com/MithrilJS/mithril.js/pull/1930)) - API: `m.request` supports `timeout` as attr - ([#1966](https://github.com/MithrilJS/mithril.js/pull/1966)) +- API: `m.request` supports `responseType` as attr - ([#2193](https://github.com/MithrilJS/mithril.js/pull/2193)) - Mocks: add limited support for the DOMParser API ([#2097](https://github.com/MithrilJS/mithril.js/pull/2097)) - API: add support for raw SVG in `m.trust()` string ([#2097](https://github.com/MithrilJS/mithril.js/pull/2097)) - render/core: remove the DOM nodes recycling pool ([#2122](https://github.com/MithrilJS/mithril.js/pull/2122)) diff --git a/docs/request.md b/docs/request.md index 44bb954b1..3412ebeae 100644 --- a/docs/request.md +++ b/docs/request.md @@ -50,6 +50,7 @@ Argument | Type | Required | Descr `options.password` | `String` | No | A password for HTTP authorization. Defaults to `undefined`. This option is provided for `XMLHttpRequest` compatibility, but you should avoid using it because it sends the password in plain text over the network. `options.withCredentials` | `Boolean` | No | Whether to send cookies to 3rd party domains. Defaults to `false` `options.timeout` | `Number` | No | The amount of milliseconds a request can take before automatically being [terminated](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout). Defaults to `undefined`. +`options.responseType` | `String` | No | The expected [type](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType) of the response. Defaults to `undefined`. `options.config` | `xhr = Function(xhr)` | No | Exposes the underlying XMLHttpRequest object for low-level configuration. Defaults to the [identity function](https://en.wikipedia.org/wiki/Identity_function). `options.headers` | `Object` | No | Headers to append to the request before sending it (applied right before `options.config`). `options.type` | `any = Function(any)` | No | A constructor to be applied to each object in the response. Defaults to the [identity function](https://en.wikipedia.org/wiki/Identity_function). diff --git a/request/request.js b/request/request.js index 8424f8895..e063361fe 100644 --- a/request/request.js +++ b/request/request.js @@ -76,6 +76,8 @@ module.exports = function($window, Promise) { if (args.withCredentials) xhr.withCredentials = args.withCredentials if (args.timeout) xhr.timeout = args.timeout + + if (args.responseType) xhr.responseType = args.responseType for (var key in args.headers) if ({}.hasOwnProperty.call(args.headers, key)) { xhr.setRequestHeader(key, args.headers[key]) diff --git a/request/tests/test-request.js b/request/tests/test-request.js index 52f840ef0..ae7e4d6bd 100644 --- a/request/tests/test-request.js +++ b/request/tests/test-request.js @@ -449,6 +449,20 @@ o.spec("xhr", function() { } }) }) + o("set responseType to xhr instance", function() { + mock.$defineRoutes({ + "GET /item": function() { + return {status: 200, responseText: ""} + } + }) + return xhr({ + method: "GET", url: "/item", + responseType: "blob", + config: function(xhr) { + o(xhr.responseType).equals("blob") + } + }) + }) /*o("data maintains after interpolate", function() { mock.$defineRoutes({ "PUT /items/:x": function() {