Skip to content

Commit 02b134e

Browse files
authored
Add warrant object literal (#13)
* Use warrant-node v3.2.0 * Add WarrantObjectLiteral
1 parent cc90853 commit 02b134e

File tree

6 files changed

+33
-17
lines changed

6 files changed

+33
-17
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
"axios": "^0.21.2"
3636
},
3737
"devDependencies": {
38-
"@warrantdev/warrant-node": "^3.0.0",
3938
"clean-webpack-plugin": "^4.0.0-alpha.0",
4039
"mocha": "^10.2.0",
4140
"ts-loader": "^9.2.3",
4241
"typescript": "^4.3.4",
4342
"webpack": "^5.41.1",
4443
"webpack-cli": "^4.7.2",
45-
"xmlhttprequest": "^1.8.0"
44+
"xmlhttprequest": "^1.8.0",
45+
"@warrantdev/warrant-node": "^3.2.0"
4646
}
4747
}

src/WarrantClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Check, {
1010
} from "./types/Check";
1111
import Permission from "./types/Permission";
1212
import ApiClient from "./HttpClient";
13+
import { isWarrantObject } from "./types/WarrantObject";
1314

1415
export default class WarrantClient {
1516
private readonly config: Config;
@@ -29,8 +30,8 @@ export default class WarrantClient {
2930
data: {
3031
op: CheckOp.AnyOf,
3132
warrants: [{
32-
objectType: check.object.getObjectType(),
33-
objectId: check.object.getObjectId(),
33+
objectType: isWarrantObject(check.object) ? check.object.getObjectType() : check.object.objectType,
34+
objectId: isWarrantObject(check.object) ? check.object.getObjectId() : check.object.objectId,
3435
relation: check.relation,
3536
context: check.context,
3637
}],
@@ -47,8 +48,8 @@ export default class WarrantClient {
4748
data: {
4849
op: check.op,
4950
warrants: check.warrants.map((warrant: CheckWarrant) => ({
50-
objectType: warrant.object.getObjectType(),
51-
objectId: warrant.object.getObjectId(),
51+
objectType: isWarrantObject(warrant.object) ? warrant.object.getObjectType() : warrant.object.objectType,
52+
objectId: isWarrantObject(warrant.object) ? warrant.object.getObjectId() : warrant.object.objectId,
5253
relation: warrant.relation,
5354
context: warrant.context,
5455
})),

src/types/Check.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Context from "./Context";
2-
import WarrantObject from "./WarrantObject";
2+
import WarrantObject, { WarrantObjectLiteral } from "./WarrantObject";
33

44
export enum CheckOp {
55
AllOf = "allOf",
66
AnyOf = "anyOf",
77
}
88

99
export interface CheckWarrant {
10-
object: WarrantObject;
10+
object: WarrantObject | WarrantObjectLiteral;
1111
relation: string;
1212
context?: Context;
1313
}

src/types/WarrantObject.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ export default interface WarrantObject {
22
getObjectType(): string;
33
getObjectId(): string;
44
}
5+
6+
export interface WarrantObjectLiteral {
7+
objectType: string;
8+
objectId: string;
9+
}
10+
11+
export function isWarrantObject(object: any): object is WarrantObject {
12+
return object.getObjectType !== undefined && object.getObjectId !== undefined;
13+
}

test/LiveTest.spec.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ describe.skip('Live Test', function () {
4040

4141
it('check', async function () {
4242
let authorized = await this.warrant.check({
43-
object: this.role,
43+
object: {
44+
objectType: this.role.getObjectType(),
45+
objectId: this.role.roleId,
46+
},
4447
relation: "member",
4548
subject: this.user,
4649
});
4750
assert(authorized);
4851

4952
authorized = await this.warrant.check({
50-
object: this.role,
53+
object: {
54+
objectType: this.role.getObjectType(),
55+
objectId: this.role.roleId,
56+
},
5157
relation: "owner",
5258
subject: this.user,
5359
});

0 commit comments

Comments
 (0)