Sv443 4 месяцев назад
Родитель
Сommit
b7f5286745
9 измененных файлов с 23 добавлено и 9 удалено
  1. 1 1
      src/axios.ts
  2. 3 0
      src/constants.ts
  3. 1 1
      src/routes/album.ts
  4. 2 1
      src/routes/index.ts
  5. 4 2
      src/routes/search.ts
  6. 1 1
      src/routes/translations.ts
  7. 5 3
      src/songData.ts
  8. 1 0
      src/types.ts
  9. 5 0
      src/utils.ts

+ 1 - 1
src/axios.ts

@@ -5,7 +5,7 @@ export const axios = _axios.create({
 });
 
 export function baseAxiosOpts() {
-  const authToken = process.env.GENIUS_ACCESS_TOKEN;
+  const authToken = process.env.GENIUS_ACCESS_TOKEN?.trim();
   return authToken && authToken.length > 0 ? {
     headers: {
       "Authorization": `Bearer ${authToken}`,

+ 3 - 0
src/constants.ts

@@ -2,6 +2,9 @@ import { resolve } from "node:path";
 import type { IRateLimiterOptions } from "rate-limiter-flexible";
 import packageJson from "../package.json" with { type: "json" };
 
+/** Max amount of results that geniURL can serve */
+export const maxResultsAmt = 10;
+
 /** The version from package.json, split into a tuple of major, minor, and patch number */
 export const splitVersion = packageJson.version.split(".").map(v => Number(v)) as [major: number, minor: number, patch: number];
 

+ 1 - 1
src/routes/album.ts

@@ -1,4 +1,4 @@
-import { Router } from "express";
+import type { Router } from "express";
 import { paramValid, respond } from "@src/utils.js";
 import { getAlbum } from "@src/songData.js";
 

+ 2 - 1
src/routes/index.ts

@@ -1,4 +1,4 @@
-import express, { Application, Router } from "express";
+import express, { type Application, Router } from "express";
 import { docsPath, verMajor } from "@src/constants.js";
 import { redirectToDocs } from "@src/utils.js";
 
@@ -25,6 +25,7 @@ export function initRouter(app: Application) {
 
   // health check
   router.get("/health", (_req, res) => res.status(200).send("Hello, World!"));
+  router.get("/ping", (_req, res) => res.status(200).send("Pong!"));
 
   if(hostHomepage) {
     // host docs files

+ 4 - 2
src/routes/search.ts

@@ -1,4 +1,4 @@
-import { Router } from "express";
+import type { Router } from "express";
 import { paramValid, respond } from "@src/utils.js";
 import { getMeta } from "@src/songData.js";
 
@@ -6,12 +6,14 @@ export function initSearchRoutes(router: Router) {
   //#region /search
   router.get("/search", async (req, res) => {
     try {
-      const { q, artist, song, format: fmt } = req.query;
+      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);
 
       if(paramValid(q) || (paramValid(artist) && paramValid(song))) {
         const meta = await getMeta({
+          limit,
           ...(q ? {
             q: String(q),
           } : {

+ 1 - 1
src/routes/translations.ts

@@ -1,4 +1,4 @@
-import { Router } from "express";
+import type { Router } from "express";
 import { paramValid, respond } from "@src/utils.js";
 import { getTranslations } from "@src/songData.js";
 

+ 5 - 3
src/songData.ts

@@ -1,5 +1,6 @@
 import { axios, baseAxiosOpts } from "@src/axios.js";
-import { charReplacements } from "@src/constants.js";
+import { charReplacements, maxResultsAmt } from "@src/constants.js";
+import { clamp } from "@src/utils.js";
 import type { Album, ApiSearchResult, ApiSongResult, GetMetaArgs, GetMetaResult, MetaSearchHit, SongTranslation } from "@src/types.js";
 
 /**
@@ -10,9 +11,10 @@ export async function getMeta({
   q,
   artist,
   song,
+  limit,
 }: GetMetaArgs): Promise<GetMetaResult | null>
 {
-  const query = q ? q : `${artist} ${song}`;
+  const query = (q ?? `${artist} ${song}`).trim();
 
   const {
     data: { response },
@@ -64,7 +66,7 @@ export async function getMeta({
 
     return {
       top: hits[0]!,
-      all: hits.slice(0, 10),
+      all: hits.slice(0, clamp(limit, 1, maxResultsAmt)),
     };
   }
   return null;

+ 1 - 0
src/types.ts

@@ -57,6 +57,7 @@ export interface GetMetaArgs {
   q?: string;
   artist?: string;
   song?: string;
+  limit?: number;
 }
 
 export type SearchFilterArgs = GetMetaArgs;

+ 5 - 0
src/utils.ts

@@ -101,3 +101,8 @@ export function getByteLength(data: string | { toString: () => string } | Record
   else
     return -1;
 }
+
+/** Ensures the passed {@linkcode value} always stays between {@linkcode min} and {@linkcode max} */
+export function clamp(value: number, min: number, max: number): number {
+  return Math.max(Math.min(value, max), min);
+}