diff --git a/README.md b/README.md index fa03353..ac26508 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ This npm package provides a simple wrapper for interacting with the Tidal API. It allows you to retrieve information about tracks, artists, albums, videos, playlists, and mixes based on their URLs. +**Best part is you don't need any api key or authentication token.** + ## Installation To install the package, run the following command: @@ -73,6 +75,7 @@ if (trackId) { #### Artist, Album, Video, Playlist, Mix Similarly, you can use the corresponding functions (`getArtistId`, `getArtistInfo`, `getAlbumId`, `getAlbumInfo`, `getVideoId`, `getVideoInfo`, `getPlaylistId`, `getPlaylistInfo`, `getMixId`, `getMixInfo`) for retrieving information about artists, albums, videos, playlists, and mixes. +You can also use `getTrackPreview` and `getVideoPreview` for retrieving preview url of a track or video. ## Contributing diff --git a/bun.lockb b/bun.lockb index 45b6165..284cdf6 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/docs/README.md b/docs/README.md deleted file mode 100644 index 4b20a33..0000000 --- a/docs/README.md +++ /dev/null @@ -1,87 +0,0 @@ -**tidal-music-api** • [**Docs**](globals.md) - ---- - -# Tidal API - -## Introduction - -This npm package provides a simple wrapper for interacting with the Tidal API. It allows you to retrieve information about tracks, artists, albums, videos, playlists, and mixes based on their URLs. - -## Installation - -To install the package, run the following command: - -```bash -npm install tidal-music-api -``` - -## Usage - -### Getting Started - -First, import the `getInfo` function from the package: - -```javascript -const { getInfo } = require("tidal-music-api"); -``` - -### Retrieving Information - -You can use the `getInfo` function to retrieve information based on a Tidal URL: - -```javascript -const url = "https://tidal.com/browse/track/12345678"; - -getInfo(url) - .then((info) => { - console.log(info); - }) - .catch((error) => { - console.error("Error:", error.message); - }); -``` - -The `getInfo` function returns a Promise that resolves to the information based on the URL type (track, artist, album, video, playlist, or mix). - -### Supported URL Types - -- Track: `https://tidal.com/browse/track/` -- Artist: `https://tidal.com/browse/artist/` -- Album: `https://tidal.com/browse/album/` -- Video: `https://tidal.com/browse/video/` -- Playlist: `https://tidal.com/browse/playlist/` -- Mix: `https://tidal.com/browse/mix/` - -### Additional Usage - -#### Track - -You can also use the `getTrackId` and `getTrackInfo` functions to retrieve information about a specific track: - -```javascript -const { getTrackId, getTrackInfo } = require("tidal-music-api"); - -const trackId = getTrackId(url); -if (trackId) { - getTrackInfo(trackId) - .then((track) => { - console.log(track); - }) - .catch((error) => { - console.error("Error:", error.message); - }); -} -``` - -#### Artist, Album, Video, Playlist, Mix - -Similarly, you can use the corresponding functions (`getArtistId`, `getArtistInfo`, `getAlbumId`, `getAlbumInfo`, `getVideoId`, `getVideoInfo`, `getPlaylistId`, `getPlaylistInfo`, `getMixId`, `getMixInfo`) for retrieving information about artists, albums, videos, playlists, and mixes. - -## Contributing - -Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request. - -## License - -This package is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. diff --git a/docs/functions/extractURLType.md b/docs/functions/extractURLType.md deleted file mode 100644 index c5f5ba7..0000000 --- a/docs/functions/extractURLType.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / extractURLType - -# Function: extractURLType() - -> **extractURLType**(`url`): `string` \| `null` - -Extracts the type of URL from a Tidal URL. - -## Parameters - -• **url**: `string` - -The Tidal URL to extract the type from. - -## Returns - -`string` \| `null` - -- The type of URL extracted from the input URL, or null if the URL does not match the expected format. - -## Source - -index.ts:55 diff --git a/docs/functions/getAlbumId.md b/docs/functions/getAlbumId.md deleted file mode 100644 index b805f3f..0000000 --- a/docs/functions/getAlbumId.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getAlbumId - -# Function: getAlbumId() - -> **getAlbumId**(`url`): `number` - -Extracts the album ID from a given URL. - -## Parameters - -• **url**: `string` - -The URL from which to extract the album ID. - -## Returns - -`number` - -- The extracted album ID, or NaN if the URL does not match the expected format. - -## Source - -services/album.ts:60 diff --git a/docs/functions/getAlbumInfo.md b/docs/functions/getAlbumInfo.md deleted file mode 100644 index 847c2d6..0000000 --- a/docs/functions/getAlbumInfo.md +++ /dev/null @@ -1,31 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getAlbumInfo - -# Function: getAlbumInfo() - -> **getAlbumInfo**(`albumId`, `countryCode`?): `Promise`\<`Album`\> - -Retrieves album information from the Tidal API based on the provided album ID and country code. - -## Parameters - -• **albumId**: `number` - -The ID of the album. - -• **countryCode?**: `string`= `"US"` - -The country code to use for retrieving the album information. Defaults to "US". - -## Returns - -`Promise`\<`Album`\> - -A promise that resolves to an Album object containing the album information. - -## Source - -services/album.ts:11 diff --git a/docs/functions/getArtistId.md b/docs/functions/getArtistId.md deleted file mode 100644 index daa6701..0000000 --- a/docs/functions/getArtistId.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getArtistId - -# Function: getArtistId() - -> **getArtistId**(`url`): `number` - -Extracts the artist ID from a Tidal URL. - -## Parameters - -• **url**: `string` - -The Tidal URL from which to extract the artist ID. - -## Returns - -`number` - -The extracted artist ID, or NaN if the URL does not match the expected format. - -## Source - -services/artist.ts:115 diff --git a/docs/functions/getArtistInfo.md b/docs/functions/getArtistInfo.md deleted file mode 100644 index 657b115..0000000 --- a/docs/functions/getArtistInfo.md +++ /dev/null @@ -1,31 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getArtistInfo - -# Function: getArtistInfo() - -> **getArtistInfo**(`artistId`, `countryCode`?): `Promise`\<`Artist`\> - -Retrieves the information of an artist from the Tidal API based on the provided artist ID and country code. - -## Parameters - -• **artistId**: `number` - -The ID of the artist. - -• **countryCode?**: `string`= `"US"` - -The country code to use for retrieving the artist information. Defaults to "US". - -## Returns - -`Promise`\<`Artist`\> - -A promise that resolves to an Artist object containing the artist information. - -## Source - -services/artist.ts:11 diff --git a/docs/functions/getInfo.md b/docs/functions/getInfo.md deleted file mode 100644 index 13d6298..0000000 --- a/docs/functions/getInfo.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getInfo - -# Function: getInfo() - -> **getInfo**(`url`): `Promise`\<`Track` \| `Artist` \| `Album` \| `Video` \| `Playlist` \| `Mix`\> - -Retrieves information based on the type of URL provided. - -## Parameters - -• **url**: `string` - -The URL to extract information from. - -## Returns - -`Promise`\<`Track` \| `Artist` \| `Album` \| `Video` \| `Playlist` \| `Mix`\> - -A promise that resolves to the information based on the URL type. - -## Source - -index.ts:19 diff --git a/docs/functions/getMixId.md b/docs/functions/getMixId.md deleted file mode 100644 index 21fdbfb..0000000 --- a/docs/functions/getMixId.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getMixId - -# Function: getMixId() - -> **getMixId**(`url`): `string` - -Extracts the mix ID from a given URL. - -## Parameters - -• **url**: `string` - -The URL from which to extract the mix ID. - -## Returns - -`string` - -- The extracted mix ID, or empty string if the URL does not match the expected format. - -## Source - -services/mix.ts:58 diff --git a/docs/functions/getMixInfo.md b/docs/functions/getMixInfo.md deleted file mode 100644 index 0c9d470..0000000 --- a/docs/functions/getMixInfo.md +++ /dev/null @@ -1,31 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getMixInfo - -# Function: getMixInfo() - -> **getMixInfo**(`mixId`, `countryCode`?): `Promise`\<`Mix`\> - -Retrieves mix information from the Tidal API based on the provided mix ID and country code. - -## Parameters - -• **mixId**: `string` - -The ID of the mix. - -• **countryCode?**: `string`= `"US"` - -The country code to use for retrieving the mix information. Defaults to "US". - -## Returns - -`Promise`\<`Mix`\> - -A promise that resolves to a Mix object containing the mix information. - -## Source - -services/mix.ts:11 diff --git a/docs/functions/getPlaylistId.md b/docs/functions/getPlaylistId.md deleted file mode 100644 index 6b42789..0000000 --- a/docs/functions/getPlaylistId.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getPlaylistId - -# Function: getPlaylistId() - -> **getPlaylistId**(`url`): `string` - -Extracts the playlist ID from a Tidal playlist URL. - -## Parameters - -• **url**: `string` - -The URL of the Tidal playlist. - -## Returns - -`string` - -The playlist ID, or empty string if the URL does not match the expected format. - -## Source - -services/playlist.ts:61 diff --git a/docs/functions/getPlaylistInfo.md b/docs/functions/getPlaylistInfo.md deleted file mode 100644 index 884ee18..0000000 --- a/docs/functions/getPlaylistInfo.md +++ /dev/null @@ -1,31 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getPlaylistInfo - -# Function: getPlaylistInfo() - -> **getPlaylistInfo**(`playlistId`, `countryCode`?): `Promise`\<`Playlist`\> - -Retrieves playlist information based on the provided playlist ID and country code. - -## Parameters - -• **playlistId**: `string` - -The ID of the playlist. - -• **countryCode?**: `string`= `"US"` - -The country code to use for retrieving the playlist information. Defaults to "US". - -## Returns - -`Promise`\<`Playlist`\> - -A promise that resolves to a Playlist object containing the playlist information. - -## Source - -services/playlist.ts:11 diff --git a/docs/functions/getTrackId.md b/docs/functions/getTrackId.md deleted file mode 100644 index 020f1d8..0000000 --- a/docs/functions/getTrackId.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getTrackId - -# Function: getTrackId() - -> **getTrackId**(`url`): `number` - -Extracts the track ID from a Tidal track URL. - -## Parameters - -• **url**: `string` - -The Tidal track URL from which to extract the ID. - -## Returns - -`number` - -The extracted track ID, or NaN if the URL does not match the expected format. - -## Source - -services/track.ts:81 diff --git a/docs/functions/getTrackInfo.md b/docs/functions/getTrackInfo.md deleted file mode 100644 index ff09fc3..0000000 --- a/docs/functions/getTrackInfo.md +++ /dev/null @@ -1,31 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getTrackInfo - -# Function: getTrackInfo() - -> **getTrackInfo**(`trackId`, `countryCode`?): `Promise`\<`Track`\> - -Retrieves track information from the Tidal API based on the provided track ID and country code. - -## Parameters - -• **trackId**: `number` - -The ID of the track. - -• **countryCode?**: `string`= `"US"` - -The country code to use for retrieving the track information. Defaults to "US". - -## Returns - -`Promise`\<`Track`\> - -A promise that resolves to a Track object containing the track information. - -## Source - -services/track.ts:11 diff --git a/docs/functions/getVideoId.md b/docs/functions/getVideoId.md deleted file mode 100644 index 0bba1ff..0000000 --- a/docs/functions/getVideoId.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getVideoId - -# Function: getVideoId() - -> **getVideoId**(`url`): `number` - -Extracts the video ID from a given URL. - -## Parameters - -• **url**: `string` - -The URL from which to extract the video ID. - -## Returns - -`number` - -- The extracted video ID, or NaN if the URL does not match the expected format. - -## Source - -services/video.ts:77 diff --git a/docs/functions/getVideoInfo.md b/docs/functions/getVideoInfo.md deleted file mode 100644 index 0fcef3b..0000000 --- a/docs/functions/getVideoInfo.md +++ /dev/null @@ -1,27 +0,0 @@ -[**tidal-music-api**](../README.md) • **Docs** - ---- - -[tidal-music-api](../globals.md) / getVideoInfo - -# Function: getVideoInfo() - -> **getVideoInfo**(`videoId`): `Promise`\<`Video`\> - -Retrieves the video information for the given video ID. - -## Parameters - -• **videoId**: `number` - -The ID of the video. - -## Returns - -`Promise`\<`Video`\> - -A promise that resolves to the video information. - -## Source - -services/video.ts:10 diff --git a/docs/globals.md b/docs/globals.md deleted file mode 100644 index 5487dd3..0000000 --- a/docs/globals.md +++ /dev/null @@ -1,22 +0,0 @@ -[**tidal-music-api**](README.md) • **Docs** - ---- - -# tidal-music-api - -## Functions - -- [extractURLType](functions/extractURLType.md) -- [getAlbumId](functions/getAlbumId.md) -- [getAlbumInfo](functions/getAlbumInfo.md) -- [getArtistId](functions/getArtistId.md) -- [getArtistInfo](functions/getArtistInfo.md) -- [getInfo](functions/getInfo.md) -- [getMixId](functions/getMixId.md) -- [getMixInfo](functions/getMixInfo.md) -- [getPlaylistId](functions/getPlaylistId.md) -- [getPlaylistInfo](functions/getPlaylistInfo.md) -- [getTrackId](functions/getTrackId.md) -- [getTrackInfo](functions/getTrackInfo.md) -- [getVideoId](functions/getVideoId.md) -- [getVideoInfo](functions/getVideoInfo.md) diff --git a/ex/getInfo.js b/ex/getInfo.js index 0e2e7a7..1976329 100644 --- a/ex/getInfo.js +++ b/ex/getInfo.js @@ -1,6 +1,6 @@ const { getInfo } = require("../lib/index"); (async () => { - const url = "https://tidal.com/album/230064409"; + const url = "https://tidal.com/album/70973001"; const info = await getInfo(url); - console.log(info); + console.log(JSON.stringify(info, null, 2)); })(); diff --git a/package.json b/package.json index 234ca5a..70e494c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tidal-music-api", - "version": "1.0.0", + "version": "1.1.0", "description": "Interact with the Tidal API to retrieve music information such as tracks, artists, albums, videos, playlists, and mixes by URL.", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -12,7 +12,6 @@ "format:src": "prettier --write 'src/**/*.ts'", "format:lib": "prettier --write 'lib/**/*.js'", "format:docs": "prettier --write 'docs/**/*.md'", - "docs": "npx typedoc --out docs --plugin typedoc-plugin-markdown ./src", "test": "node ex/test.js" }, "repository": { @@ -38,11 +37,9 @@ "bugs": { "url": "https://github.com/LakhindarPal/tidal-music-api/issues" }, - "homepage": "https://github.com/LakhindarPal/tidal-music-api#readme", + "homepage": "https://github.com/LakhindarPal/tidal-music-api/wiki", "devDependencies": { "prettier": "^3.2.5", - "typedoc": "^0.25.13", - "typedoc-plugin-markdown": "^4.0.1", "typescript": "^5.4.5" }, "dependencies": { diff --git a/src/index.ts b/src/index.ts index 2516f90..fd6e31b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,8 +8,9 @@ import { getAlbumId, getAlbumInfo } from "./services/album"; import { getArtistId, getArtistInfo } from "./services/artist"; import { getMixId, getMixInfo } from "./services/mix"; import { getPlaylistId, getPlaylistInfo } from "./services/playlist"; -import { getTrackId, getTrackInfo } from "./services/track"; -import { getVideoId, getVideoInfo } from "./services/video"; +import { getTrackId, getTrackInfo, getTrackPreview } from "./services/track"; +import { getVideoId, getVideoInfo, getVideoPreview } from "./services/video"; +import { getURLType } from "./utils/getURLType"; /** * Retrieves information based on the type of URL provided. @@ -19,26 +20,26 @@ import { getVideoId, getVideoInfo } from "./services/video"; async function getInfo( url: string, ): Promise { - const type: string | null = extractURLType(url); + const type: string | null = getURLType(url); if (!type) throw new Error("Invalid URL"); switch (type) { - case "track": + case "TRACK": const trackId: number = getTrackId(url); return await getTrackInfo(trackId); - case "artist": + case "ARTIST": const artistId: number = getArtistId(url); return await getArtistInfo(artistId); - case "album": + case "ALBUM": const albumId: number = getAlbumId(url); return await getAlbumInfo(albumId); - case "video": + case "VIDEO": const videoId: number = getVideoId(url); return await getVideoInfo(videoId); - case "playlist": + case "PLAYLIST": const playlistId: string = getPlaylistId(url); return await getPlaylistInfo(playlistId); - case "mix": + case "MIX": const mixId: string = getMixId(url); return await getMixInfo(mixId); default: @@ -46,21 +47,8 @@ async function getInfo( } } -/** - * Extracts the type of URL from a Tidal URL. - * - * @param {string} url - The Tidal URL to extract the type from. - * @return {string | null} - The type of URL extracted from the input URL, or null if the URL does not match the expected format. - */ -function extractURLType(url: string): string | null { - const regex: RegExp = - /^https:\/\/tidal\.com\/(?:browse\/)?(track|artist|album|video|playlist)\/[0-9a-zA-Z\-]+/; - const match: RegExpMatchArray | null = url.match(regex); - return match ? match[1] : null; -} - export { - extractURLType, + getURLType, getInfo, getAlbumId, getAlbumInfo, @@ -72,6 +60,8 @@ export { getPlaylistInfo, getTrackId, getTrackInfo, + getTrackPreview, getVideoId, getVideoInfo, + getVideoPreview, }; diff --git a/src/models/Album.ts b/src/models/Album.ts index 82946ae..cd84f2e 100644 --- a/src/models/Album.ts +++ b/src/models/Album.ts @@ -1,10 +1,8 @@ import Track from "./Track"; import type { TrackData } from "./Track"; - -interface Artist { - id: number; - name: string; -} +import Image from "./Image"; +import type { ImageData } from "./Image"; +import type { ArtistLite } from "./ArtistLite"; export interface AlbumData { id: number; @@ -12,9 +10,9 @@ export interface AlbumData { title: string; explicit: boolean; releaseDate: string; - artists: Artist[]; - tracks?: TrackData[]; - image: { [key: string]: string | null }; + artists: ArtistLite[]; + tracks: TrackData[] | null; + image: ImageData; } export default class Album { @@ -23,9 +21,9 @@ export default class Album { title: string; explicit: boolean; releasedAt: string; - artists: Artist[]; + artists: ArtistLite[]; tracks: Track[]; - image: { [key: string]: string | null }; + image: Image; /** * Initializes a new instance of the Album class. @@ -40,11 +38,6 @@ export default class Album { this.releasedAt = data.releaseDate; this.artists = data.artists; this.tracks = (data.tracks || []).map((item) => new Track(item)); - this.image = Object.fromEntries( - Object.entries(data.image).map(([key, value]) => [ - key, - value ? "https:" + value : null, - ]), - ); + this.image = new Image(data.image); } } diff --git a/src/models/AlbumLite.ts b/src/models/AlbumLite.ts new file mode 100644 index 0000000..be4e175 --- /dev/null +++ b/src/models/AlbumLite.ts @@ -0,0 +1,4 @@ +export interface AlbumLite { + id: number; + title: string; +} diff --git a/src/models/Artist.ts b/src/models/Artist.ts index 467b5ba..243fd89 100644 --- a/src/models/Artist.ts +++ b/src/models/Artist.ts @@ -3,11 +3,13 @@ import Video from "./Video"; import Album from "./Album"; import Playlist from "./Playlist"; import Mix from "./Mix"; +import Image from "./Image"; import type { TrackData } from "./Track"; import type { AlbumData } from "./Album"; import type { PlaylistData } from "./Playlist"; import type { VideoData } from "./Video"; import type { MixData } from "./Mix"; +import type { ImageData } from "./Image"; export interface ArtistData { id: number; @@ -21,9 +23,7 @@ export interface ArtistData { playlists: PlaylistData[]; videos: VideoData[]; tracks: TrackData[]; - image: { - [key: string]: string | null; - }; + image: ImageData; } export default class Artist { @@ -38,9 +38,7 @@ export default class Artist { playlists: Playlist[]; videos: Video[]; tracks: Track[]; - image: { - [key: string]: string | null; - }; + image: Image; /** * Initializes a new instance of the Artist class. @@ -59,11 +57,6 @@ export default class Artist { this.playlists = data.playlists.map((item) => new Playlist(item)); this.videos = data.videos.map((item) => new Video(item)); this.tracks = data.tracks.map((item) => new Track(item)); - this.image = Object.fromEntries( - Object.entries(data.image).map(([key, value]) => [ - key, - value ? "https:" + value : null, - ]), - ); + this.image = new Image(data.image); } } diff --git a/src/models/ArtistLite.ts b/src/models/ArtistLite.ts new file mode 100644 index 0000000..f98f4e7 --- /dev/null +++ b/src/models/ArtistLite.ts @@ -0,0 +1,4 @@ +export interface ArtistLite { + id: number; + name: string; +} diff --git a/src/models/Image.ts b/src/models/Image.ts new file mode 100644 index 0000000..e29c641 --- /dev/null +++ b/src/models/Image.ts @@ -0,0 +1,28 @@ +export interface ImageData { + original: string | null; + large: string; + medium: string; + small: string; + xsmall: string; +} + +export default class Image { + original: string | null; + large: string; + medium: string; + small: string; + xsmall: string; + + /** + * Initializes a new instance of the Image class. + * + * @param {ImageData} data - The data used to initialize the image instance. + */ + constructor(data: ImageData) { + this.original = data.original ? "https:" + data.original : null; + this.large = "https:" + data.large; + this.medium = "https:" + data.medium; + this.small = "https:" + data.small; + this.xsmall = "https:" + data.xsmall; + } +} diff --git a/src/models/Mix.ts b/src/models/Mix.ts index 9a44d08..b61f629 100644 --- a/src/models/Mix.ts +++ b/src/models/Mix.ts @@ -1,27 +1,24 @@ import Track from "./Track"; +import Image from "./Image"; import type { TrackData } from "./Track"; - -interface Artist { - id: number; - name: string; -} +import type { ArtistLite } from "./ArtistLite"; export interface MixData { id: number; contentType: string; title: string; - artists: Artist[]; - tracks?: TrackData[]; - image: { [key: string]: string | null }; + artists: ArtistLite[]; + tracks: TrackData[] | null; + image: Image; } export default class Mix { id: number; type: string; title: string; - artists: Artist[]; + artists: ArtistLite[]; tracks: Track[]; - image: { [key: string]: string | null }; + image: Image; /** * Initializes a new instance of the Mix class. diff --git a/src/models/Playlist.ts b/src/models/Playlist.ts index a37da19..ade6958 100644 --- a/src/models/Playlist.ts +++ b/src/models/Playlist.ts @@ -1,22 +1,20 @@ import Track from "./Track"; +import Image from "./Image"; import type { TrackData } from "./Track"; - -interface Creator { - id: number; - name: string; -} +import type { ImageData } from "./Image"; +import type { ArtistLite } from "./ArtistLite"; export interface PlaylistData { uuid: string; contentType: string; title: string; description: string; - creator: Creator; + creator: ArtistLite; created: string; lastUpdated: string; - image: { [key: string]: string | null }; + image: ImageData; numberOfTracks: number; - tracks?: TrackData[]; + tracks: TrackData[] | null; } export default class Playlist { @@ -24,10 +22,10 @@ export default class Playlist { type: string; title: string; description: string; - creator: Creator; + creator: ArtistLite; createdAt: string; updatedAt: string; - image: { [key: string]: string | null }; + image: Image; size: number; tracks: Track[]; @@ -44,12 +42,7 @@ export default class Playlist { this.creator = data.creator; this.createdAt = data.created; this.updatedAt = data.lastUpdated; - this.image = Object.fromEntries( - Object.entries(data.image).map(([key, value]) => [ - key, - value ? "https:" + value : null, - ]), - ); + this.image = new Image(data.image); this.size = data.numberOfTracks; this.tracks = (data.tracks || []).map((item) => new Track(item)); } diff --git a/src/models/Track.ts b/src/models/Track.ts index 16d0dd6..62b4dd7 100644 --- a/src/models/Track.ts +++ b/src/models/Track.ts @@ -1,12 +1,7 @@ -interface Album { - id: number; - title: string; -} - -interface Artist { - id: number; - name: string; -} +import Image from "./Image"; +import type { ImageData } from "./Image"; +import type { AlbumLite } from "./AlbumLite"; +import type { ArtistLite } from "./ArtistLite"; export interface TrackData { id: number; @@ -15,11 +10,9 @@ export interface TrackData { duration: number; explicit: boolean; trackNumber: number; - artists: Artist[]; - album: Album | null; - image: { - [key: string]: string | null; - }; + artists: ArtistLite[]; + album: AlbumLite | null; + image: ImageData; } export default class Track { @@ -29,11 +22,9 @@ export default class Track { duration: number; explicit: boolean; position: number; - artists: Artist[]; - album: Album | null; - image: { - [key: string]: string | null; - }; + artists: ArtistLite[]; + album: AlbumLite | null; + image: Image; /** * Initializes a new instance of the Track class. @@ -49,11 +40,6 @@ export default class Track { this.position = data.trackNumber; this.artists = data.artists; this.album = data.album; - this.image = Object.fromEntries( - Object.entries(data.image).map(([key, value]) => [ - key, - value ? "https:" + value : null, - ]), - ); + this.image = new Image(data.image); } } diff --git a/src/models/Video.ts b/src/models/Video.ts index 05cde29..d806d15 100644 --- a/src/models/Video.ts +++ b/src/models/Video.ts @@ -1,7 +1,6 @@ -interface Artist { - id: number; - name: string; -} +import Image from "./Image"; +import type { ImageData } from "./Image"; +import type { ArtistLite } from "./ArtistLite"; export interface VideoData { id: number; @@ -10,10 +9,8 @@ export interface VideoData { duration: number; explicit: boolean; releaseDate: string; - artists: Artist[]; - image: { - [key: string]: string | null; - }; + artists: ArtistLite[]; + image: ImageData; } export default class Video { @@ -23,10 +20,8 @@ export default class Video { duration: number; explicit: boolean; releasedAt: string; - artists: Artist[]; - image: { - [key: string]: string | null; - }; + artists: ArtistLite[]; + image: Image; /** * Initializes a new instance of the Video class. @@ -41,11 +36,6 @@ export default class Video { this.explicit = data.explicit; this.releasedAt = data.releaseDate; this.artists = data.artists; - this.image = Object.fromEntries( - Object.entries(data.image).map(([key, value]) => [ - key, - value ? "https:" + value : null, - ]), - ); + this.image = new Image(data.image); } } diff --git a/src/utils/getURLType.ts b/src/utils/getURLType.ts new file mode 100644 index 0000000..3cfc9ca --- /dev/null +++ b/src/utils/getURLType.ts @@ -0,0 +1,12 @@ +/** + * Extracts the type of URL from a Tidal URL. + * + * @param {string} url - The Tidal URL to extract the type from. + * @return {string | null} - The type of URL extracted from the input URL, or null if the URL does not match the expected format. + */ +export function getURLType(url: string): string | null { + const regex: RegExp = + /^https:\/\/tidal\.com\/(?:browse\/)?(track|artist|album|video|playlist)\/[0-9a-zA-Z\-]+/; + const match: RegExpMatchArray | null = url.match(regex); + return match ? match[1].toUpperCase() : null; +}