Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router and AutoRouter Request types are not same #253

Open
istarkov opened this issue Jul 5, 2024 · 1 comment
Open

Router and AutoRouter Request types are not same #253

istarkov opened this issue Jul 5, 2024 · 1 comment
Labels
TypeScript TypeScript-related issues

Comments

@istarkov
Copy link
Contributor

istarkov commented Jul 5, 2024

Describe the Issue

It would be beneficial to have consistent Router types for the request.

Currently, we have the following types for Router and AutoRouter:

export const Router = <
  RequestType = IRequest,
  Args extends any[] = any[],
  ResponseType = any
>(
export const AutoRouter = <
  RequestType extends IRequest = IRequest,
  Args extends any[] = any[],
  ResponseType = any
>({

The difference is that AutoRouter and IttyRouter require RequestType to be a subtype of IRequest, while Router does not.

@istarkov
Copy link
Contributor Author

To address the issue with Cloudflare types, especially the request.cf.xxx properties, I suggest the following changes:

Remove the extend keyword and use the current Router types, or adjust IRequest slightly.

The problem arises because the default worker Request type causes every cf property to have an unknown type, like this:

type MyContinent = NonNullable<Request['cf']>["continent"]; // <-- unknown

type MyContinentGood = NonNullable<Request<unknown, IncomingRequestCfProperties<unknown>>['cf']>["continent"]; // <-- ContinentCode | undefined

More details

Cloudflare Pages already use Request<unknown, IncomingRequestCfProperties> as its request type (so no issues with cf.props), but workers do not.

Using itty-router, defining the router like this yields the best results:

const router = Router<
  Request<unknown, IncomingRequestCfProperties<unknown>>
>({...});

router.get('*', (request) =>  {
  const code = request.cf.continent; // <-- code has ContinentCode | undefined type
});

However, this definition is impossible with AutoRouter because it requires Request<unknown, IncomingRequestCfProperties> to extend IRequest.

The solution is to remove extends in all routers at RequestType extends IRequest and/or also make IRequest generic, like so:

type IRequest<TRequest = Request> = {
  route: string;
  params: {
    [key: string]: string;
  };
  query: {
    [key: string]: string | string[] | undefined;
  };
  proxy?: any;
} & TRequest & ...;

This adjustment will allow for better type handling and compatibility with Cloudflare’s request.cf.xxx properties.

@kwhitley kwhitley added the TypeScript TypeScript-related issues label Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TypeScript TypeScript-related issues
Projects
None yet
Development

No branches or pull requests

2 participants