diff --git a/modelina-website/src/pages/api/generate.ts b/modelina-website/src/pages/api/generate.ts index e1065e5e33..54175718fa 100644 --- a/modelina-website/src/pages/api/generate.ts +++ b/modelina-website/src/pages/api/generate.ts @@ -16,6 +16,7 @@ import { getCSharpModels } from '@/pages/api/functions/CSharpGenerator'; import { getCplusplusModels } from './functions/CplusplusGenerator'; import { getKotlinModels } from './functions/KotlinGenerator'; import { getPhpModels } from './functions/PhpGenerator'; +import { Handler, HandlerEvent } from '@netlify/functions'; export async function generateNewCode(message: GenerateMessage): Promise { let input: any = defaultAsyncapiDocument; @@ -79,4 +80,34 @@ export default async function generate(req: NextApiRequest, res: NextApiResponse error: e.message }); } -} \ No newline at end of file +} + +/** + * Netlify function specific code, can be ignored in local development. + */ + +const handler: Handler = async (event: HandlerEvent) => { + if (event.httpMethod !== 'POST') { + return { statusCode: 405, body: `Method ${event.httpMethod} Not Allowed` }; + } + if (!event.body) { + return { statusCode: 405, body: 'Missing body' }; + } + try { + const message = JSON.parse(event.body) as GenerateMessage; + const response = await generateNewCode(message); + return { + statusCode: 200, + body: JSON.stringify(response) + }; + } catch (e) { + console.error(e); + return { + statusCode: 500, + body: JSON.stringify({ + error: 'There was an error generating the models' + }) + }; + } +} +export { handler }; \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index 731a23fdb4..f0e6844c60 100644 --- a/netlify.toml +++ b/netlify.toml @@ -5,6 +5,10 @@ [build.environment] NODE_VERSION = "18.17.0" NPM_VERSION = "9.6.7" + NEXT_PUBLIC_API_PATH = "/.netlify/functions" + +[functions] + directory = "modelina-website/.next/server/pages/api" [[plugins]] package = "@netlify/plugin-nextjs" \ No newline at end of file