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

feat: Add Typescript definitions #184

Merged
merged 37 commits into from
Sep 9, 2019
Merged

feat: Add Typescript definitions #184

merged 37 commits into from
Sep 9, 2019

Conversation

theogravity
Copy link
Contributor

@theogravity theogravity commented Sep 5, 2019

I've handcrafted TS defs that is based on the API documentation and analysis of the project files.

We have had no problems using it in a project we're working on so far.

I'm unsure if you've worked with Typescript or not, but I've done my best to make sure that for return types where a class is involved, that an interface is returned instead.

This allows for the creation of a mocked class when writing unit tests, vs doing class definition overrides.

An exception case would be TokenSet as it doesn't do anything complicated and is pretty much a plain object under the hood.

@theogravity theogravity mentioned this pull request Sep 5, 2019
@panva
Copy link
Owner

panva commented Sep 5, 2019

Whoa! @theogravity this is great, I will go through the PR to make sure nothing was missed soon.

Copy link
Owner

@panva panva left a comment

Choose a reason for hiding this comment

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

@theogravity i did a first pass review and identified a number of things that need fixing, in addition to those we neeed to add generators and the customization functionality, some of this is already part of the community maintained DefinitelyTyped.

I appreciate the effort you've put into this.

lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated
/**
* Returns the <Client> class tied to this issuer.
*/
Client: { new (metadata: IClientMetadata, jwks?: object) }
Copy link
Owner

Choose a reason for hiding this comment

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

dtto as jwks above.

@panva
Copy link
Owner

panva commented Sep 5, 2019

@ulrichb @YangusKhan @RMHonor as the three contributors to the DefinitelyTyped types, i would appreciate your review here, as well as believe its in your best interest since it would replace the types you've been maintaining.

lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated
*/
deviceAuthorization(parameters?: IDeviceAuthParameters, extras?: IDeviceAuthExtras): Promise<IDeviceFlowHandle>

[key: string]: any
Copy link

Choose a reason for hiding this comment

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

Are you sure we want this extension here? We shouldn't be assuming there are additional properties on a client

lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated
*/
keystore (forceReload?: boolean): Promise<JWKS.KeyStore>

[key: string]: any
Copy link

Choose a reason for hiding this comment

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

As above, should an Issuer allow additional types like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My linter fails if it's not included since it's implemented in IIssuer

lib/index.d.ts Outdated Show resolved Hide resolved
@big-kahuna-burger
Copy link
Contributor

Can we also have Strategy exported for the sake of completeness?

@panva
Copy link
Owner

panva commented Sep 5, 2019

I'd like someone to write .ts test file using the types so that we may have a CI job verifying them. Any takers?

@big-kahuna-burger
Copy link
Contributor

I'd like someone to write .ts test file using the types so that we may have a CI job verifying them. Any takers?

@panva I will take it (via tsd). Expect PR later in the evening.

@Bessonov
Copy link

Bessonov commented Sep 5, 2019

Great! While it's better to have definitions instead of not to have definitions at all... wouldn't it better to convert the project to typescript? What do you think @panva ? Maybe step by step.

I'd like someone to write .ts test file using the types so that we may have a CI job verifying them.

I'm not sure that this would help to check definitions. AFAIK you will check fictional[1] definitions with fictional[2] tests.

[1] fictional, because they can be not related to concrete implementation.
[2] fictional, because it doesn't make much sense to create high coverage tests just for checking typings.

Maybe it's better to convert all tests to ts as a first step?

@panva
Copy link
Owner

panva commented Sep 5, 2019

Let's park the tests for now until we're satisfied with the state of the definition.

@Bessonov exposing types is as far as I'm willing to go at this point in time.

@theogravity
Copy link
Contributor Author

Thank you everyone for the quick turnaround!

I've addressed the most recent comments - apply JSONWebKeySet, and remove the DeviceFlowHandle def.

panva
panva previously requested changes Sep 5, 2019
lib/index.d.ts Outdated Show resolved Hide resolved
lib/index.d.ts Outdated Show resolved Hide resolved
@panva
Copy link
Owner

panva commented Sep 5, 2019

@theogravity thank!

Re-iterating on the rest of missing pieces

In addition to those we neeed to add generators and the customization functionality, some of this is already part of the community maintained DefinitelyTyped.

And also the errors should probably be exported?

I appreciate the effort you've put into this.

@panva panva dismissed their stale review September 5, 2019 17:45

already in place

@panva panva merged commit c37130b into panva:master Sep 9, 2019
@panva
Copy link
Owner

panva commented Sep 9, 2019

Thank you all for feedback and contributions. This has landed in 3.7.0

@ulrichb
Copy link

ulrichb commented Sep 9, 2019

If the consuming project doesn't have @types/got installed, you get:

error TS7016: Could not find a declaration file for module 'got'.

=> Should we move @types/got from the dev-dependencies to the dependencies, so that TS compilation works out of the box?

@panva
Copy link
Owner

panva commented Sep 9, 2019

I intentionally did not do that, I don't see why non-ts users should have this as a dependency.

Maybe add a typescript readme section with this detail? Is that a big deal? Aren't ts users used to installing types? I assumed they are.

@RMHonor
Copy link

RMHonor commented Sep 9, 2019

My first instinct was to put it in peer dependencies, as it then at least notifies the user. But perhaps optional dependencies would be more appropriate? Although it's not an approach I've encountered before

@panva
Copy link
Owner

panva commented Sep 9, 2019

@RMHonor neither of these two applies

optionalDependencies

If a dependency can be used, but you would like npm to proceed if it cannot be found or fails to install, then you may put it in the optionalDependencies object.

peerDependencies

In some cases, you want to express the compatibility of your package with a host tool or library, while not necessarily doing a require of this host.

@ulrichb
Copy link

ulrichb commented Sep 9, 2019

@panva The (small) problem exists if you want to use node-openid-client, but don't use got in the consuming project. Then a) you first get the compile error mentioned above and b) you have to add an unnecessary @types\got package dependency in the consuming project.

Optional dependency looks like a good option :).

@panva
Copy link
Owner

panva commented Sep 9, 2019

Optional dependency looks like a good option :).

It isn't, it still installs for everyone. It just that it can optionally fail to install.

@panva
Copy link
Owner

panva commented Sep 9, 2019

you have to add an unnecessary @types\got package dependency in the consuming project.

How is it unnecessary? If you're using TS you need those types. If you don't you don't have to install it.

@RMHonor
Copy link

RMHonor commented Sep 9, 2019

Something like optionalPeerDependencies would be perfect. I'd be inclined to say that it's fine to leave if as it is, and a small brief in the documentation around installing the 3rd party type definitions.

@ulrichb
Copy link

ulrichb commented Sep 9, 2019

@panva:

How is it unnecessary?

If you don't use got directly in the consuming project (only indirectly via node-openid-client). Still without having @types\got installed you immediately get a compile error in this line:

https://github.com/panva/node-openid-client/blob/45402ede49375ed9bd9660f39d544f4483340bff/types/index.d.ts#L10

=> "error TS7016: Could not find a declaration file for module 'got'"

@panva
Copy link
Owner

panva commented Sep 9, 2019

In that case, i'm keen to remove the type dependency altogether and make it

{
  [key: string]: unknown;
}

and jsdoc add @see https://github.com/sindresorhus/got/tree/v9.6.0#options

@ulrichb
Copy link

ulrichb commented Sep 9, 2019

Why is a normal dependency so bad? Packages often pull dependencies which aren't used in all cases / for all users.

An example: express has a dependency to cookie (or cookie-signature, or serve-static) which obviously isn't used by every express user.

@ulrichb
Copy link

ulrichb commented Sep 9, 2019

Another argument: Also other packages to it that way.

For example react-intl (just an example; it was the first one I found in my project):

https://github.com/formatjs/react-intl/blob/899d17239545717283b1d539b011240428e9948d/package.json#L39-L40

@panva
Copy link
Owner

panva commented Sep 9, 2019

Why is a normal dependency so bad? Packages often pull dependencies which aren't used in all cases / for all users.

Packages often do, I choose not to. @types/got has three dependencies which also pack other transitive dependencies.

Showcasing bloated packages that do this is not an argument if i'm honest.

I was about to add the dependency then i checked what it means in terms of other transitive dependencies, so instead I'll move forward with the removal of that import altogether replacing it with the actual object with properties that make sense.

@panva
Copy link
Owner

panva commented Sep 9, 2019

v3.7.1 fixes this.

@ulrichb
Copy link

ulrichb commented Sep 9, 2019

I'll move forward with the removal of that import altogether replacing it with the actual object with properties that make sense.

👍

@ulrichb
Copy link

ulrichb commented Sep 9, 2019

Just tested it. Compiles fine without @types\got installed.

Many thanks @panva!

@theogravity
Copy link
Contributor Author

Thank you everyone!

@theogravity theogravity deleted the typescript-defs branch September 9, 2019 20:28
@RMHonor
Copy link

RMHonor commented Sep 10, 2019

Thanks a lot @theogravity, really appreciate these definitions

@github-actions github-actions bot locked and limited conversation to collaborators Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants