Skip to content

Commit

Permalink
feat: add USER_AGENT constant and include it in API request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
bengeois committed Nov 4, 2024
1 parent 1457b45 commit a942d66
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const STATIC_HOST = "https://static.openfoodfacts.org";

export const USER_AGENT = "OpenFoodFacts - NodeJS";
2 changes: 2 additions & 0 deletions src/folksonomy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import createClient from "openapi-fetch";
import { paths, components } from "./schemas/folksonomy";
import { formBody as formBodySerializer } from "./formbody";
import { ApiError } from "./error";
import { USER_AGENT } from "./consts";

export type FolksonomyTag = components["schemas"]["ProductTag"];
export type FolksonomyKey = {
Expand All @@ -26,6 +27,7 @@ export class Folksonomy {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
"User-Agent": USER_AGENT,
},
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
TaxoNode,
Taxonomy,
} from "./taxonomy/types";
import { USER_AGENT } from "./consts";

export type ProductV2 = componentsv2["schemas"]["Product"];
export type SearchResultV2 = externalv2["responses/search_for_products.yaml"];
Expand Down Expand Up @@ -62,6 +63,9 @@ export class OpenFoodFacts {
this.rawv2 = createClient<pathsv2>({
fetch: this.fetch,
baseUrl: this.baseUrl,
headers: {
"User-Agent": USER_AGENT,
},
});

this.robotoff = new Robotoff(fetch);
Expand All @@ -73,6 +77,7 @@ export class OpenFoodFacts {
): Promise<T> {
const res = await fetch(
`${this.baseUrl}/api/v2/taxonomy?tagtype=${taxo}&tags=${entry}`,
{ headers: { "User-Agent": USER_AGENT } },
);

return (await res.json()) as T;
Expand Down
2 changes: 2 additions & 0 deletions src/nutripatrol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import createClient from "openapi-fetch";

import { components, paths } from "./schemas/nutripatrol";
import { NutriPatrolError } from "./error";
import { USER_AGENT } from "./consts";

export type Flag = components["schemas"]["FlagCreate"];
export type Ticket = components["schemas"]["Ticket"];
Expand Down Expand Up @@ -39,6 +40,7 @@ export class NutriPatrol {
fetch,
headers: {
"Content-Type": "application/json",
"User-Agent": USER_AGENT,
},
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/prices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import createClient from "openapi-fetch";
import type { paths } from "$schemas/prices";
import { USER_AGENT } from "./consts";

type PricesQuery = paths["/api/v1/prices"]["get"]["parameters"]["query"];
export type PricesCreate =
Expand All @@ -20,6 +21,9 @@ export class PricesApi {
fetch,
baseUrl: options.baseUrl,
credentials: "include",
headers: {
"User-Agent": USER_AGENT,
},
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/robotoff.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import createClient from "openapi-fetch";

import { paths } from "./schemas/robotoff";
import { USER_AGENT } from "./consts";

export class Robotoff {
/** The fetch function used for every request */
Expand All @@ -14,6 +15,9 @@ export class Robotoff {
this.raw = createClient<paths>({
fetch: this.fetch,
baseUrl: "https://robotoff.openfoodfacts.org/api/v1",
headers: {
"User-Agent": USER_AGENT,
},
});
}

Expand Down

0 comments on commit a942d66

Please sign in to comment.