Created
January 13, 2017 22:41
-
-
Save patrickleet/eba4d589e1ba680ddcaa755ed1a1bd98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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