@fal-ai/server-proxy
Version:
The fal.ai server proxy adapter for JavaScript and TypeScript Web frameworks
71 lines • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.createHandler = exports.route = void 0;
const config_1 = require("./config");
const index_1 = require("./index");
/**
* The default Express route for the fal.ai client proxy.
*/
exports.route = index_1.DEFAULT_PROXY_ROUTE;
/**
* Creates a request handler that proxies requests to the fal API.
*
* This is a drop-in handler for Express applications so that the client can be called
* directly from the client-side code while keeping API keys safe.
*
* @param config the proxy configuration options.
* @returns an Express request handler function.
*/
const createHandler = (config = {}) => {
const resolvedConfig = (0, config_1.resolveProxyConfig)(config);
return async (request, response, next) => {
await (0, index_1.handleRequest)({
id: "express",
method: request.method,
getRequestBody: async () => JSON.stringify(request.body),
getHeaders: () => request.headers,
getHeader: (name) => request.headers[name],
sendHeader: (name, value) => response.setHeader(name, value),
respondWith: (status, data) => response.status(status).json(data),
sendResponse: async (res) => {
if (res.body instanceof ReadableStream) {
const reader = res.body.getReader();
const stream = async () => {
const { done, value } = await reader.read();
if (done) {
response.end();
return response;
}
response.write(value);
return await stream();
};
return await stream().catch((error) => {
if (!response.headersSent) {
response.status(500).send(error.message);
}
else {
response.end();
}
});
}
if (res.headers.get("content-type")?.includes("application/json")) {
return response.status(res.status).json(await res.json());
}
return response.status(res.status).send(await res.text());
},
}, resolvedConfig);
next();
};
};
exports.createHandler = createHandler;
/**
* The Express route handler for the fal.ai client proxy.
*
* @param request The Express request object.
* @param response The Express response object.
* @param next The Express next function.
*
* @deprecated Use `createHandler` instead.
*/
exports.handler = (0, exports.createHandler)();
//# sourceMappingURL=express.js.map