-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (110 loc) · 3.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict'
const fetch = require('isomorphic-unfetch')
const querystring = require('querystring')
class FoodDataCentral {
constructor (apiKey = 'DEMO_KEY') {
this.api_key = apiKey
this.basePath = 'https://api.nal.usda.gov/fdc'
}
request (endpoint = '', options = {}) {
const url = this.basePath + endpoint
const headers = {
'Content-type': 'application/json'
}
const config = {
...options,
...headers
}
return fetch(url, config).then(r => {
if (r.ok) {
return r.json()
}
throw new Error(r.statusText)
})
}
search (searchTerm, searchOptions = '') {
if (searchTerm === undefined) {
throw new Error("Search Term can't be empty. (Retrieve all foods by searching with '*')")
}
const options = {
query: searchTerm
}
if (searchOptions !== undefined) {
Object.assign(options, searchOptions)
}
const qs = options ? '&' + querystring.stringify(options) : ''
const url = `/v1/foods/search?api_key=${this.api_key}${qs}`
const config = {
method: 'GET'
}
return this.request(url, config)
}
details (fdcid, { format, nutrients } = {}) {
if (fdcid === undefined) {
throw new Error("FDC ID can't be empty.")
} else if (isNaN(fdcid) || String((Math.abs(fdcid))).length < 6) {
throw new Error('Wrong FDC ID format.')
}
const options = {}
if (format !== undefined) {
switch (format) {
case 1:
options.format = 'abridged'
break
case 2:
options.format = 'full'
break
default:
throw new Error("Possible Formats: '1' = abridged, '2' = full")
}
}
if (nutrients !== undefined) {
options.nutrients = nutrients
}
const qs = options ? '&' + querystring.stringify(options) : ''
const url = `/v1/food/${fdcid}?api_key=${this.api_key}${qs}`
const config = {
method: 'GET'
}
return this.request(url, config)
}
list (fdcIds, { format, nutrients } = {}) {
const isArrayValid = (currentValue) => {
if (typeof currentValue !== 'number' || String(Math.abs(currentValue)).length < 6) {
return false
} else {
return true
}
}
if (fdcIds === undefined) {
throw new Error("FDC IDs can't be empty. At least one FDC ID required.")
} else if (!Array.isArray(fdcIds) || fdcIds.length === 0 || !fdcIds.every(isArrayValid)) {
throw new Error('FDC IDs must be an array of at least one number.')
}
const options = {
fdcIds: fdcIds
}
if (format !== undefined) {
switch (format) {
case 1:
options.format = 'abridged'
break
case 2:
options.format = 'full'
break
default:
throw new Error("Possible Formats: '1' = abridged, '2' = full")
}
}
if (nutrients !== undefined) {
options.nutrients = nutrients
}
const qs = options ? '&' + querystring.stringify(options) : ''
const url = `/v1/foods/?api_key=${this.api_key}${qs}`
const config = {
method: 'GET'
}
return this.request(url, config)
}
}
module.exports = FoodDataCentral