Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows for first new Response argument to be options #577

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
}
}

var getClassOf = Function.prototype.call.bind(Object.prototype.toString)

var isClassOf = function (instance, className) {
return getClassOf(instance).split(' ')[1] === className + ']'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get away with a simpler check?

function isPlainObject(obj) {
  return obj != null && typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Object]'
}

}

function normalizeName(name) {
if (typeof name !== 'string') {
name = String(name)
Expand Down Expand Up @@ -374,7 +380,19 @@

Body.call(Request.prototype)

function Response(bodyInit, options) {
function Response() {
var bodyInit
var options

// if the first argument is options and not body
if (isClassOf(arguments[0], 'Object')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like

function Response(bodyInit, options) {
  if (arguments.length === 1 && isPlainObject(bodyInit)) {
    options = bodyInit
    bodyInit = null
  }
  // ...
}

options = arguments[0]
bodyInit = null
} else {
bodyInit = arguments[0]
options = arguments[1]
}

if (!options) {
options = {}
}
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,12 @@ suite('Response', function() {

assert.equal(r.headers.get('content-type'), 'text/plain')
})

test('accepts options as first parameter', function () {
var r = new Response({ status: 200 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a non-default status such as 204 to verify that the status was successfully assigned.


assert.equal(r.status, 200)
})
})

// https://fetch.spec.whatwg.org/#body-mixin
Expand Down