diff --git a/package-lock.json b/package-lock.json index b26b95d..337bfe4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "playground", + "name": "data-playground", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/src/components/RegoEditor.js b/src/components/RegoEditor.js index 99ed6ac..3cca857 100644 --- a/src/components/RegoEditor.js +++ b/src/components/RegoEditor.js @@ -35,8 +35,7 @@ export function RegoEditor({ })); } // If we make it this far, the policy is on the server, so let's compile it: - const { result, errors } = await (await compilePolicy(opa, input, mappings)).json(); - const { query } = result; + const { query, errors } = await compilePolicy(opa, input, mappings); if (errors) { parent.value = String(""); // this means "no query produced" parent.dispatchEvent(new InputEvent("input", {bubbles: true})); @@ -52,7 +51,6 @@ export function RegoEditor({ }; }); } - parent.value = String(query); parent.dispatchEvent(new InputEvent("input", {bubbles: true})); return []; diff --git a/src/components/helpers.js b/src/components/helpers.js index a85a858..307ff57 100644 --- a/src/components/helpers.js +++ b/src/components/helpers.js @@ -1,3 +1,5 @@ +import { OPAClient } from "npm:@styra/opa@^1.7.9"; + export async function putPolicy(opa, id, code, raise = true) { const resp = await fetch(`${opa}v1/policies/${id}`, { method: "PUT", @@ -10,22 +12,15 @@ export async function putPolicy(opa, id, code, raise = true) { return resp; } -export async function compilePolicy(opa, input, mappings = {}, query = "data.filters.include") { - const resp = await fetch(`${opa}v1/compile`, { - method: "POST", - body: JSON.stringify({ - input, - query, - options: { - targetSQLTableMappings: { - postgresql: mappings, - }, - }, - }), - headers: { - "Content-Type": "application/json", - "Accept": "application/vnd.styra.sql.postgresql+json" - }, - }); - return resp; +export async function compilePolicy(opa, input, tableMappings = {}, path = "filters/include") { + const href = window.location.toString(); + const u = new URL(href); // TODO(sr): better way?! + u.pathname = ""; + u.search = ""; + const client = new OPAClient(u.toString()); + return client.getFilters(path, input, + { + target: "postgresql", + tableMappings, + }).catch((e) => ({ errors: e.data$.errors })); }