Created
March 26, 2015 06:02
-
-
Save cesarandreu/1b07552f88c712a4ddd0 to your computer and use it in GitHub Desktop.
fluxible request
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
| var promises = require('./request.promisify'), | |
| superagent = require('superagent'), | |
| cookies = require('cookies-js'), | |
| methods = require('methods'), | |
| clientRequest = {} | |
| methods.concat('del').forEach(method => { | |
| if (superagent[method]) | |
| clientRequest[method] = (...args) => { | |
| return superagent[method].apply(superagent, args).use(xsrfToken).use(promises) | |
| } | |
| }) | |
| module.exports = clientRequest | |
| function xsrfToken (request) { | |
| return request.set('x-xsrf-token', cookies.get('XSRF-TOKEN')) | |
| } |
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
| 'use strict' | |
| module.exports = function promises (request) { | |
| request['then'] = _then.bind(request) | |
| request['catch'] = _catch.bind(request) | |
| return request | |
| } | |
| function _then (fulfilled, rejected) { | |
| if (!this.promise) | |
| this.promise = new Promise((resolve, reject) => { | |
| this.end((err, res) => err ? reject(err) : resolve(res)) | |
| }) | |
| return this.promise.then(fulfilled, rejected) | |
| } | |
| function _catch (rejected) { | |
| if (!this.promise) this.then() | |
| return this.promise.catch(rejected) | |
| } |
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
| var promises = require('./request.promisify'), | |
| supertest = require('supertest'), | |
| methods = require('methods') | |
| module.exports = function serverRequest (server, headers) { | |
| var request = supertest(server), out = {} | |
| methods.concat('del').forEach(method => { | |
| if (request[method]) | |
| out[method] = (...args) => request[method].apply(request, args).set(headers).use(promises) | |
| }) | |
| return out | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment