Last active
September 19, 2019 19:56
-
-
Save frzi/a1bc8e2064bf1022c48151afd91aeb2f to your computer and use it in GitHub Desktop.
Polka Typescript definitions
This file contains hidden or 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
/** | |
* Potential candidate for @types/polka. | |
*/ | |
// Type definitions for polka 1.0.0 | |
// Project: https://github.com/lukeed/polka | |
// Definitions by: Freek Zijlmans <https://github.com/frzi> | |
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | |
// TypeScript Version: 3.0 | |
/// <reference types="node" /> | |
/// <reference types="trouter" /> | |
import { IncomingMessage, ServerResponse } from 'http'; | |
import { Server } from 'net' | |
import Trouter from 'trouter'; | |
declare function polka(options?: polka.Options): polka.Polka; | |
declare namespace polka { | |
type NextHandler = (err?: Error | string) => void; | |
type RequestHandler = (req: IncomingMessage, res: ServerResponse, next?: NextHandler) => void; | |
type Middleware = Polka | RequestHandler; | |
type ErrorHandler = (err: Error | string, req: IncomingMessage, res: ServerResponse, next: NextHandler) => void; | |
interface Options { | |
onError?: ErrorHandler; | |
onNoMatch?: RequestHandler; | |
server?: typeof Server; | |
} | |
interface URLDescription { | |
_raw: string; | |
href: string; | |
path: string; | |
pathname: string; | |
query: string | null; | |
search: string | null; | |
} | |
interface Polka extends Trouter<RequestHandler> { | |
readonly server: typeof Server; | |
readonly wares: RequestHandler[]; | |
readonly onError: ErrorHandler; | |
readonly onNoMatch: RequestHandler; | |
attach: (req: IncomingMessage, res: ServerResponse) => void; | |
parse: (req: IncomingMessage) => URLDescription | void; | |
use(...handlers: Middleware[]): this; | |
use(pattern: string, ...handlers: Middleware[]): this; | |
readonly handler: RequestHandler | |
listen: Server['listen']; | |
} | |
} | |
export = polka; |
Hey hey 👋
Checking in to let you know that wares
has been removed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Overlooked some things~
A true
next
handler receives a string or Error. You have this correctly defined in yourRequestHandler
type, but for clarity, it should be extracted as its ownNextHandler
type.That means the existing
NextHandler
should be renamed, probably toPolkaOrHandler
? And it should only be foruse()
methods.Polka itself only accepts a
RequestHandler
(the new one, from #1)In the new Polka, the 3rd argument is no longer
info
. Instead, it's now also an optionalnext?
arg:The
onNoMatch
handler is just a regularRequestHandler
(the new one)