@jclind/ingredient-parser 1.0.0 → 1.0.2

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.
Files changed (2) hide show
  1. package/index.js +33 -22
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  const { mongoHttp, spoonacularHttp } = require('./http-common.ts')
2
- const dotenv = require('dotenv')
3
- dotenv.config()
4
2
  const { parse } = require('recipe-ingredient-parser-v3')
5
3
 
6
- const ingredientParser = async ingrString => {
4
+ const ingredientParser = async (ingrString, spoonacularAPIKey) => {
7
5
  const parsedIngr = parse(ingrString, 'eng')
8
6
  const formattedIngr = parsedIngr.ingredient
9
7
  .trim()
@@ -13,25 +11,23 @@ const ingredientParser = async ingrString => {
13
11
  .replace(/^(fl )/, '')
14
12
  .replace(/^(oz )/, '')
15
13
  .trim()
16
- console.log(formattedIngr)
17
- console.log(parsedIngr)
18
14
  if (formattedIngr) {
19
- const ingrData = await getIngredientInfo(formattedIngr)
15
+ const ingrData = await getIngredientInfo(formattedIngr, spoonacularAPIKey)
20
16
  return ingrData
21
17
  } else {
22
18
  return null
23
19
  }
24
20
  }
25
21
 
26
- async function getIngredientInfo(ingrName) {
22
+ async function getIngredientInfo(ingrName, spoonacularAPIKey) {
27
23
  const ingrNameLower = ingrName.toLowerCase()
28
- console.log(ingrNameLower)
29
24
  if (!ingrNameLower) return
30
25
  const mongoIngrData = await checkIngredient(ingrNameLower)
31
- console.log(mongoIngrData)
32
26
  if (!mongoIngrData.data) {
33
- const ingrData = await getSpoonacularIngrData(ingrNameLower)
34
- console.log(ingrData, 'test')
27
+ const ingrData = await getSpoonacularIngrData(
28
+ ingrNameLower,
29
+ spoonacularAPIKey
30
+ )
35
31
  return ingrData
36
32
  } else {
37
33
  return mongoIngrData.data
@@ -41,29 +37,44 @@ async function getIngredientInfo(ingrName) {
41
37
  async function checkIngredient(name) {
42
38
  return await mongoHttp.get(`checkIngredient?name=${name}`)
43
39
  }
44
- async function getSpoonacularIngrData(name) {
45
- // console.log(process.env.SPOONACULAR_API_KEY)
46
- const searchedIngr = await spoonacularHttp.get(
47
- `search?query=${name}&number=1&apiKey=${process.env.SPOONACULAR_API_KEY}`
48
- )
49
- console.log(searchedIngr, 'test2')
40
+ async function getSpoonacularIngrData(name, spoonacularAPIKey) {
41
+ let searchedIngr = null
42
+ try {
43
+ searchedIngr = await spoonacularHttp.get(
44
+ `search?query=${name}&number=1&apiKey=${spoonacularAPIKey}`
45
+ )
46
+ } catch (error) {
47
+ const res = error.response.data
48
+ if (res.code === 401) {
49
+ return { errorMsg: 'API Key Not Valid', error }
50
+ } else {
51
+ return { errorMsg: 'Error Occurred, Please Try Again', error }
52
+ }
53
+ }
50
54
 
51
55
  if (
52
56
  !searchedIngr.data ||
53
57
  !searchedIngr.data.results ||
54
58
  searchedIngr.data.results.length <= 0
55
59
  ) {
56
- // console.log('no searchedIngr data')
57
60
  return null
58
61
  }
59
62
 
60
63
  const ingrId = searchedIngr.data.results[0].id
61
64
 
62
- const ingrData = await spoonacularHttp.get(
63
- `${ingrId}/information?amount=1&apiKey=${process.env.SPOONACULAR_API_KEY}`
64
- )
65
+ let ingrData = null
66
+ try {
67
+ ingrData = await spoonacularHttp.get(
68
+ `${ingrId}/information?amount=1&apiKey=${process.env.SPOONACULAR_API_KEY}`
69
+ )
70
+ } catch (error) {
71
+ if (res.code === 401) {
72
+ return { errorMsg: 'API Key Not Valid', error }
73
+ } else {
74
+ return { errorMsg: 'Error Occurred, Please Try Again', error }
75
+ }
76
+ }
65
77
  const mongoDBIngrData = { ...ingrData.data, name }
66
- // console.log(mongoDBIngrData)
67
78
  setMongoDBIngrData(mongoDBIngrData)
68
79
  return mongoDBIngrData
69
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jclind/ingredient-parser",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Parses given sentence including ingredient information and attemps to return quantity, measurement and ingredient",
5
5
  "main": "index.js",
6
6
  "scripts": {