Skip to content

Commit a1ab6b2

Browse files
authored
Add context support to query method (#56)
1 parent 1a8c75d commit a1ab6b2

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

src/modules/WarrantModule.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { API_VERSION } from "../constants";
33
import {
44
ListResponse,
55
ListWarrantParams,
6+
QueryParams,
67
QueryListParams,
78
QueryResult,
89
Warrant,
@@ -49,13 +50,24 @@ export default class WarrantModule {
4950
});
5051
}
5152

52-
public static async query(query: string, listParams: QueryListParams = {}, options: WarrantRequestOptions = {}): Promise<ListResponse<QueryResult>> {
53+
public static async query(query: string | QueryParams, listParams: QueryListParams = {}, options: WarrantRequestOptions = {}): Promise<ListResponse<QueryResult>> {
54+
const params: any = {
55+
...listParams,
56+
};
57+
58+
// preserve backwards compatibility
59+
if (typeof query === "string") {
60+
params.q = query;
61+
} else {
62+
params.q = query.query;
63+
if (query.context) {
64+
params.context = JSON.stringify(query.context);
65+
}
66+
}
67+
5368
return await WarrantClient.httpClient.get({
5469
url: `/${API_VERSION}/query`,
55-
params: {
56-
q: query,
57-
...listParams,
58-
},
70+
params: params,
5971
options,
6072
});
6173
}

src/types/Query.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { Warrant } from "./Warrant";
1+
import { Warrant, PolicyContext } from "./Warrant";
22
import { ListParams } from "./List";
33

4+
export interface QueryParams {
5+
query: string;
6+
context?: PolicyContext;
7+
}
8+
49
export interface QueryResult {
510
objectType: string;
611
objectId: string;

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export {
5858
Subject,
5959
} from "./Warrant";
6060
export {
61+
QueryParams,
6162
QueryResult,
6263
QueryListParams,
6364
} from "./Query";

test/LiveTest.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,12 @@ describe.skip('Live Test', function () {
933933
relation: "member",
934934
subject: role1,
935935
});
936+
await this.warrant.Warrant.create({
937+
object: permission1,
938+
relation: "member",
939+
subject: role2,
940+
policy: "tenantId == 123",
941+
});
936942
await this.warrant.Role.assignRoleToUser(userA.userId, role1.roleId);
937943
await this.warrant.Role.assignRoleToUser(userB.userId, role2.roleId);
938944

@@ -958,6 +964,20 @@ describe.skip('Live Test', function () {
958964
assert.strictEqual("role", resultSet.results[0].warrant.subject.objectType);
959965
assert.strictEqual("role1", resultSet.results[0].warrant.subject.objectId);
960966

967+
resultSet = await this.warrant.Warrant.query({
968+
query: "select permission where user:userB is member",
969+
context: {
970+
tenantId: 123,
971+
},
972+
});
973+
assert.strictEqual(3, resultSet.results.length);
974+
assert.strictEqual("permission", resultSet.results[0].objectType);
975+
assert.strictEqual("perm1", resultSet.results[0].objectId);
976+
assert.strictEqual("permission", resultSet.results[1].objectType);
977+
assert.strictEqual("perm2", resultSet.results[1].objectId);
978+
assert.strictEqual("permission", resultSet.results[2].objectType);
979+
assert.strictEqual("perm3", resultSet.results[2].objectId);
980+
961981
let warrantToken = await this.warrant.Role.delete(role1.roleId);
962982
assert(warrantToken);
963983
warrantToken = await this.warrant.Role.delete(role2.roleId);
@@ -972,5 +992,5 @@ describe.skip('Live Test', function () {
972992
assert(warrantToken);
973993
warrantToken = await this.warrant.User.delete(userB.userId);
974994
assert(warrantToken);
975-
})
995+
});
976996
});

0 commit comments

Comments
 (0)