openfoodfac-ts
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,31 +0,3 @@ | ||
import * as ApiTypes from './types'; | ||
interface Options { | ||
/** @default world */ | ||
country: string; | ||
userAgent?: string; | ||
} | ||
export declare class OpenFoodFactsApi { | ||
private baseUrl; | ||
private country; | ||
private userAgent?; | ||
constructor(options?: Partial<Options>); | ||
findProductByBarcode(barcode: string, controller?: AbortController): Promise<ApiTypes.Product | null>; | ||
findProductsByBrand(brandName: string, page?: number, controller?: AbortController): Promise<ApiTypes.ProductsResponse>; | ||
findProductsByCategory(category: string, page?: number, controller?: AbortController): Promise<ApiTypes.ProductsResponse>; | ||
findCategories(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findCountries(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findIngredients(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findPackagings(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findPackagingCodes(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findPurchasePlaces(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findStates(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findTraces(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findEntryDates(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findAllergens(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findAdditives(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findLanguages(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
findBrands(controller?: AbortController): Promise<ApiTypes.TagsResponse>; | ||
private request; | ||
} | ||
import { OpenFoodFactsApi } from './OpenFoodFactsApi'; | ||
export * as ApiTypes from './OpenFoodFactsApi/types'; | ||
export default OpenFoodFactsApi; | ||
export { ApiTypes }; |
@@ -23,8 +23,2 @@ 'use strict'; | ||
var index = { | ||
__proto__: null | ||
}; | ||
var fetchify = function fetchify(input, init, controller) { | ||
@@ -85,3 +79,3 @@ if (init === void 0) { | ||
_proto.findProductsByBrand = function findProductsByBrand(brandName, page, controller) { | ||
_proto.findProductsBySearchTerm = function findProductsBySearchTerm(searchTerm, page, controller) { | ||
if (page === void 0) { | ||
@@ -94,3 +88,4 @@ page = 1; | ||
return Promise.resolve(_this4.request("/brand/" + brandName + "/" + page, controller)); | ||
var parsedTerm = encodeURIComponent(searchTerm); | ||
return Promise.resolve(_this4.request("/cgi/search.pl?search_terms=" + parsedTerm + "&page=" + page + "&search_simple=1&action=process&json=1", controller)); | ||
} catch (e) { | ||
@@ -101,3 +96,3 @@ return Promise.reject(e); | ||
_proto.findProductsByCategory = function findProductsByCategory(category, page, controller) { | ||
_proto.findProductsByBrand = function findProductsByBrand(brandName, page, controller) { | ||
if (page === void 0) { | ||
@@ -110,3 +105,3 @@ page = 1; | ||
return Promise.resolve(_this6.request("/category/" + category + "/" + page, controller)); | ||
return Promise.resolve(_this6.request("/brand/" + brandName + "/" + page, controller)); | ||
} catch (e) { | ||
@@ -117,7 +112,11 @@ return Promise.reject(e); | ||
_proto.findCategories = function findCategories(controller) { | ||
_proto.findProductsByCategory = function findProductsByCategory(category, page, controller) { | ||
if (page === void 0) { | ||
page = 1; | ||
} | ||
try { | ||
var _this8 = this; | ||
return Promise.resolve(_this8.request("/categories", controller)); | ||
return Promise.resolve(_this8.request("/category/" + category + "/" + page, controller)); | ||
} catch (e) { | ||
@@ -128,7 +127,7 @@ return Promise.reject(e); | ||
_proto.findCountries = function findCountries(controller) { | ||
_proto.findCategories = function findCategories(controller) { | ||
try { | ||
var _this10 = this; | ||
return Promise.resolve(_this10.request('/countries', controller)); | ||
return Promise.resolve(_this10.request("/categories", controller)); | ||
} catch (e) { | ||
@@ -139,7 +138,7 @@ return Promise.reject(e); | ||
_proto.findIngredients = function findIngredients(controller) { | ||
_proto.findCountries = function findCountries(controller) { | ||
try { | ||
var _this12 = this; | ||
return Promise.resolve(_this12.request('/ingredients', controller)); | ||
return Promise.resolve(_this12.request('/countries', controller)); | ||
} catch (e) { | ||
@@ -150,7 +149,7 @@ return Promise.reject(e); | ||
_proto.findPackagings = function findPackagings(controller) { | ||
_proto.findIngredients = function findIngredients(controller) { | ||
try { | ||
var _this14 = this; | ||
return Promise.resolve(_this14.request('/packaging', controller)); | ||
return Promise.resolve(_this14.request('/ingredients', controller)); | ||
} catch (e) { | ||
@@ -161,7 +160,7 @@ return Promise.reject(e); | ||
_proto.findPackagingCodes = function findPackagingCodes(controller) { | ||
_proto.findPackagings = function findPackagings(controller) { | ||
try { | ||
var _this16 = this; | ||
return Promise.resolve(_this16.request('/packager-codes', controller)); | ||
return Promise.resolve(_this16.request('/packaging', controller)); | ||
} catch (e) { | ||
@@ -172,7 +171,7 @@ return Promise.reject(e); | ||
_proto.findPurchasePlaces = function findPurchasePlaces(controller) { | ||
_proto.findPackagingCodes = function findPackagingCodes(controller) { | ||
try { | ||
var _this18 = this; | ||
return Promise.resolve(_this18.request('/purchase-places', controller)); | ||
return Promise.resolve(_this18.request('/packager-codes', controller)); | ||
} catch (e) { | ||
@@ -183,7 +182,7 @@ return Promise.reject(e); | ||
_proto.findStates = function findStates(controller) { | ||
_proto.findPurchasePlaces = function findPurchasePlaces(controller) { | ||
try { | ||
var _this20 = this; | ||
return Promise.resolve(_this20.request('/states', controller)); | ||
return Promise.resolve(_this20.request('/purchase-places', controller)); | ||
} catch (e) { | ||
@@ -194,7 +193,7 @@ return Promise.reject(e); | ||
_proto.findTraces = function findTraces(controller) { | ||
_proto.findStates = function findStates(controller) { | ||
try { | ||
var _this22 = this; | ||
return Promise.resolve(_this22.request('/traces', controller)); | ||
return Promise.resolve(_this22.request('/states', controller)); | ||
} catch (e) { | ||
@@ -205,7 +204,7 @@ return Promise.reject(e); | ||
_proto.findEntryDates = function findEntryDates(controller) { | ||
_proto.findTraces = function findTraces(controller) { | ||
try { | ||
var _this24 = this; | ||
return Promise.resolve(_this24.request('/entry-dates', controller)); | ||
return Promise.resolve(_this24.request('/traces', controller)); | ||
} catch (e) { | ||
@@ -216,7 +215,7 @@ return Promise.reject(e); | ||
_proto.findAllergens = function findAllergens(controller) { | ||
_proto.findEntryDates = function findEntryDates(controller) { | ||
try { | ||
var _this26 = this; | ||
return Promise.resolve(_this26.request('/allergens', controller)); | ||
return Promise.resolve(_this26.request('/entry-dates', controller)); | ||
} catch (e) { | ||
@@ -227,7 +226,7 @@ return Promise.reject(e); | ||
_proto.findAdditives = function findAdditives(controller) { | ||
_proto.findAllergens = function findAllergens(controller) { | ||
try { | ||
var _this28 = this; | ||
return Promise.resolve(_this28.request('/additives', controller)); | ||
return Promise.resolve(_this28.request('/allergens', controller)); | ||
} catch (e) { | ||
@@ -238,7 +237,7 @@ return Promise.reject(e); | ||
_proto.findLanguages = function findLanguages(controller) { | ||
_proto.findAdditives = function findAdditives(controller) { | ||
try { | ||
var _this30 = this; | ||
return Promise.resolve(_this30.request('/languages', controller)); | ||
return Promise.resolve(_this30.request('/additives', controller)); | ||
} catch (e) { | ||
@@ -249,7 +248,7 @@ return Promise.reject(e); | ||
_proto.findBrands = function findBrands(controller) { | ||
_proto.findLanguages = function findLanguages(controller) { | ||
try { | ||
var _this32 = this; | ||
return Promise.resolve(_this32.request('/brands', controller)); | ||
return Promise.resolve(_this32.request('/languages', controller)); | ||
} catch (e) { | ||
@@ -260,10 +259,20 @@ return Promise.reject(e); | ||
_proto.request = function request(apiPath, controller) { | ||
_proto.findBrands = function findBrands(controller) { | ||
try { | ||
var _this34 = this; | ||
var headers = _this34.userAgent ? { | ||
'User-Agent': _this34.userAgent | ||
return Promise.resolve(_this34.request('/brands', controller)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
_proto.request = function request(apiPath, controller) { | ||
try { | ||
var _this36 = this; | ||
var headers = _this36.userAgent ? { | ||
'User-Agent': _this36.userAgent | ||
} : undefined; | ||
return Promise.resolve(fetchify("" + _this34.baseUrl + apiPath + ".json", { | ||
return Promise.resolve(fetchify("" + _this36.baseUrl + apiPath + ".json", { | ||
headers: headers | ||
@@ -279,5 +288,10 @@ }, controller)); | ||
var index = { | ||
__proto__: null | ||
}; | ||
exports.ApiTypes = index; | ||
exports.OpenFoodFactsApi = OpenFoodFactsApi; | ||
exports.default = OpenFoodFactsApi; | ||
//# sourceMappingURL=openfoodfac-ts.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";function e(){return(e=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[s]=t[s])}return e}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});var r={country:"world"},t=function(){function t(t){void 0===t&&(t=r);var s=e({},r,{},t);this.userAgent=s.userAgent,this.country=s.country,this.baseUrl="https://"+this.country+".openfoodfacts.org"}var s=t.prototype;return s.findProductByBarcode=function(e,r){try{return Promise.resolve(this.request("/api/v0/product/"+e,r)).then((function(e){var r;return null!==(r=null==e?void 0:e.product)&&void 0!==r?r:null}))}catch(e){return Promise.reject(e)}},s.findProductsByBrand=function(e,r,t){void 0===r&&(r=1);try{return Promise.resolve(this.request("/brand/"+e+"/"+r,t))}catch(e){return Promise.reject(e)}},s.findProductsByCategory=function(e,r,t){void 0===r&&(r=1);try{return Promise.resolve(this.request("/category/"+e+"/"+r,t))}catch(e){return Promise.reject(e)}},s.findCategories=function(e){try{return Promise.resolve(this.request("/categories",e))}catch(e){return Promise.reject(e)}},s.findCountries=function(e){try{return Promise.resolve(this.request("/countries",e))}catch(e){return Promise.reject(e)}},s.findIngredients=function(e){try{return Promise.resolve(this.request("/ingredients",e))}catch(e){return Promise.reject(e)}},s.findPackagings=function(e){try{return Promise.resolve(this.request("/packaging",e))}catch(e){return Promise.reject(e)}},s.findPackagingCodes=function(e){try{return Promise.resolve(this.request("/packager-codes",e))}catch(e){return Promise.reject(e)}},s.findPurchasePlaces=function(e){try{return Promise.resolve(this.request("/purchase-places",e))}catch(e){return Promise.reject(e)}},s.findStates=function(e){try{return Promise.resolve(this.request("/states",e))}catch(e){return Promise.reject(e)}},s.findTraces=function(e){try{return Promise.resolve(this.request("/traces",e))}catch(e){return Promise.reject(e)}},s.findEntryDates=function(e){try{return Promise.resolve(this.request("/entry-dates",e))}catch(e){return Promise.reject(e)}},s.findAllergens=function(e){try{return Promise.resolve(this.request("/allergens",e))}catch(e){return Promise.reject(e)}},s.findAdditives=function(e){try{return Promise.resolve(this.request("/additives",e))}catch(e){return Promise.reject(e)}},s.findLanguages=function(e){try{return Promise.resolve(this.request("/languages",e))}catch(e){return Promise.reject(e)}},s.findBrands=function(e){try{return Promise.resolve(this.request("/brands",e))}catch(e){return Promise.reject(e)}},s.request=function(r,t){try{return Promise.resolve(function(r,t,s){void 0===t&&(t={});try{return void 0===s&&(s=new AbortController),Promise.resolve(fetch(r,e({signal:s.signal},t))).then((function(e){if(!e.ok)throw new Error(e.statusText);return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}}(""+this.baseUrl+r+".json",{headers:this.userAgent?{"User-Agent":this.userAgent}:void 0},t))}catch(e){return Promise.reject(e)}},t}();exports.ApiTypes={__proto__:null},exports.OpenFoodFactsApi=t,exports.default=t; | ||
"use strict";function e(){return(e=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&(e[s]=t[s])}return e}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});var r={country:"world"},t=function(){function t(t){void 0===t&&(t=r);var s=e({},r,{},t);this.userAgent=s.userAgent,this.country=s.country,this.baseUrl="https://"+this.country+".openfoodfacts.org"}var s=t.prototype;return s.findProductByBarcode=function(e,r){try{return Promise.resolve(this.request("/api/v0/product/"+e,r)).then((function(e){var r;return null!==(r=null==e?void 0:e.product)&&void 0!==r?r:null}))}catch(e){return Promise.reject(e)}},s.findProductsBySearchTerm=function(e,r,t){void 0===r&&(r=1);try{var s=encodeURIComponent(e);return Promise.resolve(this.request("/cgi/search.pl?search_terms="+s+"&page="+r+"&search_simple=1&action=process&json=1",t))}catch(e){return Promise.reject(e)}},s.findProductsByBrand=function(e,r,t){void 0===r&&(r=1);try{return Promise.resolve(this.request("/brand/"+e+"/"+r,t))}catch(e){return Promise.reject(e)}},s.findProductsByCategory=function(e,r,t){void 0===r&&(r=1);try{return Promise.resolve(this.request("/category/"+e+"/"+r,t))}catch(e){return Promise.reject(e)}},s.findCategories=function(e){try{return Promise.resolve(this.request("/categories",e))}catch(e){return Promise.reject(e)}},s.findCountries=function(e){try{return Promise.resolve(this.request("/countries",e))}catch(e){return Promise.reject(e)}},s.findIngredients=function(e){try{return Promise.resolve(this.request("/ingredients",e))}catch(e){return Promise.reject(e)}},s.findPackagings=function(e){try{return Promise.resolve(this.request("/packaging",e))}catch(e){return Promise.reject(e)}},s.findPackagingCodes=function(e){try{return Promise.resolve(this.request("/packager-codes",e))}catch(e){return Promise.reject(e)}},s.findPurchasePlaces=function(e){try{return Promise.resolve(this.request("/purchase-places",e))}catch(e){return Promise.reject(e)}},s.findStates=function(e){try{return Promise.resolve(this.request("/states",e))}catch(e){return Promise.reject(e)}},s.findTraces=function(e){try{return Promise.resolve(this.request("/traces",e))}catch(e){return Promise.reject(e)}},s.findEntryDates=function(e){try{return Promise.resolve(this.request("/entry-dates",e))}catch(e){return Promise.reject(e)}},s.findAllergens=function(e){try{return Promise.resolve(this.request("/allergens",e))}catch(e){return Promise.reject(e)}},s.findAdditives=function(e){try{return Promise.resolve(this.request("/additives",e))}catch(e){return Promise.reject(e)}},s.findLanguages=function(e){try{return Promise.resolve(this.request("/languages",e))}catch(e){return Promise.reject(e)}},s.findBrands=function(e){try{return Promise.resolve(this.request("/brands",e))}catch(e){return Promise.reject(e)}},s.request=function(r,t){try{return Promise.resolve(function(r,t,s){void 0===t&&(t={});try{return void 0===s&&(s=new AbortController),Promise.resolve(fetch(r,e({signal:s.signal},t))).then((function(e){if(!e.ok)throw new Error(e.statusText);return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}}(""+this.baseUrl+r+".json",{headers:this.userAgent?{"User-Agent":this.userAgent}:void 0},t))}catch(e){return Promise.reject(e)}},t}();exports.ApiTypes={__proto__:null},exports.default=t; | ||
//# sourceMappingURL=openfoodfac-ts.cjs.production.min.js.map |
@@ -19,8 +19,2 @@ function _extends() { | ||
var index = { | ||
__proto__: null | ||
}; | ||
var fetchify = function fetchify(input, init, controller) { | ||
@@ -81,3 +75,3 @@ if (init === void 0) { | ||
_proto.findProductsByBrand = function findProductsByBrand(brandName, page, controller) { | ||
_proto.findProductsBySearchTerm = function findProductsBySearchTerm(searchTerm, page, controller) { | ||
if (page === void 0) { | ||
@@ -90,3 +84,4 @@ page = 1; | ||
return Promise.resolve(_this4.request("/brand/" + brandName + "/" + page, controller)); | ||
var parsedTerm = encodeURIComponent(searchTerm); | ||
return Promise.resolve(_this4.request("/cgi/search.pl?search_terms=" + parsedTerm + "&page=" + page + "&search_simple=1&action=process&json=1", controller)); | ||
} catch (e) { | ||
@@ -97,3 +92,3 @@ return Promise.reject(e); | ||
_proto.findProductsByCategory = function findProductsByCategory(category, page, controller) { | ||
_proto.findProductsByBrand = function findProductsByBrand(brandName, page, controller) { | ||
if (page === void 0) { | ||
@@ -106,3 +101,3 @@ page = 1; | ||
return Promise.resolve(_this6.request("/category/" + category + "/" + page, controller)); | ||
return Promise.resolve(_this6.request("/brand/" + brandName + "/" + page, controller)); | ||
} catch (e) { | ||
@@ -113,7 +108,11 @@ return Promise.reject(e); | ||
_proto.findCategories = function findCategories(controller) { | ||
_proto.findProductsByCategory = function findProductsByCategory(category, page, controller) { | ||
if (page === void 0) { | ||
page = 1; | ||
} | ||
try { | ||
var _this8 = this; | ||
return Promise.resolve(_this8.request("/categories", controller)); | ||
return Promise.resolve(_this8.request("/category/" + category + "/" + page, controller)); | ||
} catch (e) { | ||
@@ -124,7 +123,7 @@ return Promise.reject(e); | ||
_proto.findCountries = function findCountries(controller) { | ||
_proto.findCategories = function findCategories(controller) { | ||
try { | ||
var _this10 = this; | ||
return Promise.resolve(_this10.request('/countries', controller)); | ||
return Promise.resolve(_this10.request("/categories", controller)); | ||
} catch (e) { | ||
@@ -135,7 +134,7 @@ return Promise.reject(e); | ||
_proto.findIngredients = function findIngredients(controller) { | ||
_proto.findCountries = function findCountries(controller) { | ||
try { | ||
var _this12 = this; | ||
return Promise.resolve(_this12.request('/ingredients', controller)); | ||
return Promise.resolve(_this12.request('/countries', controller)); | ||
} catch (e) { | ||
@@ -146,7 +145,7 @@ return Promise.reject(e); | ||
_proto.findPackagings = function findPackagings(controller) { | ||
_proto.findIngredients = function findIngredients(controller) { | ||
try { | ||
var _this14 = this; | ||
return Promise.resolve(_this14.request('/packaging', controller)); | ||
return Promise.resolve(_this14.request('/ingredients', controller)); | ||
} catch (e) { | ||
@@ -157,7 +156,7 @@ return Promise.reject(e); | ||
_proto.findPackagingCodes = function findPackagingCodes(controller) { | ||
_proto.findPackagings = function findPackagings(controller) { | ||
try { | ||
var _this16 = this; | ||
return Promise.resolve(_this16.request('/packager-codes', controller)); | ||
return Promise.resolve(_this16.request('/packaging', controller)); | ||
} catch (e) { | ||
@@ -168,7 +167,7 @@ return Promise.reject(e); | ||
_proto.findPurchasePlaces = function findPurchasePlaces(controller) { | ||
_proto.findPackagingCodes = function findPackagingCodes(controller) { | ||
try { | ||
var _this18 = this; | ||
return Promise.resolve(_this18.request('/purchase-places', controller)); | ||
return Promise.resolve(_this18.request('/packager-codes', controller)); | ||
} catch (e) { | ||
@@ -179,7 +178,7 @@ return Promise.reject(e); | ||
_proto.findStates = function findStates(controller) { | ||
_proto.findPurchasePlaces = function findPurchasePlaces(controller) { | ||
try { | ||
var _this20 = this; | ||
return Promise.resolve(_this20.request('/states', controller)); | ||
return Promise.resolve(_this20.request('/purchase-places', controller)); | ||
} catch (e) { | ||
@@ -190,7 +189,7 @@ return Promise.reject(e); | ||
_proto.findTraces = function findTraces(controller) { | ||
_proto.findStates = function findStates(controller) { | ||
try { | ||
var _this22 = this; | ||
return Promise.resolve(_this22.request('/traces', controller)); | ||
return Promise.resolve(_this22.request('/states', controller)); | ||
} catch (e) { | ||
@@ -201,7 +200,7 @@ return Promise.reject(e); | ||
_proto.findEntryDates = function findEntryDates(controller) { | ||
_proto.findTraces = function findTraces(controller) { | ||
try { | ||
var _this24 = this; | ||
return Promise.resolve(_this24.request('/entry-dates', controller)); | ||
return Promise.resolve(_this24.request('/traces', controller)); | ||
} catch (e) { | ||
@@ -212,7 +211,7 @@ return Promise.reject(e); | ||
_proto.findAllergens = function findAllergens(controller) { | ||
_proto.findEntryDates = function findEntryDates(controller) { | ||
try { | ||
var _this26 = this; | ||
return Promise.resolve(_this26.request('/allergens', controller)); | ||
return Promise.resolve(_this26.request('/entry-dates', controller)); | ||
} catch (e) { | ||
@@ -223,7 +222,7 @@ return Promise.reject(e); | ||
_proto.findAdditives = function findAdditives(controller) { | ||
_proto.findAllergens = function findAllergens(controller) { | ||
try { | ||
var _this28 = this; | ||
return Promise.resolve(_this28.request('/additives', controller)); | ||
return Promise.resolve(_this28.request('/allergens', controller)); | ||
} catch (e) { | ||
@@ -234,7 +233,7 @@ return Promise.reject(e); | ||
_proto.findLanguages = function findLanguages(controller) { | ||
_proto.findAdditives = function findAdditives(controller) { | ||
try { | ||
var _this30 = this; | ||
return Promise.resolve(_this30.request('/languages', controller)); | ||
return Promise.resolve(_this30.request('/additives', controller)); | ||
} catch (e) { | ||
@@ -245,7 +244,7 @@ return Promise.reject(e); | ||
_proto.findBrands = function findBrands(controller) { | ||
_proto.findLanguages = function findLanguages(controller) { | ||
try { | ||
var _this32 = this; | ||
return Promise.resolve(_this32.request('/brands', controller)); | ||
return Promise.resolve(_this32.request('/languages', controller)); | ||
} catch (e) { | ||
@@ -256,10 +255,20 @@ return Promise.reject(e); | ||
_proto.request = function request(apiPath, controller) { | ||
_proto.findBrands = function findBrands(controller) { | ||
try { | ||
var _this34 = this; | ||
var headers = _this34.userAgent ? { | ||
'User-Agent': _this34.userAgent | ||
return Promise.resolve(_this34.request('/brands', controller)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
_proto.request = function request(apiPath, controller) { | ||
try { | ||
var _this36 = this; | ||
var headers = _this36.userAgent ? { | ||
'User-Agent': _this36.userAgent | ||
} : undefined; | ||
return Promise.resolve(fetchify("" + _this34.baseUrl + apiPath + ".json", { | ||
return Promise.resolve(fetchify("" + _this36.baseUrl + apiPath + ".json", { | ||
headers: headers | ||
@@ -275,4 +284,10 @@ }, controller)); | ||
var index = { | ||
__proto__: null | ||
}; | ||
export default OpenFoodFactsApi; | ||
export { index as ApiTypes, OpenFoodFactsApi }; | ||
export { index as ApiTypes }; | ||
//# sourceMappingURL=openfoodfac-ts.esm.js.map |
{ | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -15,5 +15,21 @@ # openfoodfac-ts | ||
const foodFactsApi = new OpenFoodFactsApi(); | ||
const product = await foodFactsApi.findProductByBarcode('58918274712'); | ||
``` | ||
### Usage with config 🛠️ | ||
```ts | ||
import { OpenFoodFactsApi } from 'openfoodfac-ts'; | ||
const foodFactsApiPl = new OpenFoodFactsApi({ | ||
/** | ||
* Country id. | ||
* List of countries https://world.openfoodfacts.org/countries. | ||
*/ | ||
country: 'pl', // 'world' by default | ||
userAgent: 'Mozilla/5.0 ...' | ||
/** User Agent used while requesting the API. */ | ||
userAgent?: 'Mozilla/5.0 ...' | ||
}); | ||
@@ -20,0 +36,0 @@ |
158
src/index.ts
@@ -1,157 +0,5 @@ | ||
import * as ApiTypes from './types'; | ||
import { fetchify } from './fetchify'; | ||
import { OpenFoodFactsApi } from './OpenFoodFactsApi'; | ||
interface Options { | ||
/** @default world */ | ||
country: string | ||
userAgent?: string | ||
} | ||
export * as ApiTypes from './OpenFoodFactsApi/types'; | ||
const defaultOptions: Options = { | ||
country: 'world', | ||
} | ||
export class OpenFoodFactsApi { | ||
private baseUrl: string; | ||
private country: string; | ||
private userAgent?: string; | ||
constructor(options: Partial<Options> = defaultOptions) { | ||
const mergedOptions = { | ||
...defaultOptions, | ||
...options | ||
}; | ||
this.userAgent = mergedOptions.userAgent; | ||
this.country = mergedOptions.country; | ||
this.baseUrl = `https://${this.country}.openfoodfacts.org`; | ||
} | ||
async findProductByBarcode( | ||
barcode: string, | ||
controller?: AbortController | ||
): Promise<ApiTypes.Product | null> { | ||
const response = await this.request<ApiTypes.ProductResponse>( | ||
`/api/v0/product/${barcode}`, | ||
controller | ||
); | ||
return response?.product ?? null; | ||
} | ||
async findProductsByBrand( | ||
brandName: string, | ||
page = 1, | ||
controller?: AbortController | ||
): Promise<ApiTypes.ProductsResponse> { | ||
return this.request(`/brand/${brandName}/${page}`, controller); | ||
} | ||
async findProductsByCategory( | ||
category: string, | ||
page = 1, | ||
controller?: AbortController | ||
): Promise<ApiTypes.ProductsResponse> { | ||
return this.request(`/category/${category}/${page}`, controller); | ||
} | ||
async findCategories( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request(`/categories`, controller); | ||
} | ||
async findCountries( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/countries', controller); | ||
} | ||
async findIngredients( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/ingredients', controller); | ||
} | ||
async findPackagings( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/packaging', controller); | ||
} | ||
async findPackagingCodes( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/packager-codes', controller); | ||
} | ||
async findPurchasePlaces( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/purchase-places', controller); | ||
} | ||
async findStates( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/states', controller); | ||
} | ||
async findTraces( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/traces', controller); | ||
} | ||
async findEntryDates( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/entry-dates', controller); | ||
} | ||
async findAllergens( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/allergens', controller); | ||
} | ||
async findAdditives( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/additives', controller); | ||
} | ||
async findLanguages( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/languages', controller); | ||
} | ||
async findBrands( | ||
controller?: AbortController | ||
): Promise<ApiTypes.TagsResponse> { | ||
return this.request('/brands', controller); | ||
} | ||
private async request<T extends object>( | ||
apiPath: string, | ||
controller?: AbortController, | ||
): Promise<T> { | ||
const headers = this.userAgent ? { 'User-Agent': this.userAgent } : undefined; | ||
return fetchify<T>( | ||
`${this.baseUrl}${apiPath}.json`, | ||
{ headers }, | ||
controller, | ||
); | ||
} | ||
} | ||
export default OpenFoodFactsApi; | ||
export { ApiTypes }; | ||
export default OpenFoodFactsApi; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
106841
26
2762
37
4