Skip to content

Instantly share code, notes, and snippets.

@lukeed
Last active March 6, 2021 07:28
Show Gist options
  • Select an option

  • Save lukeed/2fb80f9f87ee40204ef8412963348098 to your computer and use it in GitHub Desktop.

Select an option

Save lukeed/2fb80f9f87ee40204ef8412963348098 to your computer and use it in GitHub Desktop.
TypeScript definitions for `polka@next` – These will be made available as "@types/polka" when 1.0 is ready!
declare module 'polka' {
import { Server } from 'net';
import { IncomingMessage, ServerResponse } from 'http';
import Trouter from 'trouter';
export interface IError extends Error {
message: string;
code?: number;
stack?: string;
status?: number;
details?: any;
}
export type NextHandler = (err?: string | IError) => void;
export type Middleware<T = Request> = Polka<T> | RequestHandler<T>;
export type RequestHandler<T> = (req: T, res: Response, next?: NextHandler) => void;
export type ErrorHandler<T = Request> = (err: string | IError, req: T, res: Response, next: NextHandler) => void;
export interface IOptions<T = Request> {
server?: Server;
onError?: ErrorHandler<T>;
onNoMatch?: RequestHandler<T>;
}
export interface ParsedURL {
_raw: string;
href: string;
path: string;
search: null | string;
query: null | Record<string, any>;
}
export type Response = ServerResponse;
export interface Request extends IncomingMessage, ParsedURL {
originalUrl: IncomingMessage['url'];
params: Record<string, string>;
body?: Record<string, any>;
_parsedUrl: ParsedURL;
_decoded?: true;
}
export interface Polka<T = Request> extends Trouter<RequestHandler<T>> {
readonly server: typeof Server;
readonly wares: RequestHandler<T>[];
readonly onError: ErrorHandler<T>;
readonly onNoMatch: RequestHandler<T>;
parse: (req: IncomingMessage) => ParsedURL | void;
use(...handlers: Middleware<T>[]): this;
use(pattern: string, ...handlers: Middleware<T>[]): this;
readonly handler: RequestHandler<T>;
listen: Server['listen'];
}
export default function <T = IncomingMessage>(options?: IOptions<T>): Polka<T>;
}
@lukeed
Copy link
Copy Markdown
Author

lukeed commented Oct 22, 2019

Related: lukeed/polka#17

@normanzb
Copy link
Copy Markdown

normanzb commented Dec 4, 2019

readonly server: typeof Server; should be readonly server: Server;

@lukeed
Copy link
Copy Markdown
Author

lukeed commented Dec 6, 2019

Fixed, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment