Skip to content

Commit

Permalink
refactor(be): ♻️ move remove leading and trailing slashes to utils/co…
Browse files Browse the repository at this point in the history
…mmon
  • Loading branch information
lehuygiang28 committed Jul 2, 2024
1 parent aca5955 commit 1929d2c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
27 changes: 27 additions & 0 deletions apps/be/common/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,30 @@ export function normalizeHeaders(headers: Record<string, unknown>) {
return { ...acc, [key.trim().toLowerCase()]: value };
}, {});
}

/**
* Removes all trailing slashes from a path
* @param path The path to remove trailing slashes
* @returns The path without any trailing slashes
*/
export function removeTrailingSlash(path: string) {
return path.replace(/\/+$/, '');
}

/**
* Removes all leading slashes from a path
* @param path The path to remove leading slashes
* @returns The path without any leading slashes
*/
export function removeLeadingSlash(path: string) {
return path.replace(/^\/+/, '');
}

/**
* Removes all trailing and leading slashes from a path
* @param path The path to remove trailing and leading slashes
* @returns The path without any trailing or leading slashes
*/
export function removeLeadingAndTrailingSlashes(path: string) {
return removeLeadingSlash(removeTrailingSlash(path));
}
8 changes: 5 additions & 3 deletions apps/be/src/app/config/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Type } from 'class-transformer';

import validateConfig from '~be/common/utils/validate-config';
import { ToBoolean } from '~be/common/utils/decorators/to-boolean.decorator';
import { removeLeadingAndTrailingSlashes } from '~be/common/utils/common';

import { AppConfig } from './app-config.type';

class EnvironmentVariablesValidator {
Expand Down Expand Up @@ -62,7 +64,7 @@ export default registerAs<AppConfig>('app', () => {
deployEnv: process.env?.DEPLOY_ENV || 'none',
workerMode: process.env?.WORKER_MODE === 'true',
globalPrefix: process.env?.GLOBAL_PREFIX
? process.env.GLOBAL_PREFIX.replace(/^\/|\\|\/$|\\$/g, '') // remove leading and trailing slashes (/ or \)
? removeLeadingAndTrailingSlashes(process.env.GLOBAL_PREFIX)
: 'api',
workerName: process.env?.WORKER_NAME || 'default',
eventsMaxLen: process.env?.BULLMQ_EVENTS_MAXLEN
Expand All @@ -71,12 +73,12 @@ export default registerAs<AppConfig>('app', () => {
port: process.env?.PORT ? parseInt(process.env?.PORT, 10) : 8000,
fallbackLanguage: process.env?.FALLBACK_LANGUAGE || 'en',
apiStatsPath: process.env?.API_STATS_PATH
? process.env.API_STATS_PATH.replace(/^\/|\\|\/$|\\$/g, '')
? removeLeadingAndTrailingSlashes(process.env.API_STATS_PATH)
: '',
apiStatsUsername: process.env?.API_STATS_USERNAME,
apiStatsPassword: process.env?.API_STATS_PASSWORD,
bullBoardPath: process.env?.BULL_BOARD_PATH
? process.env.BULL_BOARD_PATH.replace(/^\/|\\|\/$|\\$/g, '')
? removeLeadingAndTrailingSlashes(process.env.BULL_BOARD_PATH)
: '',
};
});

0 comments on commit 1929d2c

Please sign in to comment.