Skip to content

Commit

Permalink
feat: Make authToken optional for folksonomy client
Browse files Browse the repository at this point in the history
  • Loading branch information
Griffin Ciluffo authored and Griffin Ciluffo committed Nov 21, 2024
1 parent e71151d commit cd8fd58
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/folksonomy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof createClient<paths>>;

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({
Expand Down Expand Up @@ -53,6 +55,12 @@ export class Folksonomy {
}

async putTag(tag: FolksonomyTag): Promise<boolean> {
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;
Expand Down Expand Up @@ -81,6 +89,12 @@ export class Folksonomy {
* @returns if the tag was added or updated
*/
async addTag(tag: FolksonomyTag): Promise<boolean> {
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,
});
Expand All @@ -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 },
Expand Down

0 comments on commit cd8fd58

Please sign in to comment.