Skip to content

Commit 452d4d1

Browse files
committed
Cleaning up
1 parent a5afc71 commit 452d4d1

File tree

8 files changed

+17
-206
lines changed

8 files changed

+17
-206
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
coverage/
12
dist/
23
node_modules/
34
.env

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"scripts": {
1414
"build": "yarn clean && yarn tsc",
15-
"clean": "rimraf -rf ./dist",
15+
"clean": "rimraf -rf ./coverage ./dist",
1616
"dev": "nodemon --ext '*' --watch src --exec 'ts-node src/server.ts'",
1717
"lint": "yarn lint:eslint",
1818
"lint:eslint": "eslint . --ext .js,.ts",
@@ -21,13 +21,12 @@
2121
"pretty-quick": "pretty-quick --staged --pattern \"**/*.*(js|ts|json|md)\"",
2222
"start": "node dist/src/server.js",
2323
"test": "jest",
24+
"test:coverage": "yarn test --coverage",
2425
"test:watch": "yarn test --watch"
2526
},
2627
"dependencies": {
2728
"dotenv": "^16.0.1",
2829
"express": "^4.18.1",
29-
"fastify": "^4.3.0",
30-
"fastify-plugin": "^4.1.0",
3130
"pino": "^8.4.0",
3231
"pino-http": "^8.2.0",
3332
"ts-pattern": "^4.0.5",

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import express, { Express, Request, Response } from 'express';
22
import { validate } from './validation';
33

4-
export const build = () => {
4+
export const build = (): Express => {
55
const app: Express = express();
66
app.use(express.json());
77

src/server.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
import express, { Express, Request, Response } from 'express';
1+
import { Express } from 'express';
22
import pinoHTTP from 'pino-http';
33
import dotenv from 'dotenv';
4+
import { build } from './app';
45
import { logger } from './logger';
5-
import { validate } from './validation';
66

77
dotenv.config();
88

9-
const app: Express = express();
9+
const app: Express = build();
1010
app.use(pinoHTTP()); // Logging
1111

12-
app.get('/', async (req: Request, res: Response) => {
13-
res.type('application/json').status(200).send({ status: 'alive' });
14-
});
15-
16-
app.post('/validate', async (req: Request, res: Response) => {
17-
const { config, version } = req.body;
18-
try {
19-
const { valid, errors } = validate(version, config);
20-
const status = valid ? 200 : 422;
21-
res.type('application/json').status(status).send({ valid, errors });
22-
} catch (e) {
23-
res.type('application/json').status(400).send({ error: 'Invalid request' });
24-
}
25-
});
26-
2712
const PORT = Number(process.env.PORT) || 8000;
2813
app.listen(PORT, () => {
2914
logger.info(`Listening on port:${PORT}`);

src/validation.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ describe.each(SUPPORTED_VERSIONS)('validates v%s configs', (version) => {
88
expect(validation.validate(version, config)).toEqual({ valid: true, errors: [] });
99
});
1010

11+
it('throws an error if the version is not supported', () => {
12+
expect(() => validation.validate('0.6', config)).toThrowError('Unsupported version:0.6');
13+
});
14+
1115
it('validates an invalid config', () => {
1216
const invalid = JSON.parse(JSON.stringify(config));
1317
delete invalid.ois;

src/validation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const getVersionValidator = (semverStr: string) => {
2222
throw new Error(`Unsupported version:${semverStr}`);
2323
}
2424

25+
// TODO: investigate how/if this can be better done with types alone
26+
// and dynamic pattern matching
2527
return match(semver)
2628
.with({ major: 0, minor: 7 }, () => validator07.parseConfig)
2729
.otherwise(() => {

test/helpers/fastify.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)