Skip to content

Commit

Permalink
Refactored JSON method to work with preflight operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
coreybutler committed Nov 10, 2021
1 parent e50bcee commit 9a110e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngnjs/net",
"version": "1.0.0-alpha.18",
"version": "1.0.0-alpha.19",
"description": "A network communications plugin for NGN.",
"type": "module",
"author": "Corey Butler",
Expand Down
20 changes: 9 additions & 11 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,21 @@ export default class HttpClient extends NGN.EventEmitter {
* A promise representing the network request.
*/
json (url, callback) {
if (typeof url === 'string') {
url = { url }
}

const request = new NgnRequest(this.parseRequestConfig(url, 'GET'))

request.setHeader('Accept', 'application/json, application/ld+json, application/vnd.api+json, */json, */*json;q=0.8')
const cfg = typeof url === 'string' ? { url } : {}

this.preflight(request)
cfg.headers = {
'Accept': 'application/json, application/ld+json, application/vnd.api+json, */json, */*json;q=0.8'
}

const response = request.send()
const wrapper = new Promise((resolve, reject) => {
this.send('GET', cfg).then(r => resolve(r.JSON)).catch(reject)
})

if (callback) {
response.then(r => callback(null, r.JSON)).catch(callback)
return wrapper.then(data => callback(null, data)).catch(callback)
}

return new Promise((resolve, reject) => response.then(r => resolve(r.JSON)).catch(reject))
return wrapper
}

/**
Expand Down

0 comments on commit 9a110e0

Please sign in to comment.