Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typescript-Fetch] Support additionalproperties, Enum, Auth and more. #6130

Merged
merged 9 commits into from
Aug 25, 2017

Conversation

isman-usoh
Copy link
Contributor

@isman-usoh isman-usoh commented Jul 21, 2017

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master for non-breaking changes and 3.0.0 branch for breaking (non-backward compatible) changes.

Description of the PR

Support additionalproperties and more.

  • Upgrade to TypeScript 2
  • Use type definition from npm instead of typings, typings is deprecation
  • Use Enum instead of String Literal Types
  • Use typescript es6 lib for target es5
  • Support additionalproperties
  • full support JSDoc
  • Add snapshot and npmRepository option
  • Update typescript-fetch run script for linux
  • Create typescript-fetch run script for windows

@wing328
Copy link
Contributor

wing328 commented Jul 21, 2017

@isman-usoh thanks for so many enhancements 👍

cc @Vrolijkx @taxpon @sebastianhaas @kenisteward @TiFu

Copying @mgechev as well since he's also working on some enhancements for TS Fetch generator.

isman-usoh and others added 5 commits July 23, 2017 23:38
- Upgrade to TypeScript 2
- Use type definition from npm instead of typings, typings is deprecation
- Use Enum instead of String Literal Types
- Use typescript es6 lib for target es5
- Support additionalproperties
- Support JSDoc
- Add snapshot and npmRepository option
- Update typescript-fetch run script for linux
- Create typescript-fetch run script for windows
- Fix circle run script
- Fix duplicate query parameter
Copy link

@mgechev mgechev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job, that's a big improvement! I added a few comments, mostly style related, and a few with open questions.

// tslint:disable
{{>licenseInfo}}

import * as url from "url";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More consistent indentation, currently it's mixture between tabs and spaces.

{{>licenseInfo}}

import * as url from "url";
import * as isomorphicFetch from "isomorphic-fetch";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

{{>licenseInfo}}

import * as url from "url";
import * as isomorphicFetch from "isomorphic-fetch";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to introduce isomorphic-fetch as a dependency? Why not inject the fetch implementation from the outside instead?

Copy link
Contributor Author

@isman-usoh isman-usoh Jul 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isomorphic-fetch support run node.js and browser

If browser does not support Fetch API isomorphic-fetch use from fetch polyfill

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sure, that's why it's called isomorphic. My point is, does it worth coupling to a specific dependency? Why not inject it from the outside, based on the declared interface?

* @class BaseAPI
*/
export class BaseAPI {
public config: Configuration = new Configuration();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can delay the instantiation of the Configuration object for after we've verified that the config parameter has not been passed.

@@ -8,5 +11,6 @@ export class Configuration {
};
username: string;
password: string;
accessToken: string;
accessToken: string | ((name: string) => string);
basePath: string;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • accessToken var >> return current token
  • accessToken function >> use for multiple Oauth, parameter auth name
  • basePath >> for override base path

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this all looks cool. The point of my comment is that there's missing newline https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline.


CAVEAT: Due to [privilege implications](https://docs.npmjs.com/misc/scripts#user), `npm` would skip all scripts if the user is `root`. You would need to manually run it with `npm run prepublish` or run `npm install --unsafe-perm`.
### publishing
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should start with uppercase being a title.


You can also use `npm link` to link the module. However, this would not modify `package.json` of the installing project, as such you would need to relink every time you deploy that project.
### consuming
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should start with uppercase being a title.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

export class Configuration {
apiKey: {
api_key: string;
};
username: string;
password: string;
accessToken: string;
accessToken: string | ((name: string) => string);
basePath: string;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline.

*/

export * from "./api";
export * from "./configuration";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline.

"typings/browser",
"typings/main",
"typings/main.d.ts"
"node_modules"
]
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline.

@isman-usoh isman-usoh force-pushed the typescript-fetch branch 2 times, most recently from 2fcf2b2 to 633e420 Compare July 27, 2017 04:17
@isman-usoh isman-usoh changed the title Support additionalproperties and more. [Typescript-Fetch] Support additionalproperties, Enum, Auth and more. Jul 31, 2017
@wing328
Copy link
Contributor

wing328 commented Aug 2, 2017

If this looks good, I'll merge it tomorrow.

cc @Vrolijkx @taxpon @sebastianhaas @kenisteward @TiFu @mgechev

@wing328
Copy link
Contributor

wing328 commented Aug 3, 2017

@isman-usoh I got the following errors when trying to update the TS Fetch Petstore samples:

swagger-codegen|isman-usoh-typescript-fetch ⇒ ./bin/typescript-fetch-petstore-default.sh 
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256M; support was removed in 8.0
[main] INFO io.swagger.parser.Swagger20Parser - reading from modules/swagger-codegen/src/test/resources/2_0/petstore.yaml
[main] ERROR io.swagger.codegen.AbstractGenerator - typescript-fetch/index.mustache (No such file or directory)
[main] ERROR io.swagger.codegen.AbstractGenerator - can't load template typescript-fetch/index.mustache
Exception in thread "main" java.lang.RuntimeException: Could not generate supporting file 'SupportingFile:
	templateFile: index.mustache
	folder: 
	destinationFilename: index.ts
'
	at io.swagger.codegen.DefaultGenerator.generateSupportingFiles(DefaultGenerator.java:617)
	at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:734)
	at io.swagger.codegen.cmd.Generate.run(Generate.java:285)
	at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)
