Skip to content

Commit b6c00e2

Browse files
authored
Merge pull request #34 from warrant-dev/UseWarrantEndpoints
Use create/delete warrant endpoints
2 parents 2347ff3 + 82cf380 commit b6c00e2

File tree

7 files changed

+221
-166
lines changed

7 files changed

+221
-166
lines changed

src/modules/Feature.ts

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import WarrantModule from "./WarrantModule";
12
import WarrantClient from "../WarrantClient";
23
import { CreateFeatureParams, ListFeatureOptions } from "../types/Feature";
3-
import { WarrantObject } from "../types/Warrant";
4+
import Warrant, { WarrantObject } from "../types/Warrant";
45
import { ObjectType } from "../types/ObjectType";
56

67
export default class Feature implements WarrantObject {
@@ -74,26 +75,32 @@ export default class Feature implements WarrantObject {
7475
}
7576
}
7677

77-
public static async assignFeatureToPricingTier(pricingTierId: string, featureId: string): Promise<Feature> {
78-
try {
79-
const response = await WarrantClient.httpClient.post({
80-
url: `/v1/pricing-tiers/${pricingTierId}/features/${featureId}`,
81-
});
82-
83-
return new Feature(response.featureId);
84-
} catch (e) {
85-
throw e;
86-
}
78+
public static async assignFeatureToPricingTier(pricingTierId: string, featureId: string): Promise<Warrant> {
79+
return WarrantModule.create({
80+
object: {
81+
objectType: ObjectType.Feature,
82+
objectId: featureId,
83+
},
84+
relation: "member",
85+
subject: {
86+
objectType: ObjectType.PricingTier,
87+
objectId: pricingTierId,
88+
}
89+
});
8790
}
8891

8992
public static async removeFeatureFromPricingTier(pricingTierId: string, featureId: string): Promise<void> {
90-
try {
91-
return await WarrantClient.httpClient.delete({
92-
url: `/v1/pricing-tiers/${pricingTierId}/features/${featureId}`,
93-
});
94-
} catch (e) {
95-
throw e;
96-
}
93+
return WarrantModule.delete({
94+
object: {
95+
objectType: ObjectType.Feature,
96+
objectId: featureId,
97+
},
98+
relation: "member",
99+
subject: {
100+
objectType: ObjectType.PricingTier,
101+
objectId: pricingTierId,
102+
}
103+
});
97104
}
98105

99106
public static async listFeaturesForTenant(tenantId: string, listOptions: ListFeatureOptions = {}): Promise<Feature[]> {
@@ -109,26 +116,32 @@ export default class Feature implements WarrantObject {
109116
}
110117
}
111118

112-
public static async assignFeatureToTenant(tenantId: string, featureId: string): Promise<Feature> {
113-
try {
114-
const response = await WarrantClient.httpClient.post({
115-
url: `/v1/tenants/${tenantId}/features/${featureId}`,
116-
});
117-
118-
return new Feature(response.featureId);
119-
} catch (e) {
120-
throw e;
121-
}
119+
public static async assignFeatureToTenant(tenantId: string, featureId: string): Promise<Warrant> {
120+
return WarrantModule.create({
121+
object: {
122+
objectType: ObjectType.Feature,
123+
objectId: featureId,
124+
},
125+
relation: "member",
126+
subject: {
127+
objectType: ObjectType.Tenant,
128+
objectId: tenantId,
129+
}
130+
});
122131
}
123132

124133
public static async removeFeatureFromTenant(tenantId: string, featureId: string): Promise<void> {
125-
try {
126-
return await WarrantClient.httpClient.delete({
127-
url: `/v1/tenants/${tenantId}/features/${featureId}`,
128-
});
129-
} catch (e) {
130-
throw e;
131-
}
134+
return WarrantModule.delete({
135+
object: {
136+
objectType: ObjectType.Feature,
137+
objectId: featureId,
138+
},
139+
relation: "member",
140+
subject: {
141+
objectType: ObjectType.Tenant,
142+
objectId: tenantId,
143+
}
144+
});
132145
}
133146

134147
public static async listFeaturesForUser(userId: string, listOptions: ListFeatureOptions = {}): Promise<Feature[]> {
@@ -144,26 +157,32 @@ export default class Feature implements WarrantObject {
144157
}
145158
}
146159

147-
public static async assignFeatureToUser(userId: string, featureId: string): Promise<Feature> {
148-
try {
149-
const response = await WarrantClient.httpClient.post({
150-
url: `/v1/users/${userId}/features/${featureId}`,
151-
});
152-
153-
return new Feature(response.featureId);
154-
} catch (e) {
155-
throw e;
156-
}
160+
public static async assignFeatureToUser(userId: string, featureId: string): Promise<Warrant> {
161+
return WarrantModule.create({
162+
object: {
163+
objectType: ObjectType.Feature,
164+
objectId: featureId,
165+
},
166+
relation: "member",
167+
subject: {
168+
objectType: ObjectType.User,
169+
objectId: userId,
170+
}
171+
});
157172
}
158173

159174
public static async removeFeatureFromUser(userId: string, featureId: string): Promise<void> {
160-
try {
161-
return await WarrantClient.httpClient.delete({
162-
url: `/v1/users/${userId}/features/${featureId}`,
163-
});
164-
} catch (e) {
165-
throw e;
166-
}
175+
return WarrantModule.delete({
176+
object: {
177+
objectType: ObjectType.Feature,
178+
objectId: featureId,
179+
},
180+
relation: "member",
181+
subject: {
182+
objectType: ObjectType.User,
183+
objectId: userId,
184+
}
185+
});
167186
}
168187

169188
// WarrantObject methods

src/modules/Permission.ts

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import WarrantModule from "./WarrantModule";
12
import WarrantClient from "../WarrantClient";
23
import { CreatePermissionParams, ListPermissionOptions, UpdatePermissionParams } from "../types/Permission";
34
import { ObjectType } from "../types/ObjectType";
4-
import { WarrantObject } from "../types/Warrant";
5+
import Warrant, { WarrantObject } from "../types/Warrant";
56

67
export default class Permission implements WarrantObject {
78
permissionId: string;
@@ -91,26 +92,32 @@ export default class Permission implements WarrantObject {
9192
}
9293
}
9394

94-
public static async assignPermissionToUser(userId: string, permissionId: string): Promise<Permission> {
95-
try {
96-
const response = await WarrantClient.httpClient.post({
97-
url: `/v1/users/${userId}/permissions/${permissionId}`,
98-
});
99-
100-
return new Permission(response.permissionId, response.name, response.description);
101-
} catch (e) {
102-
throw e;
103-
}
95+
public static async assignPermissionToUser(userId: string, permissionId: string): Promise<Warrant> {
96+
return WarrantModule.create({
97+
object: {
98+
objectType: ObjectType.Permission,
99+
objectId: permissionId,
100+
},
101+
relation: "member",
102+
subject: {
103+
objectType: ObjectType.User,
104+
objectId: userId,
105+
}
106+
});
104107
}
105108

106109
public static async removePermissionFromUser(userId: string, permissionId: string): Promise<void> {
107-
try {
108-
return await WarrantClient.httpClient.delete({
109-
url: `/v1/users/${userId}/permissions/${permissionId}`,
110-
});
111-
} catch (e) {
112-
throw e;
113-
}
110+
return WarrantModule.delete({
111+
object: {
112+
objectType: ObjectType.Permission,
113+
objectId: permissionId,
114+
},
115+
relation: "member",
116+
subject: {
117+
objectType: ObjectType.User,
118+
objectId: userId,
119+
}
120+
});
114121
}
115122

116123
public static async listPermissionsForRole(roleId: string, listOptions: ListPermissionOptions = {}): Promise<Permission[]> {
@@ -126,26 +133,32 @@ export default class Permission implements WarrantObject {
126133
}
127134
}
128135

129-
public static async assignPermissionToRole(roleId: string, permissionId: string): Promise<Permission> {
130-
try {
131-
const response = await WarrantClient.httpClient.post({
132-
url: `/v1/roles/${roleId}/permissions/${permissionId}`,
133-
});
134-
135-
return new Permission(response.permissionId, response.name, response.description);
136-
} catch (e) {
137-
throw e;
138-
}
136+
public static async assignPermissionToRole(roleId: string, permissionId: string): Promise<Warrant> {
137+
return WarrantModule.create({
138+
object: {
139+
objectType: ObjectType.Permission,
140+
objectId: permissionId,
141+
},
142+
relation: "member",
143+
subject: {
144+
objectType: ObjectType.Role,
145+
objectId: roleId,
146+
}
147+
});
139148
}
140149

141150
public static async removePermissionFromRole(roleId: string, permissionId: string): Promise<void> {
142-
try {
143-
return await WarrantClient.httpClient.delete({
144-
url: `/v1/roles/${roleId}/permissions/${permissionId}`,
145-
});
146-
} catch (e) {
147-
throw e;
148-
}
151+
return WarrantModule.delete({
152+
object: {
153+
objectType: ObjectType.Permission,
154+
objectId: permissionId,
155+
},
156+
relation: "member",
157+
subject: {
158+
objectType: ObjectType.Role,
159+
objectId: roleId,
160+
}
161+
});
149162
}
150163

151164
// WarrantObject methods

src/modules/PricingTier.ts

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Authorization from "./Authorization";
22
import Feature from "./Feature";
3+
import WarrantModule from "./WarrantModule";
34
import WarrantClient from "../WarrantClient";
45
import { ListFeatureOptions } from "../types/Feature";
56
import { ObjectType } from "../types/ObjectType";
67
import { CreatePricingTierParams, ListPricingTierOptions } from "../types/PricingTier";
7-
import { Context, WarrantObject } from "../types/Warrant";
8+
import Warrant, { Context, WarrantObject } from "../types/Warrant";
89

910
export default class PricingTier implements WarrantObject {
1011
pricingTierId: string;
@@ -77,26 +78,32 @@ export default class PricingTier implements WarrantObject {
7778
}
7879
}
7980

80-
public static async assignPricingTierToTenant(tenantId: string, pricingTierId: string): Promise<PricingTier> {
81-
try {
82-
const response = await WarrantClient.httpClient.post({
83-
url: `/v1/tenants/${tenantId}/pricing-tiers/${pricingTierId}`,
84-
});
85-
86-
return new PricingTier(response.pricingTierId);
87-
} catch (e) {
88-
throw e;
89-
}
81+
public static async assignPricingTierToTenant(tenantId: string, pricingTierId: string): Promise<Warrant> {
82+
return WarrantModule.create({
83+
object: {
84+
objectType: ObjectType.PricingTier,
85+
objectId: pricingTierId,
86+
},
87+
relation: "member",
88+
subject: {
89+
objectType: ObjectType.Tenant,
90+
objectId: tenantId,
91+
}
92+
});
9093
}
9194

9295
public static async removePricingTierFromTenant(tenantId: string, pricingTierId: string): Promise<void> {
93-
try {
94-
return await WarrantClient.httpClient.delete({
95-
url: `/v1/tenants/${tenantId}/pricing-tiers/${pricingTierId}`,
96-
});
97-
} catch (e) {
98-
throw e;
99-
}
96+
return WarrantModule.delete({
97+
object: {
98+
objectType: ObjectType.PricingTier,
99+
objectId: pricingTierId,
100+
},
101+
relation: "member",
102+
subject: {
103+
objectType: ObjectType.Tenant,
104+
objectId: tenantId,
105+
}
106+
});
100107
}
101108

102109
public static async listPricingTiersForUser(userId: string, listOptions: ListPricingTierOptions = {}): Promise<PricingTier[]> {
@@ -112,34 +119,40 @@ export default class PricingTier implements WarrantObject {
112119
}
113120
}
114121

115-
public static async assignPricingTierToUser(userId: string, pricingTierId: string): Promise<PricingTier> {
116-
try {
117-
const response = await WarrantClient.httpClient.post({
118-
url: `/v1/users/${userId}/pricing-tiers/${pricingTierId}`,
119-
});
120-
121-
return new PricingTier(response.pricingTierId);
122-
} catch (e) {
123-
throw e;
124-
}
122+
public static async assignPricingTierToUser(userId: string, pricingTierId: string): Promise<Warrant> {
123+
return WarrantModule.create({
124+
object: {
125+
objectType: ObjectType.PricingTier,
126+
objectId: pricingTierId,
127+
},
128+
relation: "member",
129+
subject: {
130+
objectType: ObjectType.User,
131+
objectId: userId,
132+
}
133+
});
125134
}
126135

127136
public static async removePricingTierFromUser(userId: string, pricingTierId: string): Promise<void> {
128-
try {
129-
return await WarrantClient.httpClient.delete({
130-
url: `/v1/users/${userId}/pricing-tiers/${pricingTierId}`,
131-
});
132-
} catch (e) {
133-
throw e;
134-
}
137+
return WarrantModule.delete({
138+
object: {
139+
objectType: ObjectType.PricingTier,
140+
objectId: pricingTierId,
141+
},
142+
relation: "member",
143+
subject: {
144+
objectType: ObjectType.User,
145+
objectId: userId,
146+
}
147+
});
135148
}
136149

137150
// Instance methods
138151
public async listFeatures(listOptions: ListFeatureOptions = {}): Promise<Feature[]> {
139152
return Feature.listFeaturesForPricingTier(this.pricingTierId, listOptions);
140153
}
141154

142-
public async assignFeature(featureId: string): Promise<Feature> {
155+
public async assignFeature(featureId: string): Promise<Warrant> {
143156
return Feature.assignFeatureToPricingTier(this.pricingTierId, featureId);
144157
}
145158

0 commit comments

Comments
 (0)