Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions src/components/RegoEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
Expand All @@ -52,7 +51,6 @@ export function RegoEditor({
};
});
}

parent.value = String(query);
parent.dispatchEvent(new InputEvent("input", {bubbles: true}));
return [];
Expand Down
31 changes: 13 additions & 18 deletions src/components/helpers.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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 }));
}