Skip to content

Commit

Permalink
chore: separate ESM vs CJS type definitions (#200)
Browse files Browse the repository at this point in the history
* fix ESM types

* update package.json files per code review

* old style exports

* oops. forgot one

* chore: revert `url` types

* fix: update pkg `files` lists

* chore: consolidate `parse` types;

- named exports only

* fix(cluster): use namespace for CommonJS types

* fix(send): use namespace for CommonJS types

* fix(polka): named type `Trouter` import

* fix(polka): use namespace for CommonJS types

---------

Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
  • Loading branch information
benmccann and lukeed committed Dec 4, 2023
1 parent 84b50d0 commit a42c247
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 84 deletions.
7 changes: 7 additions & 0 deletions packages/cluster/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { RequestListener } from 'http';

export interface ClusterController {
listen(port: number): void;
}

export default function (app: RequestListener | { listen: Function }): ClusterController;
14 changes: 11 additions & 3 deletions packages/cluster/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import type { RequestListener } from 'http';

export interface ClusterController {
listen(port: number): void;
declare namespace cluster {
export interface ClusterController {
listen(port: number): void;
}
}

export default function (app: RequestListener | { listen: Function }): ClusterController;
declare function cluster(
app: RequestListener | {
listen: Function
}
): cluster.ClusterController;

export = cluster;
17 changes: 11 additions & 6 deletions packages/cluster/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
"license": "MIT",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./build.mjs",
"require": "./build.js"
"import": {
"types": "./index.d.mts",
"default": "./build.mjs"
},
"require": {
"types": "./index.d.ts",
"default": "./build.js"
}
},
"./package.json": "./package.json"
},
"files": [
"*.d.ts",
"build.*"
"build.*",
"index.d.*"
],
"author": {
"name": "Luke Edwards",
Expand All @@ -27,4 +32,4 @@
"publishConfig": {
"access": "public"
}
}
}
2 changes: 1 addition & 1 deletion packages/parse/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ParseOptions {
type?: string;
}

function parse<T extends IncomingMessage>(opts?: ParseOptions): Middleware<T>;
declare function parse<T extends IncomingMessage>(opts?: ParseOptions): Middleware<T>;

export {
parse,
Expand Down
17 changes: 11 additions & 6 deletions packages/parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
"license": "MIT",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./build.mjs",
"require": "./build.js"
"import": {
"types": "./index.d.ts",
"default": "./build.mjs"
},
"require": {
"types": "./index.d.ts",
"default": "./build.js"
}
},
"./package.json": "./package.json"
},
"files": [
"*.d.ts",
"build.*"
"build.*",
"index.d.*"
],
"author": {
"name": "Luke Edwards",
Expand All @@ -27,4 +32,4 @@
"publishConfig": {
"access": "public"
}
}
}
66 changes: 66 additions & 0 deletions packages/polka/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { IncomingMessage, ServerResponse } from 'http';
import type { ListenOptions, Server } from 'net';
import type { ParsedURL } from '@polka/url';
import type { Trouter } from 'trouter';

type Promisable<T> = Promise<T> | T;
type ListenCallback = () => Promisable<void>;

export interface IError extends Error {
code?: number;
status?: number;
details?: any;
}

export type NextHandler = (err?: string | IError) => Promisable<void>;
export type ErrorHandler<T extends Request = Request> = (err: string | IError, req: T, res: Response, next: NextHandler) => Promisable<void>;
export type Middleware<T extends IncomingMessage = Request> = (req: T & Request, res: Response, next: NextHandler) => Promisable<void>;

export interface IOptions<T extends Request = Request> {
server?: Server;
onNoMatch?: Middleware<T>;
onError?: ErrorHandler<T>;
}

export type Response = ServerResponse;

export interface Request extends IncomingMessage {
url: string;
method: string;
originalUrl: string;
params: Record<string, string>;
path: string;
search: string;
query: Record<string,string>;
body?: any;
_decoded?: true;
_parsedUrl: ParsedURL;
}

