-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheApi.js
103 lines (90 loc) · 2.35 KB
/
cacheApi.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
/* eslint-disable no-console */
/**
* We run this script on every deploy to update /src/config/apiCache.json
* which contains the cached data for the following resources:
* - platforms (platforms, current api version)
* - qn_targets
* - stats-about
*/
const fs = require('fs')
const fetch = require('isomorphic-unfetch')
const version = process.env.VERSION || '0.0.0'
const apiVersion = process.env.API_VERSION || 'v1'
const apiPath = `${
process.env.APT_PATH || 'https://api.refine.bio'
}/${apiVersion}`
const cacheApi = async () => {
console.log(`Fetching data to be cached from ${apiPath}`)
const [{ platforms, xSourceRevision }, qnTargets, statsAbout] =
await Promise.all([getPlatforms(), getQnTargets(), getStatsAbout()])
const cache = JSON.stringify(
{
version,
xSourceRevision,
platforms,
qnTargets,
statsAbout
},
null,
2
)
fs.writeFileSync(`src/config/apiCache.json`, cache)
}
const getPlatforms = async () => {
const url = `${apiPath}/platforms`
try {
const readableStream = await fetch(url)
const response = await readableStream.json()
return {
platforms: response.reduce(
(
accum,
{ platform_name: name, platform_accession_code: accessionCode }
) => ({
...accum,
[accessionCode]: name
}),
{}
),
xSourceRevision: readableStream.headers.get('x-source-revision').slice(1)
}
} catch (e) {
console.log('Error fetching platforms')
return {}
}
}
const getQnTargets = async () => {
const url = `${apiPath}/qn_targets`
try {
const readableStream = await fetch(url)
const response = await readableStream.json()
return response.reduce(
(accum, { name: organismName }) => ({
...accum,
[organismName]: true
}),
{}
)
} catch {
console.log('Error fetching qn targets')
return false
}
}
const getStatsAbout = async () => {
const url = `${apiPath}/stats-about/`
const defautStats = {
// a copy of the latest stats-about via API
samples_available: 1359087,
total_size_in_bytes: 838823107099551,
supported_organisms: 203,
experiments_processed: 46324
}
try {
const readableStream = fetch(url)
const response = await readableStream.json()
return response
} catch {
return defautStats
}
}
cacheApi()