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 #36

Closed
wants to merge 6 commits into from
Closed
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
41 changes: 23 additions & 18 deletions src/local.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import SpotifyUri from './spotify-uri'
import { encode } from './util'
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
public track: string
public seconds: number
public artist: string;
public album: string;
public track: string;
public seconds: number;

constructor (
constructor(
uri: string,
artist: string,
album: string,
track: string,
seconds: number
) {
super(uri, "")
this.artist = artist
this.album = album
this.track = track
this.seconds = seconds
super(uri, "", SpotifyTypes.Local);
this.artist = artist;
this.album = album;
this.track = track;
this.seconds = seconds;
}

public static is (v: any): v is Local {
return typeof v === 'object' && v.type === 'local'
public static is(v: any): v is Local {
return typeof v === "object" && v.type === "local";
}

public toURI (): string {
return `spotify:${this.type}:${encode(this.artist)}:${encode(this.album)}:${encode(this.track)}:${this.seconds}`
public toURI(): string {
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}`
public toURL(): string {
return `/${this.type}/${encode(this.artist)}/${encode(this.album)}/${encode(
this.track
)}/${this.seconds}`;
}
}
16 changes: 7 additions & 9 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 Down Expand Up @@ -56,7 +54,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 +75,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}`)
}
3 changes: 2 additions & 1 deletion src/playlist.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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
4 changes: 2 additions & 2 deletions src/spotify-uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default abstract class SpotifyUri {
public id: string;
public uri: string;

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

public static is (v: any): v is SpotifyUri {
Expand Down