Skip to content

Commit a4da18d

Browse files
authored
Merge pull request #30 from NodeFactoryIo/nmlinaric/lint-config
Add eslint import order config
2 parents afa9413 + d77f7dc commit a4da18d

File tree

24 files changed

+508
-73
lines changed

24 files changed

+508
-73
lines changed

.eslintrc.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module.exports = {
88
"parser": "@typescript-eslint/parser",
99
"plugins": [
1010
"@typescript-eslint",
11-
"prettier"
11+
"prettier",
12+
"eslint-plugin-import"
1213
],
1314
"extends": [
1415
"eslint:recommended",
@@ -31,7 +32,7 @@ module.exports = {
3132
"prefer-const": "error",
3233
"no-consecutive-blank-lines": 0,
3334
"no-console": "error",
34-
"@typescript-eslint/naming-convention": ["error",
35+
"@typescript-eslint/naming-convention": ["error",
3536
{selector: "default", format: ['camelCase']},
3637
{
3738
selector: [
@@ -52,6 +53,16 @@ module.exports = {
5253
modifiers: ["destructured"],
5354
format: null
5455
}
55-
],
56+
],
57+
"import/order": [
58+
"error",
59+
{
60+
"groups": ["builtin", "external", "parent", "internal", "sibling"],
61+
"newlines-between": "always",
62+
"alphabetize": {
63+
order: 'asc'
64+
}
65+
},
66+
]
5667
}
57-
}
68+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"env-prompt": "^1.2.3",
7676
"eslint": "^7.18.0",
7777
"eslint-config-prettier": "^7.2.0",
78+
"eslint-plugin-import": "^2.22.1",
7879
"eslint-plugin-prettier": "^3.3.1",
7980
"mocha": "^8.2.1",
8081
"nodemon": "^2.0.7",

src/App.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
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";
6-
import { fastifyHelmet } from "fastify-helmet";
6+
import fastifyHealthCheck from "fastify-healthcheck";
7+
import {fastifyHelmet} from "fastify-helmet";
8+
import fastifyMetrics from "fastify-metrics";
79
import fastifyRateLimit from "fastify-rate-limit";
810
import fastifySensible from "fastify-sensible";
911
import fastifySwagger from "fastify-swagger";
10-
import fastifyMetrics from "fastify-metrics";
11-
import fastifyHealthCheck from "fastify-healthcheck";
12-
import { Connection } from "typeorm";
13-
import { config as envPluginConfig } from "./config";
14-
import { getDatabaseConnection } from "./services/db";
15-
import { logger } from "./services/logger";
16-
import { routesPlugin } from "./services/plugins/routes";
17-
import { SWAGGER_CONFIG } from "./services/swagger";
18-
import { fastifyLogger } from "./services/logger/fastify";
12+
import {Connection} from "typeorm";
13+
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";
1920
export class App {
2021

2122
public readonly instance: FastifyInstance;
@@ -68,7 +69,7 @@ export class App {
6869
);
6970
try {
7071
await this.instance.close();
71-
} catch(e) {
72+
} catch (e) {
7273
logger.error(`Error occurred during server closing because: ${e.message}`);
7374
}
7475

@@ -102,7 +103,7 @@ export class App {
102103
}
103104
}
104105
});
105-
if(this.instance.config.NODE_ENV !== "test") {
106+
if (this.instance.config.NODE_ENV !== "test") {
106107
this.instance.register(fastifyMetrics, {
107108
blacklist: '/metrics',
108109
enableDefaultMetrics: true
@@ -116,4 +117,4 @@ declare module 'fastify' {
116117
interface FastifyInstance {
117118
db: Connection;
118119
}
119-
}
120+
}

src/config/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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: {
@@ -32,12 +32,12 @@ export const config: fasitfyEnvOpt = {
3232

3333
declare module 'fastify' {
3434
interface FastifyInstance {
35-
config: {
35+
config: {
3636
NODE_ENV: string | "test" | "prod";
3737
SERVER_ADDRESS: string;
3838
SERVER_PORT: number;
3939
CORS_ORIGIN: string;
4040
MAX_REQ_PER_MIN: number;
4141
};
4242
}
43-
}
43+
}

src/controllers/sample.ts

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

55
interface GetQuery {
@@ -8,10 +8,10 @@ interface GetQuery {
88

99
export const get: ApiController<GetQuery> = {
1010
url: "/samples",
11-
handler: async function(request, reply) {
11+
handler: async function (request, reply) {
1212
logger.info("Fetching samples", {requestId: request.id});
1313
const sampleRepository = this.db.getCustomRepository(SampleRepository);
14-
if(request.query.name) {
14+
if (request.query.name) {
1515
reply.send(
1616
await sampleRepository.findByName(request.query.name)
1717
);
@@ -29,7 +29,7 @@ export const get: ApiController<GetQuery> = {
2929
type: "string"
3030
},
3131
}
32-
}
33-
}
32+
}
33+
}
3434
}
35-
};
35+
};

src/entities/Sample.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export class Sample {
88

99
@Column("varchar")
1010
public name!: string;
11-
11+
1212
}

src/entities/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./Sample";
1+
export * from "./Sample";

src/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import {App} from "./App";
21
import nodeCleanup from "node-cleanup";
32

3+
import {App} from "./App";
4+
45
App.init().then((app) => {
56
nodeCleanup(function (exitCode, signal) {
67
app.stop(signal as string);
78
nodeCleanup.uninstall();
89
return false;
910
});
10-
11+
1112
app.start();
1213
})
13-
14-

src/repositories/sample.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {EntityRepository, Repository} from "typeorm";
2+
23
import {Sample} from "../entities";
34

45
@EntityRepository(Sample)
@@ -12,4 +13,4 @@ export class SampleRepository extends Repository<Sample> {
1213
});
1314
}
1415

15-
}
16+
}

src/routes/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {FastifyInstance} from "fastify";
2+
23
import * as sampleController from "../controllers/sample";
34

45
export function registerRoutes(server: FastifyInstance): void {
56
server.get(sampleController.get.url, sampleController.get.opts, sampleController.get.handler);
6-
}
7+
}
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {define} from "typeorm-seeding";
2+
23
import {Sample} from "../../../entities";
34

