From cd8fd585945826250cd76bd4289b61bc5d259ed9 Mon Sep 17 00:00:00 2001 From: Griffin Ciluffo Date: Wed, 20 Nov 2024 18:21:49 +0100 Subject: [PATCH] feat: Make authToken optional for folksonomy client --- src/folksonomy.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/folksonomy.ts b/src/folksonomy.ts index 005007d..4fcb35f 100644 --- a/src/folksonomy.ts +++ b/src/folksonomy.ts @@ -15,10 +15,12 @@ export type FolksonomyKey = { export class Folksonomy { private readonly fetch: typeof global.fetch; private readonly baseUrl: string; + private authToken?: string; readonly raw: ReturnType>; - constructor(fetch: typeof global.fetch, authToken: string) { + constructor(fetch: typeof global.fetch, authToken?: string) { this.baseUrl = "https://api.folksonomy.openfoodfacts.org"; + this.authToken = authToken; this.fetch = fetch; this.raw = createClient({ @@ -53,6 +55,12 @@ export class Folksonomy { } async putTag(tag: FolksonomyTag): Promise { + if (!this.authToken) { + throw new Error( + "Auth token must be provided or login method invoked to update a tag", + ); + } + const res = await this.raw.PUT("/product", { body: tag }); return res.response.status === 200; @@ -81,6 +89,12 @@ export class Folksonomy { * @returns if the tag was added or updated */ async addTag(tag: FolksonomyTag): Promise { + if (!this.authToken) { + throw new Error( + "Auth token must be provided or login method invoked to add a tag", + ); + } + const res = await this.raw.POST("/product", { body: tag, }); @@ -94,6 +108,12 @@ export class Folksonomy { * @returns if the tag was deleted */ async removeTag(tag: FolksonomyTag & { version: number }) { + if (!this.authToken) { + throw new Error( + "Auth token must be provided or login method invoked to remove a tag", + ); + } + const res = await this.raw.DELETE("/product/{product}/{k}", { params: { path: { product: tag.product, k: tag.k },