Skip to content

Commit

Permalink
Merge pull request #340 from dhis2/fix/allow-content-type-override-fo…
Browse files Browse the repository at this point in the history
…r-update

fix(api): allow Content-Type override for update()
  • Loading branch information
Birkbjo authored Nov 17, 2021
2 parents 861d9f7 + 655bd3c commit 48e28fa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
35 changes: 23 additions & 12 deletions src/api/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,29 +206,40 @@ class Api {
* @param {string} url The url for the request
* @param {*} data Any data that should be send with the request. This becomes the body of the PUT request.
* @param {boolean} [useMergeStrategy=false]
* @param {Object.<string, any>} options The request options are passed as options to the fetch request.
*
* @returns {Promise.<*>} The response body.
*/
update(url, data, useMergeStrategy = false) {
// Since we are currently using PUT to save the full state back, we have to use mergeMode=REPLACE
// to clear out existing values
update(url, data, useMergeStrategy = false, options = {}) {
let payload = data

// Ensure that headers are defined and are treated without case sensitivity
const requestOptions = {
...options,
headers: new Headers(options.headers || {}),
}

if (data !== undefined) {
if (
!requestOptions.headers.has('Content-Type') &&
typeof payload === 'string'
) {
requestOptions.headers.set('Content-Type', 'text/plain')
} else {
payload = JSON.stringify(data)
}
}

const urlForUpdate =
useMergeStrategy === true
? `${url}?${getMergeStrategyParam()}`
: url
if (typeof data === 'string') {
return this.request(
'PUT',
getUrl(this.baseUrl, urlForUpdate),
String(data),
{ headers: new Headers({ 'Content-Type': 'text/plain' }) }
)
}

return this.request(
'PUT',
getUrl(this.baseUrl, urlForUpdate),
JSON.stringify(data)
payload,
requestOptions
)
}

Expand Down
6 changes: 5 additions & 1 deletion src/datastore/BaseStoreNamespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ class BaseStoreNamespace {
update(key, value) {
return this.api.update(
[this.endPoint, this.namespace, key].join('/'),
value
value,
false,
{
headers: { 'Content-Type': 'application/json' },
}
)
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/datastore/__tests__/DatastoreNamespace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ describe('DataStoreNamespace', () => {
expect(namespace.update).toBeCalledWith(setKey, valueData)
expect(apiMock.update).toBeCalledWith(
`dataStore/DHIS/${setKey}`,
valueData
valueData,
false,
{
headers: { 'Content-Type': 'application/json' },
}
)
})
})
Expand Down Expand Up @@ -268,7 +272,11 @@ describe('DataStoreNamespace', () => {
return namespace.update(setKey, valueData).then(() => {
expect(apiMock.update).toBeCalledWith(
`dataStore/DHIS/${setKey}`,
valueData
valueData,
false,
{
headers: { 'Content-Type': 'application/json' },
}
)
})
})
Expand Down
12 changes: 10 additions & 2 deletions src/datastore/__tests__/UserDatastoreNamespace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ describe('DataStoreNamespace', () => {
expect(namespace.update).toBeCalledWith(setKey, valueData)
expect(apiMock.update).toBeCalledWith(
`dataStore/DHIS/${setKey}`,
valueData
valueData,
false,
{
headers: { 'Content-Type': 'application/json' },
}
)
})
})
Expand Down Expand Up @@ -268,7 +272,11 @@ describe('DataStoreNamespace', () => {
return namespace.update(setKey, valueData).then(() => {
expect(apiMock.update).toBeCalledWith(
`dataStore/DHIS/${setKey}`,
valueData
valueData,
false,
{
headers: { 'Content-Type': 'application/json' },
}
)
})
})
Expand Down

0 comments on commit 48e28fa

Please sign in to comment.