-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from evertdespiegeleer/linting
style: 🎨 setup linting
- Loading branch information
Showing
17 changed files
with
2,002 additions
and
470 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
}, | ||
"extends": "standard-with-typescript", | ||
"parserOptions": { | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": "off" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["*.test.ts"], | ||
"rules": { | ||
// Disable on test files so we can usu chai `...to.be.undefined` style syntax | ||
"@typescript-eslint/no-unused-expressions": "off" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
import { Server } from '../app.js'; | ||
import supertest from 'supertest'; | ||
import { describe, it, before, after } from 'node:test'; | ||
import { openapiController } from './openapi.js'; | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
import { Server } from '../app.js' | ||
import supertest from 'supertest' | ||
import { describe, it, before, after } from 'node:test' | ||
import { openapiController } from './openapi.js' | ||
|
||
describe('openapiController', () => { | ||
let http: Server; | ||
let http: Server | ||
before(async () => { | ||
http = new Server({ | ||
controllers: [openapiController] | ||
}, { port: undefined }); | ||
await http.start(); | ||
}); | ||
}, { port: undefined }) | ||
await http.start() | ||
}) | ||
|
||
after(async () => { | ||
await http.stop(); | ||
}); | ||
await http.stop() | ||
}) | ||
|
||
it('Serves a open api spec', (t, done) => { | ||
supertest(http.expressInstance).get('/openapi.json').expect(200, done); | ||
}); | ||
supertest(http.expressInstance).get('/openapi.json').expect(200, done) | ||
}) | ||
|
||
it('Serves api docs', (t, done) => { | ||
supertest(http.expressInstance).get('/api.html').expect(200, done); | ||
}); | ||
}); | ||
supertest(http.expressInstance).get('/api.html').expect(200, done) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { MiddlewareTypes, middleware } from '../util/middleware.js'; | ||
import { apiResponse } from '../util/apiResponse.js'; | ||
import { ConflictError, ZHTTPError, InternalServerError } from '../util/errors.js'; | ||
import { ILogger } from '../util/logger.js'; | ||
import { type Request, type Response, type NextFunction } from 'express' | ||
import { MiddlewareTypes, middleware } from '../util/middleware.js' | ||
import { apiResponse } from '../util/apiResponse.js' | ||
import { ConflictError, ZHTTPError, InternalServerError } from '../util/errors.js' | ||
import { type ILogger } from '../util/logger.js' | ||
|
||
export const makeErrorHandlerMiddleware = (logger: ILogger) => middleware({ | ||
name: 'ErrorHandler', | ||
type: MiddlewareTypes.AFTER, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
handler( | ||
handler ( | ||
originalError: Error, | ||
req: Request, | ||
res: Response, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
next: NextFunction, | ||
next: NextFunction | ||
) { | ||
let status = 500; | ||
let parsedError = new InternalServerError(); | ||
let status = 500 | ||
let parsedError = new InternalServerError() | ||
|
||
const log = logger('errorHandler'); | ||
const log = logger('errorHandler') | ||
|
||
if (originalError.name === 'UniqueViolationError') { | ||
status = 409; | ||
parsedError = new ConflictError(parsedError.message); | ||
status = 409 | ||
parsedError = new ConflictError(parsedError.message) | ||
} | ||
|
||
if (originalError instanceof ZHTTPError) { | ||
status = originalError.http; | ||
parsedError = originalError; | ||
status = originalError.http | ||
parsedError = originalError | ||
} | ||
|
||
// log.error(originalError); | ||
if (status >= 500) { | ||
log.error(`🔴 FAIL ${req.method} ${req.originalUrl}`, parsedError); | ||
log.error(`🔴 FAIL ${req.method} ${req.originalUrl}`, parsedError) | ||
} else { | ||
log.warn(`⚠️ FAIL ${req.method} ${req.originalUrl}`, parsedError); | ||
log.warn(`⚠️ FAIL ${req.method} ${req.originalUrl}`, parsedError) | ||
} | ||
|
||
res.status(status).json(apiResponse({}, { error: parsedError })); | ||
return res.end(); | ||
}, | ||
}); | ||
res.status(status).json(apiResponse({}, { error: parsedError })) | ||
return res.end() | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,66 @@ | ||
import { Server } from '../app.js'; | ||
import supertest, { type Response } from 'supertest'; | ||
import { expect } from 'chai'; | ||
import { describe, it, before, after } from 'node:test'; | ||
import { controller, get } from '../main.js'; | ||
import { BadRequestError, ConflictError } from '../util/errors.js'; | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
import { Server } from '../app.js' | ||
import supertest, { type Response } from 'supertest' | ||
import { expect } from 'chai' | ||
import { describe, it, before, after } from 'node:test' | ||
import { controller, get } from '../main.js' | ||
import { BadRequestError, ConflictError } from '../util/errors.js' | ||
|
||
describe('errorHandler', () => { | ||
let http: Server; | ||
let http: Server | ||
before(async () => { | ||
const testController = controller('test-controller').endpoints([ | ||
get('/unhandled-error').handler(() => { | ||
throw new Error('Unhandled error'); | ||
throw new Error('Unhandled error') | ||
}), | ||
|
||
get('/bad-request').handler(() => { | ||
throw new BadRequestError('Something went wrong :('); | ||
throw new BadRequestError('Something went wrong :(') | ||
}), | ||
|
||
get('/unique-violation').handler(() => { | ||
throw new ConflictError('Something went wrong :('); | ||
}), | ||
]); | ||
throw new ConflictError('Something went wrong :(') | ||
}) | ||
]) | ||
|
||
http = new Server( | ||
{ | ||
controllers: [testController], | ||
controllers: [testController] | ||
}, | ||
{ port: undefined }, | ||
); | ||
{ port: undefined } | ||
) | ||
|
||
await http.start(); | ||
}); | ||
await http.start() | ||
}) | ||
|
||
after(async () => { | ||
await http.stop(); | ||
}); | ||
await http.stop() | ||
}) | ||
|
||
it('Can handle unhandled errors', (t, done) => { | ||
supertest(http.expressInstance).get('/unhandled-error').expect(500, done); | ||
}); | ||
supertest(http.expressInstance).get('/unhandled-error').expect(500, done) | ||
}) | ||
|
||
it('Can handle bad requests', (t, done) => { | ||
supertest(http.expressInstance) | ||
.get('/bad-request') | ||
.expect(400) | ||
.end((err: any, res: Response) => { | ||
if (err) return done(err); | ||
expect(res.body).to.have.property('meta'); | ||
expect(res.body.meta.error).to.have.property('code', 'BadRequestError'); | ||
return done(); | ||
}); | ||
}); | ||
if (err != null) { done(err); return } | ||
expect(res.body).to.have.property('meta') | ||
expect(res.body.meta.error).to.have.property('code', 'BadRequestError') | ||
done() | ||
}) | ||
}) | ||
|
||
it('Can handle unique violations', (t, done) => { | ||
supertest(http.expressInstance) | ||
.get('/unique-violation') | ||
.expect(409) | ||
.end((err: any, res: Response) => { | ||
if (err) return done(err); | ||
expect(res.body.meta.error).to.have.property('code', 'ConflictError'); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
if (err != null) { done(err); return } | ||
expect(res.body.meta.error).to.have.property('code', 'ConflictError') | ||
done() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { Counter } from 'prom-client'; | ||
import { MiddlewareTypes, middleware } from '../util/middleware.js'; | ||
import { type NextFunction, type Request, type Response } from 'express' | ||
import { Counter } from 'prom-client' | ||
import { MiddlewareTypes, middleware } from '../util/middleware.js' | ||
|
||
const metrics = { | ||
httpRequests: new Counter({ | ||
name: 'http_requests_total', | ||
help: 'Total number of HTTP requests', | ||
labelNames: ['method', 'path', 'status'], | ||
}), | ||
}; | ||
labelNames: ['method', 'path', 'status'] | ||
}) | ||
} | ||
|
||
export const metricMiddleware = middleware({ | ||
name: 'metricMiddleware', | ||
type: MiddlewareTypes.BEFORE, | ||
handler(req: Request, res: Response, next: NextFunction) { | ||
handler (req: Request, res: Response, next: NextFunction) { | ||
res.once('finish', () => { | ||
metrics.httpRequests.inc({ | ||
method: req.method, | ||
path: req.originalUrl, | ||
status: res.statusCode, | ||
}); | ||
}); | ||
return next(); | ||
}, | ||
}); | ||
status: res.statusCode | ||
}) | ||
}) | ||
next() | ||
} | ||
}) |
Oops, something went wrong.