Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __test__/itembuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ describe("Test ItemBuilder", () => {
expect(primaryUrls).toEqual(1);
});

test("url with label", () => {
const newItem = new ItemBuilder()
.setCategory(CategoryEnum.Login)
.addUrl({ label: "1Password", href: "1password.com", primary: true })
.build();

const { urls } = newItem;
expect(urls?.[0].label).toEqual("1Password");
});

test("toggle item.favorite attribute", () => {
// Never called => undefined
const itemNotFavorite = new ItemBuilder()
Expand Down
2 changes: 1 addition & 1 deletion __test__/op-connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import CategoryEnum = Item.CategoryEnum;
import { ErrorMessageFactory, HttpErrorFactory } from "../src/lib/utils";
import { ERROR_MESSAGE } from "../src/lib/constants";
import { ApiMock } from "./mocks";
import { ItemFile } from "../dist/model/itemFile";
import { ItemFile } from "../src/model/itemFile";
import { FullItemAllOfFields } from "../src/model/models";

// eslint-disable-next-line @typescript-eslint/tslint/config
Expand Down
20 changes: 13 additions & 7 deletions src/lib/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface ItemFieldOptions {
recipe?: GeneratorRecipe;
}

interface BuilderUrls {
primaryUrl: string;
itemUrls: ItemUrls[];
}

export class ItemBuilder {
/**
* Empty Item under construction.
Expand All @@ -38,7 +43,7 @@ export class ItemBuilder {
* @private
*/
private sections: Map<string, FullItemAllOfSections>;
private urls: { [key: string]: any };
private urls: BuilderUrls;

public constructor() {
this.reset();
Expand All @@ -54,10 +59,10 @@ export class ItemBuilder {

this.item.sections = Array.from(this.sections.values());

this.item.urls = this.urls.hrefs.map((href) =>
this.item.urls = this.urls.itemUrls.map(({ label, href }) =>
this.urls.primaryUrl === href
? ({ primary: true, href } as ItemUrls)
: ({ href } as ItemUrls),
? { primary: true, label, href }
: { label, href },
);
const builtItem = cloneDeep(this.item);
debug(
Expand All @@ -78,7 +83,7 @@ export class ItemBuilder {
this.item.tags = [];

this.sections = new Map();
this.urls = { primaryUrl: "", hrefs: [] };
this.urls = { primaryUrl: "", itemUrls: [] };
}

/**
Expand Down Expand Up @@ -182,8 +187,9 @@ export class ItemBuilder {
* @returns {ItemBuilder}
*/
public addUrl(url: ItemUrls): ItemBuilder {
if (url.primary) this.urls.primaryUrl = url.href;
this.urls.hrefs.push(url.href);
const { primary, label, href } = url;
if (primary) this.urls.primaryUrl = href;
this.urls.itemUrls.push({ label, href });
return this;
}

Expand Down