fal-js icon indicating copy to clipboard operation
fal-js copied to clipboard

Remix proxy

Open amitrab opened this issue 2 years ago • 2 comments

It would be great to have a Remix proxy in the lib! I've tried to use the open branch feature/remix-proxy , but didn't manage to get it to work. Please revive and merge it for us Remix fans , Thanks!

amitrab avatar Mar 08 '24 16:03 amitrab

i second this request

agawrylak avatar Mar 11 '24 18:03 agawrylak

Cool! It's good to know there's some interest in Remix support. I'll pick it back up as soon as possible.

drochetti avatar Apr 06 '24 08:04 drochetti

try this

import { handleRequest } from '@fal-ai/serverless-proxy'
import type { ActionFunction, ActionFunctionArgs, LoaderFunction, LoaderFunctionArgs } from '@remix-run/node'
import { json } from '@remix-run/node'

const proxy = async ({ request }: ActionFunctionArgs | LoaderFunctionArgs) => {
  const responseHeaders = new Headers()
  return handleRequest({
    id: 'remix',
    method: request.method,
    respondWith: (status, data) => json(data, { status, headers: responseHeaders }),
    getHeaders: () => Object.fromEntries(request.headers.entries()),
    getHeader: (name) => request.headers.get(name),
    sendHeader: (name, value) => responseHeaders.set(name, value),
    getBody: async () => JSON.stringify(await request.json()),
  })
}

export const action: ActionFunction = proxy

export const loader: LoaderFunction = proxy

nicknikolov avatar May 22 '24 16:05 nicknikolov

We added remix-run support in the proxy v0.9.0, which was just released! See PR #82 for details.

The simplest use-case is to just re-export the proxy route handler:

api.fal.proxy.ts

import { createProxy } from "@fal-ai/serverless-proxy/remix";
import { json } from "@remix-run/node"; // or cloudflare, deno

const proxy = createProxy({
    json
});

// just re-exporting, but you can create your own logic by providing you own function
// and then calling to proxy.action(req) or proxy.loader(req)
export const { action, loader } = proxy;

Then in the client-side, you can setup the client to proxy calls via your route handler:

_index.tsx

fal.config({
  proxyUrl: "/api/fal/proxy",
});

drochetti avatar Aug 07 '24 23:08 drochetti

Closing this issue as it's been resolved. Feel free to reach out if you have any other issues or feedback.

drochetti avatar Aug 12 '24 16:08 drochetti