Skip to content

Commit

Permalink
Merge pull request #14 from mwelwankuta/typescript-support
Browse files Browse the repository at this point in the history
feat: added typed body generic option
  • Loading branch information
mwelwankuta authored Apr 17, 2024
2 parents 35640ae + 049c94d commit 19dcf78
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/merlee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Merlee {
handler(options: HandlerOptions, callback: HandlerCallback) {
const { path, method = 'get' } = options;

this.server.on('request', (req: Request, res: Response) => {
this.server.on('request', (req: Request<any>, res: Response) => {
// error cannot set headers after they have been sent
_static(req, res, this.options.static);

Expand Down Expand Up @@ -197,7 +197,7 @@ class Merlee {
redirect,
} as unknown as Response;

const request = { ...req, params, param } as unknown as Request;
const request = { ...req, params, param } as unknown as Request<any>;

// request is from router
if (typeof options != 'function') {
Expand Down
8 changes: 4 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export interface Response extends ServerResponse {
sendFile: (file: string, status: number) => void;
redirect: (path: string) => void;
}
export interface Request extends IncomingMessage {
export interface Request<T extends any> extends IncomingMessage {
query: unknown;
params: Record<string, string>;
param: (name: string) => string;
body?: any;
body?: T;
}

export interface HandlerCallback {
(req: Request, res: Response): void;
(req: Request<any>, res: Response): void;
}

type RequestMethod =
Expand All @@ -36,7 +36,7 @@ export interface HandlerOptions {

export type NextFunction = (err?: Error) => void;
export type HandlerFunction = (
req: Request,
req: Request<any>,
res: Response,
next?: NextFunction
) => void;

0 comments on commit 19dcf78

Please sign in to comment.