fix: annotated util validation functions

This commit is contained in:
Marius Begby committed 2023-08-27 01:36:58 +02:00
1 parent d38ed18842
commit 1ae9503477
5 files changed
+64 -50

No files matched your search

+9 -5
View File
@@ -1,6 +1,7 @@
import loggerModule from '../../services/logger';
import { TransformQueryParams } from '../../types/utilTypes';
export const transformQuery = async ({ query, executionId }) => {
export const transformQuery = async ({ query, executionId }: TransformQueryParams) => {
const logger = loggerModule.child({
source: 'searchQueryValidator.js',
module: 'utilValidation',
@@ -11,7 +12,7 @@ export const transformQuery = async ({ query, executionId }) => {
if (query.startsWith('https://open.spotify.com/')) {
try {
// regex to check for country-based Spotify url
const regex = new RegExp(
const regex: RegExp = new RegExp(
'https://open.spotify.com/((?:[a-z]{2,4}-[a-z]{2,4}/)|(?:[a-z]{2,4}/))(track|playlist)/([a-zA-Z0-9]+)'
);
const isCountryBased = regex.test(query);
@@ -19,9 +20,12 @@ export const transformQuery = async ({ query, executionId }) => {
// if country-based, transform to normal Spotify url
if (isCountryBased) {
logger.debug(`Transforming country-based Spotify url to normal Spotify url from query '${query}'.`);
const type = query.match(regex)[2];
const trackId = query.match(regex)[3];
query = `https://open.spotify.com/${type}/${trackId}`;
const matches = query.match(regex);
if (matches) {
const type = matches[2];
const trackId = matches[3];
query = `https://open.spotify.com/${type}/${trackId}`;
}
}
} catch (error) {
logger.error(error, 'Error while validating or transforming Spotify url.');