@jclind/ingredient-parser 1.2.10 → 1.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -13,9 +13,12 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
13
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
16
19
  Object.defineProperty(exports, "__esModule", { value: true });
17
20
  exports.parseIngredientString = exports.ingredientParser = void 0;
18
- const ingredientParser_1 = require("./src/funcs/ingredientParser");
21
+ const ingredientParser_1 = __importDefault(require("./src/funcs/ingredientParser"));
19
22
  exports.ingredientParser = ingredientParser_1.default;
20
23
  var parseIngredientString_1 = require("./src/funcs/parseIngredientString");
21
24
  Object.defineProperty(exports, "parseIngredientString", { enumerable: true, get: function () { return parseIngredientString_1.parseIngredientString; } });
@@ -1 +1,2 @@
1
1
  export declare const spoonacularHttp: import("axios").AxiosInstance;
2
+ export declare const createIngredientServerHttp: (serverUrl?: string) => import("axios").AxiosInstance;
@@ -1,10 +1,21 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.spoonacularHttp = void 0;
4
- const axios_1 = require("axios");
6
+ exports.createIngredientServerHttp = exports.spoonacularHttp = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const DEFAULT_SERVER_URL = 'https://ingredient-parser-service-production-2635.up.railway.app';
5
9
  exports.spoonacularHttp = axios_1.default.create({
6
10
  baseURL: 'https://api.spoonacular.com/food/ingredients/',
7
11
  headers: {
8
12
  'Content-type': 'application/json',
9
13
  },
10
14
  });
15
+ const createIngredientServerHttp = (serverUrl) => axios_1.default.create({
16
+ baseURL: serverUrl || DEFAULT_SERVER_URL,
17
+ headers: {
18
+ 'Content-type': 'application/json',
19
+ },
20
+ });
21
+ exports.createIngredientServerHttp = createIngredientServerHttp;
@@ -1,2 +1,4 @@
1
+ import { IngredientData } from '../../types.js';
2
+ export declare const parseAndEnrich: (ingredientString: string, spoonacularApiKey: string, serverUrl?: string) => Promise<IngredientData | null>;
1
3
  export declare const searchIngredient: (name: string, spoonacularAPIKey: string) => Promise<any>;
2
4
  export declare const getIngredientInformation: (ingrId: number, unit: boolean, spoonacularAPIKey: string) => Promise<any>;
@@ -9,8 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getIngredientInformation = exports.searchIngredient = void 0;
12
+ exports.getIngredientInformation = exports.searchIngredient = exports.parseAndEnrich = void 0;
13
13
  const http_js_1 = require("./http.js");
