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

fix: Don’t pass response to AutoRouter missing handler #254

Open
wants to merge 3 commits into
base: v5.x
Choose a base branch
from

Conversation

istarkov
Copy link

@istarkov istarkov commented Jul 6, 2024

Description

The missing handler is a RequestHandler, and the response should not be passed to it.

Probaly a breaking change for non typescript users.

(Found that prettier is not applied to the codebase, or some prettier defaults is not set see yarn prettier)

Related Issue

Link to the related issue:

Type of Change (select one and follow subtasks)

  • Documentation (README.md)
  • Maintenance or repo-level work (e.g. linting, build, tests, refactoring, etc.)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
    • I have included test coverage
  • Breaking change (fix or feature that would cause existing functionality/userland code to not work as expected)
    • Explain why a breaking change is necessary:
  • This change requires (or is) a documentation update
    • I have added necessary local documentation (if appropriate)
    • I have added necessary itty.dev documentation (if appropriate)

@istarkov istarkov changed the title fix AutoRouterOptions missing property type fix: Don’t pass response to missing handler Jul 6, 2024
@istarkov istarkov changed the title fix: Don’t pass response to missing handler fix: Don’t pass response to AutoRouter missing handler Jul 6, 2024
@kwhitley
Copy link
Owner

kwhitley commented Jul 6, 2024

@istarkov - this is great, and a good catch. I'll want to add a test for this before release, so hang tight (unless you want to add that to the PR)

@istarkov
Copy link
Author

istarkov commented Jul 6, 2024

I’ve created a simple test to verify that the first argument of the missing function is the request.

@istarkov
Copy link
Author

istarkov commented Jul 7, 2024

I also fixed the StatusError imports because they were not resolved properly when the package was imported, causing all properties of StatusError to have the any type. See the “Should give TypeScript error” comment for more details.

Example:

import { Router, type IRequest } from 'itty-router';

type CFArgs = [Env, ExecutionContext];

const router = Router<IRequest, CFArgs>({
  catch: (error) => {
    // eslint-disable-next-line no-console
    console.error(error);

    error.status satisfies number;
    error.status satisfies string; // <-- Should give typescript error
    
    return new Response(error.message, {
      status: error.status,
    });
  },
});

These errors will be visible if "moduleResolution": "bundler" is set in tsconfig.json under compilerOptions.

Alternatively, with the new module: "preserve" option in TypeScript 5.4, the bundler option will be implied.
Reference: TypeScript Handbook - Module Resolution

Additionally, the baseUrl has been removed.

So, I’ve also updated the tsconfig.json.

Splitted PR on 2 commits instead of 2 PR's.

@istarkov
Copy link
Author

istarkov commented Jul 7, 2024

Also found

catch (err: any) {
        if (!other.catch) throw err
        response = await other.catch(err, request.proxy ?? request, ...args)
      }

2 ways to fix, give an unknown type as in modern ts, or fomat error

@kwhitley
Copy link
Owner

Taking a dive on this today - we've had a bit of a hurricane rip through this week :D

@istarkov
Copy link
Author

Interesting test had no issues with [email protected] but fails on [email protected]. Investigating

@istarkov
Copy link
Author

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants