Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support browser environment #37

Merged
merged 7 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/local.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import SpotifyUri from './spotify-uri'
import { SpotifyTypes } from './types-enum'
import { encode } from './util'

export default class Local extends SpotifyUri {
public artist: string
public album: string
Expand All @@ -13,7 +15,7 @@ export default class Local extends SpotifyUri {
track: string,
seconds: number
) {
super(uri, "")
super(uri, '', SpotifyTypes.Local)
this.artist = artist
this.album = album
this.track = track
Expand All @@ -25,10 +27,14 @@ export default class Local extends SpotifyUri {
}

public toURI (): string {
return `spotify:${this.type}:${encode(this.artist)}:${encode(this.album)}:${encode(this.track)}:${this.seconds}`
return `spotify:${this.type}:${encode(this.artist)}:${encode(
this.album
)}:${encode(this.track)}:${this.seconds}`
}

public toURL (): string {
return `/${this.type}/${encode(this.artist)}/${encode(this.album)}/${encode(this.track)}/${this.seconds}`
return `/${this.type}/${encode(this.artist)}/${encode(this.album)}/${encode(
this.track
)}/${this.seconds}`
}
}
20 changes: 10 additions & 10 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { URL } from 'url'
import Local from './local'
import Search from './search'
import Playlist from './playlist'
Expand All @@ -12,7 +11,6 @@ import SpotifyUri from './spotify-uri'
import { decode } from './util'
import { ParsedSpotifyUri } from '.'
import { SpotifyTypes } from './types-enum'

/**
* Parses a "Spotify URI".
*
Expand All @@ -27,7 +25,9 @@ export default function parse (input: string | SpotifyUri): ParsedSpotifyUri {
if (hostname === 'embed.spotify.com') {
const parsedQs = Object.fromEntries(searchParams)
if (typeof parsedQs.uri !== 'string') {
throw new Error('Parsed query string was not valid: ' + searchParams.toString())
throw new Error(
'Parsed query string was not valid: ' + searchParams.toString()
)
}
return parse(parsedQs.uri)
}
Expand Down Expand Up @@ -56,7 +56,7 @@ function parseParts (uri: string, parts: string[]): ParsedSpotifyUri {
}
const len = parts.length
if (spotifyType === SpotifyTypes.Search) {
return new Search(uri, decode(parts.slice(2).join(':')))
return new Search(uri, decode(parts.slice(2).join(':')), spotifyType)
}
if (len >= 3 && spotifyType === SpotifyTypes.Local) {
return new Local(
Expand All @@ -77,22 +77,22 @@ function parseParts (uri: string, parts: string[]): ParsedSpotifyUri {
return new Playlist(uri, decode(parts[2]))
}
if (len === 3 && spotifyType === SpotifyTypes.User) {
return new User(uri, decode(parts[2]))
return new User(uri, decode(parts[2]), spotifyType)
}
if (spotifyType === SpotifyTypes.Artist) {
return new Artist(uri, parts[2])
return new Artist(uri, parts[2], spotifyType)
}
if (spotifyType === SpotifyTypes.Album) {
return new Album(uri, parts[2])
return new Album(uri, parts[2], spotifyType)
}
if (spotifyType === SpotifyTypes.Track) {
return new Track(uri, parts[2])
return new Track(uri, parts[2], spotifyType)
}
if (spotifyType === SpotifyTypes.Episode) {
return new Episode(uri, parts[2])
return new Episode(uri, parts[2], spotifyType)
}
if (spotifyType === SpotifyTypes.Show) {
return new Show(uri, parts[2])
return new Show(uri, parts[2], spotifyType)
}
throw new TypeError(`Could not determine type for: ${uri}`)
}
4 changes: 3 additions & 1 deletion src/playlist.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import SpotifyUri from './spotify-uri'
import { SpotifyTypes } from './types-enum'
import { encode } from './util'

export default class Playlist extends SpotifyUri {
public user?: string

constructor (uri: string, id: string, user?: string) {
super(uri, id)
super(uri, id, SpotifyTypes.Playlist)
if (typeof user === 'string') {
this.user = user
}
Expand Down
18 changes: 9 additions & 9 deletions src/spotify-uri.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { SpotifyTypes } from './types-enum';
import { SpotifyTypes } from './types-enum'
import { encode } from './util'

export default abstract class SpotifyUri {
public type: SpotifyTypes;
public id: string;
public uri: string;

constructor (uri: string, id : string) {
this.uri = uri;
this.id = id;
this.type = this.constructor.name.toLowerCase() as SpotifyTypes;
public type: SpotifyTypes
public id: string
public uri: string

constructor (uri: string, id: string, type: SpotifyTypes) {
this.uri = uri
this.id = id
this.type = type
}

public static is (v: any): v is SpotifyUri {
Expand Down
22 changes: 11 additions & 11 deletions src/types-enum.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export enum SpotifyTypes {
Album = 'album',
Artist = 'artist',
Episode = 'episode',
Local = 'local',
Playlist = 'playlist',
Search = 'search',
Show = 'show',
Track = 'track',
User = 'user',
Embed = 'embed'
}
Album = 'album',
Artist = 'artist',
Episode = 'episode',
Local = 'local',
Playlist = 'playlist',
Search = 'search',
Show = 'show',
Track = 'track',
User = 'user',
Embed = 'embed'
}