From 5935e2e6d9de1d2c4ead65cd62ad99c633a68fef Mon Sep 17 00:00:00 2001 From: delenius Date: Tue, 7 Mar 2017 23:26:46 -0800 Subject: [PATCH] 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. --- .../resources/Javascript/ApiClient.mustache | 32 ++++++++++++++ .../javascript-promise/src/ApiClient.js | 44 ++++++++++++++++--- .../petstore/javascript/src/ApiClient.js | 29 ++++++++++++ 3 files changed, 99 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache index ffe6c32dc8f..e5af8ff80a5 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache @@ -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; } diff --git a/samples/client/petstore/javascript-promise/src/ApiClient.js b/samples/client/petstore/javascript-promise/src/ApiClient.js index 6a64ce46bc7..4b3872b980e 100644 --- a/samples/client/petstore/javascript-promise/src/ApiClient.js +++ b/samples/client/petstore/javascript-promise/src/ApiClient.js @@ -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 data 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); diff --git a/samples/client/petstore/javascript/src/ApiClient.js b/samples/client/petstore/javascript/src/ApiClient.js index c3be4990de9..017b50b1335 100644 --- a/samples/client/petstore/javascript/src/ApiClient.js +++ b/samples/client/petstore/javascript/src/ApiClient.js @@ -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; }