@jclind/ingredient-parser 1.2.11 → 1.2.13
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; } });
|
package/dist/src/api/http.js
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
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
6
|
exports.createIngredientServerHttp = exports.spoonacularHttp = void 0;
|
|
4
|
-
const axios_1 = require("axios");
|
|
5
|
-
const DEFAULT_SERVER_URL = 'https://
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const DEFAULT_SERVER_URL = 'https://ingredient-parser-service-production-2635.up.railway.app';
|
|
6
9
|
exports.spoonacularHttp = axios_1.default.create({
|
|
7
10
|
baseURL: 'https://api.spoonacular.com/food/ingredients/',
|
|
8
11
|
headers: {
|
|
9
12
|
'Content-type': 'application/json',
|
|
10
13
|
},
|
|
14
|
+
timeout: 8000,
|
|
11
15
|
});
|
|
12
16
|
const createIngredientServerHttp = (serverUrl) => axios_1.default.create({
|
|
13
17
|
baseURL: serverUrl || DEFAULT_SERVER_URL,
|
|
14
18
|
headers: {
|
|
15
19
|
'Content-type': 'application/json',
|
|
16
20
|
},
|
|
21
|
+
timeout: 8000,
|
|
17
22
|
});
|
|
18
23
|
exports.createIngredientServerHttp = createIngredientServerHttp;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { IngredientData } from '../../types.js';
|
|
2
2
|
export declare const parseAndEnrich: (ingredientString: string, spoonacularApiKey: string, serverUrl?: string) => Promise<IngredientData | null>;
|
|
3
|
-
export declare const searchIngredient: (name: string, spoonacularAPIKey: string) => Promise<any
|
|
4
|
-
export declare const getIngredientInformation: (ingrId: number, unit: boolean, spoonacularAPIKey: string) => Promise<any
|
|
3
|
+
export declare const searchIngredient: (name: string, spoonacularAPIKey: string) => Promise<import("axios").AxiosResponse<any, any, {}>>;
|
|
4
|
+
export declare const getIngredientInformation: (ingrId: number, unit: boolean, spoonacularAPIKey: string) => Promise<import("axios").AxiosResponse<any, any, {}>>;
|
package/dist/src/api/requests.js
CHANGED
|
@@ -8,50 +8,58 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.getIngredientInformation = exports.searchIngredient = exports.parseAndEnrich = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
13
17
|
const http_js_1 = require("./http.js");
|
|
18
|
+
function handleRequestError(error) {
|
|
19
|
+
var _a, _b, _c;
|
|
20
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
21
|
+
const status = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
22
|
+
const code = (_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.code;
|
|
23
|
+
if (status === 401 || code === 401)
|
|
24
|
+
throw new Error('API Key Not Valid');
|
|
25
|
+
if (status === 429)
|
|
26
|
+
throw new Error('Rate limit exceeded, please try again later');
|
|
27
|
+
if (error.response)
|
|
28
|
+
throw new Error('Error Occurred, Please Try Again');
|
|
29
|
+
throw new Error('Network error, please try again');
|
|
30
|
+
}
|
|
31
|
+
throw new Error('Network error, please try again');
|
|
32
|
+
}
|
|
14
33
|
const parseAndEnrich = (ingredientString, spoonacularApiKey, serverUrl) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
34
|
var _a;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
35
|
+
try {
|
|
36
|
+
const serverHttp = (0, http_js_1.createIngredientServerHttp)(serverUrl);
|
|
37
|
+
const response = yield serverHttp.post('/parse', {
|
|
38
|
+
ingredientString,
|
|
39
|
+
spoonacularApiKey,
|
|
40
|
+
});
|
|
41
|
+
return (_a = response.data.data) !== null && _a !== void 0 ? _a : null;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
handleRequestError(error);
|
|
45
|
+
}
|
|
22
46
|
});
|
|
23
47
|
exports.parseAndEnrich = parseAndEnrich;
|
|
24
48
|
const searchIngredient = (name, spoonacularAPIKey) => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
-
let searchedIngr;
|
|
26
49
|
try {
|
|
27
|
-
|
|
50
|
+
return yield http_js_1.spoonacularHttp.get(`search?query=${name}&number=1&apiKey=${spoonacularAPIKey}`);
|
|
28
51
|
}
|
|
29
52
|
catch (error) {
|
|
30
|
-
|
|
31
|
-
if (res.code === 401) {
|
|
32
|
-
throw new Error('API Key Not Valid');
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
throw new Error('Error Occurred, Please Try Again');
|
|
36
|
-
}
|
|
53
|
+
handleRequestError(error);
|
|
37
54
|
}
|
|
38
|
-
return searchedIngr;
|
|
39
55
|
});
|
|
40
56
|
exports.searchIngredient = searchIngredient;
|
|
41
57
|
const getIngredientInformation = (ingrId, unit, spoonacularAPIKey) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
-
let ingrData;
|
|
43
58
|
try {
|
|
44
|
-
|
|
59
|
+
return yield http_js_1.spoonacularHttp.get(`${ingrId}/information?amount=1&${unit ? 'unit=grams&' : ''}apiKey=${spoonacularAPIKey}`);
|
|
45
60
|
}
|
|
46
61
|
catch (error) {
|
|
47
|
-
|
|
48
|
-
if (res.code === 401) {
|
|
49
|
-
throw new Error('API Key Not Valid');
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
throw new Error('Error Occurred, Please Try Again');
|
|
53
|
-
}
|
|
62
|
+
handleRequestError(error);
|
|
54
63
|
}
|
|
55
|
-
return ingrData;
|
|
56
64
|
});
|
|
57
65
|
exports.getIngredientInformation = getIngredientInformation;
|
|
@@ -8,6 +8,8 @@ const calculatePrice = (quantity, unit, price) => {
|
|
|
8
8
|
const unitPrice = (_b = price.estimatedSingleUnitPrice) !== null && _b !== void 0 ? _b : 0;
|
|
9
9
|
if (quantity === null)
|
|
10
10
|
return unitPrice || null;
|
|
11
|
+
if (quantity === 0)
|
|
12
|
+
return 0;
|
|
11
13
|
if (!unit)
|
|
12
14
|
return (unitPrice * quantity) || null;
|
|
13
15
|
let convertedUnit;
|
|
@@ -15,7 +17,7 @@ const calculatePrice = (quantity, unit, price) => {
|
|
|
15
17
|
convertedUnit = (0, ingredient_unit_converter_1.converter)(quantity, unit);
|
|
16
18
|
}
|
|
17
19
|
catch (error) {
|
|
18
|
-
return null;
|
|
20
|
+
return (unitPrice * quantity) || null;
|
|
19
21
|
}
|
|
20
22
|
if (!convertedUnit || 'error' in convertedUnit)
|
|
21
23
|
return (unitPrice * quantity) || null;
|
|
@@ -15,15 +15,13 @@ function getSpoonacularIngrData(name, spoonacularAPIKey) {
|
|
|
15
15
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
16
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
17
17
|
const searchedIngr = yield (0, requests_js_1.searchIngredient)(name, spoonacularAPIKey);
|
|
18
|
-
if (searchedIngr.error)
|
|
19
|
-
return searchedIngr;
|
|
20
18
|
const ingrId = (_c = (_b = (_a = searchedIngr === null || searchedIngr === void 0 ? void 0 : searchedIngr.data) === null || _a === void 0 ? void 0 : _a.results[0]) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : null;
|
|
21
19
|
if (!ingrId)
|
|
22
20
|
throw new Error(`No Data Found, unknown ingredient: ${name}`);
|
|
23
|
-
const ingrDataGram = yield
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
const [ingrDataGram, ingrDataSingleUnit] = yield Promise.all([
|
|
22
|
+
(0, requests_js_1.getIngredientInformation)(ingrId, true, spoonacularAPIKey),
|
|
23
|
+
(0, requests_js_1.getIngredientInformation)(ingrId, false, spoonacularAPIKey),
|
|
24
|
+
]);
|
|
27
25
|
const estimatedGramPrice = (_e = (_d = ingrDataGram.data.estimatedCost) === null || _d === void 0 ? void 0 : _d.value) !== null && _e !== void 0 ? _e : 0;
|
|
28
26
|
const estimatedSingleUnitPrice = (_g = (_f = ingrDataSingleUnit.data.estimatedCost) === null || _f === void 0 ? void 0 : _f.value) !== null && _g !== void 0 ? _g : 0;
|
|
29
27
|
const ingrData = Object.assign(Object.assign({}, ingrDataGram.data), { name, ingredientId: ingrId, estimatedPrices: { estimatedGramPrice, estimatedSingleUnitPrice } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jclind/ingredient-parser",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.13",
|
|
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
|
|
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
|
},
|