From 3185606124afdd878aae16e34560b01ead8d4141 Mon Sep 17 00:00:00 2001 From: Leon Yu Date: Mon, 9 May 2016 16:37:36 -0400 Subject: [PATCH 1/2] Update typings Expose type definition Add readme --- .../TypeScriptFetchClientCodegen.java | 7 ++- .../main/resources/TypeScript-Fetch/README.md | 44 +++++++++++++++++++ .../TypeScript-Fetch/package.json.mustache | 5 ++- .../TypeScript-Fetch/tsconfig.json.mustache | 2 + .../TypeScript-Fetch/typings.json.mustache | 2 +- 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/TypeScript-Fetch/README.md diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptFetchClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptFetchClientCodegen.java index 5bc848a7c7..c817162401 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptFetchClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptFetchClientCodegen.java @@ -24,11 +24,10 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege @Override public void processOpts() { super.processOpts(); - final String defaultFolder = apiPackage().replace('.', File.separatorChar); - - supportingFiles.add(new SupportingFile("api.mustache", null, "api.ts")); + supportingFiles.add(new SupportingFile("api.mustache", "", "api.ts")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); - supportingFiles.add(new SupportingFile("assign.ts", defaultFolder, "assign.ts")); + supportingFiles.add(new SupportingFile("assign.ts", "", "assign.ts")); + supportingFiles.add(new SupportingFile("README.md", "", "README.md")); supportingFiles.add(new SupportingFile("package.json.mustache", "", "package.json")); supportingFiles.add(new SupportingFile("typings.json.mustache", "", "typings.json")); supportingFiles.add(new SupportingFile("tsconfig.json.mustache", "", "tsconfig.json")); diff --git a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/README.md b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/README.md new file mode 100644 index 0000000000..8ec43e7649 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/README.md @@ -0,0 +1,44 @@ +# TypeScript-Fetch + +This generator creates TypeScript/JavaScript client that utilizes [Fetch API](https://fetch.spec.whatwg.org/). The codegen Node module can be used in the following environments: + +* Node.JS +* Webpack +* Browserify + +It is usable in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `typings` in `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html)) + +### Installation ### + +`swagger-codegen` does not generate JavaScript directly. The codegen Node module comes with `package.json` that bundles `typescript` and `typings` so it can self-compile. The self-compile is normally run automatically via the `npm` `postinstall` script of `npm install`. + +CAVEAT: Due to [privilege implications](https://docs.npmjs.com/misc/scripts#user), `npm` may skip `postinstall` script if the user is `root`. You would need to manually invoke `npm install` or `npm run postinstall` for the codegen module if that's the case. + +#### NPM repository ### +If you remove `"private": true` from `package.json`, you may publish the module to NPM. In which case, you would be able to install the module as any other NPM module. + +It maybe useful to use [scoped packages](https://docs.npmjs.com/misc/scope). + +#### NPM install local file ### +You should be able to directly install the module using `npm install file:///codegen_path`. + +NOTES: If you do `npm install file:///codegen_path --save` NPM might convert your path to relative path, maybe have adverse affect. `npm install` and `npm shrinkwrap` may misbehave if the installation path is not absolute. + +#### direct copy/symlink ### +You may also simply copy or symlink the codegen into a directly under your project. The syntax of the usage would differ if you take this route. (See below) + +### Usage ### +With ES6 module syntax, the following syntaxes are supported: +``` +import * as localName from 'npmName'; +import {operationId} from 'npmName'; + +import * as localName from './symlinkDir'; +import {operationId} from './symlinkDir'; +``` +With CommonJS, the following syntaxes are supported: +``` +import localName = require('npmName'); + +import localName = require('./symlinkDir')'; +``` \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/package.json.mustache b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/package.json.mustache index 7e94923c14..e6379434b8 100644 --- a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/package.json.mustache +++ b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/package.json.mustache @@ -2,8 +2,9 @@ "name": "{{#npmName}}{{{npmName}}}{{/npmName}}{{^npmName}}typescript-fetch-api{{/npmName}}", "version": "{{#npmVersion}}{{{npmVersion}}}{{/npmVersion}}{{^npmVersion}}0.0.0{{/npmVersion}}", "private": true, - "main": "dist/api.js", - "browser": "dist/api.js", + "main": "./dist/api.js", + "browser": "./dist/api.js", + "typings": "./dist/api.d.ts", "dependencies": { "isomorphic-fetch": "^2.2.1" }, diff --git a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/tsconfig.json.mustache b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/tsconfig.json.mustache index 457b7f53db..06a057d7a4 100644 --- a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/tsconfig.json.mustache +++ b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/tsconfig.json.mustache @@ -1,11 +1,13 @@ { "compilerOptions": { + "declaration": true, "target": "{{#supportsES6}}es6{{/supportsES6}}{{^supportsES6}}es5{{/supportsES6}}", "module": "commonjs", "noImplicitAny": true, "outDir": "dist" }, "exclude": [ + "dist", "node_modules", "typings/browser", "typings/main", diff --git a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/typings.json.mustache b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/typings.json.mustache index 1d1b679de8..38baf589fb 100644 --- a/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/typings.json.mustache +++ b/modules/swagger-codegen/src/main/resources/TypeScript-Fetch/typings.json.mustache @@ -4,6 +4,6 @@ "ambientDependencies": { {{^supportsES6}} "es6-promise": "registry:dt/es6-promise#0.0.0+20160423074304", {{/supportsES6}} "node": "registry:dt/node#4.0.0+20160423143914", - "isomorphic-fetch": "github:leonyu/DefinitelyTyped/isomorphic-fetch/isomorphic-fetch.d.ts#isomorphic-fetch-fix-module" + "isomorphic-fetch": "registry:dt/isomorphic-fetch#0.0.0+20160505171433" } } From cd6f5877ac7ebb08624bf5bac877ed704ac8666c Mon Sep 17 00:00:00 2001 From: wing328 Date: Tue, 10 May 2016 18:01:17 +0800 Subject: [PATCH 2/2] add bin/typescript-fetch-petstore-all.sh --- .gitignore | 10 + bin/typescript-fetch-petstore-all.sh | 5 + .../typescript-fetch/default-es6/README.md | 44 + .../typescript-fetch/default-es6/api.ts | 855 ++++++++++++++++++ .../typescript-fetch/default-es6/assign.ts | 18 + .../typescript-fetch/default-es6/git_push.sh | 52 ++ .../typescript-fetch/default-es6/package.json | 18 + .../default-es6/tsconfig.json | 16 + .../typescript-fetch/default-es6/typings.json | 8 + .../typescript-fetch/default/README.md | 44 + .../petstore/typescript-fetch/default/api.ts | 855 ++++++++++++++++++ .../typescript-fetch/default/assign.ts | 18 + .../typescript-fetch/default/git_push.sh | 52 ++ .../typescript-fetch/default/package.json | 18 + .../typescript-fetch/default/tsconfig.json | 16 + .../typescript-fetch/default/typings.json | 9 + .../with-package-metadata/README.md | 44 + .../with-package-metadata/api.ts | 855 ++++++++++++++++++ .../with-package-metadata/assign.ts | 18 + .../with-package-metadata/git_push.sh | 52 ++ .../with-package-metadata/package.json | 18 + .../with-package-metadata/tsconfig.json | 16 + .../with-package-metadata/typings.json | 9 + 23 files changed, 3050 insertions(+) create mode 100755 bin/typescript-fetch-petstore-all.sh create mode 100644 samples/client/petstore/typescript-fetch/default-es6/README.md create mode 100644 samples/client/petstore/typescript-fetch/default-es6/api.ts create mode 100644 samples/client/petstore/typescript-fetch/default-es6/assign.ts create mode 100644 samples/client/petstore/typescript-fetch/default-es6/git_push.sh create mode 100644 samples/client/petstore/typescript-fetch/default-es6/package.json create mode 100644 samples/client/petstore/typescript-fetch/default-es6/tsconfig.json create mode 100644 samples/client/petstore/typescript-fetch/default-es6/typings.json create mode 100644 samples/client/petstore/typescript-fetch/default/README.md create mode 100644 samples/client/petstore/typescript-fetch/default/api.ts create mode 100644 samples/client/petstore/typescript-fetch/default/assign.ts create mode 100644 samples/client/petstore/typescript-fetch/default/git_push.sh create mode 100644 samples/client/petstore/typescript-fetch/default/package.json create mode 100644 samples/client/petstore/typescript-fetch/default/tsconfig.json create mode 100644 samples/client/petstore/typescript-fetch/default/typings.json create mode 100644 samples/client/petstore/typescript-fetch/with-package-metadata/README.md create mode 100644 samples/client/petstore/typescript-fetch/with-package-metadata/api.ts create mode 100644 samples/client/petstore/typescript-fetch/with-package-metadata/assign.ts create mode 100644 samples/client/petstore/typescript-fetch/with-package-metadata/git_push.sh create mode 100644 samples/client/petstore/typescript-fetch/with-package-metadata/package.json create mode 100644 samples/client/petstore/typescript-fetch/with-package-metadata/tsconfig.json create mode 100644 samples/client/petstore/typescript-fetch/with-package-metadata/typings.json diff --git a/.gitignore b/.gitignore index 3c008800db..2e1d135530 100644 --- a/.gitignore +++ b/.gitignore @@ -116,3 +116,13 @@ samples/client/petstore/python/.venv/ # ts samples/client/petstore/typescript-node/npm/node_modules +samples/client/petstore/typescript-fetch/with-package-metadata/node_modules +samples/client/petstore/typescript-fetch/with-package-metadata/dist +samples/client/petstore/typescript-fetch/with-package-metadata/typings +samples/client/petstore/typescript-fetch/default/node_modules +samples/client/petstore/typescript-fetch/default/dist +samples/client/petstore/typescript-fetch/default/typings +samples/client/petstore/typescript-fetch/default-es6/node_modules +samples/client/petstore/typescript-fetch/default-es6/dist +samples/client/petstore/typescript-fetch/default-es6/typings + diff --git a/bin/typescript-fetch-petstore-all.sh b/bin/typescript-fetch-petstore-all.sh new file mode 100755 index 0000000000..d39c16d880 --- /dev/null +++ b/bin/typescript-fetch-petstore-all.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +./bin/typescript-fetch-petstore-target-es6.sh +./bin/typescript-fetch-petstore-target-with-package-metadata.sh +./bin/typescript-fetch-petstore.sh diff --git a/samples/client/petstore/typescript-fetch/default-es6/README.md b/samples/client/petstore/typescript-fetch/default-es6/README.md new file mode 100644 index 0000000000..8ec43e7649 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default-es6/README.md @@ -0,0 +1,44 @@ +# TypeScript-Fetch + +This generator creates TypeScript/JavaScript client that utilizes [Fetch API](https://fetch.spec.whatwg.org/). The codegen Node module can be used in the following environments: + +* Node.JS +* Webpack +* Browserify + +It is usable in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `typings` in `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html)) + +### Installation ### + +`swagger-codegen` does not generate JavaScript directly. The codegen Node module comes with `package.json` that bundles `typescript` and `typings` so it can self-compile. The self-compile is normally run automatically via the `npm` `postinstall` script of `npm install`. + +CAVEAT: Due to [privilege implications](https://docs.npmjs.com/misc/scripts#user), `npm` may skip `postinstall` script if the user is `root`. You would need to manually invoke `npm install` or `npm run postinstall` for the codegen module if that's the case. + +#### NPM repository ### +If you remove `"private": true` from `package.json`, you may publish the module to NPM. In which case, you would be able to install the module as any other NPM module. + +It maybe useful to use [scoped packages](https://docs.npmjs.com/misc/scope). + +#### NPM install local file ### +You should be able to directly install the module using `npm install file:///codegen_path`. + +NOTES: If you do `npm install file:///codegen_path --save` NPM might convert your path to relative path, maybe have adverse affect. `npm install` and `npm shrinkwrap` may misbehave if the installation path is not absolute. + +#### direct copy/symlink ### +You may also simply copy or symlink the codegen into a directly under your project. The syntax of the usage would differ if you take this route. (See below) + +### Usage ### +With ES6 module syntax, the following syntaxes are supported: +``` +import * as localName from 'npmName'; +import {operationId} from 'npmName'; + +import * as localName from './symlinkDir'; +import {operationId} from './symlinkDir'; +``` +With CommonJS, the following syntaxes are supported: +``` +import localName = require('npmName'); + +import localName = require('./symlinkDir')'; +``` \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/default-es6/api.ts b/samples/client/petstore/typescript-fetch/default-es6/api.ts new file mode 100644 index 0000000000..3d075fc280 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default-es6/api.ts @@ -0,0 +1,855 @@ +import * as querystring from 'querystring'; +import * as fetch from 'isomorphic-fetch'; +import {assign} from './assign'; + + +export interface Category { + "id"?: number; + "name"?: string; +} + +export interface Order { + "id"?: number; + "petId"?: number; + "quantity"?: number; + "shipDate"?: Date; + + /** + * Order Status + */ + "status"?: Order.StatusEnum; + "complete"?: boolean; +} + +export namespace Order { + +export type StatusEnum = 'placed' | 'approved' | 'delivered'; +} +export interface Pet { + "id"?: number; + "category"?: Category; + "name": string; + "photoUrls": Array; + "tags"?: Array; + + /** + * pet status in the store + */ + "status"?: Pet.StatusEnum; +} + +export namespace Pet { + +export type StatusEnum = 'available' | 'pending' | 'sold'; +} +export interface Tag { + "id"?: number; + "name"?: string; +} + +export interface User { + "id"?: number; + "username"?: string; + "firstName"?: string; + "lastName"?: string; + "email"?: string; + "password"?: string; + "phone"?: string; + + /** + * User Status + */ + "userStatus"?: number; +} + + +//export namespace { + 'use strict'; + + export class PetApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * Add a new pet to the store + * + * @param body Pet object that needs to be added to the store + */ + public addPet (params: { body?: Pet; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Deletes a pet + * + * @param petId Pet id to delete + * @param apiKey + */ + public deletePet (params: { petId: number; apiKey?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling deletePet'); + } + headerParams['api_key'] = params.apiKey; + + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public findPetsByStatus (params: { status?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise> { + const localVarPath = this.basePath + '/pet/findByStatus'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.status !== undefined) { + queryParameters['status'] = params.status; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public findPetsByTags (params: { tags?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise> { + const localVarPath = this.basePath + '/pet/findByTags'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.tags !== undefined) { + queryParameters['tags'] = params.tags; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public getPetById (params: { petId: number; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling getPetById'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Update an existing pet + * + * @param body Pet object that needs to be added to the store + */ + public updatePet (params: { body?: Pet; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'PUT', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public updatePetWithForm (params: { petId: string; name?: string; status?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let formParams: any = {}; + headerParams['Content-Type'] = 'application/x-www-form-urlencoded'; + + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling updatePetWithForm'); + } + formParams['name'] = params.name; + + formParams['status'] = params.status; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: querystring.stringify(formParams), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * uploads an image + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server + * @param file file to upload + */ + public uploadFile (params: { petId: number; additionalMetadata?: string; file?: any; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}/uploadImage' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let formParams: any = {}; + headerParams['Content-Type'] = 'application/x-www-form-urlencoded'; + + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling uploadFile'); + } + formParams['additionalMetadata'] = params.additionalMetadata; + + formParams['file'] = params.file; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: querystring.stringify(formParams), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} +//export namespace { + 'use strict'; + + export class StoreApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * 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 + */ + public deleteOrder (params: { orderId: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/store/order/{orderId}' + .replace('{' + 'orderId' + '}', String(params.orderId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'orderId' is set + if (params.orderId == null) { + throw new Error('Missing required parameter orderId when calling deleteOrder'); + } + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Returns pet inventories by status + * Returns a map of status codes to quantities + */ + public getInventory (params: { }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{ [key: string]: number; }> { + const localVarPath = this.basePath + '/store/inventory'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public getOrderById (params: { orderId: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/store/order/{orderId}' + .replace('{' + 'orderId' + '}', String(params.orderId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'orderId' is set + if (params.orderId == null) { + throw new Error('Missing required parameter orderId when calling getOrderById'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Place an order for a pet + * + * @param body order placed for purchasing the pet + */ + public placeOrder (params: { body?: Order; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/store/order'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} +//export namespace { + 'use strict'; + + export class UserApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * Create user + * This can only be done by the logged in user. + * @param body Created user object + */ + public createUser (params: { body?: User; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + public createUsersWithArrayInput (params: { body?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/createWithArray'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + public createUsersWithListInput (params: { body?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/createWithList'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Delete user + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + */ + public deleteUser (params: { username: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling deleteUser'); + } + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Get user by user name + * + * @param username The name that needs to be fetched. Use user1 for testing. + */ + public getUserByName (params: { username: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling getUserByName'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Logs user into the system + * + * @param username The user name for login + * @param password The password for login in clear text + */ + public loginUser (params: { username?: string; password?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/user/login'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.username !== undefined) { + queryParameters['username'] = params.username; + } + + if (params.password !== undefined) { + queryParameters['password'] = params.password; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Logs out current logged in user session + * + */ + public logoutUser (params: { }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/logout'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public updateUser (params: { username: string; body?: User; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling updateUser'); + } + let fetchParams = { + method: 'PUT', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} diff --git a/samples/client/petstore/typescript-fetch/default-es6/assign.ts b/samples/client/petstore/typescript-fetch/default-es6/assign.ts new file mode 100644 index 0000000000..2335514414 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default-es6/assign.ts @@ -0,0 +1,18 @@ +export function assign (target: any, ...args: any[]) { + 'use strict'; + if (target === undefined || target === null) { + throw new TypeError('Cannot convert undefined or null to object'); + } + + var output = Object(target); + for (let source of args) { + if (source !== undefined && source !== null) { + for (var nextKey in source) { + if (source.hasOwnProperty(nextKey)) { + output[nextKey] = source[nextKey]; + } + } + } + } + return output; +}; \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/default-es6/git_push.sh b/samples/client/petstore/typescript-fetch/default-es6/git_push.sh new file mode 100644 index 0000000000..ed374619b1 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default-es6/git_push.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ +# +# Usage example: /bin/sh ./git_push.sh wing328 swagger-petstore-perl "minor update" + +git_user_id=$1 +git_repo_id=$2 +release_note=$3 + +if [ "$git_user_id" = "" ]; then + git_user_id="GIT_USER_ID" + echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id" +fi + +if [ "$git_repo_id" = "" ]; then + git_repo_id="GIT_REPO_ID" + echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id" +fi + +if [ "$release_note" = "" ]; then + release_note="Minor update" + echo "[INFO] No command line input provided. Set \$release_note to $release_note" +fi + +# Initialize the local directory as a Git repository +git init + +# Adds the files in the local repository and stages them for commit. +git add . + +# Commits the tracked changes and prepares them to be pushed to a remote repository. +git commit -m "$release_note" + +# Sets the new remote +git_remote=`git remote` +if [ "$git_remote" = "" ]; then # git remote not defined + + if [ "$GIT_TOKEN" = "" ]; then + echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment." + git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git + else + git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git + fi + +fi + +git pull origin master + +# Pushes (Forces) the changes in the local repository up to the remote repository +echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git" +git push origin master 2>&1 | grep -v 'To https' + diff --git a/samples/client/petstore/typescript-fetch/default-es6/package.json b/samples/client/petstore/typescript-fetch/default-es6/package.json new file mode 100644 index 0000000000..84cf629d93 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default-es6/package.json @@ -0,0 +1,18 @@ +{ + "name": "typescript-fetch-api", + "version": "0.0.0", + "private": true, + "main": "./dist/api.js", + "browser": "./dist/api.js", + "typings": "./dist/api.d.ts", + "dependencies": { + "isomorphic-fetch": "^2.2.1" + }, + "scripts" : { + "install" : "typings install && tsc" + }, + "devDependencies": { + "typescript": "^1.8.10", + "typings": "^0.8.1" + } +} \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/default-es6/tsconfig.json b/samples/client/petstore/typescript-fetch/default-es6/tsconfig.json new file mode 100644 index 0000000000..9d5f166e76 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default-es6/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "declaration": true, + "target": "es6", + "module": "commonjs", + "noImplicitAny": true, + "outDir": "dist" + }, + "exclude": [ + "dist", + "node_modules", + "typings/browser", + "typings/main", + "typings/main.d.ts" + ] +} diff --git a/samples/client/petstore/typescript-fetch/default-es6/typings.json b/samples/client/petstore/typescript-fetch/default-es6/typings.json new file mode 100644 index 0000000000..3ce24427fc --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default-es6/typings.json @@ -0,0 +1,8 @@ +{ + "version": false, + "dependencies": {}, + "ambientDependencies": { + "node": "registry:dt/node#4.0.0+20160423143914", + "isomorphic-fetch": "registry:dt/isomorphic-fetch#0.0.0+20160505171433" + } +} diff --git a/samples/client/petstore/typescript-fetch/default/README.md b/samples/client/petstore/typescript-fetch/default/README.md new file mode 100644 index 0000000000..8ec43e7649 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default/README.md @@ -0,0 +1,44 @@ +# TypeScript-Fetch + +This generator creates TypeScript/JavaScript client that utilizes [Fetch API](https://fetch.spec.whatwg.org/). The codegen Node module can be used in the following environments: + +* Node.JS +* Webpack +* Browserify + +It is usable in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `typings` in `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html)) + +### Installation ### + +`swagger-codegen` does not generate JavaScript directly. The codegen Node module comes with `package.json` that bundles `typescript` and `typings` so it can self-compile. The self-compile is normally run automatically via the `npm` `postinstall` script of `npm install`. + +CAVEAT: Due to [privilege implications](https://docs.npmjs.com/misc/scripts#user), `npm` may skip `postinstall` script if the user is `root`. You would need to manually invoke `npm install` or `npm run postinstall` for the codegen module if that's the case. + +#### NPM repository ### +If you remove `"private": true` from `package.json`, you may publish the module to NPM. In which case, you would be able to install the module as any other NPM module. + +It maybe useful to use [scoped packages](https://docs.npmjs.com/misc/scope). + +#### NPM install local file ### +You should be able to directly install the module using `npm install file:///codegen_path`. + +NOTES: If you do `npm install file:///codegen_path --save` NPM might convert your path to relative path, maybe have adverse affect. `npm install` and `npm shrinkwrap` may misbehave if the installation path is not absolute. + +#### direct copy/symlink ### +You may also simply copy or symlink the codegen into a directly under your project. The syntax of the usage would differ if you take this route. (See below) + +### Usage ### +With ES6 module syntax, the following syntaxes are supported: +``` +import * as localName from 'npmName'; +import {operationId} from 'npmName'; + +import * as localName from './symlinkDir'; +import {operationId} from './symlinkDir'; +``` +With CommonJS, the following syntaxes are supported: +``` +import localName = require('npmName'); + +import localName = require('./symlinkDir')'; +``` \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/default/api.ts b/samples/client/petstore/typescript-fetch/default/api.ts new file mode 100644 index 0000000000..3d075fc280 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default/api.ts @@ -0,0 +1,855 @@ +import * as querystring from 'querystring'; +import * as fetch from 'isomorphic-fetch'; +import {assign} from './assign'; + + +export interface Category { + "id"?: number; + "name"?: string; +} + +export interface Order { + "id"?: number; + "petId"?: number; + "quantity"?: number; + "shipDate"?: Date; + + /** + * Order Status + */ + "status"?: Order.StatusEnum; + "complete"?: boolean; +} + +export namespace Order { + +export type StatusEnum = 'placed' | 'approved' | 'delivered'; +} +export interface Pet { + "id"?: number; + "category"?: Category; + "name": string; + "photoUrls": Array; + "tags"?: Array; + + /** + * pet status in the store + */ + "status"?: Pet.StatusEnum; +} + +export namespace Pet { + +export type StatusEnum = 'available' | 'pending' | 'sold'; +} +export interface Tag { + "id"?: number; + "name"?: string; +} + +export interface User { + "id"?: number; + "username"?: string; + "firstName"?: string; + "lastName"?: string; + "email"?: string; + "password"?: string; + "phone"?: string; + + /** + * User Status + */ + "userStatus"?: number; +} + + +//export namespace { + 'use strict'; + + export class PetApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * Add a new pet to the store + * + * @param body Pet object that needs to be added to the store + */ + public addPet (params: { body?: Pet; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Deletes a pet + * + * @param petId Pet id to delete + * @param apiKey + */ + public deletePet (params: { petId: number; apiKey?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling deletePet'); + } + headerParams['api_key'] = params.apiKey; + + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public findPetsByStatus (params: { status?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise> { + const localVarPath = this.basePath + '/pet/findByStatus'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.status !== undefined) { + queryParameters['status'] = params.status; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public findPetsByTags (params: { tags?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise> { + const localVarPath = this.basePath + '/pet/findByTags'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.tags !== undefined) { + queryParameters['tags'] = params.tags; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public getPetById (params: { petId: number; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling getPetById'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Update an existing pet + * + * @param body Pet object that needs to be added to the store + */ + public updatePet (params: { body?: Pet; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'PUT', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public updatePetWithForm (params: { petId: string; name?: string; status?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let formParams: any = {}; + headerParams['Content-Type'] = 'application/x-www-form-urlencoded'; + + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling updatePetWithForm'); + } + formParams['name'] = params.name; + + formParams['status'] = params.status; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: querystring.stringify(formParams), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * uploads an image + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server + * @param file file to upload + */ + public uploadFile (params: { petId: number; additionalMetadata?: string; file?: any; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}/uploadImage' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let formParams: any = {}; + headerParams['Content-Type'] = 'application/x-www-form-urlencoded'; + + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling uploadFile'); + } + formParams['additionalMetadata'] = params.additionalMetadata; + + formParams['file'] = params.file; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: querystring.stringify(formParams), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} +//export namespace { + 'use strict'; + + export class StoreApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * 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 + */ + public deleteOrder (params: { orderId: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/store/order/{orderId}' + .replace('{' + 'orderId' + '}', String(params.orderId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'orderId' is set + if (params.orderId == null) { + throw new Error('Missing required parameter orderId when calling deleteOrder'); + } + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Returns pet inventories by status + * Returns a map of status codes to quantities + */ + public getInventory (params: { }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{ [key: string]: number; }> { + const localVarPath = this.basePath + '/store/inventory'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public getOrderById (params: { orderId: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/store/order/{orderId}' + .replace('{' + 'orderId' + '}', String(params.orderId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'orderId' is set + if (params.orderId == null) { + throw new Error('Missing required parameter orderId when calling getOrderById'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Place an order for a pet + * + * @param body order placed for purchasing the pet + */ + public placeOrder (params: { body?: Order; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/store/order'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} +//export namespace { + 'use strict'; + + export class UserApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * Create user + * This can only be done by the logged in user. + * @param body Created user object + */ + public createUser (params: { body?: User; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + public createUsersWithArrayInput (params: { body?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/createWithArray'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + public createUsersWithListInput (params: { body?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/createWithList'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Delete user + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + */ + public deleteUser (params: { username: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling deleteUser'); + } + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Get user by user name + * + * @param username The name that needs to be fetched. Use user1 for testing. + */ + public getUserByName (params: { username: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling getUserByName'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Logs user into the system + * + * @param username The user name for login + * @param password The password for login in clear text + */ + public loginUser (params: { username?: string; password?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/user/login'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.username !== undefined) { + queryParameters['username'] = params.username; + } + + if (params.password !== undefined) { + queryParameters['password'] = params.password; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Logs out current logged in user session + * + */ + public logoutUser (params: { }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/logout'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public updateUser (params: { username: string; body?: User; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling updateUser'); + } + let fetchParams = { + method: 'PUT', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} diff --git a/samples/client/petstore/typescript-fetch/default/assign.ts b/samples/client/petstore/typescript-fetch/default/assign.ts new file mode 100644 index 0000000000..2335514414 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default/assign.ts @@ -0,0 +1,18 @@ +export function assign (target: any, ...args: any[]) { + 'use strict'; + if (target === undefined || target === null) { + throw new TypeError('Cannot convert undefined or null to object'); + } + + var output = Object(target); + for (let source of args) { + if (source !== undefined && source !== null) { + for (var nextKey in source) { + if (source.hasOwnProperty(nextKey)) { + output[nextKey] = source[nextKey]; + } + } + } + } + return output; +}; \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/default/git_push.sh b/samples/client/petstore/typescript-fetch/default/git_push.sh new file mode 100644 index 0000000000..ed374619b1 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default/git_push.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ +# +# Usage example: /bin/sh ./git_push.sh wing328 swagger-petstore-perl "minor update" + +git_user_id=$1 +git_repo_id=$2 +release_note=$3 + +if [ "$git_user_id" = "" ]; then + git_user_id="GIT_USER_ID" + echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id" +fi + +if [ "$git_repo_id" = "" ]; then + git_repo_id="GIT_REPO_ID" + echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id" +fi + +if [ "$release_note" = "" ]; then + release_note="Minor update" + echo "[INFO] No command line input provided. Set \$release_note to $release_note" +fi + +# Initialize the local directory as a Git repository +git init + +# Adds the files in the local repository and stages them for commit. +git add . + +# Commits the tracked changes and prepares them to be pushed to a remote repository. +git commit -m "$release_note" + +# Sets the new remote +git_remote=`git remote` +if [ "$git_remote" = "" ]; then # git remote not defined + + if [ "$GIT_TOKEN" = "" ]; then + echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment." + git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git + else + git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git + fi + +fi + +git pull origin master + +# Pushes (Forces) the changes in the local repository up to the remote repository +echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git" +git push origin master 2>&1 | grep -v 'To https' + diff --git a/samples/client/petstore/typescript-fetch/default/package.json b/samples/client/petstore/typescript-fetch/default/package.json new file mode 100644 index 0000000000..84cf629d93 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default/package.json @@ -0,0 +1,18 @@ +{ + "name": "typescript-fetch-api", + "version": "0.0.0", + "private": true, + "main": "./dist/api.js", + "browser": "./dist/api.js", + "typings": "./dist/api.d.ts", + "dependencies": { + "isomorphic-fetch": "^2.2.1" + }, + "scripts" : { + "install" : "typings install && tsc" + }, + "devDependencies": { + "typescript": "^1.8.10", + "typings": "^0.8.1" + } +} \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/default/tsconfig.json b/samples/client/petstore/typescript-fetch/default/tsconfig.json new file mode 100644 index 0000000000..18db23e2f5 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "declaration": true, + "target": "es5", + "module": "commonjs", + "noImplicitAny": true, + "outDir": "dist" + }, + "exclude": [ + "dist", + "node_modules", + "typings/browser", + "typings/main", + "typings/main.d.ts" + ] +} diff --git a/samples/client/petstore/typescript-fetch/default/typings.json b/samples/client/petstore/typescript-fetch/default/typings.json new file mode 100644 index 0000000000..eeca5afde9 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/default/typings.json @@ -0,0 +1,9 @@ +{ + "version": false, + "dependencies": {}, + "ambientDependencies": { + "es6-promise": "registry:dt/es6-promise#0.0.0+20160423074304", + "node": "registry:dt/node#4.0.0+20160423143914", + "isomorphic-fetch": "registry:dt/isomorphic-fetch#0.0.0+20160505171433" + } +} diff --git a/samples/client/petstore/typescript-fetch/with-package-metadata/README.md b/samples/client/petstore/typescript-fetch/with-package-metadata/README.md new file mode 100644 index 0000000000..8ec43e7649 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/with-package-metadata/README.md @@ -0,0 +1,44 @@ +# TypeScript-Fetch + +This generator creates TypeScript/JavaScript client that utilizes [Fetch API](https://fetch.spec.whatwg.org/). The codegen Node module can be used in the following environments: + +* Node.JS +* Webpack +* Browserify + +It is usable in both TypeScript and JavaScript. In TypeScript, the definition should be automatically resolved via `typings` in `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html)) + +### Installation ### + +`swagger-codegen` does not generate JavaScript directly. The codegen Node module comes with `package.json` that bundles `typescript` and `typings` so it can self-compile. The self-compile is normally run automatically via the `npm` `postinstall` script of `npm install`. + +CAVEAT: Due to [privilege implications](https://docs.npmjs.com/misc/scripts#user), `npm` may skip `postinstall` script if the user is `root`. You would need to manually invoke `npm install` or `npm run postinstall` for the codegen module if that's the case. + +#### NPM repository ### +If you remove `"private": true` from `package.json`, you may publish the module to NPM. In which case, you would be able to install the module as any other NPM module. + +It maybe useful to use [scoped packages](https://docs.npmjs.com/misc/scope). + +#### NPM install local file ### +You should be able to directly install the module using `npm install file:///codegen_path`. + +NOTES: If you do `npm install file:///codegen_path --save` NPM might convert your path to relative path, maybe have adverse affect. `npm install` and `npm shrinkwrap` may misbehave if the installation path is not absolute. + +#### direct copy/symlink ### +You may also simply copy or symlink the codegen into a directly under your project. The syntax of the usage would differ if you take this route. (See below) + +### Usage ### +With ES6 module syntax, the following syntaxes are supported: +``` +import * as localName from 'npmName'; +import {operationId} from 'npmName'; + +import * as localName from './symlinkDir'; +import {operationId} from './symlinkDir'; +``` +With CommonJS, the following syntaxes are supported: +``` +import localName = require('npmName'); + +import localName = require('./symlinkDir')'; +``` \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/with-package-metadata/api.ts b/samples/client/petstore/typescript-fetch/with-package-metadata/api.ts new file mode 100644 index 0000000000..3d075fc280 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/with-package-metadata/api.ts @@ -0,0 +1,855 @@ +import * as querystring from 'querystring'; +import * as fetch from 'isomorphic-fetch'; +import {assign} from './assign'; + + +export interface Category { + "id"?: number; + "name"?: string; +} + +export interface Order { + "id"?: number; + "petId"?: number; + "quantity"?: number; + "shipDate"?: Date; + + /** + * Order Status + */ + "status"?: Order.StatusEnum; + "complete"?: boolean; +} + +export namespace Order { + +export type StatusEnum = 'placed' | 'approved' | 'delivered'; +} +export interface Pet { + "id"?: number; + "category"?: Category; + "name": string; + "photoUrls": Array; + "tags"?: Array; + + /** + * pet status in the store + */ + "status"?: Pet.StatusEnum; +} + +export namespace Pet { + +export type StatusEnum = 'available' | 'pending' | 'sold'; +} +export interface Tag { + "id"?: number; + "name"?: string; +} + +export interface User { + "id"?: number; + "username"?: string; + "firstName"?: string; + "lastName"?: string; + "email"?: string; + "password"?: string; + "phone"?: string; + + /** + * User Status + */ + "userStatus"?: number; +} + + +//export namespace { + 'use strict'; + + export class PetApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * Add a new pet to the store + * + * @param body Pet object that needs to be added to the store + */ + public addPet (params: { body?: Pet; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Deletes a pet + * + * @param petId Pet id to delete + * @param apiKey + */ + public deletePet (params: { petId: number; apiKey?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling deletePet'); + } + headerParams['api_key'] = params.apiKey; + + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public findPetsByStatus (params: { status?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise> { + const localVarPath = this.basePath + '/pet/findByStatus'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.status !== undefined) { + queryParameters['status'] = params.status; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public findPetsByTags (params: { tags?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise> { + const localVarPath = this.basePath + '/pet/findByTags'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.tags !== undefined) { + queryParameters['tags'] = params.tags; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public getPetById (params: { petId: number; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling getPetById'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Update an existing pet + * + * @param body Pet object that needs to be added to the store + */ + public updatePet (params: { body?: Pet; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'PUT', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public updatePetWithForm (params: { petId: string; name?: string; status?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let formParams: any = {}; + headerParams['Content-Type'] = 'application/x-www-form-urlencoded'; + + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling updatePetWithForm'); + } + formParams['name'] = params.name; + + formParams['status'] = params.status; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: querystring.stringify(formParams), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * uploads an image + * + * @param petId ID of pet to update + * @param additionalMetadata Additional data to pass to server + * @param file file to upload + */ + public uploadFile (params: { petId: number; additionalMetadata?: string; file?: any; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/pet/{petId}/uploadImage' + .replace('{' + 'petId' + '}', String(params.petId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let formParams: any = {}; + headerParams['Content-Type'] = 'application/x-www-form-urlencoded'; + + // verify required parameter 'petId' is set + if (params.petId == null) { + throw new Error('Missing required parameter petId when calling uploadFile'); + } + formParams['additionalMetadata'] = params.additionalMetadata; + + formParams['file'] = params.file; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: querystring.stringify(formParams), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} +//export namespace { + 'use strict'; + + export class StoreApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * 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 + */ + public deleteOrder (params: { orderId: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/store/order/{orderId}' + .replace('{' + 'orderId' + '}', String(params.orderId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'orderId' is set + if (params.orderId == null) { + throw new Error('Missing required parameter orderId when calling deleteOrder'); + } + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Returns pet inventories by status + * Returns a map of status codes to quantities + */ + public getInventory (params: { }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{ [key: string]: number; }> { + const localVarPath = this.basePath + '/store/inventory'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public getOrderById (params: { orderId: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/store/order/{orderId}' + .replace('{' + 'orderId' + '}', String(params.orderId)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'orderId' is set + if (params.orderId == null) { + throw new Error('Missing required parameter orderId when calling getOrderById'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Place an order for a pet + * + * @param body order placed for purchasing the pet + */ + public placeOrder (params: { body?: Order; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/store/order'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} +//export namespace { + 'use strict'; + + export class UserApi { + protected basePath = 'http://petstore.swagger.io/v2'; + public defaultHeaders : any = {}; + + constructor(basePath?: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * Create user + * This can only be done by the logged in user. + * @param body Created user object + */ + public createUser (params: { body?: User; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + public createUsersWithArrayInput (params: { body?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/createWithArray'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Creates list of users with given input array + * + * @param body List of user object + */ + public createUsersWithListInput (params: { body?: Array; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/createWithList'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + let fetchParams = { + method: 'POST', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Delete user + * This can only be done by the logged in user. + * @param username The name that needs to be deleted + */ + public deleteUser (params: { username: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling deleteUser'); + } + let fetchParams = { + method: 'DELETE', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Get user by user name + * + * @param username The name that needs to be fetched. Use user1 for testing. + */ + public getUserByName (params: { username: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling getUserByName'); + } + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Logs user into the system + * + * @param username The user name for login + * @param password The password for login in clear text + */ + public loginUser (params: { username?: string; password?: string; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise { + const localVarPath = this.basePath + '/user/login'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + if (params.username !== undefined) { + queryParameters['username'] = params.username; + } + + if (params.password !== undefined) { + queryParameters['password'] = params.password; + } + + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * Logs out current logged in user session + * + */ + public logoutUser (params: { }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/logout'; + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + let fetchParams = { + method: 'GET', + headers: headerParams, + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + /** + * 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 + */ + public updateUser (params: { username: string; body?: User; }, extraQueryParams?: any, extraFetchParams?: any ) : Promise<{}> { + const localVarPath = this.basePath + '/user/{username}' + .replace('{' + 'username' + '}', String(params.username)); + + let queryParameters: any = assign({}, extraQueryParams); + let headerParams: any = assign({}, this.defaultHeaders); + headerParams['Content-Type'] = 'application/json'; + + // verify required parameter 'username' is set + if (params.username == null) { + throw new Error('Missing required parameter username when calling updateUser'); + } + let fetchParams = { + method: 'PUT', + headers: headerParams, + body: JSON.stringify(params.body), + + }; + + if (extraFetchParams) { + fetchParams = assign(fetchParams, extraFetchParams); + } + + let localVarPathWithQueryParameters = localVarPath + (localVarPath.indexOf('?') !== -1 ? '&' : '?') + querystring.stringify(queryParameters); + + return fetch(localVarPathWithQueryParameters, fetchParams).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response.json(); + } else { + let error = new Error(response.statusText); + (error as any).response = response; + throw error; + } + }); + } + } +//} diff --git a/samples/client/petstore/typescript-fetch/with-package-metadata/assign.ts b/samples/client/petstore/typescript-fetch/with-package-metadata/assign.ts new file mode 100644 index 0000000000..2335514414 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/with-package-metadata/assign.ts @@ -0,0 +1,18 @@ +export function assign (target: any, ...args: any[]) { + 'use strict'; + if (target === undefined || target === null) { + throw new TypeError('Cannot convert undefined or null to object'); + } + + var output = Object(target); + for (let source of args) { + if (source !== undefined && source !== null) { + for (var nextKey in source) { + if (source.hasOwnProperty(nextKey)) { + output[nextKey] = source[nextKey]; + } + } + } + } + return output; +}; \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/with-package-metadata/git_push.sh b/samples/client/petstore/typescript-fetch/with-package-metadata/git_push.sh new file mode 100644 index 0000000000..ed374619b1 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/with-package-metadata/git_push.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/ +# +# Usage example: /bin/sh ./git_push.sh wing328 swagger-petstore-perl "minor update" + +git_user_id=$1 +git_repo_id=$2 +release_note=$3 + +if [ "$git_user_id" = "" ]; then + git_user_id="GIT_USER_ID" + echo "[INFO] No command line input provided. Set \$git_user_id to $git_user_id" +fi + +if [ "$git_repo_id" = "" ]; then + git_repo_id="GIT_REPO_ID" + echo "[INFO] No command line input provided. Set \$git_repo_id to $git_repo_id" +fi + +if [ "$release_note" = "" ]; then + release_note="Minor update" + echo "[INFO] No command line input provided. Set \$release_note to $release_note" +fi + +# Initialize the local directory as a Git repository +git init + +# Adds the files in the local repository and stages them for commit. +git add . + +# Commits the tracked changes and prepares them to be pushed to a remote repository. +git commit -m "$release_note" + +# Sets the new remote +git_remote=`git remote` +if [ "$git_remote" = "" ]; then # git remote not defined + + if [ "$GIT_TOKEN" = "" ]; then + echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git crediential in your environment." + git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git + else + git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git + fi + +fi + +git pull origin master + +# Pushes (Forces) the changes in the local repository up to the remote repository +echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git" +git push origin master 2>&1 | grep -v 'To https' + diff --git a/samples/client/petstore/typescript-fetch/with-package-metadata/package.json b/samples/client/petstore/typescript-fetch/with-package-metadata/package.json new file mode 100644 index 0000000000..ab2a67c51d --- /dev/null +++ b/samples/client/petstore/typescript-fetch/with-package-metadata/package.json @@ -0,0 +1,18 @@ +{ + "name": "@swagger/typescript-fetch-petstore", + "version": "0.0.1", + "private": true, + "main": "./dist/api.js", + "browser": "./dist/api.js", + "typings": "./dist/api.d.ts", + "dependencies": { + "isomorphic-fetch": "^2.2.1" + }, + "scripts" : { + "install" : "typings install && tsc" + }, + "devDependencies": { + "typescript": "^1.8.10", + "typings": "^0.8.1" + } +} \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/with-package-metadata/tsconfig.json b/samples/client/petstore/typescript-fetch/with-package-metadata/tsconfig.json new file mode 100644 index 0000000000..18db23e2f5 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/with-package-metadata/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "declaration": true, + "target": "es5", + "module": "commonjs", + "noImplicitAny": true, + "outDir": "dist" + }, + "exclude": [ + "dist", + "node_modules", + "typings/browser", + "typings/main", + "typings/main.d.ts" + ] +} diff --git a/samples/client/petstore/typescript-fetch/with-package-metadata/typings.json b/samples/client/petstore/typescript-fetch/with-package-metadata/typings.json new file mode 100644 index 0000000000..eeca5afde9 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/with-package-metadata/typings.json @@ -0,0 +1,9 @@ +{ + "version": false, + "dependencies": {}, + "ambientDependencies": { + "es6-promise": "registry:dt/es6-promise#0.0.0+20160423074304", + "node": "registry:dt/node#4.0.0+20160423143914", + "isomorphic-fetch": "registry:dt/isomorphic-fetch#0.0.0+20160505171433" + } +}