Skip to content

Instantly share code, notes, and snippets.

@dtothefp
Created March 23, 2016 16:01
Show Gist options
  • Save dtothefp/2159a37c2478249b5f4f to your computer and use it in GitHub Desktop.
Save dtothefp/2159a37c2478249b5f4f to your computer and use it in GitHub Desktop.
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 :-((')
}
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