Caused by: java.lang.RuntimeException: can't load template typescript-fetch/index.mustache
	at io.swagger.codegen.AbstractGenerator.readTemplate(AbstractGenerator.java:51)
	at io.swagger.codegen.DefaultGenerator.generateSupportingFiles(DefaultGenerator.java:576)
	... 3 more

Should it be "TypeScript-Fetch/index.mustache" instead?

@isman-usoh
Copy link
Contributor Author

@wing328 I rename template folder to lowercase, to be consistent other typescript project
modules/swagger-codegen/src/main/resources/TypeScript-Fetch > modules/swagger-codegen/src/main/resources/typescript-fetch

@wing328
Copy link
Contributor

wing328 commented Aug 4, 2017

@isman-usoh yes, I noticed that. Did you get any error when running the shell script or Windows batch file for TS Fetch Petstore clients?

@isman-usoh
Copy link
Contributor Author

isman-usoh commented Aug 4, 2017

@wing328 No, I use windows os and run bin\windows\typescript-fetch-petstore.bat success, no error
capture

@bedag-moo
Copy link
Contributor

Why did you change from string literal types to enums? #6233 does the opposite (for the typescript-angular binding).

@wing328
Copy link
Contributor

wing328 commented Aug 4, 2017

Travis CI tests failed with errors. Here are some for reference:

> typescript-fetch-test@1.0.0 build /Users/travis/build/swagger-api/swagger-codegen/samples/client/petstore/typescript-fetch/tests/default

> tsc



test/PetApiFactory.ts(11,9): error TS2322: Type '{ api_key: string; }' is not assignable to type 'string | ((name: string) => string)'.

  Object literal may only specify known properties, and 'api_key' does not exist in type 'string | ((name: string) => string)'.

test/StoreApiFactory.ts(11,9): error TS2322: Type '{ api_key: string; }' is not assignable to type 'string | ((name: string) => string)'.

  Object literal may only specify known properties, and 'api_key' does not exist in type 'string | ((name: string) => string)'.

Ref: https://travis-ci.org/swagger-api/swagger-codegen/builds/261078041

Please take a look when you've time.

@@ -1,5 +0,0 @@
#!/bin/sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to keep the {lang}-petstore-all.sh convention so as to make it consistent with other languages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@alexrashed
Copy link

Are there any plans on supporting automatic redirects for OAuth2 with implicit flow using the mandatory authorizationUrl in the security definition of the swagger file?
If not, wouldn't it be better to make Configuration#accessToken and respectively ConfigurationParameters#accessToken async (return an Observable) to allow getting a new / refreshing an expired access token?

@bedag-moo
Copy link
Contributor

Implicit flow involves navigating to the identity server, which destroys all javascript objects, so I don't see how that Observable could ever complete? Could you elaborate on how you think this could work?

@kenisteward
Copy link
Contributor

kenisteward commented Aug 9, 2017 via email

@alexrashed
Copy link

@bedag-moo I'm not an expert on the auth flow, but with activated CORS on the auth-server it should be possible to directly send the request to the (in the swagger.json) configured authorizationUrl f.e. to refresh the accessToken using the refreshToken. In order to implement such a lazy-refresh, the result of the accessToken function needs to be async.

@bedag-moo
Copy link
Contributor

alexrashed: I think this is called the code flow (the implicit flow does not provide refresh tokens AFAIK). I agree this could be useful for code flow (or implicit flow in a nested iframe, come to think of it).

@wing328
Copy link
Contributor

wing328 commented Aug 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants