n8n
Version:
n8n Workflow Automation Tool
64 lines • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentsCredentialProvider = void 0;
function toResolvedCredential(data) {
const resolved = data !== null && typeof data === 'object' && !Array.isArray(data) ? data : {};
const apiKey = 'apiKey' in resolved && typeof resolved.apiKey === 'string' ? resolved.apiKey : '';
return { ...resolved, apiKey };
}
class AgentsCredentialProvider {
constructor(credentialsService, projectId, user) {
this.credentialsService = credentialsService;
this.projectId = projectId;
this.user = user;
}
async resolve(credentialId) {
const credential = await this.findCredentialEntity(credentialId);
if (!credential) {
throw new Error(`Credential "${credentialId}" not found or not accessible`);
}
const data = await this.credentialsService.decrypt(credential, true);
return toResolvedCredential(data);
}
async list() {
if (this.user) {
const accessible = await this.credentialsService.getCredentialsAUserCanUseInAWorkflow(this.user, {
projectId: this.projectId,
});
return accessible.map((c) => ({
id: c.id,
name: c.name,
type: c.type,
}));
}
const accessible = await this.getAllProjectAndGlobalCredentials();
return accessible.map((c) => ({
id: c.id,
name: c.name,
type: c.type,
}));
}
async findCredentialEntity(credentialId) {
const accessible = await this.getAllProjectAndGlobalCredentials();
return accessible.find((c) => c.id === credentialId) ?? null;
}
async getAllProjectAndGlobalCredentials() {
const projectCredentials = await this.credentialsService.findAllCredentialIdsForProject(this.projectId);
const globalCredentials = await this.credentialsService.findAllGlobalCredentialIds(true);
const allCredsSet = new Set();
const allCreds = [];
const addCreds = (creds) => {
for (const cred of creds) {
if (!allCredsSet.has(cred.id)) {
allCredsSet.add(cred.id);
allCreds.push(cred);
}
}
};
addCreds(projectCredentials);
addCreds(globalCredentials);
return allCreds;
}
}
exports.AgentsCredentialProvider = AgentsCredentialProvider;
//# sourceMappingURL=agents-credential-provider.js.map