From 9c7710e6959c850eba36f2784ce6ead94af23f6d Mon Sep 17 00:00:00 2001 From: Chris Jang Date: Wed, 20 Jul 2016 06:00:52 -0400 Subject: [PATCH] Factory interface for typescript-fetch (#3398) * Added Factory interface for typescript-fetch * added tests * Removed 'this.' * Added Factory interface for typescript-fetch * added tests * Removed 'this.' --- .../resources/TypeScript-Fetch/api.mustache | 21 +- .../typescript-fetch/builds/default/api.ts | 195 +++++++++++++++++- .../typescript-fetch/builds/es6-target/api.ts | 195 +++++++++++++++++- .../builds/with-npm-version/api.ts | 195 +++++++++++++++++- .../tests/default/test/PetApiFactory.ts | 58 ++++++ .../tests/default/test/StoreApiFactory.ts | 12 ++ .../tests/default/test/index.ts | 4 +- 7 files changed, 669 insertions(+), 11 deletions(-) create mode 100644 samples/client/petstore/typescript-fetch/tests/default/test/PetApiFactory.ts create mode 100644 samples/client/petstore/typescript-fetch/tests/default/test/StoreApiFactory.ts diff --git a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/api.mustache b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/api.mustache index df1b5e05ac..e8b9dd67fd 100644 --- a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/api.mustache +++ b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/api.mustache @@ -161,7 +161,26 @@ export class {{classname}} extends BaseAPI { return {{classname}}Fp.{{nickname}}({{#hasParams}}params{{/hasParams}})(this.fetch, this.basePath); } {{/operation}} -} +}; + +/** + * {{classname}} - factory interface{{#description}} + * {{&description}}{{/description}} + */ +export const {{classname}}Factory = function (fetch?: FetchAPI, basePath?: string) { + return { +{{#operation}} + /** {{#summary}} + * {{summary}}{{/summary}}{{#notes}} + * {{notes}}{{/notes}}{{#allParams}} + * @param {{paramName}} {{description}}{{/allParams}} + */ + {{nickname}}({{#hasParams}}params: { {{#allParams}} {{paramName}}{{^required}}?{{/required}}: {{{dataType}}};{{/allParams}} }{{/hasParams}}) { + return {{classname}}Fp.{{nickname}}({{#hasParams}}params{{/hasParams}})(fetch, basePath); + }, +{{/operation}} + } +}; {{/operations}} {{/apis}} diff --git a/samples/client/petstore/typescript-fetch/builds/default/api.ts b/samples/client/petstore/typescript-fetch/builds/default/api.ts index 92fdd806cc..ee15be389c 100644 --- a/samples/client/petstore/typescript-fetch/builds/default/api.ts +++ b/samples/client/petstore/typescript-fetch/builds/default/api.ts @@ -527,7 +527,84 @@ export class PetApi extends BaseAPI { uploadFile(params: { petId: number; additionalMetadata?: string; file?: any; }) { return PetApiFp.uploadFile(params)(this.fetch, this.basePath); } -} +}; + +/** + * PetApi - factory interface + */ +export const PetApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Add a new pet to the store + * + * @param body Pet object that needs to be added to the store + */ + addPet(params: { body?: Pet; }) { + return PetApiFp.addPet(params)(fetch, basePath); + }, + /** + * Deletes a pet + * + * @param petId Pet id to delete + * @param apiKey + */ + deletePet(params: { petId: number; apiKey?: string; }) { + return PetApiFp.deletePet(params)(fetch, basePath); + }, + /** + * Finds Pets by status + * Multiple status values can be provided with comma seperated strings + * @param status Status values that need to be considered for filter + */ + findPetsByStatus(params: { status?: Array; }) { + return PetApiFp.findPetsByStatus(params)(fetch, basePath); + }, + /** + * Finds Pets by tags + * Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. + * @param tags Tags to filter by + */ + findPetsByTags(params: { tags?: Array; }) { + return PetApiFp.findPetsByTags(params)(fetch, basePath); + }, + /** + * Find pet by ID + * Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions + * @param petId ID of pet that needs to be fetched + */ + getPetById(params: { petId: number; }) { + return PetApiFp.getPetById(params)(fetch, basePath); + }, + /** + * Update an existing pet + * + * @param body Pet object that needs to be added to the store + */ + updatePet(params: { body?: Pet; }) { + return PetApiFp.updatePet(params)(fetch, basePath); + }, + /** + * Updates a pet in the store with form data + * + * @param petId ID of pet that needs to be updated + * @param name Updated name of the pet + * @param status Updated status of the pet + */ + updatePetWithForm(params: { petId: string; name?: string; status?: string; }) { + return PetApiFp.updatePetWithForm(params)(fetch, basePath); + }, + /** + * uploads an image + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server + * @param file file to upload + */ + uploadFile(params: { petId: number; additionalMetadata?: string; file?: any; }) { + return PetApiFp.uploadFile(params)(fetch, basePath); + }, + } +}; /** @@ -733,7 +810,46 @@ export class StoreApi extends BaseAPI { placeOrder(params: { body?: Order; }) { return StoreApiFp.placeOrder(params)(this.fetch, this.basePath); } -} +}; + +/** + * StoreApi - factory interface + */ +export const StoreApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Delete purchase order by ID + * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + * @param orderId ID of the order that needs to be deleted + */ + deleteOrder(params: { orderId: string; }) { + return StoreApiFp.deleteOrder(params)(fetch, basePath); + }, + /** + * Returns pet inventories by status + * Returns a map of status codes to quantities + */ + getInventory() { + return StoreApiFp.getInventory()(fetch, basePath); + }, + /** + * Find purchase order by ID + * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + * @param orderId ID of pet that needs to be fetched + */ + getOrderById(params: { orderId: string; }) { + return StoreApiFp.getOrderById(params)(fetch, basePath); + }, + /** + * Place an order for a pet + * + * @param body order placed for purchasing the pet + */ + placeOrder(params: { body?: Order; }) { + return StoreApiFp.placeOrder(params)(fetch, basePath); + }, + } +}; /** @@ -1142,5 +1258,78 @@ export class UserApi extends BaseAPI { updateUser(params: { username: string; body?: User; }) { return UserApiFp.updateUser(params)(this.fetch, this.basePath); } -} +}; + +/** + * UserApi - factory interface + */ +export const UserApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Create user + * This can only be done by the logged in user. + * @param body Created user object + */ + createUser(params: { body?: User; }) { + return UserApiFp.createUser(params)(fetch, basePath); + }, + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + createUsersWithArrayInput(params: { body?: Array; }) { + return UserApiFp.createUsersWithArrayInput(params)(fetch, basePath); + }, + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + createUsersWithListInput(params: { body?: Array; }) { + return UserApiFp.createUsersWithListInput(params)(fetch, basePath); + }, + /** + * Delete user + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + */ + deleteUser(params: { username: string; }) { + return UserApiFp.deleteUser(params)(fetch, basePath); + }, + /** + * Get user by user name + * + * @param username The name that needs to be fetched. Use user1 for testing. + */ + getUserByName(params: { username: string; }) { + return UserApiFp.getUserByName(params)(fetch, basePath); + }, + /** + * Logs user into the system + * + * @param username The user name for login + * @param password The password for login in clear text + */ + loginUser(params: { username?: string; password?: string; }) { + return UserApiFp.loginUser(params)(fetch, basePath); + }, + /** + * Logs out current logged in user session + * + */ + logoutUser() { + return UserApiFp.logoutUser()(fetch, basePath); + }, + /** + * Updated user + * This can only be done by the logged in user. + * @param username name that need to be deleted + * @param body Updated user object + */ + updateUser(params: { username: string; body?: User; }) { + return UserApiFp.updateUser(params)(fetch, basePath); + }, + } +}; diff --git a/samples/client/petstore/typescript-fetch/builds/es6-target/api.ts b/samples/client/petstore/typescript-fetch/builds/es6-target/api.ts index 0788ce4afe..96b912c587 100644 --- a/samples/client/petstore/typescript-fetch/builds/es6-target/api.ts +++ b/samples/client/petstore/typescript-fetch/builds/es6-target/api.ts @@ -526,7 +526,84 @@ export class PetApi extends BaseAPI { uploadFile(params: { petId: number; additionalMetadata?: string; file?: any; }) { return PetApiFp.uploadFile(params)(this.fetch, this.basePath); } -} +}; + +/** + * PetApi - factory interface + */ +export const PetApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Add a new pet to the store + * + * @param body Pet object that needs to be added to the store + */ + addPet(params: { body?: Pet; }) { + return PetApiFp.addPet(params)(fetch, basePath); + }, + /** + * Deletes a pet + * + * @param petId Pet id to delete + * @param apiKey + */ + deletePet(params: { petId: number; apiKey?: string; }) { + return PetApiFp.deletePet(params)(fetch, basePath); + }, + /** + * Finds Pets by status + * Multiple status values can be provided with comma seperated strings + * @param status Status values that need to be considered for filter + */ + findPetsByStatus(params: { status?: Array; }) { + return PetApiFp.findPetsByStatus(params)(fetch, basePath); + }, + /** + * Finds Pets by tags + * Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. + * @param tags Tags to filter by + */ + findPetsByTags(params: { tags?: Array; }) { + return PetApiFp.findPetsByTags(params)(fetch, basePath); + }, + /** + * Find pet by ID + * Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions + * @param petId ID of pet that needs to be fetched + */ + getPetById(params: { petId: number; }) { + return PetApiFp.getPetById(params)(fetch, basePath); + }, + /** + * Update an existing pet + * + * @param body Pet object that needs to be added to the store + */ + updatePet(params: { body?: Pet; }) { + return PetApiFp.updatePet(params)(fetch, basePath); + }, + /** + * Updates a pet in the store with form data + * + * @param petId ID of pet that needs to be updated + * @param name Updated name of the pet + * @param status Updated status of the pet + */ + updatePetWithForm(params: { petId: string; name?: string; status?: string; }) { + return PetApiFp.updatePetWithForm(params)(fetch, basePath); + }, + /** + * uploads an image + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server + * @param file file to upload + */ + uploadFile(params: { petId: number; additionalMetadata?: string; file?: any; }) { + return PetApiFp.uploadFile(params)(fetch, basePath); + }, + } +}; /** @@ -732,7 +809,46 @@ export class StoreApi extends BaseAPI { placeOrder(params: { body?: Order; }) { return StoreApiFp.placeOrder(params)(this.fetch, this.basePath); } -} +}; + +/** + * StoreApi - factory interface + */ +export const StoreApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Delete purchase order by ID + * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + * @param orderId ID of the order that needs to be deleted + */ + deleteOrder(params: { orderId: string; }) { + return StoreApiFp.deleteOrder(params)(fetch, basePath); + }, + /** + * Returns pet inventories by status + * Returns a map of status codes to quantities + */ + getInventory() { + return StoreApiFp.getInventory()(fetch, basePath); + }, + /** + * Find purchase order by ID + * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + * @param orderId ID of pet that needs to be fetched + */ + getOrderById(params: { orderId: string; }) { + return StoreApiFp.getOrderById(params)(fetch, basePath); + }, + /** + * Place an order for a pet + * + * @param body order placed for purchasing the pet + */ + placeOrder(params: { body?: Order; }) { + return StoreApiFp.placeOrder(params)(fetch, basePath); + }, + } +}; /** @@ -1141,5 +1257,78 @@ export class UserApi extends BaseAPI { updateUser(params: { username: string; body?: User; }) { return UserApiFp.updateUser(params)(this.fetch, this.basePath); } -} +}; + +/** + * UserApi - factory interface + */ +export const UserApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Create user + * This can only be done by the logged in user. + * @param body Created user object + */ + createUser(params: { body?: User; }) { + return UserApiFp.createUser(params)(fetch, basePath); + }, + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + createUsersWithArrayInput(params: { body?: Array; }) { + return UserApiFp.createUsersWithArrayInput(params)(fetch, basePath); + }, + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + createUsersWithListInput(params: { body?: Array; }) { + return UserApiFp.createUsersWithListInput(params)(fetch, basePath); + }, + /** + * Delete user + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + */ + deleteUser(params: { username: string; }) { + return UserApiFp.deleteUser(params)(fetch, basePath); + }, + /** + * Get user by user name + * + * @param username The name that needs to be fetched. Use user1 for testing. + */ + getUserByName(params: { username: string; }) { + return UserApiFp.getUserByName(params)(fetch, basePath); + }, + /** + * Logs user into the system + * + * @param username The user name for login + * @param password The password for login in clear text + */ + loginUser(params: { username?: string; password?: string; }) { + return UserApiFp.loginUser(params)(fetch, basePath); + }, + /** + * Logs out current logged in user session + * + */ + logoutUser() { + return UserApiFp.logoutUser()(fetch, basePath); + }, + /** + * Updated user + * This can only be done by the logged in user. + * @param username name that need to be deleted + * @param body Updated user object + */ + updateUser(params: { username: string; body?: User; }) { + return UserApiFp.updateUser(params)(fetch, basePath); + }, + } +}; diff --git a/samples/client/petstore/typescript-fetch/builds/with-npm-version/api.ts b/samples/client/petstore/typescript-fetch/builds/with-npm-version/api.ts index 92fdd806cc..ee15be389c 100644 --- a/samples/client/petstore/typescript-fetch/builds/with-npm-version/api.ts +++ b/samples/client/petstore/typescript-fetch/builds/with-npm-version/api.ts @@ -527,7 +527,84 @@ export class PetApi extends BaseAPI { uploadFile(params: { petId: number; additionalMetadata?: string; file?: any; }) { return PetApiFp.uploadFile(params)(this.fetch, this.basePath); } -} +}; + +/** + * PetApi - factory interface + */ +export const PetApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Add a new pet to the store + * + * @param body Pet object that needs to be added to the store + */ + addPet(params: { body?: Pet; }) { + return PetApiFp.addPet(params)(fetch, basePath); + }, + /** + * Deletes a pet + * + * @param petId Pet id to delete + * @param apiKey + */ + deletePet(params: { petId: number; apiKey?: string; }) { + return PetApiFp.deletePet(params)(fetch, basePath); + }, + /** + * Finds Pets by status + * Multiple status values can be provided with comma seperated strings + * @param status Status values that need to be considered for filter + */ + findPetsByStatus(params: { status?: Array; }) { + return PetApiFp.findPetsByStatus(params)(fetch, basePath); + }, + /** + * Finds Pets by tags + * Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. + * @param tags Tags to filter by + */ + findPetsByTags(params: { tags?: Array; }) { + return PetApiFp.findPetsByTags(params)(fetch, basePath); + }, + /** + * Find pet by ID + * Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions + * @param petId ID of pet that needs to be fetched + */ + getPetById(params: { petId: number; }) { + return PetApiFp.getPetById(params)(fetch, basePath); + }, + /** + * Update an existing pet + * + * @param body Pet object that needs to be added to the store + */ + updatePet(params: { body?: Pet; }) { + return PetApiFp.updatePet(params)(fetch, basePath); + }, + /** + * Updates a pet in the store with form data + * + * @param petId ID of pet that needs to be updated + * @param name Updated name of the pet + * @param status Updated status of the pet + */ + updatePetWithForm(params: { petId: string; name?: string; status?: string; }) { + return PetApiFp.updatePetWithForm(params)(fetch, basePath); + }, + /** + * uploads an image + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server + * @param file file to upload + */ + uploadFile(params: { petId: number; additionalMetadata?: string; file?: any; }) { + return PetApiFp.uploadFile(params)(fetch, basePath); + }, + } +}; /** @@ -733,7 +810,46 @@ export class StoreApi extends BaseAPI { placeOrder(params: { body?: Order; }) { return StoreApiFp.placeOrder(params)(this.fetch, this.basePath); } -} +}; + +/** + * StoreApi - factory interface + */ +export const StoreApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Delete purchase order by ID + * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors + * @param orderId ID of the order that needs to be deleted + */ + deleteOrder(params: { orderId: string; }) { + return StoreApiFp.deleteOrder(params)(fetch, basePath); + }, + /** + * Returns pet inventories by status + * Returns a map of status codes to quantities + */ + getInventory() { + return StoreApiFp.getInventory()(fetch, basePath); + }, + /** + * Find purchase order by ID + * For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions + * @param orderId ID of pet that needs to be fetched + */ + getOrderById(params: { orderId: string; }) { + return StoreApiFp.getOrderById(params)(fetch, basePath); + }, + /** + * Place an order for a pet + * + * @param body order placed for purchasing the pet + */ + placeOrder(params: { body?: Order; }) { + return StoreApiFp.placeOrder(params)(fetch, basePath); + }, + } +}; /** @@ -1142,5 +1258,78 @@ export class UserApi extends BaseAPI { updateUser(params: { username: string; body?: User; }) { return UserApiFp.updateUser(params)(this.fetch, this.basePath); } -} +}; + +/** + * UserApi - factory interface + */ +export const UserApiFactory = function (fetch?: FetchAPI, basePath?: string) { + return { + /** + * Create user + * This can only be done by the logged in user. + * @param body Created user object + */ + createUser(params: { body?: User; }) { + return UserApiFp.createUser(params)(fetch, basePath); + }, + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + createUsersWithArrayInput(params: { body?: Array; }) { + return UserApiFp.createUsersWithArrayInput(params)(fetch, basePath); + }, + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + createUsersWithListInput(params: { body?: Array; }) { + return UserApiFp.createUsersWithListInput(params)(fetch, basePath); + }, + /** + * Delete user + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + */ + deleteUser(params: { username: string; }) { + return UserApiFp.deleteUser(params)(fetch, basePath); + }, + /** + * Get user by user name + * + * @param username The name that needs to be fetched. Use user1 for testing. + */ + getUserByName(params: { username: string; }) { + return UserApiFp.getUserByName(params)(fetch, basePath); + }, + /** + * Logs user into the system + * + * @param username The user name for login + * @param password The password for login in clear text + */ + loginUser(params: { username?: string; password?: string; }) { + return UserApiFp.loginUser(params)(fetch, basePath); + }, + /** + * Logs out current logged in user session + * + */ + logoutUser() { + return UserApiFp.logoutUser()(fetch, basePath); + }, + /** + * Updated user + * This can only be done by the logged in user. + * @param username name that need to be deleted + * @param body Updated user object + */ + updateUser(params: { username: string; body?: User; }) { + return UserApiFp.updateUser(params)(fetch, basePath); + }, + } +}; diff --git a/samples/client/petstore/typescript-fetch/tests/default/test/PetApiFactory.ts b/samples/client/petstore/typescript-fetch/tests/default/test/PetApiFactory.ts new file mode 100644 index 0000000000..38acc214e2 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/tests/default/test/PetApiFactory.ts @@ -0,0 +1,58 @@ +import {expect} from 'chai'; +import {PetApiFactory, Pet, Category} from 'typescript-fetch-api'; + +describe('PetApiFactory', () => { + let fixture: Pet = createTestFixture(); + + it('should add and delete Pet', () => { + return PetApiFactory().addPet({ body: fixture }).then(() => { + }); + }); + + it('should get Pet by ID', () => { + return PetApiFactory().getPetById({ petId: fixture.id }).then((result) => { + return expect(result).to.deep.equal(fixture); + }); + }); + + it('should update Pet by ID', () => { + return PetApiFactory().getPetById({ petId: fixture.id }).then( (result) => { + result.name = 'newname'; + return PetApiFactory().updatePet({ body: result }).then(() => { + return PetApiFactory().getPetById({ petId: fixture.id }).then( (result) => { + return expect(result.name).to.deep.equal('newname'); + }); + }); + }); + }); + + it('should delete Pet', () => { + return PetApiFactory().deletePet({ petId: fixture.id }); + }); + + it('should not contain deleted Pet', () => { + return PetApiFactory().getPetById({ petId: fixture.id }).then((result) => { + return expect(result).to.not.exist; + }, (err) => { + return expect(err).to.exist; + }); + }); +}); + +function createTestFixture(ts = Date.now()) { + const category: Category = { + 'id': ts, + 'name': `category${ts}`, + }; + + const pet: Pet = { + 'id': ts, + 'name': `pet${ts}`, + 'category': category, + 'photoUrls': ['http://foo.bar.com/1', 'http://foo.bar.com/2'], + 'status': 'available', + 'tags': [] + }; + + return pet; +}; diff --git a/samples/client/petstore/typescript-fetch/tests/default/test/StoreApiFactory.ts b/samples/client/petstore/typescript-fetch/tests/default/test/StoreApiFactory.ts new file mode 100644 index 0000000000..c1d5560e2a --- /dev/null +++ b/samples/client/petstore/typescript-fetch/tests/default/test/StoreApiFactory.ts @@ -0,0 +1,12 @@ +import {expect} from 'chai'; +import {StoreApiFactory} from 'typescript-fetch-api'; + +describe('StoreApiFactory', function() { + it('should get inventory', function() { + return StoreApiFactory().getInventory().then((result) => { + expect(Object.keys(result)).to.not.be.empty; + }); + }); + +}); + diff --git a/samples/client/petstore/typescript-fetch/tests/default/test/index.ts b/samples/client/petstore/typescript-fetch/tests/default/test/index.ts index 29369a2d0e..e3b9cbf4a5 100644 --- a/samples/client/petstore/typescript-fetch/tests/default/test/index.ts +++ b/samples/client/petstore/typescript-fetch/tests/default/test/index.ts @@ -1,2 +1,4 @@ import './PetApi'; -import './StoreApi'; \ No newline at end of file +import './StoreApi'; +import './PetApiFactory'; +import './StoreApiFactory'; \ No newline at end of file