Make JavaScript client work in both Node.js and browser

* Replace jQuery with SuperAgent which works in both Node.js and browser
* Use UMD pattern (returnExports.js) to make the module exporting compatible
  with all major systems: AMD, Node.js (CommonJS) and browser
* Implement support of header and form parameters. Closes #1736
* Move HTTP requesting code to `ApiClient` and allow customizing options
  in it, e.g. "basePath"
* Update unit tests accordingly and add some tests for `ApiClient`
This commit is contained in:
xhh
2016-01-19 10:46:20 +08:00
parent 0fa1b1a9f9
commit 14630c7632
22 changed files with 3575 additions and 2106 deletions

View File

@@ -1,25 +1,21 @@
if (typeof module === 'object' && module.exports) {
var expect = require('expect.js');
var requireApiWithMocks = require('../helper.js').requireApiWithMocks;
var PetApi = requireApiWithMocks('PetApi');
var Pet = require('../../src/model/Pet');
var Category = require('../../src/model/Category');
var Tag = require('../../src/model/Tag');
var SwaggerPetstore = require('../../src/index');
}
var api;
beforeEach(function() {
api = new PetApi();
api = new SwaggerPetstore.PetApi();
});
var createRandomPet = function() {
var id = new Date().getTime();
var pet = new Pet();
var pet = new SwaggerPetstore.Pet();
pet.setId(id);
pet.setName("gorilla" + id);
var category = new Category();
var category = new SwaggerPetstore.Category();
category.setName("really-happy");
pet.setCategory(category);
@@ -31,13 +27,17 @@ var createRandomPet = function() {
};
describe('PetApi', function() {
it('should create and get pet', function (done) {
it('should create and get pet', function(done) {
var pet = createRandomPet();
api.addPet(pet).then(function() {
api.getPetById(pet.id, function(fetched, textStatus, jqXHR, error) {
if (error) throw error;
api.addPet(pet, function(error) {
if (error) throw error;
api.getPetById(pet.id, function(error, fetched, response) {
if (error) throw error;
expect(response.status).to.be(200);
expect(response.ok).to.be(true);
expect(response.get('Content-Type')).to.be('application/json');
expect(textStatus).to.be('success');
expect(fetched).to.be.ok();
expect(fetched.id).to.be(pet.id);
expect(fetched.getCategory()).to.be.ok();
@@ -46,8 +46,6 @@ describe('PetApi', function() {
api.deletePet(pet.id);
done();
});
}, function(jqXHR, textStatus, errorThrown) {
throw errorThrown || textStatus;
});
});
});