Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folksonomy open api regenerate #573

Closed
Closed
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
24 changes: 23 additions & 1 deletion src/folksonomy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
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 @@
}

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 @@
* @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 @@
* @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 All @@ -119,9 +139,9 @@
| { error: ApiError }
> {
const res = await this.raw.POST("/auth", {
body: { username, password },

Check failure on line 142 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (18.x)

Property 'scope' is missing in type '{ username: string; password: string; }' but required in type '{ grant_type?: string | null | undefined; username: string; password: string; scope: string; client_id?: string | null | undefined; client_secret?: string | null | undefined; }'.

Check failure on line 142 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (20.x)

Property 'scope' is missing in type '{ username: string; password: string; }' but required in type '{ grant_type?: string | null | undefined; username: string; password: string; scope: string; client_id?: string | null | undefined; client_secret?: string | null | undefined; }'.

Check failure on line 142 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (22.x)

Property 'scope' is missing in type '{ username: string; password: string; }' but required in type '{ grant_type?: string | null | undefined; username: string; password: string; scope: string; client_id?: string | null | undefined; client_secret?: string | null | undefined; }'.

Check failure on line 142 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (23.x)

Property 'scope' is missing in type '{ username: string; password: string; }' but required in type '{ grant_type?: string | null | undefined; username: string; password: string; scope: string; client_id?: string | null | undefined; client_secret?: string | null | undefined; }'.
headers: { "Content-Type": "application/x-www-form-urlencoded" },
bodySerializer: formBodySerializer,

Check failure on line 144 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (18.x)

Type '(params: Record<string, string>) => URLSearchParams' is not assignable to type 'BodySerializer<{ parameters: { query?: undefined; header?: undefined; path?: undefined; cookie?: undefined; }; requestBody: { content: { "application/x-www-form-urlencoded": { grant_type?: string | null | undefined; ... 4 more ...; client_secret?: string | ... 1 more ... | undefined; }; }; }; responses: { ...; }; }>'.

Check failure on line 144 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (20.x)

Type '(params: Record<string, string>) => URLSearchParams' is not assignable to type 'BodySerializer<{ parameters: { query?: undefined; header?: undefined; path?: undefined; cookie?: undefined; }; requestBody: { content: { "application/x-www-form-urlencoded": { grant_type?: string | null | undefined; ... 4 more ...; client_secret?: string | ... 1 more ... | undefined; }; }; }; responses: { ...; }; }>'.

Check failure on line 144 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (22.x)

Type '(params: Record<string, string>) => URLSearchParams' is not assignable to type 'BodySerializer<{ parameters: { query?: undefined; header?: undefined; path?: undefined; cookie?: undefined; }; requestBody: { content: { "application/x-www-form-urlencoded": { grant_type?: string | null | undefined; ... 4 more ...; client_secret?: string | ... 1 more ... | undefined; }; }; }; responses: { ...; }; }>'.

Check failure on line 144 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (23.x)

Type '(params: Record<string, string>) => URLSearchParams' is not assignable to type 'BodySerializer<{ parameters: { query?: undefined; header?: undefined; path?: undefined; cookie?: undefined; }; requestBody: { content: { "application/x-www-form-urlencoded": { grant_type?: string | null | undefined; ... 4 more ...; client_secret?: string | ... 1 more ... | undefined; }; }; }; responses: { ...; }; }>'.
});

if (res.response.status !== 200) {
Expand All @@ -137,7 +157,7 @@
},
};
} else if (res.error != null) {
return { error: res.error };

Check failure on line 160 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (18.x)

Type '{ detail?: { loc: (string | number)[]; msg: string; type: string; }[] | undefined; }' is not assignable to type 'ApiError'.

Check failure on line 160 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (20.x)

Type '{ detail?: { loc: (string | number)[]; msg: string; type: string; }[] | undefined; }' is not assignable to type 'ApiError'.

Check failure on line 160 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (22.x)

Type '{ detail?: { loc: (string | number)[]; msg: string; type: string; }[] | undefined; }' is not assignable to type 'ApiError'.

Check failure on line 160 in src/folksonomy.ts

View workflow job for this annotation

GitHub Actions / Build, Lint, Format, Tests and Build Docs (23.x)

Type '{ detail?: { loc: (string | number)[]; msg: string; type: string; }[] | undefined; }' is not assignable to type 'ApiError'.
}

const token = (await res.response.json()) as {
Expand All @@ -145,6 +165,8 @@
token_type: string;
};

this.authToken = token.access_token;

return { token };
}
}
Loading
Loading