Skip to content

Commit

Permalink
Merge pull request #1289 from buttercup/fix/browser_api_final
Browse files Browse the repository at this point in the history
Provide facade/source names in API
  • Loading branch information
perry-mitchell authored Mar 12, 2024
2 parents 49f5b61 + ee4a370 commit 5d6a9cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion source/main/library/build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file updated automatically: changes made here will be overwritten!

export const VERSION = "2.25.0";
export const VERSION = "2.26.0";
9 changes: 9 additions & 0 deletions source/main/services/browser/controllers/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request, Response } from "express";
import { VaultFacade, VaultSourceStatus } from "buttercup";
import { Layerr } from "layerr";
import {
getSourceDescription,
getSourceDescriptions,
getSourceStatus,
getUnlockedSourceIDs,
Expand Down Expand Up @@ -30,7 +31,15 @@ export async function getVaultsTree(req: Request, res: Response) {
}),
{}
);
const names = sourceIDs.reduce(
(output: Record<string, string>, sourceID) => ({
...output,
[sourceID]: getSourceDescription(sourceID)?.name ?? "Untitled vault"
}),
{}
);
await respondJSON(res, {
names,
tree
});
}
Expand Down
36 changes: 18 additions & 18 deletions source/main/services/buttercup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ export async function getAttachmentDetails(
return source.attachmentManager.getAttachmentDetails(entry, attachmentID);
}

export async function getEmptyVault(password: string): Promise<string> {
const creds = Credentials.fromPassword(password);
const vault = Vault.createWithDefaults();
const tds = new TextDatasource(Credentials.fromPassword(password));
return tds.save(vault.format.getHistory(), creds);
}

export async function getEntries(
entries: Array<{ entryID: EntryID; sourceID: VaultSourceID }>
): Promise<Array<{ entry: Entry; sourceID: VaultSourceID }>> {
Expand Down Expand Up @@ -192,24 +199,6 @@ export function getSourceDescriptions(): Array<VaultSourceDescription> {
return vaultManager.sources.map((source) => describeSource(source));
}

export function getVaultFacadeBySource(sourceID: VaultSourceID): VaultFacade {
const vaultManager = getVaultManager();
const source = vaultManager.getSourceForID(sourceID);
if (!source) {
throw new Error(`Cannot generate facade: No source found for ID: ${sourceID}`);
} else if (source.status !== VaultSourceStatus.Unlocked) {
throw new Error(`Cannot generate facade: Source is not unlocked: ${sourceID}`);
}
return createVaultFacade(source.vault);
}

export async function getEmptyVault(password: string): Promise<string> {
const creds = Credentials.fromPassword(password);
const vault = Vault.createWithDefaults();
const tds = new TextDatasource(Credentials.fromPassword(password));
return tds.save(vault.format.getHistory(), creds);
}

export function getSourceAttachmentsSupport(sourceID: VaultSourceID): boolean {
const mgr = getVaultManager();
const source = mgr.getSourceForID(sourceID);
Expand All @@ -232,6 +221,17 @@ export function getUnlockedSourcesCount(): number {
return mgr.unlockedSources.length;
}

export function getVaultFacadeBySource(sourceID: VaultSourceID): VaultFacade {
const vaultManager = getVaultManager();
const source = vaultManager.getSourceForID(sourceID);
if (!source) {
throw new Error(`Cannot generate facade: No source found for ID: ${sourceID}`);
} else if (source.status !== VaultSourceStatus.Unlocked) {
throw new Error(`Cannot generate facade: Source is not unlocked: ${sourceID}`);
}
return createVaultFacade(source.vault);
}

export function getVaultFormat(sourceID: VaultSourceID): VaultFormatID {
const mgr = getVaultManager();
const source = mgr.getSourceForID(sourceID);
Expand Down

0 comments on commit 5d6a9cd

Please sign in to comment.