Fix added header request parameter, document it, regen bundles
This commit is contained in:
parent
80c429e49b
commit
f670acb132
5 changed files with 153 additions and 99 deletions
|
|
@ -14,6 +14,7 @@
|
|||
- [Using variable data formats](#using-variable-data-formats)
|
||||
- [Extracting Metadata from the Response](#extracting-metadata-from-the-response)
|
||||
- [Custom request rejections](#custom-request-rejections)
|
||||
- [Setting headers](#setting-headers)
|
||||
- [Configuring the underlying XMLHttpRequest](#configuring-the-underlying-xmlhttprequest)
|
||||
- [Aborting a request](#aborting-a-request)
|
||||
- [Using JSON-P](#using-json-p)
|
||||
|
|
@ -337,15 +338,34 @@ You can read more about the [promise exception monitor here](mithril.deferred.md
|
|||
|
||||
---
|
||||
|
||||
### Setting headers
|
||||
|
||||
The `headers` option can be used to add or modify existing headers. The example The example below shows how to configure a `POST` request where the server expects requests to have a `Content-Type: application/json` header.
|
||||
|
||||
```javascript
|
||||
m.request({
|
||||
method: "POST",
|
||||
url: "/foo",
|
||||
headers: {"Content-Type": "application/json"}
|
||||
});
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Configuring the underlying XMLHttpRequest
|
||||
|
||||
The `config` option can be used to arbitrarily configure the native XMLHttpRequest instance and to access properties that would not be accessible otherwise.
|
||||
|
||||
The example below shows how to configure a request where the server expects requests to have a `Content-Type: application/json` header
|
||||
The `config` option can be used to arbitrarily configure the native XMLHttpRequest instance and to access properties that would not be accessible otherwise. For example, this is how to listen for progress notifications.
|
||||
|
||||
```javascript
|
||||
var xhrConfig = function(xhr) {
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
xhr.onprogress = function(ev) {
|
||||
ev = ev || event;
|
||||
if (ev.lengthComputable) {
|
||||
console.log(ev.loaded + " bytes sent out of " + ev.total + " total.")
|
||||
} else {
|
||||
console.log(ev.loaded + " bytes sent.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m.request({method: "POST", url: "/foo", config: xhrConfig});
|
||||
|
|
@ -622,9 +642,13 @@ where:
|
|||
|
||||
And the data is `[{name: "John"}, {name: "Mary"}]`, then the response will contain an array of two User instances.
|
||||
|
||||
- **Object<String> headers** (optional)
|
||||
|
||||
Additional headers to set on the request, if any. Each header is specified as a key-value pair.
|
||||
|
||||
- **XMLHttpRequest? config(XMLHttpRequest xhr, XHROptions options)** (optional)
|
||||
|
||||
An initialization function that runs after `open` and before `send`. Useful for adding request headers and when using XHR2 features, such as the XMLHttpRequest's `upload` property.
|
||||
An initialization function that runs after `open` and before `send`. Useful for using XHR2 features, such as the XMLHttpRequest's `upload` property or `progress` event.
|
||||
|
||||
- **XMLHttpRequest xhr**
|
||||
|
||||
|
|
@ -671,5 +695,3 @@ where:
|
|||
- **Object<any> data** (optional)
|
||||
|
||||
Data to be sent. It's automatically placed in the appropriate section of the request with the appropriate serialization based on `method`
|
||||
|
||||
|
||||
|
|
|
|||
26
mithril.js
26
mithril.js
|
|
@ -11,7 +11,7 @@
|
|||
location: typeof location !== "undefined" ? location : {},
|
||||
clearTimeout: clearTimeout,
|
||||
setTimeout: setTimeout
|
||||
});
|
||||
})
|
||||
}
|
||||
if (typeof module === "object" && module != null && module.exports) {
|
||||
module.exports = m
|
||||
|
|
@ -524,7 +524,8 @@
|
|||
parentTag
|
||||
) {
|
||||
var nodes = cached.nodes
|
||||
if (!editable || editable !== $document.activeElement || data !== cached) {
|
||||
if (!editable || editable !== $document.activeElement ||
|
||||
data !== cached) {
|
||||
if (data.$trusted) {
|
||||
clear(nodes, cached)
|
||||
nodes = injectHTML(parentElement, index, data)
|
||||
|
|
@ -1095,10 +1096,8 @@
|
|||
// #1252 likewise when `contenteditable` is set on an element.
|
||||
try {
|
||||
if (
|
||||
tag !== "input" && !node.isContentEditable
|
||||
/* eslint-disable eqeqeq */
|
||||
|| node[attrName] != dataAttr
|
||||
/* eslint-enable eqeqeq */
|
||||
tag !== "input" && !node.isContentEditable ||
|
||||
node[attrName] != dataAttr // eslint-disable-line eqeqeq
|
||||
) {
|
||||
node[attrName] = dataAttr
|
||||
}
|
||||
|
|
@ -1436,6 +1435,8 @@
|
|||
return parameterize(component, args)
|
||||
}
|
||||
|
||||
var currentRoute, previousRoute
|
||||
|
||||
function checkPrevented(component, root, index, isPrevented) {
|
||||
if (!isPrevented) {
|
||||
m.redraw.strategy("all")
|
||||
|
|
@ -1468,6 +1469,7 @@
|
|||
if (component == null) {
|
||||
removeRootElement(root, index)
|
||||
}
|
||||
|
||||
if (previousRoute) {
|
||||
currentRoute = previousRoute
|
||||
}
|
||||
|
|
@ -1604,7 +1606,7 @@
|
|||
var modes = {pathname: "", hash: "#", search: "?"}
|
||||
var redirect = noop
|
||||
var isDefaultRoute = false
|
||||
var routeParams, currentRoute, previousRoute
|
||||
var routeParams
|
||||
|
||||
m.route = function (root, arg1, arg2, vdom) { // eslint-disable-line
|
||||
// m.route()
|
||||
|
|
@ -2166,11 +2168,6 @@
|
|||
xhr.setRequestHeader("Accept", "application/json, text/*")
|
||||
}
|
||||
|
||||
if (isFunction(options.config)) {
|
||||
var maybeXhr = options.config(xhr, options)
|
||||
if (maybeXhr != null) xhr = maybeXhr
|
||||
}
|
||||
|
||||
if (isObject(options.headers)) {
|
||||
for (var header in options.headers) {
|
||||
if (hasOwn.call(options.headers, header)) {
|
||||
|
|
@ -2179,6 +2176,11 @@
|
|||
}
|
||||
}
|
||||
|
||||
if (isFunction(options.config)) {
|
||||
var maybeXhr = options.config(xhr, options)
|
||||
if (maybeXhr != null) xhr = maybeXhr
|
||||
}
|
||||
|
||||
var data = options.method === "GET" || !options.data ? "" : options.data
|
||||
|
||||
if (data && !isString(data) && data.constructor !== global.FormData) {
|
||||
|
|
|
|||
2
mithril.min.js
vendored
2
mithril.min.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -146,7 +146,6 @@ describe("m.request()", function () {
|
|||
expect(xhr.$headers).to.not.have.property("Content-Type")
|
||||
})
|
||||
|
||||
|
||||
it("sets xhr request headers as per the headers config", function () {
|
||||
var error = m.prop()
|
||||
|
||||
|
|
@ -171,6 +170,37 @@ describe("m.request()", function () {
|
|||
"CustomValue")
|
||||
})
|
||||
|
||||
it("overwrites existing headers", function () {
|
||||
var error = m.prop()
|
||||
|
||||
m.request({
|
||||
method: "POST",
|
||||
url: "test",
|
||||
// Trigger the Content-Type addition
|
||||
data: {foo: "bar"},
|
||||
headers: {
|
||||
"Authorization" : "Bearer 12345abcd12345",
|
||||
"CustomHeader" : "CustomValue",
|
||||
"Content-Type" : "CustomType"
|
||||
}
|
||||
}).then(null, error)
|
||||
|
||||
var xhr = mock.XMLHttpRequest.$instances.pop()
|
||||
xhr.onreadystatechange()
|
||||
|
||||
expect(xhr.$headers).to.have.property(
|
||||
"Authorization",
|
||||
"Bearer 12345abcd12345")
|
||||
|
||||
expect(xhr.$headers).to.have.property(
|
||||
"CustomHeader",
|
||||
"CustomValue")
|
||||
|
||||
expect(xhr.$headers).to.have.property(
|
||||
"Content-Type",
|
||||
"CustomType")
|
||||
})
|
||||
|
||||
|
||||
it("correctly sets initial value", function () {
|
||||
var prop = m.request({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue