Sv443 4 месяцев назад
Родитель
Сommit
248b37efc0
3 измененных файлов с 7 добавлено и 4 удалено
  1. 2 1
      src/routes/search.ts
  2. 1 1
      src/songData.ts
  3. 4 2
      src/utils.ts

+ 2 - 1
src/routes/search.ts

@@ -9,7 +9,7 @@ export function initSearchRoutes(router: Router) {
       const { q, artist, song, format: fmt, limit: lmt } = req.query;
 
       const format: string = fmt ? String(fmt) : "json";
-      const limit = isNaN(Number(lmt)) ? undefined : Number(lmt);
+      const limit = !paramValid(lmt) || isNaN(Number(lmt)) ? undefined : Number(lmt);
 
       if(paramValid(q) || (paramValid(artist) && paramValid(song))) {
         const meta = await getMeta({
@@ -47,6 +47,7 @@ export function initSearchRoutes(router: Router) {
 
       if(paramValid(q) || (paramValid(artist) && paramValid(song))) {
         const meta = await getMeta({
+          limit: 1,
           ...(q ? {
             q: String(q),
           } : {

+ 1 - 1
src/songData.ts

@@ -66,7 +66,7 @@ export async function getMeta({
 
     return {
       top: hits[0]!,
-      all: hits.slice(0, clamp(limit, 1, maxResultsAmt)),
+      all: hits.slice(0, clamp(limit ?? maxResultsAmt, 1, maxResultsAmt)),
     };
   }
   return null;

+ 4 - 2
src/utils.ts

@@ -6,8 +6,10 @@ import { verMajor } from "@src/constants.js";
 import type { ResponseType } from "@src/types.js";
 
 /** Checks if the value of a passed URL parameter is a string with length > 0 */
-export function paramValid(val: unknown): val is string {
-  return typeof val === "string" && val.length > 0;
+export function paramValid(val: unknown): boolean {
+  if(typeof val === "string")
+    return val.length > 0;
+  return typeof val !== "undefined";
 }
 
 /**