export interface Polka<T extends Request = Request> extends Trouter<Middleware<T>> {
readonly server: Server;
readonly wares: Middleware<T>[];

readonly onError: ErrorHandler<T>;
readonly onNoMatch: Middleware<T>;

readonly handler: Middleware<T>;
parse: (req: IncomingMessage) => ParsedURL;

use(pattern: RegExp|string, ...handlers: (Polka<T> | Middleware<T>)[]): this;
use(...handlers: (Polka<T> | Middleware<T>)[]): this;

listen(port?: number, hostname?: string, backlog?: number, callback?: ListenCallback): this;
listen(port?: number, hostname?: string, callback?: ListenCallback): this;
listen(port?: number, backlog?: number, callback?: ListenCallback): this;
listen(port?: number, callback?: ListenCallback): this;
listen(path: string, backlog?: number, callback?: ListenCallback): this;
listen(path: string, callback?: ListenCallback): this;
listen(options: ListenOptions, callback?: ListenCallback): this;
listen(handle: any, backlog?: number, callback?: ListenCallback): this;
listen(handle: any, callback?: ListenCallback): this;
}

export default function<T extends Request = Request>(
options?: IOptions<T>
): Polka<T>;
96 changes: 51 additions & 45 deletions packages/polka/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,65 @@ import type { Trouter } from 'trouter';
type Promisable<T> = Promise<T> | T;
type ListenCallback = () => Promisable<void>;

export interface IError extends Error {
code?: number;
status?: number;
details?: any;
}
declare namespace polka {
export interface IError extends Error {
code?: number;
status?: number;
details?: any;
}

export type NextHandler = (err?: string | IError) => Promisable<void>;
export type ErrorHandler<T extends Request = Request> = (err: string | IError, req: T, res: Response, next: NextHandler) => Promisable<void>;
export type Middleware<T extends IncomingMessage = Request> = (req: T & Request, res: Response, next: NextHandler) => Promisable<void>;
export type NextHandler = (err?: string | IError) => Promisable<void>;
export type ErrorHandler<T extends Request = Request> = (err: string | IError, req: T, res: Response, next: NextHandler) => Promisable<void>;
export type Middleware<T extends IncomingMessage = Request> = (req: T & Request, res: Response, next: NextHandler) => Promisable<void>;

export interface IOptions<T extends Request = Request> {
server?: Server;
onNoMatch?: Middleware<T>;
onError?: ErrorHandler<T>;
}
export interface IOptions<T extends Request = Request> {
server?: Server;
onNoMatch?: Middleware<T>;
onError?: ErrorHandler<T>;
}

export type Response = ServerResponse;
export type Response = ServerResponse;

export interface Request extends IncomingMessage {
url: string;
method: string;
originalUrl: string;
params: Record<string, string>;
path: string;
search: string;
query: Record<string,string>;
body?: any;
_decoded?: true;
_parsedUrl: ParsedURL;
}
export interface Request extends IncomingMessage {
url: string;
method: string;
originalUrl: string;
params: Record<string, string>;
path: string;
search: string;
query: Record<string,string>;
body?: any;
_decoded?: true;
_parsedUrl: ParsedURL;
}

export interface Polka<T extends Request = Request> extends Trouter<Middleware<T>> {
readonly server: Server;
readonly wares: Middleware<T>[];
export interface Polka<T extends Request = Request> extends Trouter<Middleware<T>> {
readonly server: Server;
readonly wares: Middleware<T>[];

readonly onError: ErrorHandler<T>;
readonly onNoMatch: Middleware<T>;
readonly onError: ErrorHandler<T>;
readonly onNoMatch: Middleware<T>;

readonly handler: Middleware<T>;
parse: (req: IncomingMessage) => ParsedURL;
readonly handler: Middleware<T>;
parse: (req: IncomingMessage) => ParsedURL;

use(pattern: RegExp|string, ...handlers: (Polka<T> | Middleware<T>)[]): this;
use(...handlers: (Polka<T> | Middleware<T>)[]): this;
use(pattern: RegExp|string, ...handlers: (Polka<T> | Middleware<T>)[]): this;
use(...handlers: (Polka<T> | Middleware<T>)[]): this;

listen(port?: number, hostname?: string, backlog?: number, callback?: ListenCallback): this;
listen(port?: number, hostname?: string, callback?: ListenCallback): this;
listen(port?: number, backlog?: number, callback?: ListenCallback): this;
listen(port?: number, callback?: ListenCallback): this;
listen(path: string, backlog?: number, callback?: ListenCallback): this;
listen(path: string, callback?: ListenCallback): this;
listen(options: ListenOptions, callback?: ListenCallback): this;
listen(handle: any, backlog?: number, callback?: ListenCallback): this;
listen(handle: any, callback?: ListenCallback): this;
listen(port?: number, hostname?: string, backlog?: number, callback?: ListenCallback): this;
listen(port?: number, hostname?: string, callback?: ListenCallback): this;
listen(port?: number, backlog?: number, callback?: ListenCallback): this;
listen(port?: number, callback?: ListenCallback): this;
listen(path: string, backlog?: number, callback?: ListenCallback): this;
listen(path: string, callback?: ListenCallback): this;
listen(options: ListenOptions, callback?: ListenCallback): this;
listen(handle: any, backlog?: number, callback?: ListenCallback): this;
listen(handle: any, callback?: ListenCallback): this;
}
}

export default function <T extends Request = Request> (options?: IOptions<T>): Polka<T>;
declare function polka<T extends polka.Request = polka.Request>(
options?: polka.IOptions<T>
): polka.Polka<T>;

export = polka;
15 changes: 10 additions & 5 deletions packages/polka/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
"license": "MIT",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./build.mjs",
"require": "./build.js"
"import": {
"types": "./index.d.mts",
"default": "./build.mjs"
},
"require": {
"types": "./index.d.ts",
"default": "./build.js"
}
},
"./package.json": "./package.json"
},
"files": [
"*.d.ts",
"build.*"
"build.*",
"index.d.*"
],
"author": {
"name": "Luke Edwards",
Expand Down
2 changes: 2 additions & 0 deletions packages/redirect/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import type { ServerResponse } from 'http';
export default function (res: ServerResponse, code?: number, location?: string): void;
5 changes: 4 additions & 1 deletion packages/redirect/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import type { ServerResponse } from 'http';
export default function (res: ServerResponse, code?: number, location?: string): void;

declare function redirect(res: ServerResponse, code?: number, location?: string): void;

export = redirect;
17 changes: 11 additions & 6 deletions packages/redirect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
"license": "MIT",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./build.mjs",
"require": "./build.js"
"import": {
"types": "./index.d.mts",
"default": "./build.mjs"
},
"require": {
"types": "./index.d.ts",
"default": "./build.js"
}
},
"./package.json": "./package.json"
},
"files": [
"*.d.ts",
"build.*"
"build.*",
"index.d.*"
],
"author": {
"name": "Luke Edwards",
Expand All @@ -27,4 +32,4 @@
"publishConfig": {
"access": "public"
}
}
}
3 changes: 3 additions & 0 deletions packages/send/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { ServerResponse } from 'http';
export type OutgoingHeaders = Record<string, string|string[]>;
export default function (res: ServerResponse, status?: number, data?: any, headers?: OutgoingHeaders): void;
15 changes: 13 additions & 2 deletions packages/send/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import type { ServerResponse } from 'http';
export type OutgoingHeaders = Record<string, string|string[]>;
export default function (res: ServerResponse, status?: number, data?: any, headers?: OutgoingHeaders): void;

declare namespace send {
export type OutgoingHeaders = Record<string, string|string[]>;
}

declare function send(
res: ServerResponse,
status?: number,
data?: any,
headers?: send.OutgoingHeaders
): void;

export = send;

0 comments on commit a42c247

Please sign in to comment.