fix: proxy content encoding header (#30)
This commit is contained in:
parent
a6624f4487
commit
c8ff2af189
@ -55,7 +55,7 @@ For example, if you are using Next.js, you can:
|
|||||||
```sh
|
```sh
|
||||||
npm install --save @fal-ai/serverless-proxy
|
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
|
```ts
|
||||||
export { handler as default } from '@fal-ai/serverless-proxy/nextjs';
|
export { handler as default } from '@fal-ai/serverless-proxy/nextjs';
|
||||||
```
|
```
|
||||||
|
|||||||
@ -4,8 +4,8 @@ import { useMemo, useState } from 'react';
|
|||||||
// @snippet:start(client.config)
|
// @snippet:start(client.config)
|
||||||
fal.config({
|
fal.config({
|
||||||
requestMiddleware: fal.withProxy({
|
requestMiddleware: fal.withProxy({
|
||||||
targetUrl: '/api/_fal/proxy', // the built-int nextjs proxy
|
targetUrl: '/api/fal/proxy', // the built-int nextjs proxy
|
||||||
// targetUrl: 'http://localhost:3333/api/_fal/proxy', // or your own external proxy
|
// targetUrl: 'http://localhost:3333/api/fal/proxy', // or your own external proxy
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
// @snippet:end
|
// @snippet:end
|
||||||
|
|||||||
@ -16,7 +16,7 @@ npm install --save @fal-ai/serverless-proxy
|
|||||||
|
|
||||||
For Next.js applications using the page router:
|
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:
|
2. Re-export the proxy handler from the library as the default export:
|
||||||
```ts
|
```ts
|
||||||
export { handler as default } from '@fal-ai/serverless-proxy/nextjs';
|
export { handler as default } from '@fal-ai/serverless-proxy/nextjs';
|
||||||
@ -52,7 +52,7 @@ For Express applications:
|
|||||||
import * as falProxy from '@fal-ai/serverless-proxy/express';
|
import * as falProxy from '@fal-ai/serverless-proxy/express';
|
||||||
|
|
||||||
app.all(
|
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
|
cors(), // if external clients will use the proxy
|
||||||
falProxy.handler
|
falProxy.handler
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@fal-ai/serverless-proxy",
|
"name": "@fal-ai/serverless-proxy",
|
||||||
"version": "0.5.0",
|
"version": "0.6.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@ -17,10 +17,7 @@ export const handler: RequestHandler = async (request, response, next) => {
|
|||||||
await handleRequest({
|
await handleRequest({
|
||||||
id: 'express',
|
id: 'express',
|
||||||
method: request.method,
|
method: request.method,
|
||||||
respondWith: (status, data) =>
|
respondWith: (status, data) => response.status(status).json(data),
|
||||||
typeof data === 'string'
|
|
||||||
? response.status(status).json({ detail: data })
|
|
||||||
: response.status(status).json(data),
|
|
||||||
getHeaders: () => request.headers,
|
getHeaders: () => request.headers,
|
||||||
getHeader: (name) => request.headers[name],
|
getHeader: (name) => request.headers[name],
|
||||||
sendHeader: (name, value) => response.setHeader(name, value),
|
sendHeader: (name, value) => response.setHeader(name, value),
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
export const TARGET_URL_HEADER = 'x-fal-target-url';
|
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 = 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;
|
const FAL_KEY_ID = process.env.FAL_KEY_ID || process.env.NEXT_PUBLIC_FAL_KEY_ID;
|
||||||
@ -51,7 +51,7 @@ function getFalKey(): string | undefined {
|
|||||||
return 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
|
* A request handler that proxies the request to the fal-serverless
|
||||||
|
|||||||
@ -19,10 +19,7 @@ export const handler: NextApiHandler = async (request, response) => {
|
|||||||
return handleRequest({
|
return handleRequest({
|
||||||
id: 'nextjs-page-router',
|
id: 'nextjs-page-router',
|
||||||
method: request.method || 'POST',
|
method: request.method || 'POST',
|
||||||
respondWith: (status, data) =>
|
respondWith: (status, data) => response.status(status).json(data),
|
||||||
typeof data === 'string'
|
|
||||||
? response.status(status).json({ detail: data })
|
|
||||||
: response.status(status).json(data),
|
|
||||||
getHeaders: () => request.headers,
|
getHeaders: () => request.headers,
|
||||||
getHeader: (name) => request.headers[name],
|
getHeader: (name) => request.headers[name],
|
||||||
sendHeader: (name, value) => response.setHeader(name, value),
|
sendHeader: (name, value) => response.setHeader(name, value),
|
||||||
@ -53,7 +50,7 @@ async function routeHandler(request: NextRequest) {
|
|||||||
id: 'nextjs-app-router',
|
id: 'nextjs-app-router',
|
||||||
method: request.method,
|
method: request.method,
|
||||||
respondWith: (status, data) =>
|
respondWith: (status, data) =>
|
||||||
NextResponse.json(typeof data === 'string' ? { detail: data } : data, {
|
NextResponse.json(data, {
|
||||||
status,
|
status,
|
||||||
headers: responseHeaders,
|
headers: responseHeaders,
|
||||||
}),
|
}),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user