Skip to content

Commit

Permalink
Remove usage of any
Browse files Browse the repository at this point in the history
  • Loading branch information
Thilas committed Oct 31, 2021
1 parent 561dead commit 4d8caba
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/middlewares/error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("ErrorMiddleware", () => {
// act
handler(fakeError, fakeReq, fakeRes, fakeNext)
// assert
const expectedMessage = `Cannot ${fakeMethod} ${fakeUrl}`
const expectedMessage = `${fakeMethod} ${fakeUrl}`
resMock
.verify((e) => e.status(500), Times.Once())
.verify((e) => e.send(`${expectedMessage}: ${fakeError.message}`), Times.Once())
Expand Down
9 changes: 5 additions & 4 deletions src/middlewares/error.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { NextFunction, Request, Response } from "express"
import { ILogger } from "../logger"
import { hasMember } from "../utils"

export function getErrorHandler(logger: ILogger) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
return (err: any, req: Request, res: Response, _next: NextFunction) => {
const message = `Cannot ${req.method} ${req.originalUrl}`
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (err: unknown, req: Request, res: Response, _next: NextFunction) => {
const message = `${req.method} ${req.originalUrl}`
logger.error(`${message}:`, err)
logger.debug("Headers", req.headers)
if (Object.keys(req.params).length) {
logger.debug("Body", req.params)
}
res.status(500).send(`${message}: ${err.message ?? err}`)
res.status(500).send(`${message}: ${hasMember(err, "message") ? err.message : err}`)
}
}
23 changes: 22 additions & 1 deletion src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getFirstSupportedOrDefault } from "./utils"
import { getFirstSupportedOrDefault, hasMember } from "./utils"

describe("getFirstSupportedOrDefault", () => {
abstract class Base {
Expand Down Expand Up @@ -42,3 +42,24 @@ describe("getFirstSupportedOrDefault", () => {
expect(result).toEqual(new B())
})
})

describe("hasMember", () => {
const expectedValue = "value"
const sut = { a: expectedValue } as unknown

it("returns false if the object doesn't have a specific member", () => {
// act
const result = hasMember(sut, "b")
// assert
expect(result).toBeFalsy()
expect(result ? sut.b : expectedValue).toEqual(expectedValue)
})

it("returns true if the object has a specific member", () => {
// act
const result = hasMember(sut, "a")
// assert
expect(result).toBeTruthy()
expect(result ? sut.a : undefined).toEqual(expectedValue)
})
})
12 changes: 8 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Constructor<T, TArgs extends any[] = any[]> = new (...args: TArgs) => T

export type NewReturnType<T, TNew> = T extends (...args: infer TArgs) => unknown ? (...args: TArgs) => TNew : never

export function getFirstSupportedOrDefault<T, C extends Constructor<T>>(values: T[], types: C[]) {
for (const type of types) {
for (const value of values) {
Expand All @@ -6,7 +11,6 @@ export function getFirstSupportedOrDefault<T, C extends Constructor<T>>(values:
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Constructor<T, TArgs extends any[] = any[]> = new (...args: TArgs) => T

export type NewReturnType<T, TNew> = T extends (...args: infer TArgs) => unknown ? (...args: TArgs) => TNew : never
export function hasMember<T extends string>(value: unknown, member: T): value is { [member in T]: unknown } {
return typeof value === "object" && value !== null && member in value
}

0 comments on commit 4d8caba

Please sign in to comment.