Skip to content

Commit

Permalink
refactor: add back fetch in constructor option + provide inferred fet…
Browse files Browse the repository at this point in the history
…ch type + update openapi-fetch to latest
  • Loading branch information
Valentin Healthy App committed Dec 23, 2023
1 parent 657d024 commit 8f2af90
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"homepage": "https://github.com/openfoodfacts/openfoodfacts-nodejs#readme",
"dependencies": {
"axios": "^1.6.2",
"openapi-fetch": "^0.6.1"
"openapi-fetch": "0.8.2"
},
"devDependencies": {
"@commitlint/cli": "^18.4.1",
Expand Down
21 changes: 9 additions & 12 deletions src/controllers/products/product.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import createClient from 'openapi-fetch'

import { components, paths } from '../../../schemas/server/docs/api/ref/api'
import { Product, SearchResult } from '../../types'
import { Fetch, Product, SearchResult } from '../../types'

export class Products {
private readonly client
private readonly client: ReturnType<typeof createClient<paths>>

constructor (baseUrl: string) {
constructor (baseUrl: string, fetch?: Fetch) {
this.client = createClient<paths>({
baseUrl
baseUrl,
fetch
})
}

Expand All @@ -17,7 +18,7 @@ export class Products {
* @param barcode Barcode of the product you want to fetch details
*/
async getProduct (barcode: string): Promise<Product | undefined> {
const { data } = await this.client.get('/api/v2/product/{barcode}', {
const { data } = await this.client.GET('/api/v2/product/{barcode}', {
params: { path: { barcode } }
})

Expand All @@ -29,7 +30,7 @@ export class Products {
photoId: string,
ocrEngine: 'google_cloud_vision' = 'google_cloud_vision'
): Promise<{ status?: number | undefined } | undefined> {
const { data } = await this.client.get('/cgi/ingredients.pl', {
const { data } = await this.client.GET('/cgi/ingredients.pl', {
params: {
query: {
code: barcode,
Expand All @@ -47,7 +48,7 @@ export class Products {
fields: string,
sortBy: components['parameters']['sort_by']
): Promise<SearchResult | undefined> {
const { data } = await this.client.get('/api/v2/search', {
const { data } = await this.client.GET('/api/v2/search', {
params: { query: { fields, sort_by: sortBy } }
})

Expand All @@ -58,11 +59,7 @@ export class Products {
* It is used to get all products beginning with the given barcode string
* @param {string} beginning - Barcode string from which if the barcode begins, then product is to be fetched
* @return {Object} It returns a JSON of all products that begin with the given barcode string
* @example
* const worldOFF = new OFF()
* worldOFF.getProductsByBarcodeBeginning('3596710').then(products => {
* // use products
* })
*/
async getProductsByBarcodeBeginning (beginning: string): Promise<Product | undefined> {
const fill = 'x'.repeat(13 - beginning.length)
Expand Down
13 changes: 7 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Robotoff } from './robotoff'
import { Additives, Brands, Labels, Languages, Categories, Countries, Ingredients, Products, Packagings, States, Stores } from './controllers'

import { OffOptions } from './taxonomy/types'
import { Fetch } from './types'

export * as TaxoTypes from './taxonomy/types'
export * as Types from './types'

/** Wrapper of OFF API */
export class OpenFoodFacts {
private readonly baseUrl: string

readonly robotoff: Robotoff

readonly additives: Additives
Expand All @@ -25,13 +26,13 @@ export class OpenFoodFacts {

/**
* Create OFF object
* @param options - Options for the OFF Object
* @param fetch - fetcher to be used in the client | Default to openapi-fetch
*/
constructor (
options: OffOptions
fetch?: Fetch
) {
this.baseUrl = `https://${options?.country ?? 'world'}.openfoodfacts.org`
this.robotoff = new Robotoff('https://robotoff.openfoodfacts.org')
this.baseUrl = 'https://world.openfoodfacts.org'
this.robotoff = new Robotoff('https://robotoff.openfoodfacts.org', fetch)

this.additives = new Additives()
this.brands = new Brands(this.baseUrl)
Expand All @@ -41,7 +42,7 @@ export class OpenFoodFacts {
this.labels = new Labels()
this.languages = new Languages(this.baseUrl)
this.packagings = new Packagings()
this.products = new Products(this.baseUrl)
this.products = new Products(this.baseUrl, fetch)
this.states = new States()
this.stores = new Stores()
}
Expand Down
15 changes: 9 additions & 6 deletions src/robotoff.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { paths } from '../schemas/robotoff'
import createClient from 'openapi-fetch'
import { Fetch } from './types'

export class Robotoff {
private readonly client: ReturnType<typeof createClient<paths>>

constructor (
baseUrl: string
baseUrl: string,
fetch?: Fetch
) {
this.client = createClient<paths>({
baseUrl
baseUrl,
fetch
})
}

async annotate (
body: paths['/insights/annotate']['post']['requestBody']['content']['application/x-www-form-urlencoded']
): Promise<any> {
const { data } = await this.client.post('/insights/annotate', {
const { data } = await this.client.POST('/insights/annotate', {
body
})
return data
}

async questionsByProductCode (code: number): Promise<any> {
const { data } = await this.client.get('/questions/{barcode}', {
const { data } = await this.client.GET('/questions/{barcode}', {
params: {
path: { barcode: code }
}
Expand All @@ -31,15 +34,15 @@ export class Robotoff {
}

async insightDetail (id: string): Promise<any> {
const { data } = await this.client.get('/insights/detail/{id}', {
const { data } = await this.client.GET('/insights/detail/{id}', {
params: { path: { id } }
})
return data
}

async loadLogo (logoId: string): Promise<any> {
// @ts-expect-error TODO: still not documented
const { data } = await this.client.get('/images/logos/{logoId}', {
const { data } = await this.client.GET('/images/logos/{logoId}', {
params: {
path: { logoId }
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import { components, external } from '../schemas/server/docs/api/ref/api'

export type Product = components['schemas']['Product']
export type SearchResult = external['responses/search_for_products.yaml']

export type Fetch = typeof fetch
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
],
"module": "CommonJS",
"lib": [
"ES2022"
"ES2022",
"dom",
],
"declaration": true,
"outDir": "./dist",
"strict": true,
"esModuleInterop": true
"esModuleInterop": true,
},
"exclude": [
"node_modules",
Expand Down
19 changes: 14 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4216,10 +4216,19 @@ __metadata:
languageName: node
linkType: hard

"openapi-fetch@npm:^0.6.1":
version: 0.6.1
resolution: "openapi-fetch@npm:0.6.1"
checksum: 7da3d4092f7f544e63577d098745b570d550ec63932db6e4fd38ddf52f38efd25150ed65d7952a1842d89a0f548ff61c6f8bad45d277e8a044a56735e4a71b27
"openapi-fetch@npm:0.8.2":
version: 0.8.2
resolution: "openapi-fetch@npm:0.8.2"
dependencies:
openapi-typescript-helpers: ^0.0.5
checksum: 05541a2559f51c86e3fdd5369596002fdf3230b481241211ea79965900b5a0428cb34b8062ea24cd7e4a974feec6e54287f4d64c194e93b68925024d7f488463
languageName: node
linkType: hard

"openapi-typescript-helpers@npm:^0.0.5":
version: 0.0.5
resolution: "openapi-typescript-helpers@npm:0.0.5"
checksum: 1c1024e552af9f3bdf3ab7a76bd7208f783f9af53b9cfebc86984e700e85df071a73cf5f560cb295a547a0dc88ccd87a689a7fd286781e3b04716af8aa583898
languageName: node
linkType: hard

Expand Down Expand Up @@ -4257,7 +4266,7 @@ __metadata:
jsdoc: ^4.0.2
mocha: ^10.2.0
mockery: ^2.1.0
openapi-fetch: ^0.6.1
openapi-fetch: 0.8.2
openapi-typescript: ^6.7.2
ts-standard: ^12.0.2
typedoc: ^0.24.7
Expand Down

0 comments on commit 8f2af90

Please sign in to comment.