From 77f8dbfc91a90cc2a4b74fd4c888caf985a4fa94 Mon Sep 17 00:00:00 2001 From: Chris Zeller Date: Sun, 6 Oct 2024 22:13:57 +0200 Subject: [PATCH 1/3] fix: preserve label of ItemURLs when using ItemBuilder.addUrl (#119) --- src/lib/builders.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/lib/builders.ts b/src/lib/builders.ts index ea0a778..91abce5 100644 --- a/src/lib/builders.ts +++ b/src/lib/builders.ts @@ -24,6 +24,11 @@ export interface ItemFieldOptions { recipe?: GeneratorRecipe; } +interface BuilderUrls { + primaryUrl: string; + itemUrls: ItemUrls[]; +} + export class ItemBuilder { /** * Empty Item under construction. @@ -38,7 +43,7 @@ export class ItemBuilder { * @private */ private sections: Map; - private urls: { [key: string]: any }; + private urls: BuilderUrls; public constructor() { this.reset(); @@ -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( @@ -78,7 +83,7 @@ export class ItemBuilder { this.item.tags = []; this.sections = new Map(); - this.urls = { primaryUrl: "", hrefs: [] }; + this.urls = { primaryUrl: "", itemUrls: [] }; } /** @@ -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; } From c1d7899518806756861cf15f09f17e6b90add421 Mon Sep 17 00:00:00 2001 From: Chris Zeller Date: Sun, 6 Oct 2024 22:14:11 +0200 Subject: [PATCH 2/3] test: add test for url label --- __test__/itembuilder.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/__test__/itembuilder.test.ts b/__test__/itembuilder.test.ts index dbf5b53..e645c0e 100644 --- a/__test__/itembuilder.test.ts +++ b/__test__/itembuilder.test.ts @@ -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() From 00a6b46000c33cf96301346641a2f9dc49732f5e Mon Sep 17 00:00:00 2001 From: Chris Zeller Date: Sun, 6 Oct 2024 22:26:25 +0200 Subject: [PATCH 3/3] fix: use correct import --- __test__/op-connect.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__test__/op-connect.test.ts b/__test__/op-connect.test.ts index 50c2bd3..c5daac6 100644 --- a/__test__/op-connect.test.ts +++ b/__test__/op-connect.test.ts @@ -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