Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1750f64

Browse files
author
Matija Petrunic
committedFeb 18, 2021
Enforce strict prettier rules
1 parent a4da18d commit 1750f64

26 files changed

+334
-233
lines changed
 

‎.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
"prettier"
1919
],
2020
"rules": {
21+
"prettier/prettier": ["error", {}, { usePrettierrc: true }],
2122
"@typescript-eslint/no-require-imports": "error",
2223
"@typescript-eslint/no-unused-vars": ["error", {
2324
"varsIgnorePattern": "^_"

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"cross-env": "^7.0.3",
7474
"dotenv": "^8.2.0",
7575
"env-prompt": "^1.2.3",
76-
"eslint": "^7.18.0",
76+
"eslint": "^7.20.0",
7777
"eslint-config-prettier": "^7.2.0",
7878
"eslint-plugin-import": "^2.22.1",
7979
"eslint-plugin-prettier": "^3.3.1",

‎src/App.ts

+43-28
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import fastify, {FastifyInstance} from "fastify";
1+
import fastify, { FastifyInstance } from "fastify";
22
import fastifyCompress from "fastify-compress";
33
import fastifyCors from "fastify-cors";
44
import fastifyEnv from "fastify-env";
55
import fastifyFormBody from "fastify-formbody";
66
import fastifyHealthCheck from "fastify-healthcheck";
7-
import {fastifyHelmet} from "fastify-helmet";
7+
import { fastifyHelmet } from "fastify-helmet";
88
import fastifyMetrics from "fastify-metrics";
99
import fastifyRateLimit from "fastify-rate-limit";
1010
import fastifySensible from "fastify-sensible";
1111
import fastifySwagger from "fastify-swagger";
12-
import {Connection} from "typeorm";
12+
import { Connection } from "typeorm";
1313

14-
import {config as envPluginConfig} from "./config";
15-
import {getDatabaseConnection} from "./services/db";
16-
import {logger} from "./services/logger";
17-
import {fastifyLogger} from "./services/logger/fastify";
18-
import {routesPlugin} from "./services/plugins/routes";
19-
import {SWAGGER_CONFIG} from "./services/swagger";
14+
import { config as envPluginConfig } from "./config";
15+
import { getDatabaseConnection } from "./services/db";
16+
import { logger } from "./services/logger";
17+
import { fastifyLogger } from "./services/logger/fastify";
18+
import { routesPlugin } from "./services/plugins/routes";
19+
import { SWAGGER_CONFIG } from "./services/swagger";
2020
export class App {
21-
2221
public readonly instance: FastifyInstance;
2322

2423
protected constructor(instance: FastifyInstance) {
@@ -32,7 +31,7 @@ export class App {
3231
public static async init(): Promise<App> {
3332
const instance = fastify({
3433
logger: fastifyLogger,
35-
return503OnClosing: true
34+
return503OnClosing: true,
3635
});
3736
const app = new App(instance);
3837
await app.registerPlugins();
@@ -41,36 +40,44 @@ export class App {
4140

4241
public async start(): Promise<void> {
4342
try {
44-
4543
await this.initDb();
4644
await this.instance.ready();
4745
logger.info(this.instance.printRoutes());
4846
return new Promise((resolve, reject) => {
4947
this.instance.listen(
5048
this.instance.config.SERVER_PORT,
51-
this.instance.config.SERVER_ADDRESS, (err) => {
49+
this.instance.config.SERVER_ADDRESS,
50+
(err) => {
5251
if (err) {
5352
logger.error("Failed to start server: ", err);
5453
reject();
5554
}
5655
resolve();
57-
});
56+
}
57+
);
5858
});
5959
} catch (error) {
60-
logger.error(`Error occurred during app startup because of: ${error.stack}`);
60+
logger.error(
61+
`Error occurred during app startup because of: ${error.stack}`
62+
);
6163
this.stop(undefined);
6264
}
6365
}
6466

6567
public async stop(signal: string | undefined): Promise<void> {
66-
await this.instance.db.close()
67-
.catch(error =>
68-
logger.error(`Error occurred during database closing because: ${error.message}`)
68+
await this.instance.db
69+
.close()
70+
.catch((error) =>
71+
logger.error(
72+
`Error occurred during database closing because: ${error.message}`
73+
)
6974
);
7075
try {
7176
await this.instance.close();
7277
} catch (e) {
73-
logger.error(`Error occurred during server closing because: ${e.message}`);
78+
logger.error(
79+
`Error occurred during server closing because: ${e.message}`
80+
);
7481
}
7582

7683
if (signal !== "TEST") {
@@ -80,17 +87,25 @@ export class App {
8087

8188
private async initDb(): Promise<void> {
8289
this.instance.decorate("db", await getDatabaseConnection());
83-
await this.instance.db.runMigrations({transaction: "all"});
90+
await this.instance.db.runMigrations({ transaction: "all" });
8491
}
8592

8693
private async registerPlugins(): Promise<void> {
8794
this.instance.register(fastifyEnv, envPluginConfig);
8895
await this.instance.after();
89-
this.instance.register(fastifyCompress, {global: true, encodings: ["gzip", "deflate"]});
90-
this.instance.register(fastifyCors, {origin: this.instance.config.CORS_ORIGIN});
96+
this.instance.register(fastifyCompress, {
97+
global: true,
98+
encodings: ["gzip", "deflate"],
99+
});
100+
this.instance.register(fastifyCors, {
101+
origin: this.instance.config.CORS_ORIGIN,
102+
});
91103
this.instance.register(fastifyFormBody);
92104
this.instance.register(fastifyHelmet);
93-
this.instance.register(fastifyRateLimit, {max: this.instance.config.MAX_REQ_PER_MIN, timeWindow: '1 minute'});
105+
this.instance.register(fastifyRateLimit, {
106+
max: this.instance.config.MAX_REQ_PER_MIN,
107+
timeWindow: "1 minute",
108+
});
94109
this.instance.register(fastifySensible);
95110
this.instance.register(fastifySwagger, SWAGGER_CONFIG);
96111
this.instance.register(fastifyHealthCheck, {
@@ -100,20 +115,20 @@ export class App {
100115
healthCheckInterval: 5000,
101116
healthCheck: async () => {
102117
return true;
103-
}
104-
}
118+
},
119+
},
105120
});
106121
if (this.instance.config.NODE_ENV !== "test") {
107122
this.instance.register(fastifyMetrics, {
108-
blacklist: '/metrics',
109-
enableDefaultMetrics: true
123+
blacklist: "/metrics",
124+
enableDefaultMetrics: true,
110125
});
111126
}
112127
this.instance.register(routesPlugin);
113128
}
114129
}
115130

116-
declare module 'fastify' {
131+
declare module "fastify" {
117132
interface FastifyInstance {
118133
db: Connection;
119134
}

‎src/config/index.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
2-
import {fasitfyEnvOpt} from "fastify-env";
2+
import { fasitfyEnvOpt } from "fastify-env";
33

44
export const config: fasitfyEnvOpt = {
55
schema: {
66
type: "object",
77
properties: {
88
NODE_ENV: {
9-
type: 'string',
10-
default: 'prod'
9+
type: "string",
10+
default: "prod",
1111
},
1212
SERVER_ADDRESS: {
13-
type: 'string',
14-
default: '0.0.0.0'
13+
type: "string",
14+
default: "0.0.0.0",
1515
},
1616
SERVER_PORT: {
17-
type: 'number',
18-
default: 3000
17+
type: "number",
18+
default: 3000,
1919
},
2020
CORS_ORIGIN: {
21-
type: 'string',
22-
default: "*"
21+
type: "string",
22+
default: "*",
2323
},
2424
MAX_REQ_PER_MIN: {
25-
type: 'number',
26-
default: 100
25+
type: "number",
26+
default: 100,
2727
},
28-
}
28+
},
2929
},
30-
env: true
30+
env: true,
3131
};
3232

33-
declare module 'fastify' {
33+
declare module "fastify" {
3434
interface FastifyInstance {
3535
config: {
3636
NODE_ENV: string | "test" | "prod";

‎src/controllers/sample.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {SampleRepository} from "../repositories/sample";
2-
import {ApiController} from "../services/fastify-types";
3-
import {logger} from "../services/logger";
1+
import { SampleRepository } from "../repositories/sample";
2+
import { ApiController } from "../services/fastify-types";
3+
import { logger } from "../services/logger";
44

55
interface GetQuery {
66
name?: string;
@@ -9,12 +9,10 @@ interface GetQuery {
99
export const get: ApiController<GetQuery> = {
1010
url: "/samples",
1111
handler: async function (request, reply) {
12-
logger.info("Fetching samples", {requestId: request.id});
12+
logger.info("Fetching samples", { requestId: request.id });
1313
const sampleRepository = this.db.getCustomRepository(SampleRepository);
1414
if (request.query.name) {
15-
reply.send(
16-
await sampleRepository.findByName(request.query.name)
17-
);
15+
reply.send(await sampleRepository.findByName(request.query.name));
1816
} else {
1917
reply.send(await sampleRepository.find());
2018
}
@@ -25,11 +23,11 @@ export const get: ApiController<GetQuery> = {
2523
type: "object",
2624
required: [],
2725
properties: {
28-
"name": {
29-
type: "string"
26+
name: {
27+
type: "string",
3028
},
31-
}
32-
}
33-
}
34-
}
29+
},
30+
},
31+
},
32+
},
3533
};

‎src/entities/Sample.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";
1+
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
22

3-
@Entity({name: "samples"})
3+
@Entity({ name: "samples" })
44
export class Sample {
5-
65
@PrimaryGeneratedColumn()
76
public id!: number;
87

98
@Column("varchar")
109
public name!: string;
11-
1210
}

‎src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import nodeCleanup from "node-cleanup";
22

3-
import {App} from "./App";
3+
import { App } from "./App";
44

55
App.init().then((app) => {
66
nodeCleanup(function (exitCode, signal) {
@@ -10,4 +10,4 @@ App.init().then((app) => {
1010
});
1111

1212
app.start();
13-
})
13+
});
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
import {MigrationInterface, QueryRunner, Table} from "typeorm";
1+
import { MigrationInterface, QueryRunner, Table } from "typeorm";
22

33
export class SampleObjectTableInit1586770481858 implements MigrationInterface {
4-
54
public async up(queryRunner: QueryRunner): Promise<void> {
6-
await queryRunner.createTable(new Table({
7-
name: "samples",
8-
columns: [
9-
{
10-
name: "id",
11-
type: "int",
12-
isPrimary: true,
13-
isGenerated: true,
14-
generationStrategy: "increment"
15-
},
16-
{
17-
name: "name",
18-
type: "varchar",
19-
isNullable: false
20-
}
21-
]
22-
}), true);
5+
await queryRunner.createTable(
6+
new Table({
7+
name: "samples",
8+
columns: [
9+
{
10+
name: "id",
11+
type: "int",
12+
isPrimary: true,
13+
isGenerated: true,
14+
generationStrategy: "increment",
15+
},
16+
{
17+
name: "name",
18+
type: "varchar",
19+
isNullable: false,
20+
},
21+
],
22+
}),
23+
true
24+
);
2325
}
2426

2527
public async down(queryRunner: QueryRunner): Promise<void> {
2628
await queryRunner.dropTable("samples", true);
2729
}
28-
2930
}

‎src/repositories/sample.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import {EntityRepository, Repository} from "typeorm";
1+
import { EntityRepository, Repository } from "typeorm";
22

3-
import {Sample} from "../entities";
3+
import { Sample } from "../entities";
44

55
@EntityRepository(Sample)
66
export class SampleRepository extends Repository<Sample> {
7-
87
public findByName(name: string): Promise<Sample[]> {
98
return this.find({
109
where: {
11-
name
12-
}
10+
name,
11+
},
1312
});
1413
}
15-
1614
}

‎src/routes/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import {FastifyInstance} from "fastify";
1+
import { FastifyInstance } from "fastify";
22

33
import * as sampleController from "../controllers/sample";
44

55
export function registerRoutes(server: FastifyInstance): void {
6-
server.get(sampleController.get.url, sampleController.get.opts, sampleController.get.handler);
6+
server.get(
7+
sampleController.get.url,
8+
sampleController.get.opts,
9+
sampleController.get.handler
10+
);
711
}

‎src/services/db/factories/sample.factory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {define} from "typeorm-seeding";
1+
import { define } from "typeorm-seeding";
22

3-
import {Sample} from "../../../entities";
3+
import { Sample } from "../../../entities";
44

55
define(Sample, (faker) => {
66
const sample = new Sample();

0 commit comments

Comments
 (0)
Please sign in to comment.