diff --git a/.editorconfig b/.editorconfig index 2b192c3..b96eda8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -27,4 +27,4 @@ insert_final_newline = false [*.js] trim_trailing_whitespace = false -max_line_length = 150 +max_line_length = 120 diff --git a/.eslintrc.js b/.eslintrc.js index 90c8da7..cbcdaad 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -5,6 +5,16 @@ module.exports = { files: ['config/**/*', 'test/**/*'], extends: ['@voiceflow/eslint-config/utility', '@voiceflow/eslint-config/mocha'], rules: { + 'max-len': [ + 'error', + { + code: 120, + ignoreUrls: true, + ignoreStrings: true, + ignoreTemplateLiterals: true, + ignoreRegExpLiterals: true, + }, + ], // off 'no-unused-expressions': 'off', }, diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..ab3e2cc --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,4 @@ +module.exports = { + ...require("@voiceflow/prettier-config"), + printWidth: 120, +}; diff --git a/package.json b/package.json index d6dc1e9..7cefbae 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,6 @@ "ioredis": "^4.28.5", "jszip": "^3.7.1" }, - "prettier": "@voiceflow/prettier-config", "repository": { "type": "git", "url": "git+https://github.com/voiceflow/backend-utils.git" diff --git a/src/common/zip/reader.ts b/src/common/zip/reader.ts index 56f5f26..66f1d08 100644 --- a/src/common/zip/reader.ts +++ b/src/common/zip/reader.ts @@ -64,7 +64,11 @@ export class ZipReader { }); } - private async *getFilesRecursively(zip: JSZip, config: ZipGetFilesOptions, currentDepth = 0): AsyncGenerator { + private async *getFilesRecursively( + zip: JSZip, + config: ZipGetFilesOptions, + currentDepth = 0 + ): AsyncGenerator { const objects = zip.filter((_, file) => minimatch(file.name, config.path)); let fileCount = 0; diff --git a/src/fixtureGenerator.ts b/src/fixtureGenerator.ts index f8ea0c6..d972e57 100644 --- a/src/fixtureGenerator.ts +++ b/src/fixtureGenerator.ts @@ -27,12 +27,16 @@ const createFixture = >(serviceManager: T): T middlewares: _.mapValues(middlewares, (service) => _.mapValues(service, (method) => { if (method.callback) { - const callbackStub: sinon.SinonStubStatic & { callback?: boolean } = sinon.stub().returns(sinon.stub().callsArg(2)); + const callbackStub: sinon.SinonStubStatic & { callback?: boolean } = sinon + .stub() + .returns(sinon.stub().callsArg(2)); callbackStub.callback = true; return callbackStub; } - const methodStub: sinon.SinonStubStatic & { validations?: Record } = sinon.stub().callsArg(2); + const methodStub: sinon.SinonStubStatic & { + validations?: Record; + } = sinon.stub().callsArg(2); methodStub.validations = _.mapValues(method.validations, () => sinon.stub().callsArg(2)); return [...Object.values(methodStub.validations), methodStub]; @@ -56,7 +60,10 @@ const createFixture = >(serviceManager: T): T } as unknown) as T; }; -const checkFixture = >(fixture: T, expected: FixtureExpect): void => { +const checkFixture = >( + fixture: T, + expected: FixtureExpect +): void => { const { middlewares, controllers } = fixture; const validations = { @@ -78,7 +85,10 @@ const checkFixture = >(fixture: T, expected: validations.controllers[controller] = {}; } - validations.controllers[controller][method] = _.mapValues(controllerMethod.validations, (stub) => stub.callCount); + validations.controllers[controller][method] = _.mapValues( + controllerMethod.validations, + (stub) => stub.callCount + ); } controllers[controller][method] = controllerMethod.callCount; @@ -97,7 +107,9 @@ const checkFixture = >(fixture: T, expected: Object.keys(middlewares[service]).forEach((method) => { const expressMiddlewares = middlewares[service][method]; // if no length -> callback middleware -> it's not an array - const middlewareMethod = expressMiddlewares.length ? expressMiddlewares[expressMiddlewares.length - 1] : expressMiddlewares; + const middlewareMethod = expressMiddlewares.length + ? expressMiddlewares[expressMiddlewares.length - 1] + : expressMiddlewares; if (middlewareMethod.validations) { if (!validations.middlewares[service]) { diff --git a/src/middlewares/rateLimit.ts b/src/middlewares/rateLimit.ts index 0a88ec5..365b572 100644 --- a/src/middlewares/rateLimit.ts +++ b/src/middlewares/rateLimit.ts @@ -4,7 +4,10 @@ import { RateLimiterRes } from 'rate-limiter-flexible'; import { AbstractMiddleware, RateLimitConfig } from '../types'; -export class RateLimitMiddleware, C extends RateLimitConfig> extends AbstractMiddleware { +export class RateLimitMiddleware, C extends RateLimitConfig> extends AbstractMiddleware< + S, + C +> { static throwAuthError(): never { throw new VError('Auth Key Required', VError.HTTP_STATUS.UNAUTHORIZED); } @@ -19,7 +22,11 @@ export class RateLimitMiddleware, C extends RateLi return !req.headers.authorization; } - async consume(res: Response, next: NextFunction, { resource, isPublic }: { resource: string; isPublic?: boolean }): Promise { + async consume( + res: Response, + next: NextFunction, + { resource, isPublic }: { resource: string; isPublic?: boolean } + ): Promise { const maxPoints = isPublic ? this.config.RATE_LIMITER_POINTS_PUBLIC : this.config.RATE_LIMITER_POINTS_PRIVATE; const rateLimiterClient = this.services.rateLimitClient[isPublic ? 'public' : 'private']; diff --git a/src/responseBuilder.ts b/src/responseBuilder.ts index 5b57fca..7436a99 100644 --- a/src/responseBuilder.ts +++ b/src/responseBuilder.ts @@ -96,7 +96,11 @@ class ResponseBuilder { req?: Request & { user?: { id: number } } ): ErrorResponse { if (error && (error as any).isAxiosError) { - log.error(`@backend-utils:errorResponse - error:axios:${JSON.stringify(ResponseBuilder.getAxiosError(error as AxiosError))}`); + log.error( + `@backend-utils:errorResponse - error:axios:${JSON.stringify( + ResponseBuilder.getAxiosError(error as AxiosError) + )}` + ); } if (!(error instanceof Error)) { @@ -198,7 +202,9 @@ class ResponseBuilder { let output: ErrorResponse | ReturnType; try { - const data = await (typeof dataPromise === 'function' ? (dataPromise as any)(req, res, nextCheck) : dataPromise); + const data = await (typeof dataPromise === 'function' + ? (dataPromise as any)(req, res, nextCheck) + : dataPromise); output = data instanceof Error diff --git a/src/types.ts b/src/types.ts index 76dd9e2..f86d15a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,7 +30,11 @@ export abstract class AbstractMiddleware, C extend constructor(public services: S, public config: C) {} } -export abstract class AbstractManager, C extends Record, U extends Record = {}> { +export abstract class AbstractManager< + S extends Record, + C extends Record, + U extends Record = {} +> { services: S & U; constructor(services: S, public config: C) { diff --git a/tests/middlewares/rateLimit.unit.ts b/tests/middlewares/rateLimit.unit.ts index d210c4f..ce823b6 100644 --- a/tests/middlewares/rateLimit.unit.ts +++ b/tests/middlewares/rateLimit.unit.ts @@ -82,7 +82,9 @@ describe('rateLimit middleware unit tests', () => { const resource = 'version-id'; const res = { setHeader: sinon.stub() }; - await expect(service.consume(res as any, next, { isPublic: true, resource })).to.eventually.rejectedWith('Too Many Request'); + await expect(service.consume(res as any, next, { isPublic: true, resource })).to.eventually.rejectedWith( + 'Too Many Request' + ); expect(services.rateLimitClient.public.consume.args).to.eql([[resource]]); expect(res.setHeader.args).to.eql([['Retry-After', Math.floor(rateLimiterRes.msBeforeNext / 1000)]]);