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

fix: proxy content encoding header #30

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// @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 All @@ -22,7 +22,7 @@
// @snippet:end

type ErrorProps = {
error: any;

Check warning on line 25 in apps/demo-nextjs-page-router/pages/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
};

function Error(props: ErrorProps) {
Expand Down Expand Up @@ -93,7 +93,7 @@
},
});
setResult(result);
} catch (error: any) {

Check warning on line 96 in apps/demo-nextjs-page-router/pages/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
setError(error);
} finally {
setLoading(false);
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
Loading