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(typescript): add typescript definitions #19

Merged
merged 3 commits into from
Jun 15, 2019

Conversation

hspedro
Copy link
Contributor

@hspedro hspedro commented Jun 4, 2019

The default export is the fastifySensible plugin, whereas the http
errors are also being namely exported on index.d.ts.

The need came from a project using TypeScript, in which we would
like to augment fastify plugin using fastify-sensible's httpErrors. Thus,
needing the definitions.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Would you mind adding a unit test?

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

Good work! As @mcollina said, if you can add unit test it would be great!

@hspedro
Copy link
Contributor Author

hspedro commented Jun 5, 2019

Sure guys! Will add some unit tests

@hspedro
Copy link
Contributor Author

hspedro commented Jun 6, 2019

Added some types test, I've used fastify-jwt project as a base for it. Also, used the JS tests as a reference to the modules and types being tested using tsc.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

package.json Outdated
@@ -23,6 +23,9 @@
},
"homepage": "https://github.com/fastify/fastify-sensible#readme",
"devDependencies": {
"@types/http-errors": "^1.6.1",
"@types/proxy-addr": "^2.0.0",
"@types/vary": "^1.1.0",
Copy link
Member

Choose a reason for hiding this comment

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

It seems these are used in the types. These should be dependencies, but I’m -1 for that. Would you mind just integrating those types with ours?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @mcollina , I don't mind doing that. Another alternative is to upload fastify-sensible types definition on @types domain (https://github.com/DefinitelyTyped/DefinitelyTyped). What do you think about this option?

Copy link
Member

Choose a reason for hiding this comment

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

I'm -1 to that, types on @types tend to be of lower quality, harder to maintain, and essentially more problematic than the ones we maintain directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mcollina , there is a caveat to that... @types/proxy-addr and @types/vary depend on @types/node, we use in import * as http from 'http'. The @types/node has a lot of definitions that are used by http.d.ts, thus the need to import everyone. Is this a problem? If so, we can either leave @types/node as dependency or simply @types/proxy-addr and @types/vary.

Copy link
Member

Choose a reason for hiding this comment

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

@types/node is fine, all ts users will add that.

Choose a reason for hiding this comment

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

@mcollina why not just peerDependencies the other packages?

Copy link
Member

Choose a reason for hiding this comment

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

npm will install them anyway.

package.json Outdated
@@ -28,6 +28,9 @@
"tap": "^12.1.1"
},
"dependencies": {
"@types/http-errors": "^1.6.1",
"@types/proxy-addr": "^2.0.0",
"@types/vary": "^1.1.0",
Copy link
Contributor Author

@hspedro hspedro Jun 10, 2019

Choose a reason for hiding this comment

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

@mcollina , just to clarify, the requested changes were regarding using @types as dependencies, instead of devDependencies, or importing those types definitions (http-errors.d.ts, proxy-addr.d.ts, vary.d.ts and its other dependencies) inside the project itself?

Copy link
Member

Choose a reason for hiding this comment

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

I'm exactly -1 to this approach. We should not do this.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

@Ethan-Arrowood Ethan-Arrowood Jun 10, 2019

Choose a reason for hiding this comment

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

@hspedro we'd like to keep types as simple as possible with as minimal dependencies as possible too. The solution here is to implement any relevant parts of @types/http-errors in the type files so we do not need to rely on the package as a dependency. If things change in the future we can update the types in this repo and ship without having to deal with any external things.

For example you're using this type definition from @types/http-errors:

 interface HttpError extends Error {
        status: number;
        statusCode: number;
        expose: boolean;
        headers?: {
            [key: string]: string;
        };
        [key: string]: any;
}

This is a very simple interface that could be copied in your type file.

I hope this clarifies some things!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it @Ethan-Arrowood. Thanks, will update with requested changes as soon as possible.

Copy link
Contributor Author

@hspedro hspedro Jun 11, 2019

Choose a reason for hiding this comment

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

@Ethan-Arrowood , I've managed to refactor the dependencies for the vast majority. But, how about the more complex types such as HttpServer that comes from @types/node, can I leave @types/node as a project dep or should I reimplement the http.Server, http.IncomingMessage, http.ServerResponse?

Copy link
Member

Choose a reason for hiding this comment

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

Leave @types/node as a dependency. If users are using Fastify with TypeScript they will have it installed so it is okay for us to include.

@hspedro hspedro force-pushed the ts-definitions branch 2 times, most recently from 361a430 to d5a6081 Compare June 11, 2019 14:36
package.json Outdated
@@ -23,11 +23,12 @@
},
"homepage": "https://github.com/fastify/fastify-sensible#readme",
"devDependencies": {
"fastify": "^2.0.0-rc.2",
"fastify": "^2.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

update fastify to latest v2 release

Copy link
Contributor Author

@hspedro hspedro Jun 11, 2019

Choose a reason for hiding this comment

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

This is actually from b8985e8. Should I update to use 2.4.1 (latest from @fastify)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @Ethan-Arrowood , can you confirm request regarding this change? Thanks

Copy link
Member

@Ethan-Arrowood Ethan-Arrowood Jun 14, 2019

Choose a reason for hiding this comment

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

Actually keep it as is. I believe the ^2.0.0 resolves to latest of version 2 anyways.

tsconfig.json Outdated
"module": "commonjs",
"noEmit": true,
"strict": true,
"noImplicitAny": false,
Copy link
Member

Choose a reason for hiding this comment

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

I'd like this to be true. What types are any when this is enabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this should be true. No types should default to any

hspedro added 3 commits June 11, 2019 12:00
The default export is the fastifySensible plugin, whereas the http
errors are also being exported but namely.
The tests were based on fastify-jwt and other projects that have
type definition and types test. The test will cover types on each of the
modules: 'assert', 'errorHandler', 'forwarded', 'httpErrors', 'is',
'proxyaddr', 'to' and 'vary' (coverage was based on current JS tests).

As only the types will be checked, `tsc` is enough for that.
Added `tsconfig.json` to be used by `tsc` when package.json 'test'
script.
Copy link
Member

@Ethan-Arrowood Ethan-Arrowood left a comment

Choose a reason for hiding this comment

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

All looks good to me now

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@mcollina mcollina merged commit 7cb00bb into fastify:master Jun 15, 2019
@despian despian mentioned this pull request Jun 17, 2019
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