lib/assets/javascripts/unpoly/classes/request.coffee in unpoly-rails-0.56.7 vs lib/assets/javascripts/unpoly/classes/request.coffee in unpoly-rails-0.57.0
- old
+ new
@@ -25,20 +25,14 @@
@param {string} url
@stable
###
###**
- Parameters that should be sent as the request's payload.
+ [Parameters](/up.params) that should be sent as the request's payload.
- Parameters may be passed as one of the following forms:
-
- 1. An object where keys are param names and the values are param values
- 2. An array of `{ name: 'param-name', value: 'param-value' }` objects
- 3. A [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) object
-
- @property up.Request#data
- @param {String} data
+ @property up.Request#params
+ @param {object|FormData|string|Array} params
@stable
###
###**
The CSS selector that will be sent as an [`X-Up-Target` header](/up.protocol#optimizing-responses).
@@ -76,15 +70,18 @@
###
fields: ->
[
'method',
'url',
- 'data',
+ 'params',
+ 'data', # deprecated. use #params.
'target',
'failTarget',
'headers',
- 'timeout'
+ 'timeout',
+ 'preload' # since up.proxy.request() options are sometimes wrapped in this class
+ 'cache' # since up.proxy.request() options are sometimes wrapped in this class
]
###**
@constructor up.Request
@param {string} [attributes]
@@ -92,41 +89,38 @@
constructor: (options) ->
super(options)
@normalize()
normalize: =>
+ u.deprecateRenamedKey(@, 'data', 'params')
@method = u.normalizeMethod(@method)
@headers ||= {}
@extractHashFromUrl()
if u.methodAllowsPayload(@method)
- @transferSearchToData()
+ @transferSearchToParams()
else
- @transferDataToUrl()
+ @transferParamsToUrl()
extractHashFromUrl: =>
urlParts = u.parseUrl(@url)
# Remember the #hash for later revealing.
# It will be lost during normalization.
- @hash = urlParts.hash
+ @hash = u.presence(urlParts.hash)
@url = u.normalizeUrl(urlParts, hash: false)
- transferDataToUrl: =>
- if @data && !u.isFormData(@data)
- # GET methods are not allowed to have a payload, so we transfer { data } params to the URL.
- query = u.requestDataAsQuery(@data)
- separator = if u.contains(@url, '?') then '&' else '?'
- @url += separator + query
- # Now that we have transfered the params into the URL, we delete them from the { data } option.
- @data = undefined
+ transferParamsToUrl: =>
+ if @params && !u.isFormData(@params)
+ # GET methods are not allowed to have a payload, so we transfer { params } params to the URL.
+ @url = up.params.buildURL(@url, @params)
+ # Now that we have transfered the params into the URL, we delete them from the { params } option.
+ @params = undefined
- transferSearchToData: =>
- urlParts = u.parseUrl(@url)
- query = urlParts.search
- if query
- @data = u.mergeRequestData(@data, query)
- @url = u.normalizeUrl(urlParts, search: false)
+ transferSearchToParams: =>
+ if query = up.params.fromURL(@url)
+ @params = up.params.merge(@params, query)
+ @url = u.normalizeUrl(@url, search: false)
isSafe: =>
up.proxy.isSafeMethod(@method)
send: =>
@@ -134,31 +128,29 @@
# This would confuse API clients and cache key logic in up.proxy.
new Promise (resolve, reject) =>
xhr = new XMLHttpRequest()
xhrHeaders = u.copy(@headers)
- xhrData = @data
+ xhrPayload = @params
xhrMethod = @method
xhrUrl = @url
- [xhrMethod, xhrData] = up.proxy.wrapMethod(xhrMethod, xhrData)
+ [xhrMethod, xhrPayload] = up.proxy.wrapMethod(xhrMethod, xhrPayload)
- if u.isFormData(xhrData)
+ if xhrPayload
delete xhrHeaders['Content-Type'] # let the browser set the content type
- else if u.isPresent(xhrData)
- xhrData = u.requestDataAsQuery(xhrData, purpose: 'form')
- xhrHeaders['Content-Type'] = 'application/x-www-form-urlencoded'
+ xhrPayload = up.params.toFormData(xhrPayload)
else
# XMLHttpRequest expects null for an empty body
- xhrData = null
+ xhrPayload = null
- xhrHeaders[up.protocol.config.targetHeader] = @target if @target
- xhrHeaders[up.protocol.config.failTargetHeader] = @failTarget if @failTarget
+ pc = up.protocol.config
+ xhrHeaders[pc.targetHeader] = @target if @target
+ xhrHeaders[pc.failTargetHeader] = @failTarget if @failTarget
xhrHeaders['X-Requested-With'] ||= 'XMLHttpRequest' unless @isCrossDomain()
-
if csrfToken = @csrfToken()
- xhrHeaders[up.protocol.config.csrfHeader] = csrfToken
+ xhrHeaders[pc.csrfHeader] = csrfToken
xhr.open(xhrMethod, xhrUrl)
for header, value of xhrHeaders
xhr.setRequestHeader(header, value)
@@ -175,16 +167,16 @@
xhr.onerror = resolveWithResponse
xhr.ontimeout = resolveWithResponse
xhr.timeout = @timeout if @timeout
- xhr.send(xhrData)
+ xhr.send(xhrPayload)
navigate: =>
# GET forms cannot have an URL with a query section in their [action] attribute.
# The query section would be overridden by the serialized input values on submission.
- @transferSearchToData()
+ @transferSearchToParams()
$form = $('<form class="up-page-loader"></form>')
addField = (field) -> $('<input type="hidden">').attr(field).appendTo($form)
@@ -200,13 +192,13 @@
$form.attr(method: formMethod, action: @url)
if (csrfParam = up.protocol.csrfParam()) && (csrfToken = @csrfToken())
addField(name: csrfParam, value: csrfToken)
- # @data will be undefined for GET requests, since we have already
+ # @params will be undefined for GET requests, since we have already
# transfered all params to the URL during normalize().
- u.each u.requestDataAsArray(@data), addField
+ u.each(up.params.toArray(@params), addField)
$form.hide().appendTo('body')
up.browser.submitForm($form)
# Returns a csrfToken if this request requires it
@@ -234,13 +226,14 @@
responseAttrs.title = up.protocol.titleFromXhr(xhr)
new up.Response(responseAttrs)
isCachable: =>
- @isSafe() && !u.isFormData(@data)
+ @isSafe() && !u.isFormData(@params)
cacheKey: =>
- [@url, @method, u.requestDataAsQuery(@data), @target].join('|')
+ query = up.params.toQuery(@params)
+ [@url, @method, query, @target].join('|')
@wrap: (object) ->
if object instanceof @
# This object has gone through instantiation and normalization before.
object