@jclind/ingredient-parser 1.2.12 → 1.2.14
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/src/api/http.js +11 -2
- package/dist/src/api/requests.d.ts +4 -3
- package/dist/src/api/requests.js +40 -28
- package/dist/src/funcs/getIngredientInfo.d.ts +1 -1
- package/dist/src/funcs/getIngredientInfo.js +10 -7
- package/dist/src/funcs/getSpoonacularIngrData.js +4 -6
- package/package.json +1 -1
package/dist/src/api/http.js
CHANGED
|
@@ -5,17 +5,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.createIngredientServerHttp = exports.spoonacularHttp = void 0;
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
// Webpack 5 can resolve require("axios") to the ESM entry without setting
|
|
9
|
+
// __esModule, causing __importDefault to double-wrap it. The result is that
|
|
10
|
+
// _axios is the module namespace object rather than the axios instance, so
|
|
11
|
+
// .create lands one level deeper than expected.
|
|
12
|
+
const axios = typeof axios_1.default.create === 'function'
|
|
13
|
+
? axios_1.default
|
|
14
|
+
: axios_1.default.default;
|
|
8
15
|
const DEFAULT_SERVER_URL = 'https://ingredient-parser-service-production-2635.up.railway.app';
|
|
9
|
-
exports.spoonacularHttp =
|
|
16
|
+
exports.spoonacularHttp = axios.create({
|
|
10
17
|
baseURL: 'https://api.spoonacular.com/food/ingredients/',
|
|
11
18
|
headers: {
|
|
12
19
|
'Content-type': 'application/json',
|
|
13
20
|
},
|
|
21
|
+
timeout: 8000,
|
|
14
22
|
});
|
|
15
|
-
const createIngredientServerHttp = (serverUrl) =>
|
|
23
|
+
const createIngredientServerHttp = (serverUrl) => axios.create({
|
|
16
24
|
baseURL: serverUrl || DEFAULT_SERVER_URL,
|
|
17
25
|
headers: {
|
|
18
26
|
'Content-type': 'application/json',
|
|
19
27
|
},
|
|
28
|
+
timeout: 8000,
|
|
20
29
|
});
|
|
21
30
|
exports.createIngredientServerHttp = createIngredientServerHttp;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IngredientData } from '../../types.js';
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
2
|
+
export declare const getIngredientFromServer: (name: string, serverUrl?: string) => Promise<IngredientData | null>;
|
|
3
|
+
export declare const saveIngredientToServer: (name: string, ingredientData: IngredientData, serverUrl?: string) => void;
|
|
4
|
+
export declare const searchIngredient: (name: string, spoonacularAPIKey: string) => Promise<import("axios").AxiosResponse<any, any, {}>>;
|
|
5
|
+
export declare const getIngredientInformation: (ingrId: number, unit: boolean, spoonacularAPIKey: string) => Promise<import("axios").AxiosResponse<any, any, {}>>;
|
package/dist/src/api/requests.js
CHANGED
|
@@ -9,49 +9,61 @@ 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 = exports.
|
|
12
|
+
exports.getIngredientInformation = exports.searchIngredient = exports.saveIngredientToServer = exports.getIngredientFromServer = void 0;
|
|
13
|
+
const axios_1 = require("axios");
|
|
13
14
|
const http_js_1 = require("./http.js");
|
|
14
|
-
|
|
15
|
+
function handleRequestError(error) {
|
|
16
|
+
var _a, _b, _c;
|
|
17
|
+
if ((0, axios_1.isAxiosError)(error)) {
|
|
18
|
+
const status = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
19
|
+
const code = (_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.code;
|
|
20
|
+
if (status === 401 || code === 401)
|
|
21
|
+
throw new Error('API Key Not Valid');
|
|
22
|
+
if (status === 429)
|
|
23
|
+
throw new Error('Rate limit exceeded, please try again later');
|
|
24
|
+
if (error.response)
|
|
25
|
+
throw new Error('Error Occurred, Please Try Again');
|
|
26
|
+
throw new Error('Network error, please try again');
|
|
27
|
+
}
|
|
28
|
+
throw new Error('Network error, please try again');
|
|
29
|
+
}
|
|
30
|
+
const getIngredientFromServer = (name, serverUrl) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
31
|
var _a;
|
|
32
|
+
try {
|
|
33
|
+
const serverHttp = (0, http_js_1.createIngredientServerHttp)(serverUrl);
|
|
34
|
+
const response = yield serverHttp.get(`/ingredient/${encodeURIComponent(name)}`);
|
|
35
|
+
return (_a = response.data.data) !== null && _a !== void 0 ? _a : null;
|
|
36
|
+
}
|
|
37
|
+
catch (_b) {
|
|
38
|
+
// Treat any server error as a cache miss and fall through to Spoonacular
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
exports.getIngredientFromServer = getIngredientFromServer;
|
|
43
|
+
const saveIngredientToServer = (name, ingredientData, serverUrl) => {
|
|
16
44
|
const serverHttp = (0, http_js_1.createIngredientServerHttp)(serverUrl);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
45
|
+
serverHttp
|
|
46
|
+
.post('/ingredient', { name, ingredientData })
|
|
47
|
+
.catch(() => {
|
|
48
|
+
// Fire-and-forget — a failed write doesn't affect the caller
|
|
20
49
|
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
exports.parseAndEnrich = parseAndEnrich;
|
|
50
|
+
};
|
|
51
|
+
exports.saveIngredientToServer = saveIngredientToServer;
|
|
24
52
|
const searchIngredient = (name, spoonacularAPIKey) => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
-
let searchedIngr;
|
|
26
53
|
try {
|
|
27
|
-
|
|
54
|
+
return yield http_js_1.spoonacularHttp.get(`search?query=${name}&number=1&apiKey=${spoonacularAPIKey}`);
|
|
28
55
|
}
|
|
29
56
|
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
|
-
}
|
|
57
|
+
handleRequestError(error);
|
|
37
58
|
}
|
|
38
|
-
return searchedIngr;
|
|
39
59
|
});
|
|
40
60
|
exports.searchIngredient = searchIngredient;
|
|
41
61
|
const getIngredientInformation = (ingrId, unit, spoonacularAPIKey) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
-
let ingrData;
|
|
43
62
|
try {
|
|
44
|
-
|
|
63
|
+
return yield http_js_1.spoonacularHttp.get(`${ingrId}/information?amount=1&${unit ? 'unit=grams&' : ''}apiKey=${spoonacularAPIKey}`);
|
|
45
64
|
}
|
|
46
65
|
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
|
-
}
|
|
66
|
+
handleRequestError(error);
|
|
54
67
|
}
|
|
55
|
-
return ingrData;
|
|
56
68
|
});
|
|
57
69
|
exports.getIngredientInformation = getIngredientInformation;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { IngredientData } from '../../types.js';
|
|
2
|
-
export declare function getIngredientInfo(
|
|
2
|
+
export declare function getIngredientInfo(ingredientName: string, spoonacularAPIKey: string, serverUrl?: string): Promise<IngredientData | null>;
|
|
@@ -11,13 +11,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.getIngredientInfo = getIngredientInfo;
|
|
13
13
|
const requests_js_1 = require("../api/requests.js");
|
|
14
|
-
|
|
14
|
+
const getSpoonacularIngrData_js_1 = require("./getSpoonacularIngrData.js");
|
|
15
|
+
function getIngredientInfo(ingredientName, spoonacularAPIKey, serverUrl) {
|
|
15
16
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
const cached = yield (0, requests_js_1.getIngredientFromServer)(ingredientName, serverUrl);
|
|
18
|
+
if (cached)
|
|
19
|
+
return cached;
|
|
20
|
+
const spoonacularData = yield (0, getSpoonacularIngrData_js_1.getSpoonacularIngrData)(ingredientName, spoonacularAPIKey);
|
|
21
|
+
if (!spoonacularData)
|
|
22
|
+
return null;
|
|
23
|
+
(0, requests_js_1.saveIngredientToServer)(ingredientName, spoonacularData, serverUrl);
|
|
24
|
+
return spoonacularData;
|
|
22
25
|
});
|
|
23
26
|
}
|
|
@@ -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