diff --git a/docs/change-log.md b/docs/change-log.md index 28aabf32..ad28942a 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -27,6 +27,7 @@ - API: Introduction of `m.redraw.sync()` ([#1592](https://github.com/MithrilJS/mithril.js/pull/1592)) - 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)) #### Ospec improvements: diff --git a/docs/request.md b/docs/request.md index fb013df0..44bb954b 100644 --- a/docs/request.md +++ b/docs/request.md @@ -49,6 +49,7 @@ Argument | Type | Required | Descr `options.user` | `String` | No | A username for HTTP authorization. Defaults to `undefined`. `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.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 b5190ba7..8424f889 100644 --- a/request/request.js +++ b/request/request.js @@ -75,6 +75,8 @@ module.exports = function($window, Promise) { } if (args.withCredentials) xhr.withCredentials = args.withCredentials + if (args.timeout) xhr.timeout = args.timeout + 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 5ad5da91..5c3c4cce 100644 --- a/request/tests/test-request.js +++ b/request/tests/test-request.js @@ -435,6 +435,20 @@ o.spec("xhr", function() { done() }) }) + o("set timeout to xhr instance", function() { + mock.$defineRoutes({ + "GET /item": function() { + return {status: 200, responseText: ''} + } + }) + return xhr({ + method: "GET", url: "/item", + timeout: 42, + config: function(xhr) { + o(xhr.timeout).equals(42) + } + }) + }) /*o("data maintains after interpolate", function() { mock.$defineRoutes({ "PUT /items/:x": function() {