14
+ const parseAndEnrich = (ingredientString, spoonacularApiKey, serverUrl) => __awaiter(void 0, void 0, void 0, function* () {
15
+ var _a;
16
+ const serverHttp = (0, http_js_1.createIngredientServerHttp)(serverUrl);
17
+ const response = yield serverHttp.post('/parse', {
18
+ ingredientString,
19
+ spoonacularApiKey,
20
+ });
21
+ return (_a = response.data.data) !== null && _a !== void 0 ? _a : null;
22
+ });
23
+ exports.parseAndEnrich = parseAndEnrich;
14
24
  const searchIngredient = (name, spoonacularAPIKey) => __awaiter(void 0, void 0, void 0, function* () {
15
25
  let searchedIngr;
16
26
  try {
@@ -1,4 +1,4 @@
1
1
  export declare const calculatePrice: (quantity: number | null, unit: string | null, price: {
2
- estimatedSingleUnitPrice: number;
3
- estimatedGramPrice: number;
2
+ estimatedSingleUnitPrice?: number;
3
+ estimatedGramPrice?: number;
4
4
  }) => number | null;
@@ -3,21 +3,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.calculatePrice = void 0;
4
4
  const ingredient_unit_converter_1 = require("@jclind/ingredient-unit-converter");
5
5
  const calculatePrice = (quantity, unit, price) => {
6
+ var _a, _b;
7
+ const gramPrice = (_a = price.estimatedGramPrice) !== null && _a !== void 0 ? _a : 0;
8
+ const unitPrice = (_b = price.estimatedSingleUnitPrice) !== null && _b !== void 0 ? _b : 0;
6
9
  if (quantity === null)
7
- return price.estimatedSingleUnitPrice;
10
+ return unitPrice || null;
11
+ if (quantity === 0)
12
+ return 0;
8
13
  if (!unit)
9
- return price.estimatedSingleUnitPrice * quantity;
14
+ return (unitPrice * quantity) || null;
10
15
  let convertedUnit;
11
16
  try {
12
17
  convertedUnit = (0, ingredient_unit_converter_1.converter)(quantity, unit);
13
18
  }
14
19
  catch (error) {
15
- return null;
20
+ return (unitPrice * quantity) || null;
16
21
  }
17
22
  if (!convertedUnit || 'error' in convertedUnit)
18
- return price.estimatedSingleUnitPrice * quantity;
23
+ return (unitPrice * quantity) || null;
19
24
  const convertedGrams = Number(convertedUnit.quantity);
20
- const total = convertedGrams * price.estimatedGramPrice;
25
+ const total = convertedGrams * gramPrice;
21
26
  return Math.ceil(total * 100) / 100;
22
27
  };
23
28
  exports.calculatePrice = calculatePrice;
@@ -1 +1,2 @@
1
- export declare function getIngredientInfo(ingrName: string, spoonacularAPIKey: string): Promise<any>;
1
+ import { IngredientData } from '../../types.js';
2
+ export declare function getIngredientInfo(ingredientString: string, spoonacularAPIKey: string, serverUrl?: string): Promise<IngredientData | null>;
@@ -10,13 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getIngredientInfo = getIngredientInfo;
13
- const getSpoonacularIngrData_js_1 = require("./getSpoonacularIngrData.js");
14
- function getIngredientInfo(ingrName, spoonacularAPIKey) {
13
+ const requests_js_1 = require("../api/requests.js");
14
+ function getIngredientInfo(ingredientString, spoonacularAPIKey, serverUrl) {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
- const ingrNameLower = ingrName.toLowerCase();
17
- if (!ingrNameLower)
18
- throw new Error('Ingredient Invalid');
19
- const ingrData = yield (0, getSpoonacularIngrData_js_1.getSpoonacularIngrData)(ingrNameLower, spoonacularAPIKey);
20
- return ingrData;
16
+ try {
17
+ return yield (0, requests_js_1.parseAndEnrich)(ingredientString, spoonacularAPIKey, serverUrl);
18
+ }
19
+ catch (error) {
20
+ throw new Error(error.message || 'Failed to fetch ingredient data');
21
+ }
21
22
  });
22
23
  }
@@ -29,7 +29,7 @@ const ingredientParser = (ingrString, spoonacularAPIKey, options) => __awaiter(v
29
29
  const parsedIngr = (0, parseIngredientString_js_1.parseIngredientString)(ingrString);
30
30
  let ingrData = null;
31
31
  try {
32
- ingrData = yield (0, getIngredientInfo_js_1.getIngredientInfo)(parsedIngr.ingredient || '', spoonacularAPIKey);
32
+ ingrData = yield (0, getIngredientInfo_js_1.getIngredientInfo)(parsedIngr.ingredient || '', spoonacularAPIKey, options === null || options === void 0 ? void 0 : options.serverUrl);
33
33
  }
34
34
  catch (error) {
35
35
  return {
@@ -40,10 +40,12 @@ const ingredientParser = (ingrString, spoonacularAPIKey, options) => __awaiter(v
40
40
  }
41
41
  if (parsedIngr.ingredient && ingrData) {
42
42
  const { estimatedPrices, meta, categoryPath, unit, unitShort, unitLong, original, id, nutrition } = ingrData, reducedIngrData = __rest(ingrData, ["estimatedPrices", "meta", "categoryPath", "unit", "unitShort", "unitLong", "original", "id", "nutrition"]);
43
- const totalPrice = (0, calculatePrice_js_1.calculatePrice)(parsedIngr.quantity, parsedIngr.unit, estimatedPrices);
43
+ const totalPrice = estimatedPrices
44
+ ? (0, calculatePrice_js_1.calculatePrice)(parsedIngr.quantity, parsedIngr.unit, estimatedPrices)
45
+ : null;
44
46
  const imageSize = (_a = options === null || options === void 0 ? void 0 : options.imageSize) !== null && _a !== void 0 ? _a : '100x100';
45
47
  const imagePath = `https://spoonacular.com/cdn/ingredients_${imageSize}/${reducedIngrData.image}`;
46
- const updatedIngrData = Object.assign(Object.assign(Object.assign({}, reducedIngrData), { imagePath, totalPriceUSACents: totalPrice }), ((options === null || options === void 0 ? void 0 : options.returnNutritionData) && { nutrition }));
48
+ const updatedIngrData = Object.assign(Object.assign(Object.assign(Object.assign({}, reducedIngrData), { imagePath }), (totalPrice !== null && { totalPriceUSACents: totalPrice })), ((options === null || options === void 0 ? void 0 : options.returnNutritionData) && { nutrition }));
47
49
  return {
48
50
  ingredientData: updatedIngrData,
49
51
  parsedIngredient: parsedIngr,
package/dist/types.d.ts CHANGED
@@ -24,19 +24,30 @@ export type ParsedIngredient = {
24
24
  comment: StringOrNull;
25
25
  };
26
26
  export interface IngredientData {
27
- _id: string;
28
- ingredientId: number;
29
- originalName: string;
27
+ _id?: string;
28
+ ingredientId?: number;
29
+ originalName?: string;
30
30
  name: string;
31
- amount: number;
32
- possibleUnits: string[];
33
- consistency: string;
34
- shoppingListUnits: string[];
35
- aisle: string;
36
- image: string;
37
- imagePath: string;
38
- nutrition: Nutrition;
39
- totalPriceUSACents: number;
31
+ amount?: number;
32
+ possibleUnits?: string[];
33
+ consistency?: string;
34
+ shoppingListUnits?: string[];
35
+ aisle?: string;
36
+ image?: string;
37
+ imagePath?: string;
38
+ nutrition?: Nutrition;
39
+ totalPriceUSACents?: number;
40
+ estimatedPrices?: {
41
+ estimatedGramPrice?: number;
42
+ estimatedSingleUnitPrice?: number;
43
+ };
44
+ meta?: any;
45
+ categoryPath?: any;
46
+ unit?: string;
47
+ unitShort?: string;
48
+ unitLong?: string;
49
+ original?: any;
50
+ id?: number;
40
51
  }
41
52
  export interface Nutrition {
42
53
  nutrients: Nutrient[];
@@ -74,5 +85,6 @@ export type SpoonacularImageSize = '100x100' | '250x250' | '500x500';
74
85
  export type OptionsType = {
75
86
  returnNutritionData?: boolean;
76
87
  imageSize?: SpoonacularImageSize;
88
+ serverUrl?: string;
77
89
  };
78
90
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jclind/ingredient-parser",
3
- "version": "1.2.10",
3
+ "version": "1.2.12",
4
4
  "description": "Parses given sentence including ingredient information and attempts to return quantity, measurement and ingredient data",
5
5
  "keywords": [
6
6
  "recipe",
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "homepage": "https://github.com/jclind/ingredient-parser#readme",
38
38
  "dependencies": {
39
- "@jclind/ingredient-unit-converter": "^1.0.6",
39
+ "@jclind/ingredient-unit-converter": "^1.1.0",
40
40
  "axios": "^1.0.0",
41
41
  "recipe-ingredient-parser-v3": "^1.5.0"
42
42
  },