Skip to content

Commit 5c59621

Browse files
committed
helpers: use @styra/opa
1 parent 0257f11 commit 5c59621

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/RegoEditor.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ export function RegoEditor({
3535
}));
3636
}
3737
// If we make it this far, the policy is on the server, so let's compile it:
38-
const { result, errors } = await (await compilePolicy(opa, input, mappings)).json();
39-
const { query } = result;
38+
const { query, errors } = await compilePolicy(opa, input, mappings);
4039
if (errors) {
4140
parent.value = String(""); // this means "no query produced"
4241
parent.dispatchEvent(new InputEvent("input", {bubbles: true}));
@@ -52,7 +51,6 @@ export function RegoEditor({
5251
};
5352
});
5453
}
55-
5654
parent.value = String(query);
5755
parent.dispatchEvent(new InputEvent("input", {bubbles: true}));
5856
return [];

src/components/helpers.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { OPAClient } from "npm:@styra/opa@^1.7.9";
2+
13
export async function putPolicy(opa, id, code, raise = true) {
24
const resp = await fetch(`${opa}v1/policies/${id}`, {
35
method: "PUT",
@@ -10,22 +12,15 @@ export async function putPolicy(opa, id, code, raise = true) {
1012
return resp;
1113
}
1214

13-
export async function compilePolicy(opa, input, mappings = {}, query = "data.filters.include") {
14-
const resp = await fetch(`${opa}v1/compile`, {
15-
method: "POST",
16-
body: JSON.stringify({
17-
input,
18-
query,
19-
options: {
20-
targetSQLTableMappings: {
21-
postgresql: mappings,
22-
},
23-
},
24-
}),
25-
headers: {
26-
"Content-Type": "application/json",
27-
"Accept": "application/vnd.styra.sql.postgresql+json"
28-
},
29-
});
30-
return resp;
15+
export async function compilePolicy(opa, input, tableMappings = {}, path = "filters/include") {
16+
const href = window.location.toString();
17+
const u = new URL(href); // TODO(sr): better way?!
18+
u.pathname = "";
19+
u.search = "";
20+
const client = new OPAClient(u.toString());
21+
return client.getFilters(path, input,
22+
{
23+
target: "postgresql",
24+
tableMappings,
25+
}).catch((e) => ({ errors: e.data$.errors }));
3126
}

0 commit comments

Comments
 (0)