Explorar o código

ref: path aliases

Sv443 hai 4 meses
pai
achega
a5579d5bb6
Modificáronse 10 ficheiros con 37 adicións e 22 borrados
  1. 1 1
      src/filter.ts
  2. 2 2
      src/index.ts
  3. 2 2
      src/routes/album.ts
  4. 9 5
      src/routes/index.ts
  5. 2 2
      src/routes/search.ts
  6. 2 2
      src/routes/translations.ts
  7. 4 4
      src/server.ts
  8. 3 3
      src/songData.ts
  9. 6 1
      src/utils.ts
  10. 6 0
      tsconfig.json

+ 1 - 1
src/filter.ts

@@ -1,4 +1,4 @@
-import type { MetaSearchHit, ScoredResults, SearchFilterArgs } from "./types.js";
+import type { MetaSearchHit, ScoredResults, SearchFilterArgs } from "@src/types.js";
 
 export function filterSearchResults(_args: SearchFilterArgs, _results: MetaSearchHit[]): MetaSearchHit[] {
   const scored: ScoredResults<MetaSearchHit>[] = [];

+ 2 - 2
src/index.ts

@@ -1,7 +1,7 @@
 import "dotenv/config";
 
-import * as server from "./server.js";
-import { error } from "./error.js";
+import * as server from "@src/server.js";
+import { error } from "@src/error.js";
 
 const { env } = process;
 

+ 2 - 2
src/routes/album.ts

@@ -1,6 +1,6 @@
 import { Router } from "express";
-import { paramValid, respond } from "../utils.js";
-import { getAlbum } from "../songData.js";
+import { paramValid, respond } from "@src/utils.js";
+import { getAlbum } from "@src/songData.js";
 
 export function initAlbumRoutes(router: Router) {
   //#region /album

+ 9 - 5
src/routes/index.ts

@@ -1,10 +1,11 @@
 import { resolve } from "node:path";
 import express, { Application, Router } from "express";
-import { verMajor } from "../constants.js";
+import { verMajor } from "@src/constants.js";
+import { redirectToDocs } from "@src/utils.js";
 
-import { initSearchRoutes } from "./search.js";
-import { initTranslationsRoutes } from "./translations.js";
-import { initAlbumRoutes } from "./album.js";
+import { initSearchRoutes } from "@routes/search.js";
+import { initTranslationsRoutes } from "@routes/translations.js";
+import { initAlbumRoutes } from "@routes/album.js";
 
 const routeFuncs: ((router: Router) => unknown)[] = [
   initSearchRoutes,
@@ -21,9 +22,12 @@ export function initRouter(app: Application) {
   // host docs files
   router.use("/docs", express.static(resolve("./www/.vuepress/dist")));
 
+  // redirect to docs page
+  router.get("/", (_req, res) => redirectToDocs(res));
+
   // mount router
   app.use(`/v${verMajor}`, router);
 
   // redirect to docs page
-  app.get("/docs", (_req, res) => res.redirect(`/v${verMajor}/docs/`));
+  app.get("/docs", (_req, res) => redirectToDocs(res));
 }

+ 2 - 2
src/routes/search.ts

@@ -1,6 +1,6 @@
 import { Router } from "express";
-import { paramValid, respond } from "../utils.js";
-import { getMeta } from "../songData.js";
+import { paramValid, respond } from "@src/utils.js";
+import { getMeta } from "@src/songData.js";
 
 export function initSearchRoutes(router: Router) {
   //#region /search

+ 2 - 2
src/routes/translations.ts

@@ -1,6 +1,6 @@
 import { Router } from "express";
-import { paramValid, respond } from "../utils.js";
-import { getTranslations } from "../songData.js";
+import { paramValid, respond } from "@src/utils.js";
+import { getTranslations } from "@src/songData.js";
 
 export function initTranslationsRoutes(router: Router) {
   //#region /translations

+ 4 - 4
src/server.ts

@@ -8,10 +8,10 @@ import cors from "cors";
 import { getClientIp } from "request-ip";
 
 import packageJson from "../package.json" with { type: "json" };
-import { error } from "./error.js";
-import { initRouter } from "./routes/index.js";
-import { hashStr, respond } from "./utils.js";
-import { rateLimitOptions, rlIgnorePaths } from "./constants.js";
+import { error } from "@src/error.js";
+import { hashStr, respond } from "@src/utils.js";
+import { rateLimitOptions, rlIgnorePaths } from "@src/constants.js";
+import { initRouter } from "@routes/index.js";
 
 const { env } = process;
 const app = express();

+ 3 - 3
src/songData.ts

@@ -1,6 +1,6 @@
-import { axios, baseAxiosOpts } from "./axios.js";
-import { charReplacements } from "./constants.js";
-import type { Album, ApiSearchResult, ApiSongResult, GetMetaArgs, GetMetaResult, MetaSearchHit, SongTranslation } from "./types.js";
+import { axios, baseAxiosOpts } from "@src/axios.js";
+import { charReplacements } from "@src/constants.js";
+import type { Album, ApiSearchResult, ApiSongResult, GetMetaArgs, GetMetaResult, MetaSearchHit, SongTranslation } from "@src/types.js";
 
 /**
  * Returns meta information about the top results of a search using the genius API

+ 6 - 1
src/utils.ts

@@ -2,7 +2,8 @@ import { createHash, type BinaryToTextEncoding } from "node:crypto";
 import { Response } from "express";
 import { parse as jsonToXml } from "js2xmlparser";
 import type { Stringifiable } from "svcorelib";
-import { ResponseType } from "./types.js";
+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 {
@@ -72,6 +73,10 @@ export function respond(res: Response, type: ResponseType | number, data: String
   res.status(statusCode).send(finalData);
 }
 
+export function redirectToDocs(res: Response) {
+  res.redirect(`/v${verMajor}/docs/`);
+}
+
 /** Hashes a string using SHA-512, encoded as "hex" by default */
 export function hashStr(str: string, encoding: BinaryToTextEncoding = "hex"): Promise<string> {
   return new Promise((resolve, reject) => {

+ 6 - 0
tsconfig.json

@@ -25,6 +25,12 @@
     "strict": true,
     "useDefineForClassFields": true,
     "noImplicitThis": false,
+    "paths": {
+      "@src/*": ["src/*"],
+      "@routes/*": ["src/routes/*"],
+      "@www/*": ["www/*"],
+      "@root/*": ["."],
+    }
   },
   "ts-node": {
     "esm": true,