Skip to content

Commit

Permalink
feat(proxy): add remix-run support (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti authored Aug 7, 2024
1 parent c3a3c3d commit 2ff4816
Show file tree
Hide file tree
Showing 6 changed files with 20,377 additions and 16,833 deletions.
10 changes: 9 additions & 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.8.0",
"version": "0.9.0",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -20,6 +20,7 @@
".": "./src/index.js",
"./express": "./src/express.js",
"./nextjs": "./src/nextjs.js",
"./remix": "./src/remix.js",
"./svelte": "./src/svelte.js"
},
"typesVersions": {
Expand All @@ -30,6 +31,9 @@
"nextjs": [
"src/nextjs.d.ts"
],
"remix": [
"src/remix.d.ts"
],
"svelte": [
"src/svelte.d.ts"
]
Expand All @@ -38,13 +42,17 @@
"main": "./src/index.js",
"types": "./src/index.d.ts",
"peerDependencies": {
"@remix-run/dev": "^2.0.0",
"@sveltejs/kit": "^2.0.0",
"express": "^4.0.0",
"next": "13.4 - 14",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"peerDependenciesMeta": {
"@remix-run/dev": {
"optional": true
},
"@sveltejs/kit": {
"optional": true
},
Expand Down
2 changes: 2 additions & 0 deletions libs/proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ export function fromHeaders(
}

export const responsePassthrough = (res: Response) => Promise.resolve(res);

export const resolveApiKeyFromEnv = () => Promise.resolve(getFalKey());
58 changes: 58 additions & 0 deletions libs/proxy/src/remix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type {
ActionFunction,
ActionFunctionArgs,
LoaderFunction,
LoaderFunctionArgs,
json as jsonFunction,
} from "@remix-run/node";
import {
fromHeaders,
handleRequest,
resolveApiKeyFromEnv,
responsePassthrough,
} from "./index";

export type FalRemixProxy = {
action: ActionFunction;
loader: LoaderFunction;
};

export type FalRemixProxyOptions = {
/**
* The reference to the `json` function from the Remix runtime.
* e.g. `import { json } from "@remix-run/node";`
*/
json: typeof jsonFunction;
/**
* A function to resolve the API key used by the proxy.
* By default, it uses the `FAL_KEY` environment variable.
*/
resolveApiKey?: () => Promise<string | undefined>;
};

export function createProxy({
json,
resolveApiKey = resolveApiKeyFromEnv,
}: FalRemixProxyOptions): FalRemixProxy {
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: () => fromHeaders(request.headers),
getHeader: (name) => request.headers.get(name),
sendHeader: (name, value) => responseHeaders.set(name, value),
getRequestBody: async () => JSON.stringify(await request.json()),
sendResponse: responsePassthrough,
resolveApiKey,
});
};
return {
action: proxy,
loader: proxy,
};
}
4 changes: 4 additions & 0 deletions libs/proxy/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"compilerOptions": {
"target": "ES2020",
"importHelpers": false
},
"references": [
{
"path": "./tsconfig.lib.json"
Expand Down
Loading

0 comments on commit 2ff4816

Please sign in to comment.