Skip to content

Commit c6e314c

Browse files
authored
Simplify query method to take the requested query as a string which the client can construct however they want (#40)
1 parent a3f5159 commit c6e314c

File tree

4 files changed

+31
-158
lines changed

4 files changed

+31
-158
lines changed

src/modules/WarrantModule.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import WarrantClient from "../WarrantClient";
22
import { WarrantRequestOptions } from "../types/WarrantRequestOptions";
3-
import Query from "../types/Query";
43
import Warrant, { isSubject, isWarrantObject, ListWarrantOptions, WarrantParams } from "../types/Warrant";
4+
import { QueryOptions, QueryResponse } from "../types/Query";
55

66
export default class WarrantModule {
77
public static async create(warrant: WarrantParams, options: WarrantRequestOptions = {}): Promise<Warrant> {
@@ -40,13 +40,13 @@ export default class WarrantModule {
4040
}
4141
}
4242

43-
public static async queryWarrants(query: Query, listOptions: ListWarrantOptions = {}, options: WarrantRequestOptions = {}): Promise<Warrant[]> {
43+
public static async query(query: string, queryOptions: QueryOptions = {}, options: WarrantRequestOptions = {}): Promise<QueryResponse> {
4444
try {
4545
return await WarrantClient.httpClient.get({
4646
url: "/v1/query",
4747
params: {
48-
...query.toObject(),
49-
...listOptions
48+
q: query,
49+
...options,
5050
},
5151
options,
5252
});

src/types/Query.ts

Lines changed: 12 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,18 @@
1-
import { isSubject, isWarrantObject, PolicyContext, Subject, WarrantObject, WarrantObjectLiteral } from "./Warrant";
1+
import Warrant from "./Warrant";
22

3-
export interface ForClause {
4-
object?: WarrantObject | WarrantObjectLiteral;
5-
relation?: string;
6-
subject?: Subject | WarrantObject;
7-
context?: PolicyContext;
3+
export interface QueryOptions {
4+
lastId?: string;
85
}
96

10-
export interface WhereClause {
11-
object?: WarrantObject | WarrantObjectLiteral;
12-
relation?: string;
13-
subject?: Subject | WarrantObject;
14-
context?: PolicyContext;
7+
export interface QueryResponse {
8+
results: QueryResult[];
9+
lastId?: string;
1510
}
1611

17-
export default class Query {
18-
selectClause: string;
19-
forClause: ForClause;
20-
whereClause: WhereClause;
21-
22-
constructor(selectClause: string, forClause: ForClause = {}, whereClause: WhereClause = {}) {
23-
this.selectClause = selectClause;
24-
this.forClause = forClause;
25-
this.whereClause = whereClause;
26-
}
27-
28-
public static selectWarrants(): Query {
29-
return new Query("warrants");
30-
}
31-
32-
public static selectExplicitWarrants(): Query {
33-
return new Query("explicit warrants");
34-
}
35-
36-
public for(forClause: ForClause): Query {
37-
this.forClause = forClause;
38-
39-
return this;
40-
}
41-
42-
public where(whereClause: WhereClause): Query {
43-
this.whereClause = whereClause;
44-
45-
return this;
46-
}
47-
48-
public toObject(): Object {
49-
let queryParams: { [key: string]: string } = {};
50-
51-
queryParams["select"] = this.selectClause;
52-
53-
if (Object.keys(this.forClause).length > 0) {
54-
let forParamsString: string = "";
55-
for (const [key, value] of Object.entries(this.forClause)) {
56-
switch (key) {
57-
case 'object':
58-
if (isWarrantObject(value)) {
59-
forParamsString += `${key}=${value.getObjectType()}:${value.getObjectId()},`;
60-
} else {
61-
forParamsString += `${key}=${value.objectType}:${value.objectId},`;
62-
}
63-
break;
64-
case 'relation':
65-
forParamsString += `${key}=${value},`;
66-
break;
67-
case 'subject':
68-
if (isSubject(value)) {
69-
if (value.relation) {
70-
forParamsString += `${key}=${value.objectType}:${value.objectId}#${value.relation},`;
71-
} else {
72-
forParamsString += `${key}=${value.objectType}:${value.objectId},`;
73-
}
74-
} else {
75-
forParamsString += `${key}=${value.getObjectType()}:${value.getObjectId()},`;
76-
}
77-
break;
78-
case 'context':
79-
let contextString = '';
80-
for (const [contextKey, contextValue] of Object.entries(value)) {
81-
contextString += `${contextKey}=${contextValue} `;
82-
}
83-
contextString = contextString.slice(0, -1);
84-
forParamsString += `${key}=${contextString},`;
85-
break;
86-
}
87-
}
88-
forParamsString = forParamsString.slice(0, -1);
89-
90-
queryParams["for"] = forParamsString;
91-
}
92-
93-
if (Object.keys(this.whereClause).length > 0) {
94-
let whereParamsString: string = "";
95-
for (const [key, value] of Object.entries(this.whereClause)) {
96-
switch (key) {
97-
case 'object':
98-
if (isWarrantObject(value)) {
99-
whereParamsString += `${key}=${value.getObjectType()}:${value.getObjectId()},`;
100-
} else {
101-
whereParamsString += `${key}=${value.objectType}:${value.objectId},`;
102-
}
103-
break;
104-
case 'relation':
105-
whereParamsString += `${key}=${value},`;
106-
break;
107-
case 'subject':
108-
if (isSubject(value)) {
109-
if (value.relation) {
110-
whereParamsString += `${key}=${value.objectType}:${value.objectId}#${value.relation},`;
111-
} else {
112-
whereParamsString += `${key}=${value.objectType}:${value.objectId},`;
113-
}
114-
} else {
115-
whereParamsString += `${key}=${value.getObjectType()}:${value.getObjectId()},`;
116-
}
117-
break;
118-
case 'context':
119-
let contextString = '';
120-
for (const [contextKey, contextValue] of Object.entries(value)) {
121-
contextString += `${contextKey}=${contextValue} `;
122-
}
123-
contextString = contextString.slice(0, -1);
124-
whereParamsString += `${key}=${contextString},`;
125-
break;
126-
}
127-
}
128-
whereParamsString = whereParamsString.slice(0, -1);
129-
130-
queryParams["where"] = whereParamsString;
131-
}
132-
133-
return queryParams;
134-
}
12+
export interface QueryResult {
13+
objectType: string;
14+
objectId: string;
15+
warrant: Warrant;
16+
isImplicit: boolean;
17+
meta: { [key: string]: any; };
13518
}

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export { default as Config } from "./Config";
33
export { CreateFeatureParams, ListFeatureOptions } from "./Feature";
44
export { CreatePermissionParams, ListPermissionOptions, UpdatePermissionParams } from "./Permission";
55
export { CreatePricingTierParams, ListPricingTierOptions } from "./PricingTier";
6-
export { default as Query } from "./Query";
6+
export { QueryOptions, QueryResponse, QueryResult } from "./Query";
77
export { CreateRoleParams, ListRoleOptions, UpdateRoleParams } from "./Role";
88
export { SessionParams, SelfServiceSessionParams, SelfServiceStrategy } from "./Session";
99
export { CreateTenantParams, ListTenantOptions, UpdateTenantParams } from "./Tenant";

test/LiveTest.spec.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { SelfServiceStrategy, Query, WarrantClient } = require("../dist/index");
1+
const { SelfServiceStrategy, WarrantClient } = require("../dist/index");
22
var assert = require('assert');
33

4-
// Uncomment .skip and add your API_KEY to run tests
4+
// Remove .skip and add your API_KEY to run tests
55
describe.skip('Live Test', function () {
66
before(function () {
77
this.warrant = new WarrantClient({ apiKey: "", endpoint: "https://api.warrant.dev" });
@@ -166,7 +166,7 @@ describe.skip('Live Test', function () {
166166
await this.warrant.User.delete("user-2");
167167
await this.warrant.Tenant.delete("tenant-1");
168168
await this.warrant.Tenant.delete("tenant-2");
169-
})
169+
});
170170

171171
it('multi-tenancy example', async function () {
172172
// Create users
@@ -243,7 +243,7 @@ describe.skip('Live Test', function () {
243243

244244
adminRolePermissions = await this.warrant.Permission.listPermissionsForRole(adminRole.roleId, { limit: 100 }, { warrantToken: "latest" });
245245
assert.strictEqual(adminRolePermissions.length, 1);
246-
assert.strictEqual(adminRolePermissions[0].permissionId, createPermission.permissionId)
246+
assert.strictEqual(adminRolePermissions[0].permissionId, createPermission.permissionId);
247247

248248
await this.warrant.Permission.removePermissionFromRole(adminRole.roleId, createPermission.permissionId);
249249
await this.warrant.Role.removeRoleFromUser(adminUser.userId, adminRole.roleId);
@@ -506,23 +506,13 @@ describe.skip('Live Test', function () {
506506
});
507507
assert.strictEqual(userHasPermission, true);
508508

509-
// const warrantQuery = Query
510-
// .selectWarrants()
511-
// .for({
512-
// subject: newUser
513-
// })
514-
// .where({
515-
// subject: {
516-
// objectType: "user",
517-
// objectId: newUser.userId
518-
// }
519-
// });
520-
// const warrants = await this.warrant.Warrant.queryWarrants(warrantQuery, { limit: 100 });
521-
522-
// assert.strictEqual(warrants.length, 1);
523-
// assert.strictEqual(warrants[0].objectType, "permission");
524-
// assert.strictEqual(warrants[0].objectId, "perm1");
525-
// assert.strictEqual(warrants[0].relation, "member");
509+
const query = `select permission where user:${newUser.userId} is member`;
510+
const response = await this.warrant.Warrant.query(query);
511+
512+
assert.strictEqual(response.results.length, 1);
513+
assert.strictEqual(response.results[0].objectType, "permission");
514+
assert.strictEqual(response.results[0].objectId, "perm1");
515+
assert.strictEqual(response.results[0].warrant.relation, "member");
526516

527517
await this.warrant.Warrant.delete({
528518
object: newPermission,
@@ -543,7 +533,7 @@ describe.skip('Live Test', function () {
543533
await this.warrant.Permission.delete(newPermission.permissionId);
544534
});
545535

546-
it('warrant with policy', async function() {
536+
it('warrant with policy', async function () {
547537
await this.warrant.Warrant.create({
548538
object: {
549539
objectType: "permission",
@@ -557,7 +547,7 @@ describe.skip('Live Test', function () {
557547
policy: `geo == "us"`
558548
});
559549

560-
checkResult = await this.warrant.Authorization.check({
550+
let checkResult = await this.warrant.Authorization.check({
561551
object: {
562552
objectType: "permission",
563553
objectId: "test-permission"
@@ -606,4 +596,4 @@ describe.skip('Live Test', function () {
606596
policy: `geo == "us"`
607597
});
608598
});
609-
})
599+
});

0 commit comments

Comments
 (0)