Skip to content

Commit

Permalink
refactor: extract regex pattern into a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
tykachev committed Jan 17, 2025
1 parent 3814500 commit 21daa7c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export const DEFAULT_IGNORED_URLS = [
"/_healthz",
"/favicon.ico",
];
export const TRIM_SLASHES_PATTERN = /^\/|\/$/g;
7 changes: 3 additions & 4 deletions src/promInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
DEFAULT_HTTP_REQUESTS_METRIC_NAME,
DEFAULT_IGNORED_URLS,
DEFAULT_PROM_OPTIONS_TOKEN,
TRIM_SLASHES_PATTERN,
} from "./constants";
import { PromModuleOptions } from "./interfaces";
import { Normalizer } from "./utils";
Expand Down Expand Up @@ -65,16 +66,14 @@ export class PromInterceptor implements NestInterceptor {
const request = context.switchToHttp().getRequest<Request>();

const controllerPathIndex = request.url.indexOf(
// eslint-disable-next-line unicorn/prefer-string-replace-all
controllerPath.replace(/^\/|\/$/g, ""),
controllerPath.replace(TRIM_SLASHES_PATTERN, ""),
);
const prefixPath =
controllerPathIndex === -1
? ""
: request.url.slice(0, controllerPathIndex);

// eslint-disable-next-line unicorn/prefer-string-replace-all
const path = `${prefixPath}${controllerPath.replace(/^\/|\/$/g, "")}/${methodPath.replace(/^\/|\/$/g, "")}`;
const path = `${prefixPath}${controllerPath.replace(TRIM_SLASHES_PATTERN, "")}/${methodPath.replace(TRIM_SLASHES_PATTERN, "")}`;

const ignoredUrls =
this.options.httpRequestBucket?.ignoredUrls ?? DEFAULT_IGNORED_URLS;
Expand Down

0 comments on commit 21daa7c

Please sign in to comment.