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

Add generated models and factories types #225

Conversation

oleksandrpravosudko-okta
Copy link
Contributor

@oleksandrpravosudko-okta oleksandrpravosudko-okta commented Feb 16, 2021

Resolves of OKTA-241753

  • add generated *.d.ts files
  • update return type Promise<Collection> to Collection for model methods and generated client
  • update return type Promise<undefined> to Promise<Response> for model methods and generated client
  • mark optional parameters

@oleksandrpravosudko-okta oleksandrpravosudko-okta force-pushed the op-okta-291118-operations-typescript-definitions branch from 59c916f to cb3980a Compare February 16, 2021 15:26
@oleksandrpravosudko-okta oleksandrpravosudko-okta force-pushed the op-okta-241753-models-typescript-definitions branch 2 times, most recently from fe2e929 to 5aa2dee Compare February 16, 2021 15:33
Base automatically changed from op-okta-291118-operations-typescript-definitions to op-ts-types-support February 24, 2021 12:57
@oleksandrpravosudko-okta oleksandrpravosudko-okta force-pushed the op-okta-241753-models-typescript-definitions branch from 5aa2dee to 9e81035 Compare February 24, 2021 13:09
@oleksandrpravosudko-okta oleksandrpravosudko-okta force-pushed the op-okta-241753-models-typescript-definitions branch from 74f3a88 to 63e65b4 Compare February 26, 2021 11:15
@oleksandrpravosudko-okta oleksandrpravosudko-okta marked this pull request as ready for review February 26, 2021 17:44
@oleksandrpravosudko-okta oleksandrpravosudko-okta force-pushed the op-okta-241753-models-typescript-definitions branch 3 times, most recently from cb6968b to f52ad1a Compare March 4, 2021 17:18
Comment on lines 14 to 16
/**
* THIS FILE IS AUTO-GENERATED - SEE CONTRIBUTOR DOCUMENTATION
*/
Copy link
Contributor

@swiftone swiftone Mar 4, 2021

Choose a reason for hiding this comment

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

This does raise an issue - these files are autogenerated, yes? Can we add a comment to that extent? I lost a day recently in another codebase editing a generated file and it is not fun to discover.

Or are the Factories not generated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Handlebars files are not autogenerated but they contain this banner so it gets into generated .d.ts and .js files. This template is deleted because there seem to be no need in having index file for model types.

It makes sense to have it for for JS files so all models can be imported into GeneratedClient using Node syntax and referenced in methods bodies. For type definitions we import model types that are referenced by methods signatures explicitly.

Thanks for mentioning this - it looks like some of templates do not have warning banner included - going to fix that.

