-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.ts
147 lines (120 loc) · 4 KB
/
server.ts
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
137
138
139
140
141
142
143
144
145
146
147
const { head, tail } = require("lodash");
const express = require('express');
// @ts-ignore false redeclare warning
const path = require('path');
const app = express();
require('dotenv').config()
// @ts-ignore false redeclare warning
const fetch = require('node-fetch');
const compression = require('compression');
const logger = require('pino')({
prettyPrint: true,
messageFormat: '☰nuz: {levelLabel} - {pid} - url:{request.url}'
});
const cors = require('cors');
// const { Client, Pool } = require('pg');
// const pool = new Pool();
// async function getStuff (limit, offset = 0) {
// const res = await pool.query('SELECT * FROM release_notes ORDER BY version DESC LIMIT $1 OFFSET $2', [limit, offset]);
// console.log(res);
// //await pool.end();
// return res.rows;
// }
const isLocal = process.env.NODE_ENV === 'local';
let middleware, compiler;
if (isLocal) {
const Webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const webpackConfig = require('./webpack.config');
compiler = Webpack(webpackConfig);
middleware = require('webpack-dev-middleware');
}
const GH_URL = 'https://api.github.com/repos/EmmaRamirez/nuzlocke-generator';
const GH_ACCESS_TOKEN = process.env.GH_ACCESS_TOKEN;
const productionFlag = process.env.NODE_ENV === 'production';
app.use(express.json({ limit: '50mb' }));
app.use(cors());
app.use(compression());
if (isLocal && middleware && compiler) {
logger.info(`Running server in development mode.`);
app.use(
middleware(compiler, {})
);
} else {
logger.info(`Running server in production mode.`);
}
interface ReportArgs {
title?: string;
report?: string;
data?: string;
}
const PORT = process.env.PORT || 8080;
app.get('/', async (req, res, next) => {
app.use(express.static(path.join(__dirname, 'dist')))
next();
});
app.post('/report', async (req, res, next) => {
const { report, title, data } = req.body as ReportArgs;
logger.info(report, title, data);
if (!title) next(new Error('Missing report title.'));
const githubCall = await fetch(`${GH_URL}/issues`, {
method: 'POST',
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Token ${process.env.GH_ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
mode: 'cors',
body: JSON.stringify({
title: title,
body: `${report}
\`\`\`json
${data ? data : 'User chose not to attach nuzlocke.json'}
\`\`\`
`,
assigness: ['EmmaRamirez'],
labels: ['User Submitted', 'Type: Bug'],
})
});
if (githubCall?.status?.toString()[0] === '2') {
logger.info(`Successfully called Github`);
}
res.send({ status: githubCall.status });
next();
});
app.get('/release/:type', async (req, res, next) => {
const type = req.params.type; /* latest, all, or version tags */
const releases = await fetch(`${GH_URL}/releases`, {
method: 'GET',
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Token ${process.env.GH_ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
mode: 'cors',
}).then(res => res.json()).then(res => res?.map(rel => ({ id: rel.id, url: rel.html_url, version: rel.tag_name, note: rel.body, timestamp: rel.published_at })))
if (type === 'latest') {
const notes = head(releases);
res.send({ status: 200, payload: { notes: [notes] } });
} else if (type === 'all') {
const notes = tail(releases);
res.send({ status: 200, payload: { notes, } });
} else {
logger.error(`Invalid release type param`);
res.send({ status: 400, error: `Invalid release type param`});
}
next();
});
app.get('/nuzlocke/:id', async (req, res, next) => {
logger.info('Retrieving nuzlocke ', req.params.id);
res.send({ status: 200 });
next();
});
app.post('/nuzlocke', async (req, res, next) => {
});
app.get('/nuzlockes', async (req, res, next) => {
});
app.listen(PORT, () => {
logger.info(`Current environment: ${process.env.NODE_ENV}`);
logger.info(`Running server on http://localhost:${PORT} 🚀`);
});