-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
105 lines (98 loc) · 2.65 KB
/
helpers.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
const fetch = require("node-fetch")
/**
*
* @param {object} query
* @param {object} queriesMap
* @return {boolean}
* @description Determines whether or not a query object contains all the required fields
* associated with it's query type
*/
const hasRequiredFields = (query, queriesMap) => {
if (!query.params || !Object.keys(query.params).length) {
return false
}
const queryParamsKeys = Object.keys(query.params)
const requiredKeys = queriesMap[query.type].requiredKeys
return requiredKeys.every(
requiredField =>
queryParamsKeys.includes(requiredField) && !!query.params[requiredField]
)
}
/**
*
* @param {string} queryType
* @param {array} queryParams
* @param {object} queriesMap
* @param {string} yahooFinanceBaseURL
* @return {string}
* @description Generates a URL with query parameters
*/
const getQueryURLWithParams = (
queryType,
queryParams,
queriesMap,
yahooFinanceBaseURL
) => {
const querySlug = queriesMap[queryType].slug
const queryURL = new URL(`${yahooFinanceBaseURL}/${querySlug}`)
Object.entries(queryParams).forEach(([key, value]) =>
queryURL.searchParams.append(key, value)
)
return queryURL.href
}
/**
*
* @param {string} key
* @param {object} query
* @param {object} queriesMap
* @param {string} yahooFinanceAPIHost
* @param {string} yahooFinanceBaseURL
* @return {Promise}
* @description Fetches query data after validating that all required query parameters are set
*/
exports.executeQuery = async (
key,
query,
queriesMap,
yahooFinanceAPIHost,
yahooFinanceBaseURL
) => {
/* Make sure that a query with the given query type exists in the queriesMap */
if (!queriesMap[query.type]) {
return Promise.reject(
new Error(
`[gatsby-yahoo-finance-api] ${query.type} is not a supported query.`
)
)
}
/* Validate required fields */
if (!hasRequiredFields(query, queriesMap)) {
return Promise.reject(
new Error(
`[gatsby-yahoo-finance-api] Some or all required fields are missing for ${query.type} query.`
)
)
}
const apiEndpoint = getQueryURLWithParams(
query.type,
query.params,
queriesMap,
yahooFinanceBaseURL
)
try {
const response = await fetch(apiEndpoint, {
headers: {
"x-rapidapi-host": yahooFinanceAPIHost,
"x-rapidapi-key": key,
},
})
if (!response.ok) {
const errorMsg = `[gatsby-yahoo-finance-api] ${query.type} request failed with message: ${response.statusText}`
return Promise.reject(new Error(errorMsg))
}
const data = await response.json()
return { ...data, queryType: query.type }
} catch (error) {
return Promise.reject(error)
}
}