@btfuse/core
Version:
A native-first framework for building hybdrid web-native applications
71 lines (68 loc) • 2.73 kB
JavaScript
;
/*
Copyright 2023 Breautek
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FusePermissionRequest = void 0;
const ContentType_1 = require("./ContentType");
const FuseError_1 = require("./FuseError");
const FusePermissionGrantResult_1 = require("./FusePermissionGrantResult");
/**
* Abstract class to handle permission request.
* Concrete classes should implement the protected _request method to call on their
* permission request Fuse API.
*/
class FusePermissionRequest {
constructor(apiBridge, permissionSet, justificationHandler = null) {
if (!permissionSet || (permissionSet && permissionSet.length === 0)) {
throw new FuseError_1.FuseError(FusePermissionRequest.TAG, 'At least one permission is required');
}
this.$api = apiBridge;
this.$permissionSet = permissionSet;
this.$justificationHandler = justificationHandler;
}
getPermissionSet() {
return this.$permissionSet;
}
async $request(isJustified) {
const response = await this.$api(ContentType_1.ContentType.JSON, {
permissionSet: this.getPermissionSet(),
isJustified: isJustified
});
if (response.isError()) {
throw await response.readAsError();
}
return new FusePermissionGrantResult_1.FusePermissionGrantResult(await response.readAsJSON());
}
async $onJustificationRequest() {
if (!this.$justificationHandler) {
console.warn('Permission requires justification, but this request has no TJustificationHandler');
return false;
}
return await this.$justificationHandler();
}
async request() {
let results = await this.$request(false);
if (results.shouldJustify()) {
if (await this.$onJustificationRequest()) {
results = await this.$request(true);
}
else {
results.rejectJustifications();
}
}
return results;
}
}
exports.FusePermissionRequest = FusePermissionRequest;
FusePermissionRequest.TAG = 'PermissionRequest';
//# sourceMappingURL=FusePermissionRequest.js.map