@jclind/ingredient-parser 1.2.13 → 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
CHANGED
|
@@ -5,15 +5,22 @@ 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
|
},
|
|
14
21
|
timeout: 8000,
|
|
15
22
|
});
|
|
16
|
-
const createIngredientServerHttp = (serverUrl) =>
|
|
23
|
+
const createIngredientServerHttp = (serverUrl) => axios.create({
|
|
17
24
|
baseURL: serverUrl || DEFAULT_SERVER_URL,
|
|
18
25
|
headers: {
|
|
19
26
|
'Content-type': 'application/json',
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { IngredientData } from '../../types.js';
|
|
2
|
-
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;
|
|
3
4
|
export declare const searchIngredient: (name: string, spoonacularAPIKey: string) => Promise<import("axios").AxiosResponse<any, any, {}>>;
|
|
4
5
|
export declare const getIngredientInformation: (ingrId: number, unit: boolean, spoonacularAPIKey: string) => Promise<import("axios").AxiosResponse<any, any, {}>>;
|
package/dist/src/api/requests.js
CHANGED
|
@@ -8,16 +8,13 @@ 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
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.getIngredientInformation = exports.searchIngredient = exports.
|
|
16
|
-
const axios_1 =
|
|
12
|
+
exports.getIngredientInformation = exports.searchIngredient = exports.saveIngredientToServer = exports.getIngredientFromServer = void 0;
|
|
13
|
+
const axios_1 = require("axios");
|
|
17
14
|
const http_js_1 = require("./http.js");
|
|
18
15
|
function handleRequestError(error) {
|
|
19
16
|
var _a, _b, _c;
|
|
20
|
-
if (axios_1.
|
|
17
|
+
if ((0, axios_1.isAxiosError)(error)) {
|
|
21
18
|
const status = (_a = error.response) === null || _a === void 0 ? void 0 : _a.status;
|
|
22
19
|
const code = (_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.code;
|
|
23
20
|
if (status === 401 || code === 401)
|
|
@@ -30,21 +27,28 @@ function handleRequestError(error) {
|
|
|
30
27
|
}
|
|
31
28
|
throw new Error('Network error, please try again');
|
|
32
29
|
}
|
|
33
|
-
const
|
|
30
|
+
const getIngredientFromServer = (name, serverUrl) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
31
|
var _a;
|
|
35
32
|
try {
|
|
36
33
|
const serverHttp = (0, http_js_1.createIngredientServerHttp)(serverUrl);
|
|
37
|
-
const response = yield serverHttp.
|
|
38
|
-
ingredientString,
|
|
39
|
-
spoonacularApiKey,
|
|
40
|
-
});
|
|
34
|
+
const response = yield serverHttp.get(`/ingredient/${encodeURIComponent(name)}`);
|
|
41
35
|
return (_a = response.data.data) !== null && _a !== void 0 ? _a : null;
|
|
42
36
|
}
|
|
43
|
-
catch (
|
|
44
|
-
|
|
37
|
+
catch (_b) {
|
|
38
|
+
// Treat any server error as a cache miss and fall through to Spoonacular
|
|
39
|
+
return null;
|
|
45
40
|
}
|
|
46
41
|
});
|
|
47
|
-
exports.
|
|
42
|
+
exports.getIngredientFromServer = getIngredientFromServer;
|
|
43
|
+
const saveIngredientToServer = (name, ingredientData, serverUrl) => {
|
|
44
|
+
const serverHttp = (0, http_js_1.createIngredientServerHttp)(serverUrl);
|
|
45
|
+
serverHttp
|
|
46
|
+
.post('/ingredient', { name, ingredientData })
|
|
47
|
+
.catch(() => {
|
|
48
|
+
// Fire-and-forget — a failed write doesn't affect the caller
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
exports.saveIngredientToServer = saveIngredientToServer;
|
|
48
52
|
const searchIngredient = (name, spoonacularAPIKey) => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
53
|
try {
|
|
50
54
|
return yield http_js_1.spoonacularHttp.get(`search?query=${name}&number=1&apiKey=${spoonacularAPIKey}`);
|
|
@@ -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
|
}
|
package/package.json
CHANGED