spotify-uri
Advanced tools
Comparing version 3.0.4 to 4.0.0
@@ -0,7 +1,22 @@ | ||
declare enum SpotifyTypes { | ||
Album = "album", | ||
Artist = "artist", | ||
Episode = "episode", | ||
Local = "local", | ||
Playlist = "playlist", | ||
Search = "search", | ||
Show = "show", | ||
Track = "track", | ||
User = "user", | ||
Embed = "embed" | ||
} | ||
declare abstract class SpotifyUri { | ||
type: SpotifyTypes; | ||
id: string; | ||
uri: string; | ||
abstract toURL(): string; | ||
abstract toURI(): string; | ||
constructor(uri: string); | ||
constructor(uri: string, id: string); | ||
static is(v: any): v is SpotifyUri; | ||
toURI(): string; | ||
toURL(): string; | ||
toEmbedURL(): string; | ||
@@ -15,3 +30,3 @@ toOpenURL(): string; | ||
* | ||
* @param {String} uri | ||
* @param {String} input | ||
* @return {Object} parsed Spotify uri object | ||
@@ -23,12 +38,7 @@ * @api public | ||
declare class Search extends SpotifyUri { | ||
type: string; | ||
query: string; | ||
constructor(uri: string, query: string); | ||
get query(): string; | ||
static is(v: any): v is Search; | ||
toURI(): string; | ||
toURL(): string; | ||
} | ||
declare class Local extends SpotifyUri { | ||
type: string; | ||
artist: string; | ||
@@ -45,4 +55,2 @@ album: string; | ||
declare class Playlist extends SpotifyUri { | ||
type: string; | ||
id: string; | ||
user?: string; | ||
@@ -56,53 +64,24 @@ constructor(uri: string, id: string, user?: string); | ||
declare class Artist extends SpotifyUri { | ||
type: string; | ||
id: string; | ||
constructor(uri: string, id: string); | ||
static is(v: any): v is Artist; | ||
toURI(): string; | ||
toURL(): string; | ||
} | ||
declare class Album extends SpotifyUri { | ||
type: string; | ||
id: string; | ||
constructor(uri: string, id: string); | ||
static is(v: any): v is Album; | ||
toURI(): string; | ||
toURL(): string; | ||
} | ||
declare class Track extends SpotifyUri { | ||
type: string; | ||
id: string; | ||
constructor(uri: string, id: string); | ||
static is(v: any): v is Track; | ||
toURI(): string; | ||
toURL(): string; | ||
} | ||
declare class User extends SpotifyUri { | ||
type: string; | ||
user: string; | ||
constructor(uri: string, user: string); | ||
get user(): string; | ||
static is(v: any): v is User; | ||
toURI(): string; | ||
toURL(): string; | ||
} | ||
declare class Episode extends SpotifyUri { | ||
type: string; | ||
id: string; | ||
constructor(uri: string, id: string); | ||
static is(v: any): v is Episode; | ||
toURI(): string; | ||
toURL(): string; | ||
} | ||
declare class Show extends SpotifyUri { | ||
type: string; | ||
id: string; | ||
constructor(uri: string, id: string); | ||
static is(v: any): v is Show; | ||
toURI(): string; | ||
toURL(): string; | ||
} | ||
@@ -109,0 +88,0 @@ |
@@ -45,6 +45,6 @@ "use strict"; | ||
function decode(str) { | ||
return decodeURIComponent(str).replace(/\+/g, " "); | ||
return decodeURIComponent(str.replace(/\+/g, " ")); | ||
} | ||
function encode(str) { | ||
return escape(str.replace(/ /g, "+")); | ||
return encodeURIComponent(str).replace(/%20/g, "+").replace(/[!'()*]/g, escape); | ||
} | ||
@@ -54,8 +54,19 @@ | ||
var SpotifyUri = class { | ||
constructor(uri) { | ||
type; | ||
id; | ||
uri; | ||
constructor(uri, id) { | ||
this.uri = uri; | ||
this.id = id; | ||
this.type = this.constructor.name.toLowerCase(); | ||
} | ||
static is(v) { | ||
return Boolean(typeof v === "object" && typeof v.uri === "string"); | ||
return typeof v === "object" && typeof v.uri === "string"; | ||
} | ||
toURI() { | ||
return `spotify:${this.type}:${encode(this.id)}`; | ||
} | ||
toURL() { | ||
return `/${this.type}/${encode(this.id)}`; | ||
} | ||
toEmbedURL() { | ||
@@ -74,5 +85,8 @@ return `https://embed.spotify.com/?uri=${this.toURI()}`; | ||
var Local = class extends SpotifyUri { | ||
artist; | ||
album; | ||
track; | ||
seconds; | ||
constructor(uri, artist, album, track, seconds) { | ||
super(uri); | ||
this.type = "local"; | ||
super(uri, ""); | ||
this.artist = artist; | ||
@@ -84,9 +98,9 @@ this.album = album; | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "local"); | ||
return typeof v === "object" && v.type === "local"; | ||
} | ||
toURI() { | ||
return `spotify:local:${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}`; | ||
} | ||
toURL() { | ||
return `/local/${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}`; | ||
} | ||
@@ -97,16 +111,8 @@ }; | ||
var Search = class extends SpotifyUri { | ||
constructor(uri, query) { | ||
super(uri); | ||
this.type = "search"; | ||
this.query = query; | ||
get query() { | ||
return this.id; | ||
} | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "search"); | ||
return typeof v === "object" && v.type === "search"; | ||
} | ||
toURI() { | ||
return `spotify:search:${encode(this.query)}`; | ||
} | ||
toURL() { | ||
return `/search/${encode(this.query)}`; | ||
} | ||
}; | ||
@@ -116,6 +122,5 @@ | ||
var Playlist = class extends SpotifyUri { | ||
user; | ||
constructor(uri, id, user) { | ||
super(uri); | ||
this.type = "playlist"; | ||
this.id = id; | ||
super(uri, id); | ||
if (typeof user === "string") { | ||
@@ -126,3 +131,3 @@ this.user = user; | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "playlist"); | ||
return typeof v === "object" && v.type === "playlist"; | ||
} | ||
@@ -151,16 +156,5 @@ toURI() { | ||
var Artist = class extends SpotifyUri { | ||
constructor(uri, id) { | ||
super(uri); | ||
this.type = "artist"; | ||
this.id = id; | ||
} | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "artist"); | ||
return typeof v === "object" && v.type === "artist"; | ||
} | ||
toURI() { | ||
return `spotify:${this.type}:${encode(this.id)}`; | ||
} | ||
toURL() { | ||
return `/${this.type}/${encode(this.id)}`; | ||
} | ||
}; | ||
@@ -170,16 +164,5 @@ | ||
var Album = class extends SpotifyUri { | ||
constructor(uri, id) { | ||
super(uri); | ||
this.type = "album"; | ||
this.id = id; | ||
} | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "album"); | ||
return typeof v === "object" && v.type === "album"; | ||
} | ||
toURI() { | ||
return `spotify:${this.type}:${encode(this.id)}`; | ||
} | ||
toURL() { | ||
return `/${this.type}/${encode(this.id)}`; | ||
} | ||
}; | ||
@@ -189,16 +172,5 @@ | ||
var Track = class extends SpotifyUri { | ||
constructor(uri, id) { | ||
super(uri); | ||
this.type = "track"; | ||
this.id = id; | ||
} | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "track"); | ||
return typeof v === "object" && v.type === "track"; | ||
} | ||
toURI() { | ||
return `spotify:${this.type}:${encode(this.id)}`; | ||
} | ||
toURL() { | ||
return `/${this.type}/${encode(this.id)}`; | ||
} | ||
}; | ||
@@ -208,16 +180,5 @@ | ||
var Episode = class extends SpotifyUri { | ||
constructor(uri, id) { | ||
super(uri); | ||
this.type = "episode"; | ||
this.id = id; | ||
} | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "episode"); | ||
return typeof v === "object" && v.type === "episode"; | ||
} | ||
toURI() { | ||
return `spotify:${this.type}:${encode(this.id)}`; | ||
} | ||
toURL() { | ||
return `/${this.type}/${encode(this.id)}`; | ||
} | ||
}; | ||
@@ -227,16 +188,5 @@ | ||
var Show = class extends SpotifyUri { | ||
constructor(uri, id) { | ||
super(uri); | ||
this.type = "show"; | ||
this.id = id; | ||
} | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "show"); | ||
return typeof v === "object" && v.type === "show"; | ||
} | ||
toURI() { | ||
return `spotify:${this.type}:${encode(this.id)}`; | ||
} | ||
toURL() { | ||
return `/${this.type}/${encode(this.id)}`; | ||
} | ||
}; | ||
@@ -246,16 +196,8 @@ | ||
var User = class extends SpotifyUri { | ||
constructor(uri, user) { | ||
super(uri); | ||
this.type = "user"; | ||
this.user = user; | ||
get user() { | ||
return this.id; | ||
} | ||
static is(v) { | ||
return Boolean(typeof v === "object" && v.type === "user"); | ||
return typeof v === "object" && v.type === "user"; | ||
} | ||
toURI() { | ||
return `spotify:${this.type}:${encode(this.user)}`; | ||
} | ||
toURL() { | ||
return `/${this.type}/${encode(this.user)}`; | ||
} | ||
}; | ||
@@ -270,3 +212,3 @@ | ||
if (typeof parsedQs.uri !== "string") { | ||
throw new Error("fo"); | ||
throw new Error("Parsed query string was not valid: " + searchParams.toString()); | ||
} | ||
@@ -286,10 +228,12 @@ return parse(parsedQs.uri); | ||
function parseParts(uri, parts) { | ||
const len = parts.length; | ||
if (parts[1] === "embed") { | ||
let spotifyType = parts[1]; | ||
if (spotifyType === "embed" /* Embed */) { | ||
parts = parts.slice(1); | ||
spotifyType = parts[1]; | ||
} | ||
if (parts[1] === "search") { | ||
const len = parts.length; | ||
if (spotifyType === "search" /* Search */) { | ||
return new Search(uri, decode(parts.slice(2).join(":"))); | ||
} | ||
if (len >= 3 && parts[1] === "local") { | ||
if (len >= 3 && spotifyType === "local" /* Local */) { | ||
return new Local( | ||
@@ -303,32 +247,29 @@ uri, | ||
} | ||
if (len === 3 && parts[1] === "playlist") { | ||
if (len >= 4 || spotifyType === "playlist" /* Playlist */) { | ||
if (len >= 5) { | ||
return new Playlist(uri, decode(parts[4]), decode(parts[2])); | ||
} | ||
if (parts[3] === "starred") { | ||
return new Playlist(uri, "starred", decode(parts[2])); | ||
} | ||
return new Playlist(uri, decode(parts[2])); | ||
} | ||
if (len === 3 && parts[1] === "user") { | ||
if (len === 3 && spotifyType === "user" /* User */) { | ||
return new User(uri, decode(parts[2])); | ||
} | ||
if (len >= 5) { | ||
return new Playlist(uri, decode(parts[4]), decode(parts[2])); | ||
} | ||
if (len >= 4 && parts[3] === "starred") { | ||
return new Playlist(uri, "starred", decode(parts[2])); | ||
} | ||
if (parts[1] === "artist") { | ||
if (spotifyType === "artist" /* Artist */) { | ||
return new Artist(uri, parts[2]); | ||
} | ||
if (parts[1] === "album") { | ||
if (spotifyType === "album" /* Album */) { | ||
return new Album(uri, parts[2]); | ||
} | ||
if (parts[1] === "track") { | ||
if (spotifyType === "track" /* Track */) { | ||
return new Track(uri, parts[2]); | ||
} | ||
if (parts[1] === "episode") { | ||
if (spotifyType === "episode" /* Episode */) { | ||
return new Episode(uri, parts[2]); | ||
} | ||
if (parts[1] === "show") { | ||
if (spotifyType === "show" /* Show */) { | ||
return new Show(uri, parts[2]); | ||
} | ||
if (parts[1] === "playlist") { | ||
return new Playlist(uri, parts[2]); | ||
} | ||
throw new TypeError(`Could not determine type for: ${uri}`); | ||
@@ -335,0 +276,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/TooTallNate/spotify-uri#readme", | ||
"version": "3.0.4", | ||
"version": "4.0.0", | ||
"main": "./dist/index.js", | ||
@@ -75,12 +75,6 @@ "module": "./dist/index.mjs", | ||
], | ||
"license": "MIT", | ||
"exports": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsup src/index.ts --format esm,cjs --dts", | ||
"prebuild": "rm -rf dist", | ||
"prepublishOnly": "npm run build", | ||
"prerelease": "npm run test", | ||
@@ -91,3 +85,10 @@ "pretest": "npm run build", | ||
"test-lint": "ts-standard" | ||
} | ||
} | ||
}, | ||
"license": "MIT", | ||
"exports": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"types": "./dist/index.d.ts" | ||
} |
Sorry, the diff of this file is not supported yet
22685
6
600