casdoor-nodejs-sdk
Version:
Node.js client SDK for Casdoor
62 lines (61 loc) • 2.15 kB
JavaScript
;
// Copyright 2024 The Casdoor Authors. All Rights Reserved.
//
// 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.PolicySDK = void 0;
class PolicySDK {
constructor(config, request) {
this.config = config;
this.request = request;
}
async getPolicies(enforcerName, adapterId) {
if (!this.request) {
throw new Error('request init failed');
}
return (await this.request.get('/get-policies', {
params: {
id: `${this.config.orgName}/${enforcerName}`,
adapterId: adapterId,
},
}));
}
async modifyPolicy(method, enforcer, policies) {
if (!this.request) {
throw new Error('request init failed');
}
let data;
if (method === 'update-policy') {
data = JSON.stringify(policies);
}
else {
data = JSON.stringify(policies[0]);
}
const url = `/${method}`;
return (await this.request.post(url, data, {
params: {
id: `${enforcer.owner}/${enforcer.name}`,
},
}));
}
async addPolicy(enforcer, policy) {
return this.modifyPolicy('add-policy', enforcer, [policy]);
}
async updatePolicy(enforcer, oldPolicy, newPolicy) {
return this.modifyPolicy('update-policy', enforcer, [oldPolicy, newPolicy]);
}
async deletePolicy(enforcer, policy) {
return this.modifyPolicy('remove-policy', enforcer, [policy]);
}
}
exports.PolicySDK = PolicySDK;