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

Bun middleware that allows you to extend Response #41

Closed
EvHaus opened this issue Feb 22, 2024 · 3 comments · Fixed by #42
Closed

Bun middleware that allows you to extend Response #41

EvHaus opened this issue Feb 22, 2024 · 3 comments · Fixed by #42
Assignees
Labels
enhancement New feature or request

Comments

@EvHaus
Copy link

EvHaus commented Feb 22, 2024

👋 Hey. It's me again.

I'm trying to implement a CORS middleware in Bun that adds some additional headers to all responses. This is proving to be quite difficult at the moment. I see you have this example but that's not quite enough to fully handle CORS headers. I'm trying to do something more like this.

I'm wondering if it would be possible for middleware in Bun to be able to extend the Response object somehow. At the moment, await next() in middleware returns undefined. It would be great if it returned the response of the promise chain.

Ideally, I would like to write a middleware like this:

import type { NextFunction } from 'node-file-router';

const headers = async (req: Request, next: NextFunction) => {
	const res = await next();

	// Ensure all API requests always return these headers
	res.headers.set('Access-Control-Allow-Origin', '*');
	res.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

	return res;
};

export default headers;

Do you think this would be possible to add support for?

@Danilqa
Copy link
Owner

Danilqa commented Feb 22, 2024

@EvHaus Hey! 👋 Nice to meet you!

Cool, sure, I had the same thought earlier as well! It will be delivered tomorrow.
One thing to keep in mind: if your CORS middleware is positioned as follows:
[useLogger] -> [useCors] -> [useAuth].

then useAuth should also return next:

export async function useAuth(req: Request, next: NextFunction) {
  // ...

  return next();
}

@Danilqa Danilqa self-assigned this Feb 23, 2024
@Danilqa Danilqa added the enhancement New feature or request label Feb 23, 2024
@Danilqa Danilqa linked a pull request Feb 23, 2024 that will close this issue
@Danilqa Danilqa reopened this Feb 24, 2024
@Danilqa
Copy link
Owner

Danilqa commented Feb 24, 2024

@EvHaus Hey! I've released version 0.6.0 with this feature :) Please feel free to reach out if you have any issues or additional suggestions. The documentation can be found here, and it aligns with what you described earlier.

@EvHaus
Copy link
Author

EvHaus commented Feb 25, 2024

Works beautifully! Thanks again @Danilqa. I really appreciate your fast reponses and updates to the library.

@EvHaus EvHaus closed this as completed Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants