diff --git a/src/pages/api/run.ts b/src/pages/api/run.ts index 2868a11..5ae60c6 100644 --- a/src/pages/api/run.ts +++ b/src/pages/api/run.ts @@ -15,6 +15,12 @@ const exec = promisify(execSync) let tmpDir: string +/** + * body is the stl + * query + * price: algorithm from settings + * adionnal settings from https://manual.slic3r.org/advanced/command-line + */ export const post: APIRoute = async ({ request }) => { if (!tmpDir) { tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'paas-')) @@ -121,9 +127,9 @@ export const post: APIRoute = async ({ request }) => { await fs.rm(stlPath) await fs.rm(gcodePath) const params = getParams(gcode) - let price = -1 + let price: string | undefined if (query?.algo) { - let algo = query.algo as string + let algo = decodeURI(query.algo as string) // objectLoop(params, (value, key) => { // if (typeof value !== 'number') { // return @@ -132,13 +138,39 @@ export const post: APIRoute = async ({ request }) => { // algo = algo.replace(key, value.toString()) // } // }) - price = evaluate(algo, params) + try { + const tmp = evaluate(algo, params) + if (typeof tmp === 'number') { + price = tmp.toFixed(2) + } else { + return buildRFC7807Error({ + type: '/algorithm-error', + status: 500, + title: 'Algorithm compilation error', + details: 'It seems the algorithm resolution failed', + algorithm: algo, + algorithmError: 'Algorithm return a Unit', + parameters: params + }) + } + } catch (e) { + console.dir(e) + return buildRFC7807Error({ + type: '/algorithm-error', + status: 500, + title: 'Algorithm compilation error', + details: 'It seems the algorithm resolution failed', + algorithm: algo, + algorithmError: e, + parameters: params + }) + } } console.log('request successfull :)', file) return { status: 200, body: JSON.stringify({ - price: parseFloat(price.toFixed(2)), + price: price ? parseFloat(price) : undefined, ...getParams(gcode), gcode })