4-
define(Sample, (faker)=> {
5+
define(Sample, (faker) => {
56
const sample = new Sample();
67
sample.name = faker.name.jobType();
78
return sample;
8-
});
9+
});

src/services/db/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import {Connection, createConnection, getConnectionOptions, ObjectType, getConnection} from "typeorm";
2-
import {sleep} from "../utils";
3-
import {logger} from "../logger";
42
import {PostgresConnectionCredentialsOptions} from "typeorm/driver/postgres/PostgresConnectionCredentialsOptions";
3+
4+
import {logger} from "../logger";
55
import {TypeOrmLogger} from "../logger/typeorm";
6+
import {sleep} from "../utils";
67

78
export async function getDatabaseConnection(): Promise<Connection> {
89
const opts = await getConnectionOptions();
910
let conn: Connection;
1011
try {
1112
conn = getConnection();
12-
} catch{
13+
} catch {
1314
conn = await createConnection({
1415
...opts,
1516
logger: new TypeOrmLogger()
@@ -20,7 +21,7 @@ export async function getDatabaseConnection(): Promise<Connection> {
2021
}
2122

2223
async function openConnection(conn: Connection): Promise<Connection> {
23-
if(conn.isConnected) {
24+
if (conn.isConnected) {
2425
logger.info(`Connected to database at ${getUrl(conn.options as PostgresConnectionCredentialsOptions)}`);
2526
return conn;
2627
}
@@ -41,4 +42,4 @@ export async function getRepository<T>(customRepository: ObjectType<T>): Promise
4142

4243
function getUrl(opts: PostgresConnectionCredentialsOptions): string {
4344
return (opts.url ?? `${opts.host}:${opts.port}`) + ` (${opts.database})`
44-
}
45+
}
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {Factory, Seeder} from "typeorm-seeding";
2+
23
import {Sample} from "../../../entities";
34

45
export class SampleSeed implements Seeder {
56
public async run(factory: Factory): Promise<void> {
67
await factory(Sample)().seedMany(10);
78
}
8-
}
9+
}

src/services/fastify-types.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault, RequestBodyDefault, RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault, RouteShorthandOptions } from 'fastify';
2-
import { RouteHandlerMethod } from 'fastify/types/route';
3-
import { ReplyDefault } from 'fastify/types/utils';
1+
import {RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault, RequestBodyDefault, RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault, RouteShorthandOptions} from 'fastify';
2+
import {RouteHandlerMethod} from 'fastify/types/route';
3+
import {ReplyDefault} from 'fastify/types/utils';
44

55
export type DefaultQuery = RequestQuerystringDefault;
66
export type DefaultParams = RequestParamsDefault;
77
export type DefaultBody = RequestBodyDefault;
88
export type DefaultHeaders = RequestHeadersDefault;
99
export interface ApiController<
1010
Query = DefaultQuery, Params = DefaultParams, Body = DefaultBody, Headers = DefaultHeaders
11-
> {
11+
> {
1212
url: string;
1313
opts: RouteShorthandOptions;
1414
handler: RouteHandlerMethod<RawServerDefault, RawRequestDefaultExpression<RawServerDefault>, RawReplyDefaultExpression<RawServerDefault>, {Querystring: Query; Params: Params; Body: Body; Headers: Headers; Reply: ReplyDefault}>;
15-
}
15+
}

src/services/logger/fastify.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import {Stream, Transform} from "stream";
2-
import {logger} from "./index";
31
import {IncomingMessage} from "http";
2+
import {Stream, Transform} from "stream";
3+
44
import {FastifyLoggerOptions, FastifyRequest, RawServerDefault} from "fastify";
55

6+
import {logger} from "./index";
7+
68
function format(req: FastifyRequest<Record<string, unknown>, RawServerDefault>): {msg: string} {
79
return {msg: `${req.ip} -> ${req.hostname}\t${req.method}:${req.url}\tRequestId: ${req.id}`};
810
}

src/services/logger/typeorm.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import {Logger} from "typeorm";
21
import {Logger as WinstonLogger} from "@nodefactory/winston";
2+
import {Logger} from "typeorm";
3+
34
import {logger} from "./index";
45

56
export class TypeOrmLogger implements Logger {
6-
7+
78
private readonly logger: WinstonLogger;
8-
9+
910
constructor() {
1011
this.logger = logger.child({label: "database"});
1112
}
@@ -33,4 +34,4 @@ export class TypeOrmLogger implements Logger {
3334
logSchemaBuild(message: string): void {
3435
this.logger.debug(message);
3536
}
36-
}
37+
}

src/services/metrics/auth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FastifyError, FastifyReply, FastifyRequest } from "fastify";
1+
import {FastifyError, FastifyReply, FastifyRequest} from "fastify";
22

33
export async function onlyWhitelisted(
44
request: FastifyRequest

src/services/plugins/routes.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { FastifyPluginAsync } from "fastify";
2-
import { registerRoutes } from "../../routes";
3-
import { onlyWhitelisted } from "../metrics/auth";
1+
import {FastifyPluginAsync} from "fastify";
2+
3+
import {registerRoutes} from "../../routes";
4+
import {onlyWhitelisted} from "../metrics/auth";
45

56
export const routesPlugin: FastifyPluginAsync = async function (instance) {
67
registerRoutes(instance);

src/services/swagger/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ export const SWAGGER_CONFIG: FastifyDynamicSwaggerOptions = {
1818
consumes: ['application/json'],
1919
produces: ['application/json'],
2020
}
21-
};
21+
};

src/services/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export async function sleep(ms: number): Promise<void> {
22
return new Promise((resolve => {
33
setTimeout(resolve, ms);
44
}));
5-
}
5+
}

src/subscribers/SampleSubscriber.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {EntitySubscriberInterface, EventSubscriber, InsertEvent} from "typeorm";
2+
23
import {Sample} from "../entities";
34

45
@EventSubscriber()

test/e2e/sample.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {expect} from "chai";
2-
import {app} from "./app-setup";
2+
33
import {logger} from "../../src/services/logger";
44

5+
import {app} from "./app-setup";
6+
57
describe("Sample e2e test", function () {
68

79
beforeEach(async function () {

0 commit comments

Comments
 (0)