import path from "path"; import { pathToFileURL } from "url"; import fs from "fs"; export async function callGate(opts: { client: string; action: string; user?: any; session?: any; data?: any; }) { const { client, action, user, session, data } = opts; // candidate paths to try (dev and prod) const candidates = [ path.join(__dirname, "bank", client, `${action}.ts`), path.join(__dirname, "bank", client, `${action}.js`), ]; let lastErr: any = null; let existsFile = null as any; for (const candidate of candidates) { try { if (fs.existsSync(candidate)) { existsFile = candidate; break; } } catch (ex) {} } if (!existsFile) { throw new Error(`handler not found for gate: ${client}/${action}`); } let mod: any; try { // eslint-disable-next-line @typescript-eslint/no-var-requires mod = require(existsFile); } catch (reqErr) { // try dynamic import with file:// URL const url = pathToFileURL(existsFile).href; // eslint-disable-next-line no-await-in-loop mod = await import(url); } if (!mod || typeof mod.default !== "function") { throw new Error( `handler at ${existsFile} not found or default export not a function` ); } // call the handler with (user, session, data) return await mod.default({ user, session, data }); } export default callGate;