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] Support InversifyJS #6489

Merged
merged 28 commits into from
Jun 13, 2020
Merged

[Typescript] Support InversifyJS #6489

merged 28 commits into from
Jun 13, 2020

Conversation

bodograumann
Copy link
Contributor

@bodograumann bodograumann commented May 29, 2020

To be able to use the new typescript generator as a replacement for the existing typescript-inversify generator, we need to add a few things. This PR does just that.

This is not a 1:1 replacement. Instead I have tried to incorporate my experience with using typescript-inversify and not pollute the templates too much.

The option for enabling this functionality is called useInversify.
I had to change the way the SecurityAuthentication objects are generated in order to allow injecting parameters into them.
Normally we should depend on abstractions (interfaces), not implementations, so I added some interfaces. For <service>RequestFactory and <service>ResponseProcessor this seemed like overkill though.
Abstract classes are used as service identifiers. This has the advantage that during binding the types can be checked automatically and they are compiled into just an empty object.

It would be nice to have tests here, though I'm not quite sure where to put them. Just create a new sample with useInversify enabled and copy the default test setup? @TiFu

My previous PR #6425, is still included here, so if that is merged I can rebase.

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

CC @TiFu
CC TS Committee @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02)

Possible next steps

  • Bind to provider and dynamic import
  • More convenience method on the ApiServiceBinder

@macjohnny
Copy link
Member

cc @gualtierim

@gualtierim
Copy link
Contributor

@bodograumann the idea of make this feature optionally it's great to not create a breaking change. In my experience the configuration never required any parameter, but it is a possible scenario. I approve these changes.

@TiFu
Copy link
Contributor

TiFu commented May 29, 2020

It would be nice to have tests here, though I'm not quite sure where to put them. Just create a new sample with useInversify enabled and copy the default test setup? @TiFu

