Skip to content

Commit

Permalink
feat: implemented new helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehonal committed Sep 25, 2024
1 parent 21d47eb commit 40b8fe6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
21 changes: 17 additions & 4 deletions event-manager/src/__tests__/event.helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, jest } from "@jest/globals";
import { getLogLevelByStatus } from "../event.helper.js";
import { HttpStatus } from "@nestjs/common";
import { describe, expect, it, jest } from '@jest/globals';
import { getLogLevelByStatus, isErrorEvent } from '../event.helper.js';
import { HttpException, HttpStatus } from '@nestjs/common';
import { DefaultError } from '@nestjs-yalc/errors/default.error.js';

describe('EventHelper', () => {
it('should return the correct log level', () => {
Expand All @@ -9,4 +10,16 @@ describe('EventHelper', () => {
expect(getLogLevelByStatus(HttpStatus.BAD_REQUEST)).toBe('log');
expect(getLogLevelByStatus(200)).toBe('log');
});
})

it('should return true if the event is an error event', () => {
expect(isErrorEvent({})).toBeFalsy();
expect(isErrorEvent({ errorClass: true })).toBeTruthy();
expect(isErrorEvent({ errorClass: DefaultError })).toBeTruthy();
expect(isErrorEvent({ errorClass: HttpException })).toBeFalsy();
expect(
isErrorEvent({
errorClass: new HttpException('test', HttpStatus.BAD_REQUEST),
}),
).toBeFalsy();
});
});
18 changes: 18 additions & 0 deletions event-manager/src/event.helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { DefaultError } from '@nestjs-yalc/errors/default.error.js';
import { LogLevelEnum } from '@nestjs-yalc/logger/logger.enum.js';
import { isClass } from '@nestjs-yalc/utils/class.helper.js';
import { HttpStatus } from '@nestjs/common';
import { LogLevel } from 'typeorm';
import { IErrorEventOptions } from './event.js';

export function getLogLevelByStatus(statusCode: number) {
let loggerLevel: LogLevel;
Expand All @@ -19,3 +22,18 @@ export function getLogLevelByStatus(statusCode: number) {

return loggerLevel;
}

export function isErrorEvent(options: IErrorEventOptions) {
if (!options.errorClass) {
return false;
}

return (
options.errorClass === true ||
isClass(options.errorClass, DefaultError.name) ||
(!isClass(options.errorClass) &&
options.errorClass.getStatus &&
getLogLevelByStatus(options.errorClass.getStatus()) ===
LogLevelEnum.ERROR)
);
}
8 changes: 8 additions & 0 deletions utils/src/__tests__/class.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
beforeAll,
beforeEach,
} from '@jest/globals';
import { DefaultError } from '@nestjs-yalc/errors/default.error.js';

describe('Test Class Helpers', () => {
it('should test a class correctly', () => {
Expand All @@ -24,4 +25,11 @@ describe('Test Class Helpers', () => {
it('expect an instance of a class to not be a class', () => {
expect(isClass(new (class {})())).toBeFalsy();
});

it('should test a class against a name', () => {
const defaultError = DefaultError;
expect(isClass(class Test {}, 'Test')).toBeTruthy();
expect(isClass(class Test {}, 'Test2')).toBeFalsy();
expect(isClass(defaultError, DefaultError.name)).toBeTruthy();
});
});
6 changes: 4 additions & 2 deletions utils/src/class.helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ClassType } from '@nestjs-yalc/types/globals.d.js';

export function isClass(func: any): func is ClassType {
export function isClass(func: any, className?: string): func is ClassType {
return (
typeof func === 'function' && /^class\s/.test(func.toString.call(func))
typeof func === 'function' &&
/^class\s/.test(func.toString.call(func)) &&
(className ? func.name === className : true)
);
}

0 comments on commit 40b8fe6

Please sign in to comment.