forked from oceanprotocol-archive/github-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·84 lines (71 loc) · 2.1 KB
/
index.js
File metadata and controls
executable file
·84 lines (71 loc) · 2.1 KB
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
/* eslint-disable no-console */
const fetch = require('node-fetch')
const chalk = require('chalk')
const ms = require('ms')
let cache = null
const log = text => console.log(text)
const logError = text => console.log(chalk.bold.red(text))
// Request options for all fetch calls
const options = {
headers: {
// For getting topics, see note on https://developer.github.com/v3/search/
// Accept: 'application/vnd.github.mercy-preview+json'
Accept: 'application/vnd.github.preview'
}
}
//
// Fetch all public GitHub repos
//
const fetchRepos = async () => {
const url = 'https://api.github.com/orgs/oceanprotocol/repos?type=public&per_page=100'
const start = Date.now()
const response = await fetch(url, options)
if (response.status !== 200) {
logError(`Non-200 response code from GitHub: ${response.status}`)
return null
}
const json = await response.json()
/* eslint-disable camelcase */
const dataRepos = await json
.map(({
name,
description,
html_url,
stargazers_count,
forks_count,
fork,
archived,
topics
}) => ({
name,
description,
url: html_url,
stars: stargazers_count,
forks: forks_count,
isArchived: archived,
isFork: fork,
topics
})).sort((p1, p2) => p2.stars - p1.stars)
/* eslint-enable camelcase */
log(
'Re-built projects cache. ' +
`Total: ${dataRepos.length} public Ocean Protocol projects. ` +
`Elapsed: ${new Date() - start}ms`
)
return {
data: dataRepos,
lastUpdate: Date.now()
}
}
//
// Create the response
//
module.exports = async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET')
if (!cache || Date.now() - cache.lastUpdate > ms('5m')) {
// eslint-disable-next-line require-atomic-updates
cache = await fetchRepos()
}
res.end(JSON.stringify(cache.data))
}