Yeah just create a new sample

  • Modify bin/typescript.sh, generate sample into a new subfolder
  • Copy files from samples/oai3/client/petstore/typescript/builds/default/* that are not generated by bin/typescript.sh to the new folder
  • Create the test cases in samples/oai3/client/petstore/typescript/tests/[subfolder] by copying whatever you need from tests/default, change the project name in the pom.xml` in the test directory
  • Add the test directory to the root pom.xml
  • Add the node_modules folder to .travis.yml

@TiFu
Copy link
Contributor

TiFu commented Jun 4, 2020

@bodograumann I just integrated the feedback from macjohnny into ts-refactor-master (cmp. #6341 ). Could you please update this PR?

FYI: I added a new type called ObjectParamAPI that accepts objects as function parameters and uses either PromiseAPI or ObservableAPI internally depending on the cli args. This probably affects this PR.

@bodograumann
Copy link
Contributor Author

Thank you for the notification @TiFu. When I get back to this task I will take those changes into account.

@bodograumann
Copy link
Contributor Author

I have rebased and add the small change. Still need to add suitable tests and add inversify support to the new object_params api.

@bodograumann
Copy link
Contributor Author

I think this should be ready.
Any idea what is going wrong with the tests, @TiFu?

@TiFu
Copy link
Contributor

TiFu commented Jun 11, 2020

Travis complains about Project 'org.openapitools:TypeScriptDefaultPetstoreClientTests:1.0-SNAPSHOT' is duplicated in the reactor

Solution: Rename TypeScriptDefaultPetstoreClientTests in pom.xml in one of the test directories in samples/oai3/petstore/client/ts/*.

CircleCI complains about some changes to docs/contributing.md - probably auto-generated from one of the files in bin/utils/*

@bodograumann
Copy link
Contributor Author

Yeah, no more failed checks 🎉
Feel free to merge, @TiFu 😉

@TiFu
Copy link
Contributor

TiFu commented Jun 13, 2020

Yay 🎊 Great work!

Thanks for the PR which was merged into ts-refactor-master

@TiFu TiFu merged commit 4608264 into OpenAPITools:typescript-refactor-master Jun 13, 2020
TiFu added a commit that referenced this pull request Jun 14, 2020
macjohnny added a commit that referenced this pull request Jun 15, 2020
… jquery (#6341)

* Added http module draft

* Added generic enum

* Modified http lib, added config & middleware definition to ts-fetch

* Added model generation with imports

* Added auth module

* Added servers

* Added sample for typescript client

* WIP: Models & API

* Updated auth

* WIP: api modeling

* Implemented RequestFactory and Processor completely

* Implemented fetch client

* Ignore dist folder in typescript client sample

* Added middleware to fetch

* Restructured TypeScript generator

* Reverted: http library.send returns string again

* Removed TODOs

* Added pom.xml files to TypeScript PetStore client samples

* Removed tabs from TypeScriptClientCodegen

* Added ts client codegen to root pom.xml and travis

* Added server variable configuration to ts-refactor

* [TS-Refactor] Added tests for Object Serializer

* Added simple test for PetApi

* Fixed ObjectSerializer test

* Added handling for different http status codes and test for deletePet

* Removed tabs in TypeScriptClientCodegen

* Removed tabs in DefaultCodegen

* Additional tests for pet store api

* Fixed file uploads

* Made api call configuration separately settable

* Use string union for enums

* Remove tab

* Restructured module layout

* Use observables internally

* Added promise based middleware

* Made discriminator and attributeTypeMap readonly

* Configure discriminator correctly

* Set discriminator value automatically

* Fixed date-time and date handling

* Added comments & license info

* Added comments

* Ignore openapi-generator-cli/bin

* Removed accidentally created generated code

* Fixed compilation issues in TypeScriptClientCodegen

* Added typescript to docs/generators

* Updated docs

* Added gitignore and git_push

* Added jquery library

* Added pom.xmls, fixed packagejsons and hopefully webppack

* Removed tabs in TypeScriptClientCodegen

* Fixed a couple issues with pom.xml

* Ensured up to date

* Fixed missing fetch definition in TS default tests

* Updated typescript docs

* Refactor typescript merge master (#4319)

Merge master into ts-refactor

* Typescript refactor: stub rxjs (#4424)

* Remove unused supportsES6 field from codegen

* Add a new switch for RXJS

* Remove redundant npm dependency on rxjs4 types

* Fix return type of PromiseMiddleware methods

* Install webpack dependency to run jquery tests

* Update form-data to 2.5 which includes typings

* Add missing dependency on node typings

* Fix test artifact name typo

* Stub rxjs when it is not explicitly enabled

* Typescript refactor: Platform select for browser and node (#4500)

* Use string form of filename parameter

This works for the form-data library and is also compatible with the
browser FormData object.

* Add new option to select platform node or browser

When no platform is selected, a default is chosen by the framework
option and likewise the file data type option is implied by the
platform.

* Remove redundant import of node dns module

* Only use form-data library for node platform

* Generate npm package from npmName option

* Use method convertPropertyToBooleanAndWriteBack

* Generate typescript samples with ensure-up-to-date

* Removed tab from DefaultCodegen

* Readded missing change

* Mark typescript client codegen as experimental

* Removed whitespace

* [TS-Refactor] Top-level exports for fetch & jquery (#6138)

* Added top-level exports
* Updated generator README
* Updated typescript generator docs

* Allow browsers File type for files (#5521)

* Allow passing file parameters as File objects
* Add test for jquery upload
* Use HttpFile object for node platform
* Regenerate samples

This is by far the most common use case. A `File` object already
contains the name attribute. This commit allows that information to be
used directly.
When sending a `Blob`, in most browsers the `File` constructor can be
used to assign a file name. In all other browsers the alternative is
```typescript
Object.assign(data, { name: "foobar.txt" });
```
That is why we explicitely pass the name as third parameter to
`FormData.append`. This `Object.assign` method also works for `Buffer`
objects in node.

If one really does not want to touch the data object in the browser it
is possible to define another reference to the data with
```typescript
new Blob([data], { type: data.type })
```
or in node via
```typescript
Buffer.from(data)
```

* [TS-Refactor] Added options for npm version, repository, name and updated readme (#6139)

* Added options for npm version, repository, name and updated readme

* Removed `this`  where not required

* Updated typescript docs

* Typescript refactor fixes (#6027)

Fixes a handful of issues identified in #802 (comment)

List of changes

* Clean: Remove redundant cliOption definition

* Remove redundant directory structure in templates

If we need to have different index.ts files for the different
frameworks, we can mostly do that in the one mustache file. In the cases
where that is not possible, we can still add a new override file later.

* Use File.separator consistently

* Only export selected api type

* Simplify promise polyfill import

The behaviour should be the same, according to the es6-promise docs.
Previously tsc would report the error:
> error TS2307: Cannot find module 'es6-promise'.

* Import HttpFile in all models

* Export server configurations

* Use undefined as default body value

The empty string is not interpreted as "no body" by the browser fetch
api and thus leads to an exception during get requests

* Improve codestyle: prefer guards to nesting

* Remove verbose debug output

This should not be commited, because every developer has very different
requirements what debug information he needs to see.

* Fix: Use cleaned model names for imports

* Fix: do not call toString on undefined

* Fix typo in doc comment

* Introduce RequestBody type and remove method check

* Support media types other than json (#6177)

List of changes:

* Add */* as fallback to accept header

* Use more sophisticated media type selection

* Handle object stringify in ObjectSerializer

* Parse response with ObejctSerializer

* Fix: Correctly extract response headers in browser

* Create HttpFile objects from responses

* Handle binary responses

* Clean up dependencies and replace isomorphic-fetch

Instead of isomorphic-fetch, which is unmaintained, we directly use
node-fetch and whatwg-fetch polyfills.

* Updated versions in ts-default/jquery and ts docs

* Replaced isSuccessCode with is2xx

* [TypeScript-Refactor] Use OAIv3 spec and fix bugs in JQuery Blob download (#6416)

* Change to OAIv3 spec for TS-Refactor

* Moved samples to oaiv3 folder

* Updated package-lock

* Update pom to use OAIv3 paths for Typescript-refactor

* Renamed ts-refactor samples & tests in pom.xmls

* Fixed compile issues in ts-refactor jquery http test

* Fixed bugs in blob handling of jquery

* [Typescript] Support http bearer authentication with token provider (#6425)

* Add http bearer security

* Update typescript to 3.9

* Fix: Use Authorization header for basic and bearer

* Allow asynchronous tokenProvider in bearer auth

* Add TS-Rewrite-Jquery tests node_modules to travis caching

* Remove NoAuthentication

* Added file to generate TS samples on Windows

* Exclude btoa in browser

* Regen samples

* Remove outdated ToDo comments

* Document and optimize `getReturnType` in TSClientCodegen

* Added option to generate objects for operation function arguments

* Upgrade typescript docs

* Updated generators

* Updated samples

* Updated docs

* Readded pom.xml

* [Typescript] Support InversifyJS (#6489)

* Add config option to enable InversifyJS

* Add pascal case lambda for mustache

* Generate a class for each auth method

* Add service identifiers and service binder helper

* Split Configuration into interface and factory

This way we don't need to import the factory everywhere to do
typechecking.

* Define minimal interface for ServerConfiguration

* Add annotations for inversify when enabled

* Always expose list of server configurations

* Add samples and defalt tests for useInversify

* Simplify sample generation script

* Fix: Add object_params arg description to help

* Fix: Properly enable inversify with bool property

* Build tests in pom instead of prepublish

Otherwise running `npm install`, when the build failed was impossible.

* Update dependencies for inversify tests

* Test basic api service resolution

* Remove Promise and Observable prefix from exports

* Fix, RxJS: Import Observable in object params api

* Add ioc service identifier for object param api

* Add hint about unimpeded development

* Simplify api service binder syntax

* Remove default tests for inversify

* Add wrapper for easy promise based http libraries

This wrapper allows defining and injecting http libraries that do not
need to know anything about observables, especially when useRxJS is not
enabled. I will employ this in the tests for InversifyJS.

Not sure if we should also use this wrapper internally.

* Add named injects for remaining auth parameters

* Directly inject promise services without RxJS

* Add tests for api service binder

* Add convenience method to bind all api services

* Fix: Rename inversify test artifact

* Run bin/utils/copy-to-website.sh

* Restore changes to CONTRIBUTING.md from PR #6489

Co-authored-by: Bodo Graumann <mail@bodograumann.de>
Co-authored-by: Esteban Gehring <esteban.gehring@bithost.ch>
@wing328 wing328 added this to the 5.0.0 milestone Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants