Skip to content

Commit 736f597

Browse files
authored
Merge pull request #41 from warrant-dev/feat/AddWarrantTokenSupport
Add warrant token support
2 parents ea5960c + 5bb0b9c commit 736f597

File tree

12 files changed

+305
-230
lines changed

12 files changed

+305
-230
lines changed

src/HttpClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ApiError from "./types/ApiError";
2+
import { WarrantRequestOptions } from "./types/WarrantRequestOptions";
23

34
const { version } = require("../package.json");
45

@@ -20,6 +21,7 @@ export interface HttpClientRequestOptions {
2021
data?: any;
2122
params?: any;
2223
url: string;
24+
options?: WarrantRequestOptions;
2325
}
2426

2527
interface RequestHeaders {
@@ -107,6 +109,10 @@ export default class ApiClient implements HttpClient {
107109
baseUrl = requestOptions.baseUrl;
108110
}
109111

112+
if (requestOptions?.options?.warrantToken) {
113+
fetchRequestOptions.headers['Warrant-Token'] = requestOptions.options.warrantToken;
114+
}
115+
110116
let requestUrl = `${baseUrl}${requestOptions.url}`;
111117
if (requestOptions?.params) {
112118
const queryParams = new URLSearchParams(requestOptions.params);

src/modules/Authorization.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Feature from "./Feature";
22
import Permission from "./Permission";
33
import Check, { AccessCheckRequest, CheckMany, CheckWarrant, FeatureCheck, PermissionCheck } from "../types/Check";
4+
import { WarrantRequestOptions } from "../types/WarrantRequestOptions";
45
import Warrant, { isSubject, isWarrantObject } from "../types/Warrant";
56
import WarrantClient from "../WarrantClient";
67

78
export default class Authorization {
8-
public static async check(check: Check): Promise<boolean> {
9+
public static async check(check: Check, options: WarrantRequestOptions = {}): Promise<boolean> {
910
const accessCheckRequest: AccessCheckRequest = {
1011
warrants: [{
1112
object: check.object,
@@ -16,13 +17,13 @@ export default class Authorization {
1617
debug: check.debug
1718
}
1819
if (WarrantClient.config.authorizeEndpoint) {
19-
return this.edgeAuthorize(accessCheckRequest);
20+
return this.edgeAuthorize(accessCheckRequest, options);
2021
}
2122

22-
return this.authorize(accessCheckRequest);
23+
return this.authorize(accessCheckRequest, options);
2324
}
2425

25-
public static async checkMany(check: CheckMany): Promise<boolean> {
26+
public static async checkMany(check: CheckMany, options: WarrantRequestOptions = {}): Promise<boolean> {
2627
let warrants: CheckWarrant[] = check.warrants.map((warrant) => {
2728
return {
2829
object: warrant.object,
@@ -38,34 +39,34 @@ export default class Authorization {
3839
}
3940

4041
if (WarrantClient.config.authorizeEndpoint) {
41-
return this.edgeAuthorize(accessCheckRequest);
42+
return this.edgeAuthorize(accessCheckRequest, options);
4243
}
4344

44-
return this.authorize(accessCheckRequest);
45+
return this.authorize(accessCheckRequest, options);
4546
}
4647

47-
public static async hasFeature(featureCheck: FeatureCheck): Promise<boolean> {
48+
public static async hasFeature(featureCheck: FeatureCheck, options: WarrantRequestOptions = {}): Promise<boolean> {
4849
return this.check({
4950
object: new Feature(featureCheck.featureId),
5051
relation: "member",
5152
subject: featureCheck.subject,
5253
context: featureCheck.context,
5354
debug: featureCheck.debug
54-
})
55+
}, options)
5556
}
5657

57-
public static async hasPermission(permissionCheck: PermissionCheck): Promise<boolean> {
58+
public static async hasPermission(permissionCheck: PermissionCheck, options: WarrantRequestOptions = {}): Promise<boolean> {
5859
return this.check({
5960
object: new Permission(permissionCheck.permissionId),
6061
relation: "member",
6162
subject: permissionCheck.subject,
6263
context: permissionCheck.context,
6364
debug: permissionCheck.debug
64-
})
65+
}, options)
6566
}
6667

6768
// Private methods
68-
private static async authorize(accessCheckRequest: AccessCheckRequest): Promise<boolean> {
69+
private static async authorize(accessCheckRequest: AccessCheckRequest, options: WarrantRequestOptions = {}): Promise<boolean> {
6970
try {
7071

7172
const response = await WarrantClient.httpClient.post({
@@ -74,6 +75,7 @@ export default class Authorization {
7475
...accessCheckRequest,
7576
warrants: this.mapWarrantsForRequest(accessCheckRequest.warrants),
7677
},
78+
options,
7779
});
7880

7981
return response.code === 200;
@@ -82,7 +84,7 @@ export default class Authorization {
8284
}
8385
}
8486

85-
private static async edgeAuthorize(accessCheckRequest: AccessCheckRequest): Promise<boolean> {
87+
private static async edgeAuthorize(accessCheckRequest: AccessCheckRequest, options: WarrantRequestOptions = {}): Promise<boolean> {
8688
try {
8789
const response = await WarrantClient.httpClient.post({
8890
baseUrl: WarrantClient.config.authorizeEndpoint,
@@ -91,6 +93,7 @@ export default class Authorization {
9193
...accessCheckRequest,
9294
warrants: this.mapWarrantsForRequest(accessCheckRequest.warrants),
9395
},
96+
options,
9497
});
9598

9699
return response.code === 200;

src/modules/Feature.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import WarrantModule from "./WarrantModule";
22
import WarrantClient from "../WarrantClient";
33
import { CreateFeatureParams, ListFeatureOptions } from "../types/Feature";
4+
import { WarrantRequestOptions } from "../types/WarrantRequestOptions";
45
import Warrant, { WarrantObject } from "../types/Warrant";
56
import { ObjectType } from "../types/ObjectType";
67

@@ -14,11 +15,12 @@ export default class Feature implements WarrantObject {
1415
//
1516
// Static methods
1617
//
17-
public static async create(feature: CreateFeatureParams): Promise<Feature> {
18+
public static async create(feature: CreateFeatureParams, options: WarrantRequestOptions = {}): Promise<Feature> {
1819
try {
1920
const response = await WarrantClient.httpClient.post({
2021
url: "/v1/features",
2122
data: feature,
23+
options,
2224
});
2325

2426
return new Feature(response.featureId);
@@ -27,10 +29,11 @@ export default class Feature implements WarrantObject {
2729
}
2830
}
2931

30-
public static async get(featureId: string): Promise<Feature> {
32+
public static async get(featureId: string, options: WarrantRequestOptions = {}): Promise<Feature> {
3133
try {
3234
const response = await WarrantClient.httpClient.get({
3335
url: `/v1/features/${featureId}`,
36+
options,
3437
});
3538

3639
return new Feature(response.featureId);
@@ -39,21 +42,23 @@ export default class Feature implements WarrantObject {
3942
}
4043
}
4144

42-
public static async delete(featureId: string): Promise<void> {
45+
public static async delete(featureId: string, options: WarrantRequestOptions = {}): Promise<void> {
4346
try {
4447
return await WarrantClient.httpClient.delete({
4548
url: `/v1/features/${featureId}`,
49+
options,
4650
});
4751
} catch (e) {
4852
throw e;
4953
}
5054
}
5155

52-
public static async listFeatures(listOptions: ListFeatureOptions = {}): Promise<Feature[]> {
56+
public static async listFeatures(listOptions: ListFeatureOptions = {}, options: WarrantRequestOptions = {}): Promise<Feature[]> {
5357
try {
5458
const response = await WarrantClient.httpClient.get({
5559
url: "/v1/features",
5660
params: listOptions,
61+
options,
5762
});
5863

5964
return response.map((feature: Feature) => new Feature(feature.featureId));
@@ -62,11 +67,12 @@ export default class Feature implements WarrantObject {
6267
}
6368
}
6469

65-
public static async listFeaturesForPricingTier(pricingTierId: string, listOptions: ListFeatureOptions = {}): Promise<Feature[]> {
70+
public static async listFeaturesForPricingTier(pricingTierId: string, listOptions: ListFeatureOptions = {}, options: WarrantRequestOptions = {}): Promise<Feature[]> {
6671
try {
6772
const response = await WarrantClient.httpClient.get({
6873
url: `/v1/pricing-tiers/${pricingTierId}/features`,
6974
params: listOptions,
75+
options,
7076
});
7177

7278
return response.map((feature: Feature) => new Feature(feature.featureId));
@@ -75,7 +81,7 @@ export default class Feature implements WarrantObject {
7581
}
7682
}
7783

78-
public static async assignFeatureToPricingTier(pricingTierId: string, featureId: string): Promise<Warrant> {
84+
public static async assignFeatureToPricingTier(pricingTierId: string, featureId: string, options: WarrantRequestOptions = {}): Promise<Warrant> {
7985
return WarrantModule.create({
8086
object: {
8187
objectType: ObjectType.Feature,
@@ -86,10 +92,10 @@ export default class Feature implements WarrantObject {
8692
objectType: ObjectType.PricingTier,
8793
objectId: pricingTierId,
8894
}
89-
});
95+
}, options);
9096
}
9197

92-
public static async removeFeatureFromPricingTier(pricingTierId: string, featureId: string): Promise<void> {
98+
public static async removeFeatureFromPricingTier(pricingTierId: string, featureId: string, options: WarrantRequestOptions = {}): Promise<void> {
9399
return WarrantModule.delete({
94100
object: {
95101
objectType: ObjectType.Feature,
@@ -100,14 +106,15 @@ export default class Feature implements WarrantObject {
100106
objectType: ObjectType.PricingTier,
101107
objectId: pricingTierId,
102108
}
103-
});
109+
}, options);
104110
}
105111

106-
public static async listFeaturesForTenant(tenantId: string, listOptions: ListFeatureOptions = {}): Promise<Feature[]> {
112+
public static async listFeaturesForTenant(tenantId: string, listOptions: ListFeatureOptions = {}, options: WarrantRequestOptions = {}): Promise<Feature[]> {
107113
try {
108114
const response = await WarrantClient.httpClient.get({
109115
url: `/v1/tenants/${tenantId}/features`,
110116
params: listOptions,
117+
options,
111118
});
112119

113120
return response.map((feature: Feature) => new Feature(feature.featureId));
@@ -116,7 +123,7 @@ export default class Feature implements WarrantObject {
116123
}
117124
}
118125

119-
public static async assignFeatureToTenant(tenantId: string, featureId: string): Promise<Warrant> {
126+
public static async assignFeatureToTenant(tenantId: string, featureId: string, options: WarrantRequestOptions = {}): Promise<Warrant> {
120127
return WarrantModule.create({
121128
object: {
122129
objectType: ObjectType.Feature,
@@ -127,10 +134,10 @@ export default class Feature implements WarrantObject {
127134
objectType: ObjectType.Tenant,
128135
objectId: tenantId,
129136
}
130-
});
137+
}, options);
131138
}
132139

133-
public static async removeFeatureFromTenant(tenantId: string, featureId: string): Promise<void> {
140+
public static async removeFeatureFromTenant(tenantId: string, featureId: string, options: WarrantRequestOptions = {}): Promise<void> {
134141
return WarrantModule.delete({
135142
object: {
136143
objectType: ObjectType.Feature,
@@ -141,14 +148,15 @@ export default class Feature implements WarrantObject {
141148
objectType: ObjectType.Tenant,
142149
objectId: tenantId,
143150
}
144-
});
151+
}, options);
145152
}
146153

147-
public static async listFeaturesForUser(userId: string, listOptions: ListFeatureOptions = {}): Promise<Feature[]> {
154+
public static async listFeaturesForUser(userId: string, listOptions: ListFeatureOptions = {}, options: WarrantRequestOptions = {}): Promise<Feature[]> {
148155
try {
149156
const response = await WarrantClient.httpClient.get({
150157
url: `/v1/users/${userId}/features`,
151158
params: listOptions,
159+
options,
152160
});
153161

154162
return response.map((feature: Feature) => new Feature(feature.featureId));
@@ -157,7 +165,7 @@ export default class Feature implements WarrantObject {
157165
}
158166
}
159167

160-
public static async assignFeatureToUser(userId: string, featureId: string): Promise<Warrant> {
168+
public static async assignFeatureToUser(userId: string, featureId: string, options: WarrantRequestOptions = {}): Promise<Warrant> {
161169
return WarrantModule.create({
162170
object: {
163171
objectType: ObjectType.Feature,
@@ -168,10 +176,10 @@ export default class Feature implements WarrantObject {
168176
objectType: ObjectType.User,
169177
objectId: userId,
170178
}
171-
});
179+
}, options);
172180
}
173181

174-
public static async removeFeatureFromUser(userId: string, featureId: string): Promise<void> {
182+
public static async removeFeatureFromUser(userId: string, featureId: string, options: WarrantRequestOptions = {}): Promise<void> {
175183
return WarrantModule.delete({
176184
object: {
177185
objectType: ObjectType.Feature,
@@ -182,7 +190,7 @@ export default class Feature implements WarrantObject {
182190
objectType: ObjectType.User,
183191
objectId: userId,
184192
}
185-
});
193+
}, options);
186194
}
187195

188196
// WarrantObject methods

0 commit comments

Comments
 (0)