-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (94 loc) · 3.61 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
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLList } = require('graphql');
const GitHub = require('./github');
const PORT = process.env.PORT || 3000;
const ReleaseType = require('./routes/releases');
const TagType = require('./routes/tags');
const WatchersType = require('./routes/watchers');
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
releases: {
type: new GraphQLList(ReleaseType),
resolve: async () => {
return await GitHub.releases().then(data => {
if (Reflect.ownKeys(data).includes('message') && Reflect.ownKeys(data).length == 2) {
console.log('API rate limit');
return [{ message: 'API rate limit' }];
} else {
let resourceReleases = [];
let count = 0;
for (const release of data) {
if (resourceReleases[count] == undefined) resourceReleases[count] = {};
if (resourceReleases[count]['assets'] == undefined) resourceReleases[count]['assets'] = [];
for (const property of Reflect.ownKeys(release)) {
if (['name', 'tag_name', 'created_at', 'published_at', 'prerelease'].includes(property)) resourceReleases[count][property] = release[property];
if (property == 'assets') {
for (let index = 0; index < release['assets'].length; index++) {
const archive = release['assets'][index];
resourceReleases[count]['assets'][index] = {
name: archive['name'],
download_count: archive['download_count'],
created_at: archive['created_at'],
updated_at: archive['updated_at'],
browser_download_url: archive['browser_download_url']
};
}
}
}
count++;
}
return resourceReleases;
}
});
}
},
tags: {
type: new GraphQLList(TagType),
resolve: async () => {
return await GitHub.tags().then((data) => {
let resource = [];
let count = 0;
if (Reflect.ownKeys(data).includes('message') && Reflect.ownKeys(data).length == 2) {
return [{ message: 'API rate limit' }];
} else {
for (const tag of data) {
for (const property of Reflect.ownKeys(tag)) {
if (['name', 'zipball_url', 'tarball_url'].includes(property)) {
if (resource[count] == undefined) resource[count] = {};
resource[count][property] = tag[property];
}
}
count++;
}
return resource;
}
});
}
},
watchers: {
type: new GraphQLList(WatchersType),
resolve: async () => {
return await GitHub.stargazers().then(data => {
if (Reflect.ownKeys(data).includes('message') && Reflect.ownKeys(data).length == 2) {
return [{ message: 'API rate limit' }];
} else {
return [{ count: data?.length || 0 }];
}
});
}
}
}
});
const schema = new GraphQLSchema({
query: QueryType
});
const app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true
}));
app.listen(PORT, () => {
console.log(` Server reasy on: http://localhost:${PORT}`);
});