瀏覽代碼

unfinished stuff

Sv443 2 年之前
父節點
當前提交
04da25656c
共有 4 個文件被更改,包括 61 次插入28 次删除
  1. 1 0
      .gitignore
  2. 9 1
      changelog.md
  3. 26 12
      src/songMeta.ts
  4. 25 15
      src/types.d.ts

+ 1 - 0
.gitignore

@@ -1,5 +1,6 @@
 node_modules/
 test.js
+test.ts
 .env
 *.log
 out/

+ 9 - 1
changelog.md

@@ -1,10 +1,18 @@
 ## Version History:
-- **[1.0.0](#v100)**
+- [1.1.0](#v110)
+- [1.0.0](#v100)
 - [0.2.0](#v020)
 - [0.1.0](#v010)
 
 <br><br>
 
+### v1.1.0
+- Migrated code to TypeScript
+- Added new metadata:
+    - release date
+
+<br>
+
 ### v1.0.0
 - Added `?artist` and `?song` parameters as an alternative to `?q` for getting better search results through fuzzy filtering ([#4](https://github.com/Sv443/geniURL/issues/4))
 - Added `matches` property that's set to the number of results (`0` if none were found, or `null` on error)

+ 26 - 12
src/songMeta.ts

@@ -31,21 +31,31 @@ export async function getMeta({ q, artist, song }: Partial<Record<"q" | "artist"
                 url: result.url,
                 path: result.path,
                 meta: {
-                    title: normalizeString(result.title),
-                    fullTitle: normalizeString(result.full_title),
-                    artists: normalizeString(result.artist_names),
+                    title: normalizeString(result.title) ?? null,
+                    fullTitle: normalizeString(result.full_title) ?? null,
+                    artists: normalizeString(result.artist_names) ?? null,
                     primaryArtist: {
-                        name: normalizeString(result.primary_artist.name),
-                        url: result.primary_artist.url,
+                        name: normalizeString(result.primary_artist.name) ?? null,
+                        url: result.primary_artist.url ?? null,
+                        headerImage: result.primary_artist.header_image_url ?? null,
+                        image: result.primary_artist.image_url ?? null,
                     },
-                    release: result.release_date_components,
+                    featuredArtists: Array.isArray(result.featured_artists) && result.featured_artists.length > 0
+                        ? result.featured_artists.map((a) => ({
+                            name: a.name ?? null,
+                            url: a.url ?? null,
+                            headerImage: a.header_image_url ?? null,
+                            image: a.image_url ?? null,
+                        }))
+                        : [],
+                    releaseDate: result.release_date_components ?? null,
                 },
                 resources: {
-                    thumbnail: result.song_art_image_thumbnail_url,
-                    image: result.song_art_image_url,
+                    thumbnail: result.song_art_image_thumbnail_url ?? null,
+                    image: result.song_art_image_url ?? null,
                 },
-                lyricsState: result.lyrics_state,
-                id: result.id,
+                lyricsState: result.lyrics_state ?? null,
+                id: result.id ?? null,
             }));
 
         if(artist && song)
@@ -109,9 +119,13 @@ export async function getMeta({ q, artist, song }: Partial<Record<"q" | "artist"
 }
 
 /**
- * Removes invisible characters and control characters from a string
+ * Removes invisible characters and control characters from a string  
+ * Returns null if the input is not a string
  */
-function normalizeString(str: string)
+function normalizeString(str: unknown)
 {
+    if(!str || typeof str !== "string")
+        return null;
+
     return str.replace(/[\u0000-\u001F\u007F-\u009F\u200B]/g, "").replace(/\u00A0/g, " ");
 }

+ 25 - 15
src/types.d.ts

@@ -1,23 +1,33 @@
 //#SECTION meta
 
+interface Artist {
+    name: string | null;
+    url: string | null;
+    image: string | null;
+    headerImage: string | null;
+}
+
 export interface SongMeta {
-    url: string;
-    path: string;
+    url: string | null;
+    path: string | null;
     meta: {
-        title: string;
-        fullTitle: string;
-        artists: string;
-        primaryArtist: {
-            name: string;
-            url: string;
-        },
-    },
+        title: string | null;
+        fullTitle: string | null;
+        artists: string | null;
+        releaseDate: {
+            year: number | null;
+            month: number | null;
+            day: number | null;
+        };
+        primaryArtist: Artist | null;
+        featuredArtists: Artist[];
+    };
     resources: {
-        thumbnail: string;
-        image: string;
-    },
-    lyricsState: string;
-    id: number;
+        thumbnail: string | null;
+        image: string | null;
+    };
+    lyricsState: string | null;
+    id: number | null;
 }
 
 //#SECTION server