feat(proxy): add remix-run support (#82)
This commit is contained in:
parent
c3a3c3d21a
commit
2ff481614b
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@fal-ai/serverless-proxy",
|
"name": "@fal-ai/serverless-proxy",
|
||||||
"version": "0.8.0",
|
"version": "0.9.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -20,6 +20,7 @@
|
|||||||
".": "./src/index.js",
|
".": "./src/index.js",
|
||||||
"./express": "./src/express.js",
|
"./express": "./src/express.js",
|
||||||
"./nextjs": "./src/nextjs.js",
|
"./nextjs": "./src/nextjs.js",
|
||||||
|
"./remix": "./src/remix.js",
|
||||||
"./svelte": "./src/svelte.js"
|
"./svelte": "./src/svelte.js"
|
||||||
},
|
},
|
||||||
"typesVersions": {
|
"typesVersions": {
|
||||||
@ -30,6 +31,9 @@
|
|||||||
"nextjs": [
|
"nextjs": [
|
||||||
"src/nextjs.d.ts"
|
"src/nextjs.d.ts"
|
||||||
],
|
],
|
||||||
|
"remix": [
|
||||||
|
"src/remix.d.ts"
|
||||||
|
],
|
||||||
"svelte": [
|
"svelte": [
|
||||||
"src/svelte.d.ts"
|
"src/svelte.d.ts"
|
||||||
]
|
]
|
||||||
@ -38,6 +42,7 @@
|
|||||||
"main": "./src/index.js",
|
"main": "./src/index.js",
|
||||||
"types": "./src/index.d.ts",
|
"types": "./src/index.d.ts",
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
|
"@remix-run/dev": "^2.0.0",
|
||||||
"@sveltejs/kit": "^2.0.0",
|
"@sveltejs/kit": "^2.0.0",
|
||||||
"express": "^4.0.0",
|
"express": "^4.0.0",
|
||||||
"next": "13.4 - 14",
|
"next": "13.4 - 14",
|
||||||
@ -45,6 +50,9 @@
|
|||||||
"react-dom": "^18.0.0"
|
"react-dom": "^18.0.0"
|
||||||
},
|
},
|
||||||
"peerDependenciesMeta": {
|
"peerDependenciesMeta": {
|
||||||
|
"@remix-run/dev": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
"@sveltejs/kit": {
|
"@sveltejs/kit": {
|
||||||
"optional": true
|
"optional": true
|
||||||
},
|
},
|
||||||
|
|||||||
@ -136,3 +136,5 @@ export function fromHeaders(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const responsePassthrough = (res: Response) => Promise.resolve(res);
|
export const responsePassthrough = (res: Response) => Promise.resolve(res);
|
||||||
|
|
||||||
|
export const resolveApiKeyFromEnv = () => Promise.resolve(getFalKey());
|
||||||
|
|||||||
58
libs/proxy/src/remix.ts
Normal file
58
libs/proxy/src/remix.ts
Normal file
@ -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,
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -2,6 +2,10 @@
|
|||||||
"extends": "../../tsconfig.base.json",
|
"extends": "../../tsconfig.base.json",
|
||||||
"files": [],
|
"files": [],
|
||||||
"include": [],
|
"include": [],
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2020",
|
||||||
|
"importHelpers": false
|
||||||
|
},
|
||||||
"references": [
|
"references": [
|
||||||
{
|
{
|
||||||
"path": "./tsconfig.lib.json"
|
"path": "./tsconfig.lib.json"
|
||||||
|
|||||||
4910
package-lock.json
generated
4910
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -30,6 +30,8 @@
|
|||||||
"@msgpack/msgpack": "^3.0.0-beta2",
|
"@msgpack/msgpack": "^3.0.0-beta2",
|
||||||
"@oclif/core": "^2.3.0",
|
"@oclif/core": "^2.3.0",
|
||||||
"@oclif/plugin-help": "^5.2.5",
|
"@oclif/plugin-help": "^5.2.5",
|
||||||
|
"@remix-run/dev": "^2.11.1",
|
||||||
|
"@remix-run/node": "^2.11.1",
|
||||||
"axios": "^1.0.0",
|
"axios": "^1.0.0",
|
||||||
"chalk": "^5.3.0",
|
"chalk": "^5.3.0",
|
||||||
"change-case": "^4.1.2",
|
"change-case": "^4.1.2",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user