Skip to content

Conversation

@sstur
Copy link
Owner

@sstur sstur commented Jan 26, 2024

This implements a way to add a listener for incoming requests (or only requests that match a route) and modify the response object, e.g. add CORS headers.

import { createApplication, HttpError } from '@nbit/bun';

const { defineRoutes, attachRoutes } = createApplication({
  onRequest: (event) => {
    const request = event.request;
    // Check the request if you need to, then add a listener to modify the response.
    event.addListener('response', (response) => {
      response.headers.set('Access-Control-Allow-Methods', 'GET, HEAD, POST, OPTIONS');
      response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Accept');
      response.headers.set('Access-Control-Allow-Origin', 'https://example.com');
    });
  },
  getContext: (request) => ({ ... }),
});

const routes = defineRoutes((app) => [
  // ...
]);

Alternatively you can do this only if a route matches.

const { defineRoutes, attachRoutes } = createApplication({
  onRouteMatch: (event) => {
    const { request, route } = event;
    const { pattern, params } = route;
    // The `pattern` will be like "/foo/:bar"
    // The `params` will be like `{ bar: 'abc' }`
    event.addListener('response', (response) => {
      response.headers.set('Access-Control-Allow-Methods', 'GET, HEAD, POST, OPTIONS');
      response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Accept');
      response.headers.set('Access-Control-Allow-Origin', 'https://example.com');
    });
  },
  getContext: (request) => ({ ... }),
});

If it's useful, we could expose a helper like so:

import { createApplication, HttpError, enableCors } from '@nbit/bun';

const { defineRoutes, attachRoutes } = createApplication({
  ...enableCors(),
  getContext: (request) => ({ ... }),
});

TODO:

  • Consider running the response transformers later in the process, after errorHandler
  • Consider renaming addListener to onResponse or transformResponse
  • Add tests

Copy link
Contributor

@svnty svnty left a comment

Choose a reason for hiding this comment

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

looks good

@svnty
Copy link
Contributor

svnty commented Mar 19, 2024

@sstur Any chance you could publish this?

@sstur
Copy link
Owner Author

sstur commented Mar 21, 2024

Will do. I need to take one more pass at this to evaluate if the API is right and I need to see why the tests are failing. Will get on that as soon as possible.

@svnty
Copy link
Contributor

svnty commented Mar 27, 2024

Will do. I need to take one more pass at this to evaluate if the API is right and I need to see why the tests are failing. Will get on that as soon as possible.

I love you bro, you're the goat

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.

3 participants