Created
March 23, 2016 16:01
-
-
Save dtothefp/2159a37c2478249b5f4f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 {isJSON} from '../utils/is-json'; | |
import request from './superagent'; | |
export async function({method = 'GET', url}) { | |
method = method.toUpperCase(); | |
const methods = ['GET', 'POST']; | |
let [foundMethod] = methods.filter(m => method === m); | |
if (!foundMethod) { | |
throw new Error('you specified an unknown method') | |
} | |
let response | |
try { | |
response = await superagent(method, url); | |
} catch (err) { | |
return Promise.reject(err); //async function will automatically wrap this in a returned promise | |
} | |
return isJson(response) ? response : Promise.reject('Bad JSON :-((') | |
} |
This file contains hidden or 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 superagent from 'superagent'; | |
import superagentPromise from 'superagent-promise'; | |
const agent = superagentPromise(superagent, Promise); | |
export default agent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment