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 typescript definition files #100

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

rauno56
Copy link

@rauno56 rauno56 commented Apr 27, 2021

  • Rebased Added typescript definition files #76,
  • Brought typescript up to date,
  • Added all HTTP methods, as requested in the original PR,
  • Removed usages of any where possible,
  • Fixed some bugs in the definitions.

@rauno56 rauno56 force-pushed the added-types branch 2 times, most recently from f5b4ba2 to f29f620 Compare April 29, 2021 08:19
@rodion-arr rodion-arr added the pr label Apr 29, 2021
index.d.ts Outdated

route(prefix: PathParams): IRoute;
// Stack of configured routes
stack: Layer[];
Copy link
Contributor

Choose a reason for hiding this comment

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

As a general question, are the typings supposed to include all properties and methods including private ones, so should this be removed since it is not a public property?

Copy link
Author

Choose a reason for hiding this comment

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

You are right. API documented private in the code should be defined private in TS as well. I didn't pay much attention to that since

  • "it's JS - accessible anyways!" and
  • I've actually needed to access those myself.

I'll go over it and add access modifiers where appropriate next week.

Copy link
Author

Choose a reason for hiding this comment

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

@dougwilson, I looked into it a bit, but don't know a good way to implement it. I'd leave it as it is for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

I assume the way to implement is to not have it in here at all. My understanding (which is weak, please correct me if wrong) is that the TSD is just the public API, which is why there is no mechanism to mark something as private.

Copy link
Author

Choose a reason for hiding this comment

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

Commented out handle() and stack - left Layer type because it's useful for documenting the internal API, which will be inaccessible nevertheless.

mkcol: IRouterMatcher<this>;
move: IRouterMatcher<this>;
notify: IRouterMatcher<this>;
pri: IRouterMatcher<this>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually a method on there?

$ node -pe 'require(".")().pri'
undefined

Copy link
Author

@rauno56 rauno56 May 16, 2021

Choose a reason for hiding this comment

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

I took that from require('http').METHODS. v14.16.1 has it at least.

Now... this is not ideal obviously and I'm sure there's a way to handle supporting different versions better, but my current approach has been "close is better than nothing".

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, gotcha. I was using Node.js 16.1. So that does bring up a good point: I guess this should be all the methods possible in the various Node.js versions? I wonder what other methods got removed; probably should go through all the versions this module supports and union them together.

Copy link
Author

Choose a reason for hiding this comment

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

I asked around in TS discord and they didn't recall a better way to handle it than what you proposed - adding collecting all of them from all versions.

index.d.ts Outdated Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
index.d.ts Outdated
type NextFunction = (err?: Error | "route" | "router") => void;
type Callback = (err?: Error) => void;

type ErrorRequestHandler = (err: Error, req: IncomingRequest, res: http.ServerResponse, next: NextFunction) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

We may not want the err to be typed as just a Error object, as in the 1.x of this router, there is no enforcement on that; using a middleware you didn't write could have a string or plain object here, which I've seen a lot. We wouldn't want someone's TS code to think they don't need some kind of guard on the err value.

Copy link
Author

@rauno56 rauno56 Jun 29, 2021

Choose a reason for hiding this comment

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

What's the more appropriate type for that then? any?

Copy link
Contributor

Choose a reason for hiding this comment

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

That is probably fine. It does have to be truthy, though, so not sure if that helps at all.

Copy link
Author

Choose a reason for hiding this comment

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

Changed it to any.

@dougwilson dougwilson force-pushed the master branch 2 times, most recently from 7e90c9e to 8643ec6 Compare May 19, 2021 16:51
@rauno56
Copy link
Author

rauno56 commented May 21, 2021

Rebased and reimplemented the CI changes on GHA.

@terry-au
Copy link

Is there anything blocking this PR from being merged into master?

@rauno56
Copy link
Author

rauno56 commented Jun 29, 2021

Except for #100 (comment), I would not block it.

index.d.ts Outdated
route?: IRoute
}

type RequestParamHandler = (req: IncomingRequest, res: http.ServerResponse, next: NextFunction, value: string, name: string) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

I tried to figure out where it went wrong in the TS, and I think this line is the culprit: inside of the router.param callback, there is req.params, req.route, and all the other things available. I think that this should be the "routed request" type here.

Copy link
Author

Choose a reason for hiding this comment

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

Changed that to RoutedRequest.

I'm leaving `any | "route" | "router"` for NextFunctions param to
document the special cases, even though the union doesn't actually widen
`any` further.
I'm going to leave Layer type for documentation again and because it
might be useful in cases where someone bypasses to the private API(my
case). For example when writing OTel instrumentations.
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.

5 participants