@oleksandrpravosudko-okta oleksandrpravosudko-okta force-pushed the op-okta-241753-models-typescript-definitions branch 4 times, most recently from e20069a to e66f50b Compare March 11, 2021 17:10
@@ -0,0 +1,21 @@
/*!
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we probably can get rid of the banners from the templates as we already have logic to handle it in one place (https://github.com/okta/okta-sdk-nodejs/blob/master/package.json#L14)

@shuowu
Copy link
Contributor

shuowu commented Mar 11, 2021

LGTM! Please squash the commits when merge to the upper-level branch.

@oleksandrpravosudko-okta oleksandrpravosudko-okta force-pushed the op-okta-241753-models-typescript-definitions branch from 8b81ec3 to 34d57b7 Compare March 12, 2021 12:33
@oleksandrpravosudko-okta oleksandrpravosudko-okta merged commit c55ffd5 into op-ts-types-support Mar 12, 2021
@oleksandrpravosudko-okta oleksandrpravosudko-okta deleted the op-okta-241753-models-typescript-definitions branch March 12, 2021 13:11
eng-prod-CI-bot-okta pushed a commit that referenced this pull request Mar 16, 2021
add generated models and factories types (#225)

* WIP on generating TS type declarations for operations

* add templates for type files and add template helper functions

* move typings to src/types

* re-use signature getter for models and generated client

* add types index and remove unused templates

* unify export syntax

* reduce blocklisted models filtering duplication

* export model types

* add factories templates and push generated model types

* switch to named model exports

* actually run eslint on templates dir

* set proper return type for Collection and mark optional arguments

* mark read-only properties

* do default export models along with named export

* expose Client type and add missing types

* add re-generated models and remove manually-generated types

* restore array type value and read-only modifier

* bring models named export back

* generate factories types

* export GeneratedClient
* to be switched to Client in the downnstream branch

* specify correct types for Model constructor arguments

* import Client for all models

* remove default exports from models

* remove models and factories index template

* add extra newline for generated models, remove indents from type map, do not re-export model types

* move eslint target for templates to downstream branch

* add missing 'autogenerated' warning banner to templates

* remove extra whitespaces, regenerate files

* parametrize Collection return type in generated files

* include generic type into imports list and re-generate types

* preserve non-optinal query parameters

* correct return type to be Response instead of undefined, include CRUD return type into imports

* CR: remove copyright banners from templates
Add type definitions for core objects, add typescript-eslint-plugin + tsd (re-opened) (#230)

* WIP on generating TS type declarations for operations

* WIP on adding types for core objects

* update type references in core objects

* WIP on adding types for core objects

* update type references in core objects

* run typescript-eslint on src/types

* WIP on fixing ts-lint errors

* represent object type as Record<string, unknown>

* define RequestOptions type

* define more specific type for core objects

* add basic tsd test

* install @types/node-fetch types

* specify sourceType: module for types eslint config

* add more tsd assertions

* complete basic coverage for core object types assertions

* include tsd run into test target

* export types matching code exports

* fix syntactic error in command

* fix indents and apply basic eslint rules to definitions files

* parametrize Collection type definition

* list known config properties, update jwt function return type

* widen type for cache middleware next function

* replace MemoryStore references with CacheStorage

* extract IRequestExecutor interface, make MemoryStore parameters optional

* allow request executors w/o event emission

* correct types for collection subscribers

* CR: cascade eslint configuration

* fix revealed eslint errors

* move type tests to a separate directory

* CR: break down type tests

* add type assertions for generated client methods

* use named import for Response

* allow imports for create and delete operations only

* remove unused import

* fix rebase artifact
remove irrelevant changes
update changelog entry for 4.5.0

OKTA-373751
<<<Jenkins Check-In of Tested SHA: 1013098 for [email protected]>>>
Artifact: okta-sdk-nodejs
@theseyi
Copy link

theseyi commented Apr 5, 2021

Thanks for working on this. Any plans to release the types as part of the published SDK? The latest version 4.5.0 as of Apr 5, 2021 does not ship with types even though it is referenced in the package manifest @oleksandrpravosudko-okta

@oleksandrpravosudko-okta
Copy link
Contributor Author

oleksandrpravosudko-okta commented Apr 5, 2021

@theseyi Types look available in 4.5.0. Would you mind describing a scenario in which you are not able to use them?

@theseyi
Copy link

theseyi commented Apr 5, 2021

@theseyi Types look available in 4.5.0. Would you mind describing a scenario in which you are not able to use them?

Hey @oleksandrpravosudko-okta this was my fault. I just updated to 4.5 from 4.4 and it took a second for my editor to pick up the types folder in node_modules. I've just reverified and the types are there as expected. Thanks for responding, and sorry for the distraction! 😓

@theseyi
Copy link

theseyi commented Apr 6, 2021

Is it possible to make accessible externally, the User class?

Trying to import from src/types/models/User raises the following typescript error since it is not exported under index.d.ts

Error: Cannot find module '@okta/okta-sdk-nodejs/src/types/models/User'
Require stack:

@oleksandrpravosudko-okta
Copy link
Contributor Author

Hi @theseyi - types that are imported 'outside' of index.d.ts can not be used in new ClassName() expressions as it results in attempt to load type modules in runtime with commonJS loader.

Unfortunately, there is an issue with generated client's method signatures (some methods expect model instances to be passed which should not be the case). I am working on getting those fixed and will release a new version afterwards - sorry for inconvenience.

@theseyi
Copy link

theseyi commented Apr 6, 2021

Hi @theseyi - types that are imported 'outside' of index.d.ts can not be used in new ClassName() expressions as it results in attempt to load type modules in runtime with commonJS loader.

I see, thanks for clarifying @oleksandrpravosudko-okta. I was looking at the example you provided and on line1: import { Application } from "@okta/okta-sdk-nodejs/src/types/models/Application"; so in reality I'm assuming the example should not work since Application not explicitly exported in the index? or is there a difference here?

Unfortunately, there is an issue with generated client's method signatures (some methods expect model instances to be passed which should not be the case). I am working on getting those fixed and will release a new version afterwards - sorry for inconvenience.

Thanks for looking into this

@oleksandrpravosudko-okta
Copy link
Contributor Author

@theseyi please check out 4.5.1 release.

There is a short section in 4.5 README describing the suggested usage.

so in reality I'm assuming the example should not work since Application not explicitly exported in the index? or is there a difference here?

Unless I am misunderstanding the question, import { Application, ApplicationOptions } from "@okta/okta-sdk-nodejs/src/types/models/Application" should work as long as Application and ApplicationOptions are used as types only - please let me know if you are referring to a different scenario.

@theseyi
Copy link

theseyi commented Apr 9, 2021

I thought I saw new Application(...) in the original example, seems like it misread, thanks. I guess that means exposing the User class at runtime for instantiation is not a planned implementation?

@oleksandrpravosudko-okta
Copy link
Contributor Author

@theseyi Right, imports from src/types/* are intended for specifying types only. There are community suggestions on this approach being non-optimal - I'll see if this can be changed.

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.

4 participants