itty-router-extras icon indicating copy to clipboard operation
itty-router-extras copied to clipboard

Correct type of request not recognized when using withCookies and withContent

Open RicardoViteriR opened this issue 3 years ago • 3 comments

I have the following function that I cannot get to recognize the request when using the withCookies and withContent middleware.

I tried using import { Request as IttyRequest} from 'itty-router' but I get Property 'content' does not exist on type 'Request'

export async function handleRefresh(req: any): Promise<Response> {
  const payload: RefreshTokenData = req.content ?? {};
}

Can someone tell me how I can get the right type?

RicardoViteriR avatar Dec 04 '21 22:12 RicardoViteriR

I ran into this problem today and solved it like this:

Create a path and a file to track custom types src/@types/itty-router/index.d.ts:

interface Request {
  content?: any;
  cookies?: any;
  params?: any;
}

Add this line in tsconfig.json:

 {
   "compilerOptions": {
+    "typeRoots": ["./src/@types"],
   },
 }

For reference: https://github.com/mkuchak/cloudflare-workers-template/blob/main/src/%40types/itty-router/index.d.ts https://github.com/mkuchak/cloudflare-workers-template/blob/main/tsconfig.json#L15

mkuchak avatar Apr 09 '22 20:04 mkuchak

Hi @mkuchak, thanks for sharing. Do you know if this solution would extend or replace the Request interface?

RicardoViteriR avatar Apr 09 '22 21:04 RicardoViteriR

This extends the interface Request

mkuchak avatar Apr 09 '22 21:04 mkuchak