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

Interceptors #861

Open
pmcelhaney opened this issue Apr 19, 2024 · 0 comments
Open

Interceptors #861

pmcelhaney opened this issue Apr 19, 2024 · 0 comments
Labels
brainstorm Ideas that aren't fully baked enhancement New feature or request

Comments

@pmcelhaney
Copy link
Owner

pmcelhaney commented Apr 19, 2024

There ought to be a way to add behavior that applies to all routes or all routes within a directory. This will be useful for use cases like handling authentication globally.

I'm thinking about adding an extension point via an _.intercept.ts file.

The file would export a function that looks like this:

export const intercept: INTERCEPT = ($, respondTo, path, method) => {
  return respondTo($);
}

The respondTo function is the operation function (GET, POST, etc) in the file corresponding to the path. The idea here is we can modify the request before it's passed to that function and modify the response after it's returned from the function. We can also skip the function altogether and return a different response.

It's basically a middleware but I hesitate to call it that because relating it to express / koa middleware may cause confusion.

Type safety may be a bit involved here. That INTERCEPT type will have to be overloaded with a type for each combination of path and method. The argument and return type of respondTo can be imported from files that specify path types.

@pmcelhaney pmcelhaney added brainstorm Ideas that aren't fully baked enhancement New feature or request labels Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
brainstorm Ideas that aren't fully baked enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant