-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
rauno56
wants to merge
14
commits into
pillarjs:master
Choose a base branch
from
rauno56:added-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
654e8c7
Add typescript definition files
ilikejames ba7e63b
fix: fix the IncommingRequest typo
rauno56 0bead50
fix: type `req` to IncomingMessage and `res` to ServerResponse
rauno56 1d5bb74
feat: sort http methods by name
rauno56 8b503dc
feat: add less common HTTP methods
rauno56 4fc5c25
feat: remove usages of any
rauno56 2cc21cd
fix: fix places that mistakenly reuse handler signatures
rauno56 e941dc2
fix: remove the workaround for callable types
rauno56 8b08465
style: avoid using var
rauno56 e7d495b
fix: remove url and method since they are present in IncomingMessage
rauno56 f384d5a
fix: remove invalid(/deprecated) param signature
rauno56 4aae496
fix: broaden error type since there are actually no guards in place
rauno56 1cbf48b
fix: remove private API from the types
rauno56 4a034cf
fix: use RoutedRequest in RequestParamHandler
rauno56 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,27 +34,32 @@ jobs: | |
- name: Node.js 0.8 | ||
node-version: "0.8" | ||
npm-i: [email protected] [email protected] | ||
npm-rm: nyc | ||
npm-rm: nyc typescript @types/node | ||
|
||
- name: Node.js 0.10 | ||
node-version: "0.10" | ||
npm-i: [email protected] [email protected] [email protected] | ||
npm-rm: typescript @types/node | ||
|
||
- name: Node.js 0.12 | ||
node-version: "0.12" | ||
npm-i: [email protected] [email protected] [email protected] | ||
npm-rm: typescript @types/node | ||
|
||
- name: io.js 1.x | ||
node-version: "1.8" | ||
npm-i: [email protected] [email protected] [email protected] | ||
npm-rm: typescript @types/node | ||
|
||
- name: io.js 2.x | ||
node-version: "2.5" | ||
npm-i: [email protected] [email protected] [email protected] | ||
npm-rm: typescript @types/node | ||
|
||
- name: io.js 3.x | ||
node-version: "3.3" | ||
npm-i: [email protected] [email protected] [email protected] | ||
npm-rm: typescript @types/node | ||
|
||
- name: Node.js 4.x | ||
node-version: "4.9" | ||
|
@@ -161,6 +166,10 @@ jobs: | |
if: steps.list_env.outputs.eslint != '' | ||
run: npm run lint | ||
|
||
- name: Test types | ||
if: steps.list_env.outputs.typescript != '' | ||
run: npm run test-types | ||
|
||
- name: Collect code coverage | ||
uses: coverallsapp/github-action@master | ||
if: steps.list_env.outputs.nyc != '' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
import * as http from 'http'; | ||
|
||
export = Router; | ||
|
||
declare namespace Router { | ||
|
||
interface RouterOptions { | ||
strict?: boolean; | ||
caseSensitive?: boolean; | ||
mergeParams?: boolean; | ||
} | ||
|
||
// Private | ||
interface Layer { | ||
name: string, | ||
handle: RequestHandler | ErrorRequestHandler, | ||
handle_request: RequestHandler, | ||
handle_error: ErrorRequestHandler, | ||
match: (path: string) => boolean, | ||
} | ||
|
||
interface IncomingRequest extends http.IncomingMessage { | ||
originalUrl?: string, | ||
params?: { | ||
[key: string]: any, | ||
}, | ||
} | ||
|
||
interface RoutedRequest extends IncomingRequest { | ||
baseUrl: string, | ||
next?: NextFunction, | ||
route?: IRoute | ||
} | ||
|
||
type RequestParamHandler = (req: RoutedRequest, res: http.ServerResponse, next: NextFunction, value: string, name: string) => void; | ||
|
||
type RouteHandler = (req: RoutedRequest, res: http.ServerResponse, next: NextFunction) => void; | ||
type RequestHandler = (req: IncomingRequest, res: http.ServerResponse, next: NextFunction) => void; | ||
|
||
type NextFunction = (err?: any | "route" | "router") => void; | ||
type Callback = (err?: any) => void; | ||
|
||
type ErrorRequestHandler = (err: any, req: IncomingRequest, res: http.ServerResponse, next: NextFunction) => void; | ||
|
||
type PathParams = string | RegExp | Array<string | RegExp>; | ||
|
||
type RequestHandlerParams = RouteHandler | ErrorRequestHandler | Array<RouteHandler | ErrorRequestHandler>; | ||
|
||
interface IRouterMatcher<T> { | ||
(path: PathParams, ...handlers: RouteHandler[]): T; | ||
(path: PathParams, ...handlers: RequestHandlerParams[]): T; | ||
} | ||
|
||
interface IRouterHandler<T> { | ||
(...handlers: RouteHandler[]): T; | ||
(...handlers: RequestHandlerParams[]): T; | ||
} | ||
|
||
interface IRouter { | ||
/** | ||
* Map the given param placeholder `name`(s) to the given callback(s). | ||
* | ||
* Parameter mapping is used to provide pre-conditions to routes | ||
* which use normalized placeholders. For example a _:user_id_ parameter | ||
* could automatically load a user's information from the database without | ||
* any additional code, | ||
* | ||
* The callback uses the same signature as middleware, the only differencing | ||
* being that the value of the placeholder is passed, in this case the _id_ | ||
* of the user. Once the `next()` function is invoked, just like middleware | ||
* it will continue on to execute the route, or subsequent parameter functions. | ||
* | ||
* app.param('user_id', function(req, res, next, id){ | ||
* User.find(id, function(err, user){ | ||
* if (err) { | ||
* next(err); | ||
* } else if (user) { | ||
* req.user = user; | ||
* next(); | ||
* } else { | ||
* next(new Error('failed to load user')); | ||
* } | ||
* }); | ||
* }); | ||
*/ | ||
param(name: string, handler: RequestParamHandler): this; | ||
|
||
/** | ||
* Special-cased "all" method, applying the given route `path`, | ||
* middleware, and callback to _every_ HTTP method. | ||
*/ | ||
all: IRouterMatcher<this>; | ||
|
||
use: IRouterHandler<this> & IRouterMatcher<this>; | ||
|
||
// private | ||
// handle: (req: http.IncomingMessage, res: http.ServerResponse, cb: Callback) => void; | ||
|
||
route(prefix: PathParams): IRoute; | ||
// Stack of configured routes. private | ||
// stack: Layer[]; | ||
|
||
// Common HTTP methods | ||
delete: IRouterMatcher<this>; | ||
get: IRouterMatcher<this>; | ||
head: IRouterMatcher<this>; | ||
options: IRouterMatcher<this>; | ||
patch: IRouterMatcher<this>; | ||
post: IRouterMatcher<this>; | ||
put: IRouterMatcher<this>; | ||
|
||
// Exotic HTTP methods | ||
acl: IRouterMatcher<this>; | ||
bind: IRouterMatcher<this>; | ||
checkout: IRouterMatcher<this>; | ||
connect: IRouterMatcher<this>; | ||
copy: IRouterMatcher<this>; | ||
link: IRouterMatcher<this>; | ||
lock: IRouterMatcher<this>; | ||
"m-search": IRouterMatcher<this>; | ||
merge: IRouterMatcher<this>; | ||
mkactivity: IRouterMatcher<this>; | ||
mkcalendar: IRouterMatcher<this>; | ||
mkcol: IRouterMatcher<this>; | ||
move: IRouterMatcher<this>; | ||
notify: IRouterMatcher<this>; | ||
pri: IRouterMatcher<this>; | ||
propfind: IRouterMatcher<this>; | ||
proppatch: IRouterMatcher<this>; | ||
purge: IRouterMatcher<this>; | ||
rebind: IRouterMatcher<this>; | ||
report: IRouterMatcher<this>; | ||
search: IRouterMatcher<this>; | ||
source: IRouterMatcher<this>; | ||
subscribe: IRouterMatcher<this>; | ||
trace: IRouterMatcher<this>; | ||
unbind: IRouterMatcher<this>; | ||
unlink: IRouterMatcher<this>; | ||
unlock: IRouterMatcher<this>; | ||
unsubscribe: IRouterMatcher<this>; | ||
} | ||
|
||
interface IRoute { | ||
path: string; | ||
|
||
// Stack of configured routes. Private. | ||
// stack: Layer[]; | ||
|
||
all: IRouterHandler<this>; | ||
|
||
// Common HTTP methods | ||
delete: IRouterHandler<this>; | ||
get: IRouterHandler<this>; | ||
head: IRouterHandler<this>; | ||
options: IRouterHandler<this>; | ||
patch: IRouterHandler<this>; | ||
post: IRouterHandler<this>; | ||
put: IRouterHandler<this>; | ||
|
||
// Exotic HTTP methods | ||
acl: IRouterHandler<this>; | ||
bind: IRouterHandler<this>; | ||
checkout: IRouterHandler<this>; | ||
connect: IRouterHandler<this>; | ||
copy: IRouterHandler<this>; | ||
link: IRouterHandler<this>; | ||
lock: IRouterHandler<this>; | ||
"m-search": IRouterHandler<this>; | ||
merge: IRouterHandler<this>; | ||
mkactivity: IRouterHandler<this>; | ||
mkcalendar: IRouterHandler<this>; | ||
mkcol: IRouterHandler<this>; | ||
move: IRouterHandler<this>; | ||
notify: IRouterHandler<this>; | ||
pri: IRouterHandler<this>; | ||
propfind: IRouterHandler<this>; | ||
proppatch: IRouterHandler<this>; | ||
purge: IRouterHandler<this>; | ||
rebind: IRouterHandler<this>; | ||
report: IRouterHandler<this>; | ||
search: IRouterHandler<this>; | ||
source: IRouterHandler<this>; | ||
subscribe: IRouterHandler<this>; | ||
trace: IRouterHandler<this>; | ||
unbind: IRouterHandler<this>; | ||
unlink: IRouterHandler<this>; | ||
unlock: IRouterHandler<this>; | ||
unsubscribe: IRouterHandler<this>; | ||
} | ||
|
||
interface RouterConstructor extends IRouter { | ||
new(options?: RouterOptions): IRouter & ((req: http.IncomingMessage, res: http.ServerResponse, cb: Callback) => void); | ||
} | ||
|
||
} | ||
|
||
declare const Router: Router.RouterConstructor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { createServer, IncomingMessage, ServerResponse } from 'http'; | ||
import { | ||
default as Router, | ||
RouterOptions, | ||
IncomingRequest, | ||
RouteHandler, | ||
IRoute, | ||
RoutedRequest | ||
} from '..'; | ||
|
||
const options: RouterOptions = { | ||
strict: false, | ||
caseSensitive: false, | ||
mergeParams: false | ||
}; | ||
|
||
const r = new Router(); | ||
const router = new Router(options); | ||
const routerHandler: RouteHandler = (req, res) => { | ||
res.end('FIN'); | ||
}; | ||
|
||
router.get('/', routerHandler); | ||
router.post('/', routerHandler); | ||
router.delete('/', routerHandler); | ||
router.patch('/', routerHandler); | ||
router.options('/', routerHandler); | ||
router.head('/', routerHandler); | ||
router.unsubscribe('/', routerHandler); | ||
|
||
// param | ||
router.param('user_id', (req, res, next, id) => { | ||
const baseUrl: string = req.baseUrl; | ||
const route: string = req.route?.path || 'na'; | ||
const param = req.params?.user_id; | ||
const val: string = id; | ||
next(); | ||
}); | ||
|
||
// middleware | ||
router.use((req, res, next) => { | ||
next(); | ||
}); | ||
|
||
const api: IRoute = router.route('/api/'); | ||
|
||
router.route('/') | ||
.all((req, res, next) => { | ||
const url: string = req.baseUrl; | ||
next(); | ||
}) | ||
.get((req, res) => { | ||
res.setHeader('x-header', 'value'); | ||
res.end('.'); | ||
}); | ||
|
||
|
||
// test router handling | ||
var server = createServer(function(req: IncomingMessage, res: ServerResponse) { | ||
router(req, res, (err) => { | ||
if (err) { | ||
const e: Error = err; | ||
} | ||
// | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"lib": [ | ||
"es6" | ||
], | ||
"esModuleInterop": true, | ||
"noImplicitAny": false, | ||
"noImplicitThis": true, | ||
"strictNullChecks": true, | ||
"strictFunctionTypes": true, | ||
"types": ["node"], | ||
"noEmit": true, | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"include": [ | ||
"test/**/*" | ||
], | ||
"files": [ | ||
"index.d.ts" | ||
] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.