Skip to content

Instantly share code, notes, and snippets.

@patrickleet
Created January 13, 2017 22:41
Show Gist options
  • Save patrickleet/eba4d589e1ba680ddcaa755ed1a1bd98 to your computer and use it in GitHub Desktop.
Save patrickleet/eba4d589e1ba680ddcaa755ed1a1bd98 to your computer and use it in GitHub Desktop.
import pRequest from 'util/promiseRequest'
Object.resolve = function(path, obj) {
return path.split('.').reduce(function(prev, curr) {
return prev ? prev[curr] : undefined
}, obj)
}
export class RESTConnector {
constructor({ rootUrl, keyPath }) {
this.rootUrl = rootUrl
this.keyPath = keyPath
}
async get(path) {
const options = {
method: 'GET',
url: this.rootUrl + path,
headers: {
'content-type': 'application/json',
accept: 'application/json'
},
json: true
}
const result = await pRequest(options)
return Object.resolve(this.keyPath, result) || []
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment