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

feat: handleRequest. Deprecate onUnhandledRequest middleware option #340

Merged
merged 2 commits into from
Sep 29, 2022

Conversation

baoshan
Copy link
Contributor

@baoshan baoshan commented Sep 7, 2022

To:

  1. be idiomatic for all runtimes and frameworks (Node.js / Express.js / Deno / AWS Lambda / Cloudflare), and
  2. simplify the implementation of a specific middleware

this PR deprecates the onUnhandledRequest option.

Without the onUnhandledRequest option, middlewares are simpler and more idiomatic, which can be verified in my next PR #341.

@timrogers
Copy link

Thanks for taking the time to make this PR ✨

What's the impact on the user of not being able to define an onUnhandledRequest handler? What would people realistically use that for? Reducing the complexity of creating middleware is a good thing, but I'm wondering what we lose.

@wolfy1339
Copy link
Member

For reference, this was split from #333

In that PR it was mentioned:

I removed the onUnhandledRequest option. Callers can still custom unhandled requests: use the express * router (as covered by the test cases) or check if the response is a 404.

@timrogers
Copy link

Thanks! Do all of the current middleware use Express? Or would the pattern for doing that be different across middleware? For example, it doesn't look like the Lambda one uses Express.

@baoshan
Copy link
Contributor Author

baoshan commented Sep 7, 2022

This is a great question!

I believe the below behavior for the generic handler (aka handle-request.ts) is more reasonable and predictable:

  • Returns null when the request is not under the pathPrefix (e.g. GET /foo)
  • Return 404 for unhandled requests which is under the pathPrefix (e.g., GET /api/github/oauth/foo)

All middlewares naturally share the same pattern when dealing with /api/github/oauth/foo. How they deal with /foo is different by the nature of the runtime/framework:

  • For native Node.js (createServer), the middleware could return true when the request is handled (response already written to res) or false when not handled. You can check the return value and render a custom response. Anyway, we name it as createNodeMiddleware, not createNodeHandler, right?
  • Express.js relies on proper router middlewares for other requests.
  • For Cloudflare / Deno / AWS Lambda, the middleware should simply map the generic handler: returns a Response/APIGatewayProxyStructuredResultV2 for requests at pathPrefix or null for other requests.

Does that make sense? If not, may I know your use case?

@oscard0m oscard0m added the Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR label Sep 7, 2022
src/middleware/web-worker/index.ts Show resolved Hide resolved
src/middleware/node/index.ts Show resolved Hide resolved
src/middleware/aws-lambda/api-gateway-v2.ts Show resolved Hide resolved
@gr2m
Copy link
Contributor

gr2m commented Sep 7, 2022

Do all of the current middleware use Express

No. Hence the onUnhandledRequest option. This middleware is meant to be universal, it should run in Deno and browsers and future JavaScript runtime environments. We cannot assume express or even Node APIs to be present

If I recall correctly, I think we depend on onUnhandledRequest to compose middlewares. E.g. the @octokit/app module is using @oauth-app.js internally and uses onUnhandledRequest to add more routes. But I didn't check the code

@baoshan
Copy link
Contributor Author

baoshan commented Sep 8, 2022

We cannot assume express or even Node APIs to be present

I understand that. That’s exactly why this PR is proposed.

the @octokit/app module is using @oauth-app.js internally

Thanks for that information. I believe @octokit/app can be updated (or even made simpler) with onUnhandledRequest removed (I’m willing to help.)

First, I suggest all the middlewares (the cartesian product of [@octokit/app, @octokit/oauth-app] ⨯ [Node.js, Express.js, Deno, Browser, Cloudflare, AWS Lambda]) do the same thing:

  • Do not handle the request when the request does not match pathPrefix (e.g. GET /foo)
  • Return 404 for unhandled requests matching pathPrefix (e.g., GET /api/github/oauth/foo)

Second, injecting the onUnhandledRequest is not the idiomatic way for all runtimes or frameworks. Actually is not the idiomatic way for any runtime/framework IMHO.

Making middlewares idiomatic is more important than unifying the interface. A more functional approach (#341) makes Deno / Cloudflare / AWS Lambda feels better, IMHO.

wolfy1339
wolfy1339 previously approved these changes Sep 12, 2022
src/index.ts Show resolved Hide resolved
feat(middleware): export `handleRequest`, `OctokitRequest`, and `OctokitResponse` for 3rd-party middlewares
@baoshan baoshan force-pushed the deprecate_on-unhandled-request-handler branch from e6baaf1 to cc32524 Compare September 13, 2022 02:33
@wolfy1339
Copy link
Member

@gr2m @oscard0m Can you re-review this so this can get merged

@gr2m gr2m changed the title refactor: deprecate onUnhandledRequest middleware option feat: deprecate onUnhandledRequest middleware option Sep 27, 2022
src/index.ts Show resolved Hide resolved
@gr2m gr2m changed the title feat: deprecate onUnhandledRequest middleware option feat: handleRequest. Deprecate onUnhandledRequest middleware option Sep 27, 2022
@baoshan baoshan force-pushed the deprecate_on-unhandled-request-handler branch from 3f5535a to 720032b Compare September 29, 2022 10:53
1. Parse the HTTP request using (1).
2. Process the `OctokitRequest` object using `handleRequest`.
3. Render the `OctokitResponse` object using (2).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful, thank you!!!

@gr2m gr2m dismissed oscard0m’s stale review September 29, 2022 21:00

all requests have been addressed

@gr2m gr2m merged commit f5c1b66 into octokit:master Sep 29, 2022
@github-actions
Copy link

🎉 This PR is included in version 4.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link

🎉 This PR is included in version 5.0.0-beta.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released on @beta Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants