Add support for saving/returning cookies (#4192)

This adds a `saveCookies` boolean flag to ApiClient. If set to true,
the client will save and return cookies to the server.

This is useful for supporting @SessionScoped beans in Java servers.
Works both in Node.js desktop apps, and in the browser.
This commit is contained in:
delenius 2017-03-07 23:26:46 -08:00 committed by wing328
parent 3e3d360027
commit 5935e2e6d9
3 changed files with 99 additions and 6 deletions

View File

@ -75,6 +75,22 @@
* @default true
*/
this.cache = true;
{{#emitJSDoc}} /**
* If set to true, the client will save the cookies from each server
* response, and return them in the next request.
* @default false
*/
{{/emitJSDoc}} this.enableCookies = false;
/*
* Used to save and return cookies in a node.js (non-browser) setting,
* if this.enableCookies is set to true.
*/
if (typeof window === 'undefined') {
this.agent = new superagent.agent();
}
};
{{#emitJSDoc}} /**
@ -405,6 +421,16 @@
request.accept(accept);
}
// Attach previously saved cookies, if enabled
if (this.enableCookies){
if (typeof window === 'undefined') {
this.agent.attachCookies(request);
}
else {
request.withCredentials();
}
}
{{#usePromises}} return new Promise(function(resolve, reject) {
request.end(function(error, response) {
if (error) {
@ -412,6 +438,9 @@
} else {
try {
var data = _this.deserialize(response, returnType);
if (_this.enableCookies && typeof window === 'undefined'){
_this.agent.saveCookies(response);
}
resolve({data, response});
} catch (err) {
reject(err);
@ -425,6 +454,9 @@
if (!error) {
try {
data = _this.deserialize(response, returnType);
if (_this.enableCookies && typeof window === 'undefined'){
_this.agent.saveCookies(response);
}
} catch (err) {
error = err;
}

View File

@ -14,18 +14,18 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['superagent'], factory);
define(['superagent', 'querystring'], factory);
} else if (typeof module === 'object' && module.exports) {
// CommonJS-like environments that support module.exports, like Node.
module.exports = factory(require('superagent'));
module.exports = factory(require('superagent'), require('querystring'));
} else {
// Browser globals (root is window)
if (!root.SwaggerPetstore) {
root.SwaggerPetstore = {};
}
root.SwaggerPetstore.ApiClient = factory(root.superagent);
root.SwaggerPetstore.ApiClient = factory(root.superagent, root.querystring);
}
}(this, function(superagent) {
}(this, function(superagent, querystring) {
'use strict';
/**
@ -78,6 +78,22 @@
* @default true
*/
this.cache = true;
/**
* If set to true, the client will save the cookies from each server
* response, and return them in the next request.
* @default false
*/
this.enableCookies = false;
/*
* Used to save and return cookies in a node.js (non-browser) setting,
* if this.enableCookies is set to true.
*/
if (typeof window === 'undefined') {
this.agent = new superagent.agent();
}
};
/**
@ -377,7 +393,7 @@
}
if (contentType === 'application/x-www-form-urlencoded') {
request.send(this.normalizeParams(formParams));
request.send(querystring.stringify(this.normalizeParams(formParams)));
} else if (contentType == 'multipart/form-data') {
var _formParams = this.normalizeParams(formParams);
for (var key in _formParams) {
@ -399,6 +415,16 @@
request.accept(accept);
}
// Attach previously saved cookies, if enabled
if (this.enableCookies){
if (typeof window === 'undefined') {
this.agent.attachCookies(request);
}
else {
request.withCredentials();
}
}
return new Promise(function(resolve, reject) {
request.end(function(error, response) {
if (error) {
@ -406,6 +432,9 @@
} else {
try {
var data = _this.deserialize(response, returnType);
if (_this.enableCookies && typeof window === 'undefined'){
_this.agent.saveCookies(response);
}
resolve({data, response});
} catch (err) {
reject(err);
@ -431,9 +460,12 @@
* or the constructor function for a complex type. Pass an array containing the type name to return an array of that type. To
* return an object, pass an object with one property whose name is the key type and whose value is the corresponding value type:
* all properties on <code>data<code> will be converted to this type.
* @returns An instance of the specified type.
* @returns An instance of the specified type or null or undefined if data is null or undefined.
*/
exports.convertToType = function(data, type) {
if (data === null || data === undefined)
return data
switch (type) {
case 'Boolean':
return Boolean(data);

View File

@ -78,6 +78,22 @@
* @default true
*/
this.cache = true;
/**
* If set to true, the client will save the cookies from each server
* response, and return them in the next request.
* @default false
*/
this.enableCookies = false;
/*
* Used to save and return cookies in a node.js (non-browser) setting,
* if this.enableCookies is set to true.
*/
if (typeof window === 'undefined') {
this.agent = new superagent.agent();
}
};
/**
@ -408,6 +424,16 @@
request.accept(accept);
}
// Attach previously saved cookies, if enabled
if (this.enableCookies){
if (typeof window === 'undefined') {
this.agent.attachCookies(request);
}
else {
request.withCredentials();
}
}
request.end(function(error, response) {
if (callback) {
@ -415,6 +441,9 @@
if (!error) {
try {
data = _this.deserialize(response, returnType);
if (_this.enableCookies && typeof window === 'undefined'){
_this.agent.saveCookies(response);
}
} catch (err) {
error = err;
}