@jclind/ingredient-parser 1.2.10 → 1.2.11

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.
@@ -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,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.spoonacularHttp = void 0;
3
+ exports.createIngredientServerHttp = exports.spoonacularHttp = void 0;
4
4
  const axios_1 = require("axios");
5
+ const DEFAULT_SERVER_URL = 'https://prepify-ingredients.up.railway.app';
5
6
  exports.spoonacularHttp = axios_1.default.create({
6
7
  baseURL: 'https://api.spoonacular.com/food/ingredients/',
7
8
  headers: {
8
9
  'Content-type': 'application/json',
9
10
  },
10
11
  });
12
+ const createIngredientServerHttp = (serverUrl) => axios_1.default.create({
13
+ baseURL: serverUrl || DEFAULT_SERVER_URL,
14
+ headers: {
15
+ 'Content-type': 'application/json',
16
+ },
17
+ });
18
+ 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,10 +3,13 @@ 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;
8
11
  if (!unit)
9
- return price.estimatedSingleUnitPrice * quantity;
12
+ return (unitPrice * quantity) || null;
10
13
  let convertedUnit;
11
14
  try {
12
15
  convertedUnit = (0, ingredient_unit_converter_1.converter)(quantity, unit);
@@ -15,9 +18,9 @@ const calculatePrice = (quantity, unit, price) => {
15
18
  return null;
16
19
  }
17
20
  if (!convertedUnit || 'error' in convertedUnit)
18
- return price.estimatedSingleUnitPrice * quantity;
21
+ return (unitPrice * quantity) || null;
19
22
  const convertedGrams = Number(convertedUnit.quantity);
20
- const total = convertedGrams * price.estimatedGramPrice;
23
+ const total = convertedGrams * gramPrice;
21
24
  return Math.ceil(total * 100) / 100;
22
25
  };
23
26
  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.11",
4
4
  "description": "Parses given sentence including ingredient information and attempts to return quantity, measurement and ingredient data",
5
5
  "keywords": [
6
6
  "recipe",