Browse Source

ref: minor stuff

Sv443 4 months ago
parent
commit
212a1f2e1e
6 changed files with 16 additions and 10 deletions
  1. 2 1
      src/axios.ts
  2. 7 0
      src/constants.ts
  3. 2 1
      src/index.ts
  4. 1 1
      src/routes/album.ts
  5. 2 2
      src/routes/search.ts
  6. 2 5
      src/routes/translations.ts

+ 2 - 1
src/axios.ts

@@ -1,7 +1,8 @@
 import { default as _axios } from "axios";
+import { axiosTimeout } from "./constants.js";
 
 export const axios = _axios.create({
-  timeout: 1000 * 15,
+  timeout: axiosTimeout,
 });
 
 export function baseAxiosOpts() {

+ 7 - 0
src/constants.ts

@@ -2,6 +2,10 @@ import { resolve } from "node:path";
 import type { IRateLimiterOptions } from "rate-limiter-flexible";
 import type { ResponseFormat } from "./types.js";
 import packageJson from "../package.json" with { type: "json" };
+import type { axios } from "./axios.js";
+
+// for @linkcode in tsdoc comments
+void [{} as typeof axios];
 
 //#region rate limiting
 
@@ -29,6 +33,9 @@ export const docsMaxAge = 1000 * 60 * 60 * 24 * 2; // 2 days
 /** Max amount of results that geniURL can serve */
 export const maxResultsAmt = 10;
 
+/** Timeout for all requests sent using the common {@linkcode axios} instance in milliseconds */
+export const axiosTimeout = 1000 * 15;
+
 //#region other
 
 /** The version from package.json, split into a tuple of major, minor, and patch number */

+ 2 - 1
src/index.ts

@@ -3,7 +3,7 @@ import "dotenv/config";
 import * as server from "@src/server.js";
 import { error } from "@src/error.js";
 
-const { env } = process;
+const { env, exit } = process;
 
 async function init() {
   try {
@@ -19,6 +19,7 @@ async function init() {
   }
   catch(err) {
     error("Encountered fatal error while initializing:", err instanceof Error ? err : undefined, true);
+    setImmediate(() => exit(1));
   }
 }
 

+ 1 - 1
src/routes/album.ts

@@ -23,7 +23,7 @@ export function initAlbumRoutes(router: Router) {
 
       const album = await getAlbum(Number(songId));
 
-      if(!album) // TODO: verify
+      if(!album)
         return respond(res, "noResults", {}, format);
 
       return respond(res, "success", { album }, format, 1);

+ 2 - 2
src/routes/search.ts

@@ -22,7 +22,7 @@ export function initSearchRoutes(router: Router) {
           }),
         });
 
-        if(!meta || meta.all.length < 1) // TODO: verify
+        if(!meta || meta.all.length < 1)
           return respond(res, "noResults",  format !== "xml" ? { top: null, all: [] } : { top: null, all: { "result": [] } }, format);
 
         // js2xmlparser needs special treatment when using arrays to produce a decent XML structure
@@ -56,7 +56,7 @@ export function initSearchRoutes(router: Router) {
           }),
         });
 
-        if(!meta || !meta.top) // TODO: verify
+        if(!meta || !meta.top)
           return respond(res, "noResults", {}, format);
 
         return respond(res, "success", meta.top, format, 1);

+ 2 - 5
src/routes/translations.ts

@@ -21,12 +21,9 @@ export function initTranslationsRoutes(router: Router) {
       if(!paramValid(songId) || isNaN(Number(songId)))
         return respond(res, "clientError", "Provided song ID is invalid", format);
 
-      const translations = await getTranslations(Number(songId));
+      const translations = await getTranslations(Number(songId)) ?? undefined;
 
-      if(translations === null || (Array.isArray(translations) && translations.length === 0)) // TODO: verify
-        return respond(res, "noResults", {}, format);
-
-      if(translations === undefined) // TODO: verify
+      if(!translations || (Array.isArray(translations) && translations.length === 0))
         return respond(res, "noResults", {}, format);
 
       return respond(res, "success", { translations }, format, translations.length);