Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #52 from zooniverse/optional-credentials
Browse files Browse the repository at this point in the history
split makeHTTPRequest  to distinct methods
  • Loading branch information
srallen authored Nov 22, 2019
2 parents 556855d + ac2a05c commit 2c7fed0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/json-api-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class JSONAPIClient extends Model
else if method in WRITE_OPS
@update writes: @writes + 1

request = makeHTTPRequest method, fullURL, fullPayload, allHeaders, query
request = makeHTTPRequest.makeHTTPRequest method, fullURL, fullPayload, allHeaders, query

request
.catch =>
Expand Down Expand Up @@ -110,7 +110,8 @@ class JSONAPIClient extends Model
@_typesCache[name]

module.exports = JSONAPIClient
module.exports.makeHTTPRequest = makeHTTPRequest
module.exports.makeHTTPRequest = makeHTTPRequest.makeHTTPRequest
module.exports.makeCredentialHTTPRequest = makeHTTPRequest.makeCredentialHTTPRequest
module.exports.Emitter = Emitter
module.exports.Type = Type
module.exports.Model = Model
Expand Down
26 changes: 19 additions & 7 deletions src/make-http-request.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@ normalizeUrl = require 'normalizeurl'

getsInProgress = {}

if request.agent?
request = request.agent()

# Superagent will only auto-parse responses from a Content-Type header it recognizes.
# Add the Accept in use by the JSON API spec, which is what will be sent back from the server.
request.parse ?= {}
request.parse[DEFAULT_HEADERS['Accept']] = JSON.parse.bind JSON

# isolate the credential requests from the superagent singleton
# via the agent() to ensure correct credentials for both request types
# http://visionmedia.github.io/superagent/#agents-for-global-state
if request.agent?
credentialRequest = request.agent()
if credentialRequest.withCredentials?
credentialRequest = credentialRequest.withCredentials()
else
# ensure the credentialRequest is set, though it would use the
# singleton superagent and fail to correctly send credential requests
credentialRequest = request

makeHTTPRequest = (method, url, data, headers = {}, query) ->
makeRequest(request, method, url, data, headers, query)

makeCredentialHTTPRequest = (method, url, data, headers = {}, query) ->
makeRequest(credentialRequest, method, url, data, headers, query)

makeRequest = (request, method, url, data, headers, query) ->
originalArguments = Array::slice.call arguments # In case we need to retry
method = method.toLowerCase()
url = normalizeUrl url
Expand All @@ -35,9 +50,6 @@ makeHTTPRequest = (method, url, data, headers = {}, query) ->

req = req.set headers

if req.withCredentials?
req = req.withCredentials()

req.end (error, response) ->
delete getsInProgress[requestID]
if error?.status is 408
Expand All @@ -54,4 +66,4 @@ makeHTTPRequest = (method, url, data, headers = {}, query) ->

promisedRequest

module.exports = makeHTTPRequest
module.exports = { makeHTTPRequest, makeCredentialHTTPRequest }

0 comments on commit 2c7fed0

Please sign in to comment.