fal-ai / fal-js

The JavaScript client and utilities to fal-serverless with built-in TypeScript definitions
https://fal.ai
MIT License
91 stars 19 forks source link

Expose Request Method in requestConfig for Middleware #86

Closed fco-fbatch closed 2 months ago

fco-fbatch commented 2 months ago

Description:

I would like to propose an enhancement to the fal.config method where a requestMiddleware function can be further configured. Currently, the requestConfig object only exposes the headers and URL information. However, it would be beneficial if the requestConfig also exposed the HTTP request method.

Use Case:

In our use case, we need to supply an API auth token to the FAL proxy server through the middleware. Here’s a basic example of how we configure the client:

fal.config({
  proxyUrl: "http://localhost:8080/api/fal/proxy",
  requestMiddleware: (requestConfig) => {
    return Promise.resolve({
      ...requestConfig,
      headers: {
        ...requestConfig.headers,
        "x-my-bearer": `Bearer ${bearer}`,
        "x-my-content-id": contentId,
      },
    });
  },
});

This middleware adds authentication tokens to the request headers. However, to enhance security, it would be ideal to omit sending tokens in GET requests, lowering exposure.

Proposed Enhancement:

Consider exposing the request method in the requestConfig object passed to the requestMiddleware. This additional information would allow developers to conditionally include or exclude sensitive headers based on the request method, reducing the risk of exposing secrets in scenarios where they are not needed.

Example Usage:

fal.config({
  proxyUrl: "http://localhost:8080/api/fal/proxy",
  requestMiddleware: (requestConfig) => {
    if (requestConfig.method === 'POST') {
      return Promise.resolve({
        ...requestConfig,
        headers: {
          ...requestConfig.headers,
          "x-my-bearer": `Bearer ${bearer}`,
          "x-my-content-id": contentId,
        },
      });
    } else {
      return Promise.resolve(requestConfig);
    }
  },
});
drochetti commented 2 months ago

Hey @fco-fbatch, thanks for the thoughtful feature request. I opened a PR for this: #87