/** * Module dependencies. */ var CookieJar = require('cookiejar').CookieJar; var CookieAccess = require('cookiejar').CookieAccessInfo; var parse = require('url').parse; var request = require('./index'); var methods = require('methods'); /** * Expose `Agent`. */ module.exports = Agent; /** * Initialize a new `Agent`. * * @api public */ function Agent(options) { if (!(this instanceof Agent)) return new Agent(options); if (options) this._ca = options.ca; this.jar = new CookieJar; } /** * Save the cookies in the given `res` to * the agent's cookie jar for persistence. * * @param {Response} res * @api private */ Agent.prototype.saveCookies = function(res){ var cookies = res.headers['set-cookie']; if (cookies) this.jar.setCookies(cookies); }; /** * Attach cookies when available to the given `req`. * * @param {Request} req * @api private */ Agent.prototype.attachCookies = function(req){ var url = parse(req.url); var access = CookieAccess(url.hostname, url.pathname, 'https:' == url.protocol); var cookies = this.jar.getCookies(access).toValueString(); req.cookies = cookies; }; // generate HTTP verb methods if (methods.indexOf('del') == -1) { // create a copy so we don't cause conflicts with // other packages using the methods package and // npm 3.x methods = methods.slice(0); methods.push('del'); } methods.forEach(function(method){ var name = method; method = 'del' == method ? 'delete' : method; method = method.toUpperCase(); Agent.prototype[name] = function(url, fn){ var req = request(method, url); req.ca(this._ca); req.on('response', this.saveCookies.bind(this)); req.on('redirect', this.saveCookies.bind(this)); req.on('redirect', this.attachCookies.bind(this, req)); this.attachCookies(req); fn && req.end(fn); return req; }; });
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 19553 | swellard | Move and rename clients | ||
//guest/perforce_software/helix-web-services/main/source/clients/2016.1.0/javascript/node_modules/superagent/lib/node/agent.js | |||||
#1 | 18810 | tjuricek |
First-pass at JavaScript client SDK. JavaScript requires Node with Gulp to "browserfy" the library. It's the easiest way I found to use the swagger-js project; bundle up a wrapping method. There is no JavaScript reference guide. The swagger-js doesn't really document what they do very well, actually. Overall I'm not particularly impressed by swagger-js, it was hard to even figure out what the right method syntax was. We may want to invest time in doing it better. This required setting CORS response headers, which are currently defaulted to a fairly insecure setting. |