Skip to content

Commit

Permalink
Add CORS header (any origin)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongwartz committed Jun 17, 2024
1 parent 3de1d95 commit 8777485
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions src/app/api/pkl/evaluate/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
import { evaluate } from "@pkl-community/pkl-eval";

const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST",
} as const;

export async function OPTIONS() {
return new Response("", {
headers,
});
}

export async function POST(req: Request) {
const { pklInput, outputFormat } = (await req.json()) as {
pklInput: string;
outputFormat: any;
};
let pklInput: string;
let outputFormat: string;
try {
const body = (await req.json()) as {
pklInput: string;
outputFormat: string;
};
pklInput = body.pklInput;
outputFormat = body.outputFormat;
} catch (err) {
return Response.json(
{ error: err instanceof Error ? err.message : JSON.stringify(err) },
{ status: 400, headers }
);
}

try {
return Response.json({
output: await evaluate(pklInput, {
format: outputFormat ?? "pcf",
allowedResources: ["package:"],
allowedModules: ["pkl:", "repl:"],
cache: {
enabled: true,
directory: "/tmp/pkl",
},
}),
});
return Response.json(
{
output: await evaluate(pklInput, {
format: (outputFormat as any) ?? "pcf",
allowedResources: ["package:"],
allowedModules: ["pkl:", "repl:"],
cache: {
enabled: true,
directory: "/tmp/pkl",
},
}),
},
{
headers,
}
);
} catch (err) {
return Response.json(
{ error: err instanceof Error ? err.message : JSON.stringify(err) },
{ status: 500 }
{ status: 500, headers }
);
}
}

0 comments on commit 8777485

Please sign in to comment.