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

Commit

Permalink
Add query params to PUT and POST
Browse files Browse the repository at this point in the history
Adds an optional query object argument to .save().
Passes that query down to the refresh() and request() methods so that query params can be added to the appropriate HTTP requests.
  • Loading branch information
eatyourgreens committed Nov 1, 2017
1 parent b9a12b5 commit 49a0341
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/json-api-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class JSONAPIClient extends Model
beforeEveryRequest: ->
Promise.resolve();

request: (method, url, payload, headers) ->
request: (method, url, payload, headers, query) ->
@beforeEveryRequest().then =>
method = method.toUpperCase()
fullURL = @root + url
Expand All @@ -42,7 +42,7 @@ class JSONAPIClient extends Model
else if method in WRITE_OPS
@update writes: @writes + 1

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

request
.catch =>
Expand Down
6 changes: 3 additions & 3 deletions src/make-http-request.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if request.agent?
request.parse ?= {}
request.parse[DEFAULT_HEADERS['Accept']] = JSON.parse.bind JSON

makeHTTPRequest = (method, url, data, headers = {}, modify) ->
makeHTTPRequest = (method, url, data, headers = {}, query) ->
originalArguments = Array::slice.call arguments # In case we need to retry
method = method.toLowerCase()
url = normalizeUrl url
Expand All @@ -29,8 +29,8 @@ makeHTTPRequest = (method, url, data, headers = {}, modify) ->
req = switch method
when 'get' then request.get(url).query data
when 'head' then request.head(url).query data
when 'put' then request.put(url).send data
when 'post' then request.post(url).send data
when 'put' then request.put(url).query(query).send data
when 'post' then request.post(url).query(query).send data
when 'delete' then request.del(url).query data

req = req.set headers
Expand Down
16 changes: 8 additions & 8 deletions src/resource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Resource extends Model
@_type.emit 'change'
value

save: ->
save: (query) ->
payload = {}
changes = @toJSON.call @getChangesSinceSave()
payload[@_type._name] = changes
Expand All @@ -49,10 +49,10 @@ class Resource extends Model
null
.then =>
save = if @id
@refresh(true).then =>
@_type._client.put @_getURL(), payload, @_getHeadersForModification()
@refresh(true, query).then =>
@_type._client.put @_getURL(), payload, @_getHeadersForModification(), query
else
@_type._client.post @_type._getURL(), payload
@_type._client.post @_type._getURL(), payload, {}, query

new ResourcePromise save.then ([result]) =>
for key of changes
Expand All @@ -73,13 +73,13 @@ class Resource extends Model
changes[key] = @[key]
changes

refresh: (saveChanges) ->
refresh: (saveChanges, query={}) ->
if saveChanges
changes = @getChangesSinceSave()
@refresh().then =>
@refresh(false, query).then =>
@update changes
else if @id
@_type._client.get @_getURL()
@_type._client.get @_getURL(), query
else
throw new Error 'Can\'t refresh a resource with no ID'

Expand All @@ -97,7 +97,7 @@ class Resource extends Model
null
.then =>
deletion = if @id
@refresh(true).then =>
@refresh(true, query).then =>
@_type._client.delete @_getURL(), null, @_getHeadersForModification()
else
Promise.resolve()
Expand Down

0 comments on commit 49a0341

Please sign in to comment.