-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(proxy): add remix-run support (#82)
- Loading branch information
Showing
6 changed files
with
20,377 additions
and
16,833 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.