Skip to content

Commit

Permalink
fix: proxy content encoding header
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti committed Nov 26, 2023
1 parent a6624f4 commit 2b09b70
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ For example, if you are using Next.js, you can:
```sh
npm install --save @fal-ai/serverless-proxy
```
2. Add the proxy as an API endpoint of your app, see an example here in [pages/api/\_fal/proxy.ts](https://github.com/fal-ai/serverless-js/blob/main/apps/demo-nextjs-app/pages/api/_fal/proxy.ts)
2. Add the proxy as an API endpoint of your app, see an example here in [pages/api/\fal/proxy.ts](https://github.com/fal-ai/serverless-js/blob/main/apps/demo-nextjs-app/pages/api/fal/proxy.ts)
```ts
export { handler as default } from '@fal-ai/serverless-proxy/nextjs';
```
Expand Down
4 changes: 2 additions & 2 deletions apps/demo-nextjs-page-router/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useMemo, useState } from 'react';
// @snippet:start(client.config)
fal.config({
requestMiddleware: fal.withProxy({
targetUrl: '/api/_fal/proxy', // the built-int nextjs proxy
// targetUrl: 'http://localhost:3333/api/_fal/proxy', // or your own external proxy
targetUrl: '/api/fal/proxy', // the built-int nextjs proxy
// targetUrl: 'http://localhost:3333/api/fal/proxy', // or your own external proxy
}),
});
// @snippet:end
Expand Down
4 changes: 2 additions & 2 deletions libs/proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm install --save @fal-ai/serverless-proxy

For Next.js applications using the page router:

1. Create an API route in your Next.js app, as a convention we suggest using `pages/api/_fal/proxy.js` (or `.ts` if you're using TypeScript):
1. Create an API route in your Next.js app, as a convention we suggest using `pages/api/fal/proxy.js` (or `.ts` if you're using TypeScript):
2. Re-export the proxy handler from the library as the default export:
```ts
export { handler as default } from '@fal-ai/serverless-proxy/nextjs';
Expand Down Expand Up @@ -52,7 +52,7 @@ For Express applications:
import * as falProxy from '@fal-ai/serverless-proxy/express';
app.all(
falProxy.route, // '/api/_fal/proxy' or you can use your own
falProxy.route, // '/api/fal/proxy' or you can use your own
cors(), // if external clients will use the proxy
falProxy.handler
);
Expand Down
2 changes: 1 addition & 1 deletion libs/proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fal-ai/serverless-proxy",
"version": "0.5.0",
"version": "0.6.0",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
5 changes: 1 addition & 4 deletions libs/proxy/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export const handler: RequestHandler = async (request, response, next) => {
await handleRequest({
id: 'express',
method: request.method,
respondWith: (status, data) =>
typeof data === 'string'
? response.status(status).json({ detail: data })
: response.status(status).json(data),
respondWith: (status, data) => response.status(status).json(data),
getHeaders: () => request.headers,
getHeader: (name) => request.headers[name],
sendHeader: (name, value) => response.setHeader(name, value),
Expand Down
4 changes: 2 additions & 2 deletions libs/proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const TARGET_URL_HEADER = 'x-fal-target-url';

export const DEFAULT_PROXY_ROUTE = '/api/_fal/proxy';
export const DEFAULT_PROXY_ROUTE = '/api/fal/proxy';

const FAL_KEY = process.env.FAL_KEY || process.env.NEXT_PUBLIC_FAL_KEY;
const FAL_KEY_ID = process.env.FAL_KEY_ID || process.env.NEXT_PUBLIC_FAL_KEY_ID;
Expand Down Expand Up @@ -51,7 +51,7 @@ function getFalKey(): string | undefined {
return undefined;
}

const EXCLUDED_HEADERS = ['content-length'];
const EXCLUDED_HEADERS = ['content-length', 'content-encoding'];

/**
* A request handler that proxies the request to the fal-serverless
Expand Down
7 changes: 2 additions & 5 deletions libs/proxy/src/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export const handler: NextApiHandler = async (request, response) => {
return handleRequest({
id: 'nextjs-page-router',
method: request.method || 'POST',
respondWith: (status, data) =>
typeof data === 'string'
? response.status(status).json({ detail: data })
: response.status(status).json(data),
respondWith: (status, data) => response.status(status).json(data),
getHeaders: () => request.headers,
getHeader: (name) => request.headers[name],
sendHeader: (name, value) => response.setHeader(name, value),
Expand Down Expand Up @@ -53,7 +50,7 @@ async function routeHandler(request: NextRequest) {
id: 'nextjs-app-router',
method: request.method,
respondWith: (status, data) =>
NextResponse.json(typeof data === 'string' ? { detail: data } : data, {
NextResponse.json(data, {
status,
headers: responseHeaders,
}),
Expand Down

0 comments on commit 2b09b70

Please sign in to comment.