@social-embed/lib
Version:
URL detection and parsing for embed providers (YouTube, other OEmbed compatible providers),
97 lines • 3.66 kB
TypeScript
import { EmbedProvider } from '../provider';
/**
* A list of supported Spotify content types recognized by the library:
* - track
* - album
* - playlist
* - artist
* - show (podcast)
* - episode (podcast episode)
*
* @remarks
* These values are used to parse and build embeddable Spotify URLs for each specific content type.
*/
export declare const SPOTIFY_TYPES: readonly ["track", "album", "playlist", "artist", "show", "episode"];
/**
* A regex that matches open or embed-style Spotify URLs.
*
* @example
* - `https://open.spotify.com/track/1w4etUoKfql47wtTFq031f`
* - `https://open.spotify.com/album/1DFixLWuPkv3KT3TnV35m3`
* - `https://open.spotify.com/show/5YEXv3C5fnMA3lFzNim4Ya`
* - `https://open.spotify.com/embed/episode/4XplJhQEj1Qp6QzrbA5sYk`
*
* @remarks
* - Derived from [a gist by TrevorJTClarke](https://gist.github.com/TrevorJTClarke/a14c37db3c11ee23a700).
* Thank you, **@TrevorJTClarke**!
*
* **Pattern Explanation**:
* 1. Optional protocol (`https?`)
* 2. Optional `embed.` or `open.` subdomain
* 3. Must have `"spotify.com/"`
* 4. Captures one of the recognized `SPOTIFY_TYPES`
* 5. Followed by a 22-character ID
* 6. Optionally captures a `?si=` param
*/
export declare const spotifyUrlRegex: RegExp;
/**
* A regex that matches Spotify URIs, such as:
* - `spotify:track:1w4etUoKfql47wtTFq031f`
* - `spotify:album:1DFixLWuPkv3KT3TnV35m3`
* - `spotify:artist:1Xyo4u8uXC1ZmMpatF05PJ`
* - `spotify:show:5YEXv3C5fnMA3lFzNim4Ya`
* - `spotify:episode:4XplJhQEj1Qp6QzrbA5sYk`
*
* @remarks
* Captures both the content type and the 22-character ID.
*/
export declare const spotifySymbolRegex: RegExp;
/**
* Extracts the Spotify ID and type from a given Spotify URL or URI.
*
* @param url - A Spotify web URL or `spotify:` URI.
* @returns A tuple `[id, type]`, where `id` is the 22-char ID and `type` is one of `track`, `album`, `playlist`, `artist`, `show`, or `episode`.
* @example
* ```ts
* // For "https://open.spotify.com/track/1w4etUoKfql47wtTFq031f"
* // => ["1w4etUoKfql47wtTFq031f", "track"]
*
* // For "spotify:album:1DFixLWuPkv3KT3TnV35m3"
* // => ["1DFixLWuPkv3KT3TnV35m3", "album"]
* ```
*
* @remarks
* Returns `["", ""]` if there's no valid match.
*/
export declare function getSpotifyIdAndTypeFromUrl(url: string): [string, string];
/**
* Constructs an embeddable Spotify URL from an ID and optional content type.
*
* @param id - The 22-character Spotify ID (e.g. track/album/playlist ID).
* @param args - The first element in `args` may be the content type. If not provided or invalid, defaults to `"track"`.
* @returns A valid embed URL, e.g. `"https://open.spotify.com/embed/track/12345"`.
*
* @remarks
* - If a recognized `SpotifyType` isn't passed, defaults to `track`.
* - The function is often used inside the {@link SpotifyProvider}.
*
* @example
* ```ts
* // Normally you'd pass [id, type] as args:
* console.log(getSpotifyEmbedUrlFromIdAndType("7ouMYWpwJ422jRcDASZB7P", "track"));
* // => "https://open.spotify.com/embed/track/7ouMYWpwJ422jRcDASZB7P"
* ```
*/
export declare function getSpotifyEmbedUrlFromIdAndType(id: string, ...args: unknown[]): string;
/**
* A provider object implementing the {@link EmbedProvider} interface for Spotify.
*
* @remarks
* - `canParseUrl()` checks both web and `spotify:` URIs.
* - `getIdFromUrl()` returns `[id, type]`.
* - `getEmbedUrlFromId()` calls {@link getSpotifyEmbedUrlFromIdAndType}.
*
* This provider allows handling any recognized Spotify media (tracks, albums, playlists, artists, shows, episodes).
*/
export declare const SpotifyProvider: EmbedProvider;
//# sourceMappingURL=spotify.d.ts.map