From 40d83e7f527e320496c58720d56b938f47ef44c3 Mon Sep 17 00:00:00 2001 From: xhh Date: Tue, 19 Jan 2016 22:52:05 +0800 Subject: [PATCH] Some cleanup --- .../main/resources/Javascript/api.mustache | 4 +- .../main/resources/Javascript/index.mustache | 2 +- .../main/resources/Javascript/model.mustache | 2 +- .../petstore/javascript/src/api/PetApi.js | 4 +- .../petstore/javascript/src/api/StoreApi.js | 4 +- .../petstore/javascript/src/api/UserApi.js | 4 +- .../client/petstore/javascript/src/index.js | 2 +- .../petstore/javascript/src/model/Pet.js | 2 +- .../petstore/javascript/test/superagent.js | 1396 ----------------- 9 files changed, 12 insertions(+), 1408 deletions(-) delete mode 100644 samples/client/petstore/javascript/test/superagent.js diff --git a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache index a61e491259..a0ac34c48d 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/api.mustache @@ -4,7 +4,7 @@ define(['../ApiClient'{{#imports}}, '../model/{{import}}'{{/imports}}], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(require('../ApiClient.js'){{#imports}}, require('../model/{{import}}.js'){{/imports}}); + module.exports = factory(require('../ApiClient'){{#imports}}, require('../model/{{import}}'){{/imports}}); } else { // Browser globals (root is window) if (!root.{{moduleName}}) { @@ -18,7 +18,7 @@ var {{classname}} = function {{classname}}(apiClient) { this.apiClient = apiClient || ApiClient.default; - var self = this; + var self = this; {{#operations}} {{#operation}} /** diff --git a/modules/swagger-codegen/src/main/resources/Javascript/index.mustache b/modules/swagger-codegen/src/main/resources/Javascript/index.mustache index 7bb0329fa0..5d03b68aed 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/index.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/index.mustache @@ -4,7 +4,7 @@ define(['./ApiClient'{{#models}}, './model/{{importPath}}'{{/models}}{{#apiInfo}}{{#apis}}, './api/{{importPath}}'{{/apis}}{{/apiInfo}}], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(require('./ApiClient.js'){{#models}}, require('./model/{{importPath}}.js'){{/models}}{{#apiInfo}}{{#apis}}, require('./api/{{importPath}}.js'){{/apis}}{{/apiInfo}}); + module.exports = factory(require('./ApiClient'){{#models}}, require('./model/{{importPath}}'){{/models}}{{#apiInfo}}{{#apis}}, require('./api/{{importPath}}'){{/apis}}{{/apiInfo}}); } }(function(ApiClient{{#models}}, {{importPath}}{{/models}}{{#apiInfo}}{{#apis}}, {{importPath}}{{/apis}}{{/apiInfo}}) { 'use strict'; diff --git a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache index 9f265c89cf..a5778003c9 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache @@ -4,7 +4,7 @@ define([undefined{{#imports}}, './{{import}}'{{/imports}}], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(undefined{{#imports}}, require('./{{import}}.js'){{/imports}}); + module.exports = factory(undefined{{#imports}}, require('./{{import}}'){{/imports}}); } else { // Browser globals (root is window) if (!root.{{moduleName}}) { diff --git a/samples/client/petstore/javascript/src/api/PetApi.js b/samples/client/petstore/javascript/src/api/PetApi.js index b6c1163111..281fa1e214 100644 --- a/samples/client/petstore/javascript/src/api/PetApi.js +++ b/samples/client/petstore/javascript/src/api/PetApi.js @@ -4,7 +4,7 @@ define(['../ApiClient', '../model/Pet'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(require('../ApiClient.js'), require('../model/Pet.js')); + module.exports = factory(require('../ApiClient'), require('../model/Pet')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { @@ -18,7 +18,7 @@ var PetApi = function PetApi(apiClient) { this.apiClient = apiClient || ApiClient.default; - var self = this; + var self = this; /** diff --git a/samples/client/petstore/javascript/src/api/StoreApi.js b/samples/client/petstore/javascript/src/api/StoreApi.js index 28928fd40c..7ef38358e9 100644 --- a/samples/client/petstore/javascript/src/api/StoreApi.js +++ b/samples/client/petstore/javascript/src/api/StoreApi.js @@ -4,7 +4,7 @@ define(['../ApiClient', '../model/Order'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(require('../ApiClient.js'), require('../model/Order.js')); + module.exports = factory(require('../ApiClient'), require('../model/Order')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { @@ -18,7 +18,7 @@ var StoreApi = function StoreApi(apiClient) { this.apiClient = apiClient || ApiClient.default; - var self = this; + var self = this; /** diff --git a/samples/client/petstore/javascript/src/api/UserApi.js b/samples/client/petstore/javascript/src/api/UserApi.js index 656bf56dd8..528214b2f8 100644 --- a/samples/client/petstore/javascript/src/api/UserApi.js +++ b/samples/client/petstore/javascript/src/api/UserApi.js @@ -4,7 +4,7 @@ define(['../ApiClient', '../model/User'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(require('../ApiClient.js'), require('../model/User.js')); + module.exports = factory(require('../ApiClient'), require('../model/User')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { @@ -18,7 +18,7 @@ var UserApi = function UserApi(apiClient) { this.apiClient = apiClient || ApiClient.default; - var self = this; + var self = this; /** diff --git a/samples/client/petstore/javascript/src/index.js b/samples/client/petstore/javascript/src/index.js index 725eb48607..a6b290f5e6 100644 --- a/samples/client/petstore/javascript/src/index.js +++ b/samples/client/petstore/javascript/src/index.js @@ -4,7 +4,7 @@ define(['./ApiClient', './model/User', './model/Category', './model/Pet', './model/Tag', './model/Order', './api/UserApi', './api/StoreApi', './api/PetApi'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(require('./ApiClient.js'), require('./model/User.js'), require('./model/Category.js'), require('./model/Pet.js'), require('./model/Tag.js'), require('./model/Order.js'), require('./api/UserApi.js'), require('./api/StoreApi.js'), require('./api/PetApi.js')); + module.exports = factory(require('./ApiClient'), require('./model/User'), require('./model/Category'), require('./model/Pet'), require('./model/Tag'), require('./model/Order'), require('./api/UserApi'), require('./api/StoreApi'), require('./api/PetApi')); } }(function(ApiClient, User, Category, Pet, Tag, Order, UserApi, StoreApi, PetApi) { 'use strict'; diff --git a/samples/client/petstore/javascript/src/model/Pet.js b/samples/client/petstore/javascript/src/model/Pet.js index 21de326507..88674c64bc 100644 --- a/samples/client/petstore/javascript/src/model/Pet.js +++ b/samples/client/petstore/javascript/src/model/Pet.js @@ -4,7 +4,7 @@ define([undefined, './Category', './Tag'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(undefined, require('./Category.js'), require('./Tag.js')); + module.exports = factory(undefined, require('./Category'), require('./Tag')); } else { // Browser globals (root is window) if (!root.SwaggerPetstore) { diff --git a/samples/client/petstore/javascript/test/superagent.js b/samples/client/petstore/javascript/test/superagent.js deleted file mode 100644 index eb66194d88..0000000000 --- a/samples/client/petstore/javascript/test/superagent.js +++ /dev/null @@ -1,1396 +0,0 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.superagent = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o= 200 && res.status < 300) { - return self.callback(err, res); - } - - var new_err = new Error(res.statusText || 'Unsuccessful HTTP response'); - new_err.original = err; - new_err.response = res; - new_err.status = res.status; - - self.callback(new_err, res); - }); -} - -/** - * Mixin `Emitter`. - */ - -Emitter(Request.prototype); - -/** - * Allow for extension - */ - -Request.prototype.use = function(fn) { - fn(this); - return this; -} - -/** - * Set timeout to `ms`. - * - * @param {Number} ms - * @return {Request} for chaining - * @api public - */ - -Request.prototype.timeout = function(ms){ - this._timeout = ms; - return this; -}; - -/** - * Clear previous timeout. - * - * @return {Request} for chaining - * @api public - */ - -Request.prototype.clearTimeout = function(){ - this._timeout = 0; - clearTimeout(this._timer); - return this; -}; - -/** - * Abort the request, and clear potential timeout. - * - * @return {Request} - * @api public - */ - -Request.prototype.abort = function(){ - if (this.aborted) return; - this.aborted = true; - this.xhr.abort(); - this.clearTimeout(); - this.emit('abort'); - return this; -}; - -/** - * Set header `field` to `val`, or multiple fields with one object. - * - * Examples: - * - * req.get('/') - * .set('Accept', 'application/json') - * .set('X-API-Key', 'foobar') - * .end(callback); - * - * req.get('/') - * .set({ Accept: 'application/json', 'X-API-Key': 'foobar' }) - * .end(callback); - * - * @param {String|Object} field - * @param {String} val - * @return {Request} for chaining - * @api public - */ - -Request.prototype.set = function(field, val){ - if (isObject(field)) { - for (var key in field) { - this.set(key, field[key]); - } - return this; - } - this._header[field.toLowerCase()] = val; - this.header[field] = val; - return this; -}; - -/** - * Remove header `field`. - * - * Example: - * - * req.get('/') - * .unset('User-Agent') - * .end(callback); - * - * @param {String} field - * @return {Request} for chaining - * @api public - */ - -Request.prototype.unset = function(field){ - delete this._header[field.toLowerCase()]; - delete this.header[field]; - return this; -}; - -/** - * Get case-insensitive header `field` value. - * - * @param {String} field - * @return {String} - * @api private - */ - -Request.prototype.getHeader = function(field){ - return this._header[field.toLowerCase()]; -}; - -/** - * Set Content-Type to `type`, mapping values from `request.types`. - * - * Examples: - * - * superagent.types.xml = 'application/xml'; - * - * request.post('/') - * .type('xml') - * .send(xmlstring) - * .end(callback); - * - * request.post('/') - * .type('application/xml') - * .send(xmlstring) - * .end(callback); - * - * @param {String} type - * @return {Request} for chaining - * @api public - */ - -Request.prototype.type = function(type){ - this.set('Content-Type', request.types[type] || type); - return this; -}; - -/** - * Force given parser - * - * Sets the body parser no matter type. - * - * @param {Function} - * @api public - */ - -Request.prototype.parse = function(fn){ - this._parser = fn; - return this; -}; - -/** - * Set Accept to `type`, mapping values from `request.types`. - * - * Examples: - * - * superagent.types.json = 'application/json'; - * - * request.get('/agent') - * .accept('json') - * .end(callback); - * - * request.get('/agent') - * .accept('application/json') - * .end(callback); - * - * @param {String} accept - * @return {Request} for chaining - * @api public - */ - -Request.prototype.accept = function(type){ - this.set('Accept', request.types[type] || type); - return this; -}; - -/** - * Set Authorization field value with `user` and `pass`. - * - * @param {String} user - * @param {String} pass - * @return {Request} for chaining - * @api public - */ - -Request.prototype.auth = function(user, pass){ - var str = btoa(user + ':' + pass); - this.set('Authorization', 'Basic ' + str); - return this; -}; - -/** -* Add query-string `val`. -* -* Examples: -* -* request.get('/shoes') -* .query('size=10') -* .query({ color: 'blue' }) -* -* @param {Object|String} val -* @return {Request} for chaining -* @api public -*/ - -Request.prototype.query = function(val){ - if ('string' != typeof val) val = serialize(val); - if (val) this._query.push(val); - return this; -}; - -/** - * Write the field `name` and `val` for "multipart/form-data" - * request bodies. - * - * ``` js - * request.post('/upload') - * .field('foo', 'bar') - * .end(callback); - * ``` - * - * @param {String} name - * @param {String|Blob|File} val - * @return {Request} for chaining - * @api public - */ - -Request.prototype.field = function(name, val){ - if (!this._formData) this._formData = new root.FormData(); - this._formData.append(name, val); - return this; -}; - -/** - * Queue the given `file` as an attachment to the specified `field`, - * with optional `filename`. - * - * ``` js - * request.post('/upload') - * .attach(new Blob(['hey!'], { type: "text/html"})) - * .end(callback); - * ``` - * - * @param {String} field - * @param {Blob|File} file - * @param {String} filename - * @return {Request} for chaining - * @api public - */ - -Request.prototype.attach = function(field, file, filename){ - if (!this._formData) this._formData = new root.FormData(); - this._formData.append(field, file, filename); - return this; -}; - -/** - * Send `data`, defaulting the `.type()` to "json" when - * an object is given. - * - * Examples: - * - * // querystring - * request.get('/search') - * .end(callback) - * - * // multiple data "writes" - * request.get('/search') - * .send({ search: 'query' }) - * .send({ range: '1..5' }) - * .send({ order: 'desc' }) - * .end(callback) - * - * // manual json - * request.post('/user') - * .type('json') - * .send('{"name":"tj"}') - * .end(callback) - * - * // auto json - * request.post('/user') - * .send({ name: 'tj' }) - * .end(callback) - * - * // manual x-www-form-urlencoded - * request.post('/user') - * .type('form') - * .send('name=tj') - * .end(callback) - * - * // auto x-www-form-urlencoded - * request.post('/user') - * .type('form') - * .send({ name: 'tj' }) - * .end(callback) - * - * // defaults to x-www-form-urlencoded - * request.post('/user') - * .send('name=tobi') - * .send('species=ferret') - * .end(callback) - * - * @param {String|Object} data - * @return {Request} for chaining - * @api public - */ - -Request.prototype.send = function(data){ - var obj = isObject(data); - var type = this.getHeader('Content-Type'); - - // merge - if (obj && isObject(this._data)) { - for (var key in data) { - this._data[key] = data[key]; - } - } else if ('string' == typeof data) { - if (!type) this.type('form'); - type = this.getHeader('Content-Type'); - if ('application/x-www-form-urlencoded' == type) { - this._data = this._data - ? this._data + '&' + data - : data; - } else { - this._data = (this._data || '') + data; - } - } else { - this._data = data; - } - - if (!obj || isHost(data)) return this; - if (!type) this.type('json'); - return this; -}; - -/** - * Invoke the callback with `err` and `res` - * and handle arity check. - * - * @param {Error} err - * @param {Response} res - * @api private - */ - -Request.prototype.callback = function(err, res){ - var fn = this._callback; - this.clearTimeout(); - fn(err, res); -}; - -/** - * Invoke callback with x-domain error. - * - * @api private - */ - -Request.prototype.crossDomainError = function(){ - var err = new Error('Request has been terminated\nPossible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.'); - err.crossDomain = true; - - err.status = this.status; - err.method = this.method; - err.url = this.url; - - this.callback(err); -}; - -/** - * Invoke callback with timeout error. - * - * @api private - */ - -Request.prototype.timeoutError = function(){ - var timeout = this._timeout; - var err = new Error('timeout of ' + timeout + 'ms exceeded'); - err.timeout = timeout; - this.callback(err); -}; - -/** - * Enable transmission of cookies with x-domain requests. - * - * Note that for this to work the origin must not be - * using "Access-Control-Allow-Origin" with a wildcard, - * and also must set "Access-Control-Allow-Credentials" - * to "true". - * - * @api public - */ - -Request.prototype.withCredentials = function(){ - this._withCredentials = true; - return this; -}; - -/** - * Initiate request, invoking callback `fn(res)` - * with an instanceof `Response`. - * - * @param {Function} fn - * @return {Request} for chaining - * @api public - */ - -Request.prototype.end = function(fn){ - var self = this; - var xhr = this.xhr = request.getXHR(); - var query = this._query.join('&'); - var timeout = this._timeout; - var data = this._formData || this._data; - - // store callback - this._callback = fn || noop; - - // state change - xhr.onreadystatechange = function(){ - if (4 != xhr.readyState) return; - - // In IE9, reads to any property (e.g. status) off of an aborted XHR will - // result in the error "Could not complete the operation due to error c00c023f" - var status; - try { status = xhr.status } catch(e) { status = 0; } - - if (0 == status) { - if (self.timedout) return self.timeoutError(); - if (self.aborted) return; - return self.crossDomainError(); - } - self.emit('end'); - }; - - // progress - var handleProgress = function(e){ - if (e.total > 0) { - e.percent = e.loaded / e.total * 100; - } - self.emit('progress', e); - }; - if (this.hasListeners('progress')) { - xhr.onprogress = handleProgress; - } - try { - if (xhr.upload && this.hasListeners('progress')) { - xhr.upload.onprogress = handleProgress; - } - } catch(e) { - // Accessing xhr.upload fails in IE from a web worker, so just pretend it doesn't exist. - // Reported here: - // https://connect.microsoft.com/IE/feedback/details/837245/xmlhttprequest-upload-throws-invalid-argument-when-used-from-web-worker-context - } - - // timeout - if (timeout && !this._timer) { - this._timer = setTimeout(function(){ - self.timedout = true; - self.abort(); - }, timeout); - } - - // querystring - if (query) { - query = request.serializeObject(query); - this.url += ~this.url.indexOf('?') - ? '&' + query - : '?' + query; - } - - // initiate request - xhr.open(this.method, this.url, true); - - // CORS - if (this._withCredentials) xhr.withCredentials = true; - - // body - if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !isHost(data)) { - // serialize stuff - var contentType = this.getHeader('Content-Type'); - var serialize = this._parser || request.serialize[contentType ? contentType.split(';')[0] : '']; - if (!serialize && isJSON(contentType)) serialize = request.serialize['application/json']; - if (serialize) data = serialize(data); - } - - // set header fields - for (var field in this.header) { - if (null == this.header[field]) continue; - xhr.setRequestHeader(field, this.header[field]); - } - - // send stuff - this.emit('request', this); - - // IE11 xhr.send(undefined) sends 'undefined' string as POST payload (instead of nothing) - // We need null here if data is undefined - xhr.send(typeof data !== 'undefined' ? data : null); - return this; -}; - -/** - * Faux promise support - * - * @param {Function} fulfill - * @param {Function} reject - * @return {Request} - */ - -Request.prototype.then = function (fulfill, reject) { - return this.end(function(err, res) { - err ? reject(err) : fulfill(res); - }); -} - -/** - * Expose `Request`. - */ - -request.Request = Request; - -/** - * Issue a request: - * - * Examples: - * - * request('GET', '/users').end(callback) - * request('/users').end(callback) - * request('/users', callback) - * - * @param {String} method - * @param {String|Function} url or callback - * @return {Request} - * @api public - */ - -function request(method, url) { - // callback - if ('function' == typeof url) { - return new Request('GET', method).end(url); - } - - // url first - if (1 == arguments.length) { - return new Request('GET', method); - } - - return new Request(method, url); -} - -/** - * GET `url` with optional callback `fn(res)`. - * - * @param {String} url - * @param {Mixed|Function} data or fn - * @param {Function} fn - * @return {Request} - * @api public - */ - -request.get = function(url, data, fn){ - var req = request('GET', url); - if ('function' == typeof data) fn = data, data = null; - if (data) req.query(data); - if (fn) req.end(fn); - return req; -}; - -/** - * HEAD `url` with optional callback `fn(res)`. - * - * @param {String} url - * @param {Mixed|Function} data or fn - * @param {Function} fn - * @return {Request} - * @api public - */ - -request.head = function(url, data, fn){ - var req = request('HEAD', url); - if ('function' == typeof data) fn = data, data = null; - if (data) req.send(data); - if (fn) req.end(fn); - return req; -}; - -/** - * DELETE `url` with optional callback `fn(res)`. - * - * @param {String} url - * @param {Function} fn - * @return {Request} - * @api public - */ - -function del(url, fn){ - var req = request('DELETE', url); - if (fn) req.end(fn); - return req; -}; - -request.del = del; -request.delete = del; - -/** - * PATCH `url` with optional `data` and callback `fn(res)`. - * - * @param {String} url - * @param {Mixed} data - * @param {Function} fn - * @return {Request} - * @api public - */ - -request.patch = function(url, data, fn){ - var req = request('PATCH', url); - if ('function' == typeof data) fn = data, data = null; - if (data) req.send(data); - if (fn) req.end(fn); - return req; -}; - -/** - * POST `url` with optional `data` and callback `fn(res)`. - * - * @param {String} url - * @param {Mixed} data - * @param {Function} fn - * @return {Request} - * @api public - */ - -request.post = function(url, data, fn){ - var req = request('POST', url); - if ('function' == typeof data) fn = data, data = null; - if (data) req.send(data); - if (fn) req.end(fn); - return req; -}; - -/** - * PUT `url` with optional `data` and callback `fn(res)`. - * - * @param {String} url - * @param {Mixed|Function} data or fn - * @param {Function} fn - * @return {Request} - * @api public - */ - -request.put = function(url, data, fn){ - var req = request('PUT', url); - if ('function' == typeof data) fn = data, data = null; - if (data) req.send(data); - if (fn) req.end(fn); - return req; -}; - -/** - * Expose `request`. - */ - -module.exports = request; - -},{"emitter":1,"reduce":2}]},{},[])("superagent") -}); \ No newline at end of file