Git Product home page Git Product logo

openfoodfac-ts's Introduction

openfoodfac-ts

Open Food Facts Api written in TypeScript ๐Ÿฅซ๐Ÿ•๐Ÿผ

Node CI

Features ๐Ÿš€

  • Complete TypeScript support with type-safe responses ๐Ÿ’ช
  • Clean API โœจ
  • Mock testing ready โœ”

Getting started ๐Ÿพ

Installation ๐Ÿ’ฟ

npm i openfoodfac-ts

Usage ๐Ÿ› ๏ธ

import { OpenFoodFactsApi } from 'openfoodfac-ts';

const openFoodFactsApi = new OpenFoodFactsApi();

const product = await openFoodFactsApi.findProductByBarcode('58918274712');

const countries = await openFoodFactsApi.findCountries();

Usage with config ๐Ÿ› ๏ธ

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

const abortController = new AbortController();

const tomatoeProducts = await openFoodFactsApi
  .setAbortController(abortController)
  .findProductsBySearchTerm('Tomatoes');

API ๐Ÿ—บ

class OpenFoodFactsApi {

    findProductByBarcode(barcode: string): Promise<ApiTypes.Product | null>;
    
    findProductsBySearchTerm(searchTerm: string, page?: number): Promise<ApiTypes.ProductsResponse>;
    
    findProductsByBrand(brandName: string, page?: number): Promise<ApiTypes.ProductsResponse>;
    
    findProductsByCategory(category: string, page?: number): Promise<ApiTypes.ProductsResponse>;
    
    findCategories(): Promise<ApiTypes.TagsResponse>;
    
    findCountries(): Promise<ApiTypes.TagsResponse>;
    
    findIngredients(): Promise<ApiTypes.TagsResponse>;
    
    findPackagings(): Promise<ApiTypes.TagsResponse>;
    
    findPackagingCodes(): Promise<ApiTypes.TagsResponse>;
    
    findPurchasePlaces(): Promise<ApiTypes.TagsResponse>;
    
    findStates(): Promise<ApiTypes.TagsResponse>;
    
    findTraces(): Promise<ApiTypes.TagsResponse>;
    
    findEntryDates(): Promise<ApiTypes.TagsResponse>;
    
    findAllergens(): Promise<ApiTypes.TagsResponse>;
    
    findAdditives(): Promise<ApiTypes.TagsResponse>;
    
    findLanguages(): Promise<ApiTypes.TagsResponse>;
    
    findBrands(): Promise<ApiTypes.TagsResponse>;
    
    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.

jest.mock('openfoodfac-ts/OpenFoodFactsApi');

License โš–๏ธ

MIT

openfoodfac-ts's People

Contributors

adamlewkowicz avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

openfoodfac-ts's Issues

Invalid segment (https://qr.nutella.com/). Paths must not contain // in them.

Hi there!

First of all, thank you for your awesome plugin! It works really great so far.
But I see many exceptions from users of my app which uses your plugin which says something like this:

Invalid segment (https://qr.nutella.com/). Paths must not contain // in them.

Do you maybe know where this exception comes from? This happens with many products and I have no idea at all where this comes from.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.