https://github.com/adamlewkowicz/openfoodfac-ts
Open Food Facts Api written in TypeScript 🥫🍕🍼
https://github.com/adamlewkowicz/openfoodfac-ts
api food-products nutrition typescript
Last synced: about 1 month ago
JSON representation
Open Food Facts Api written in TypeScript 🥫🍕🍼
- Host: GitHub
- URL: https://github.com/adamlewkowicz/openfoodfac-ts
- Owner: adamlewkowicz
- License: mit
- Created: 2020-04-21T11:29:37.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-11T11:10:53.000Z (over 1 year ago)
- Last Synced: 2023-12-11T12:57:44.833Z (over 1 year ago)
- Topics: api, food-products, nutrition, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/openfoodfac-ts
- Size: 1.38 MB
- Stars: 5
- Watchers: 1
- Forks: 3
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# openfoodfac-ts
[Open Food Facts](https://world.openfoodfacts.org/) Api written in TypeScript 🥫🍕🍼

## Features 🚀
- Complete TypeScript support with type-safe responses 💪
- Clean API ✨
- Mock testing ready ✔## Getting started 🐾
### Installation 💿
`npm i openfoodfac-ts`### Usage 🛠️
```ts
import { OpenFoodFactsApi } from 'openfoodfac-ts';const openFoodFactsApi = new OpenFoodFactsApi();
const product = await openFoodFactsApi.findProductByBarcode('58918274712');
const countries = await openFoodFactsApi.findCountries();
```### Usage with config 🛠️
```ts
import { OpenFoodFactsApi } from 'openfoodfac-ts';const openFoodFactsApi = new OpenFoodFactsApi({
/**
* Country id.
* List of countries https://world.openfoodfacts.org/countries.
* @default 'world'
*/
country: 'pl'
/**
* User Agent used while requesting the API.
*/
userAgent: 'Mozilla/5.0 ...'
/**
* AbortController instance used while requesting the API.
* Allows you to cancel request at any time.
*/
abortController: new AbortController()
});const products = await openFoodFactsApi.findProductsByCategory('vegetables');
```### Setting abort controller dynamically
```ts
const abortController = new AbortController();const tomatoeProducts = await openFoodFactsApi
.setAbortController(abortController)
.findProductsBySearchTerm('Tomatoes');
```## API 🗺
```ts
class OpenFoodFactsApi {findProductByBarcode(barcode: string): Promise;
findProductsBySearchTerm(searchTerm: string, page?: number): Promise;
findProductsByBrand(brandName: string, page?: number): Promise;
findProductsByCategory(category: string, page?: number): Promise;
findCategories(): Promise;
findCountries(): Promise;
findIngredients(): Promise;
findPackagings(): Promise;
findPackagingCodes(): Promise;
findPurchasePlaces(): Promise;
findStates(): Promise;
findTraces(): Promise;
findEntryDates(): Promise;
findAllergens(): Promise;
findAdditives(): Promise;
findLanguages(): Promise;
findBrands(): Promise;
setController(abortController?: AbortController): this;setUserAgent(userAgent?: string): this;
}
```## Testing 🧰
### Usage with Jest
There is a mock version available that uses mocked json response instead of making real API calls.
To make jest use mocked version, paste below code in your `jest.setup.*` file.
```ts
jest.mock('openfoodfac-ts/OpenFoodFactsApi');
```## License ⚖️
[MIT](LICENSE)