Skip to content

Commit

Permalink
ref: path aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Nov 28, 2024
1 parent 47ce307 commit a5579d5
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/filter.ts
Original file line number Diff line number Diff line change
@@ -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>[] = [];
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/routes/album.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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));
}
4 changes: 2 additions & 2 deletions src/routes/search.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/routes/translations.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/songData.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"strict": true,
"useDefineForClassFields": true,
"noImplicitThis": false,
"paths": {
"@src/*": ["src/*"],
"@routes/*": ["src/routes/*"],
"@www/*": ["www/*"],
"@root/*": ["."],
}
},
"ts-node": {
"esm": true,
Expand Down

0 comments on commit a5579d5

Please sign in to comment.