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 4 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}`;
}
}
17 changes: 8 additions & 9 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { URL } from 'url'
import { URL } from './universal-url'
Copy link
Collaborator

@Kikobeats Kikobeats May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just use URL since it's global in browser (via window) and Node.js (via globalThis), so we can't drop the typecheck code of src/universal-url.ts, saving some bytes 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I have just updated my code.
CleanShot 2024-05-23 at 04 58 59@2x

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What Kiko means is that this universal-url file isn't necessary at all. The global URL class is available in all modern JS environments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. I just updated it. If I remember correctly, I saw somewhere that it mentioned the need for compatibility with older versions of Node. Yeah, using the global URL would be great.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with dropping older (EOL) Node.js version support.

import Local from './local'
import Search from './search'
import Playlist from './playlist'
Expand All @@ -12,7 +12,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 +55,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 +76,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
19 changes: 19 additions & 0 deletions src/universal-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let URLClass: typeof URL;

declare var window: any;

if (typeof window !== "undefined" && typeof window.URL !== "undefined") {
// for browser environment
URLClass = window.URL;
} else if (typeof global !== "undefined" && typeof global.URL !== "undefined") {
// for react native environment
URLClass = global.URL;
} else if (typeof require !== "undefined") {
// for Node.js environment
const { URL } = require("url");
URLClass = URL;
} else {
throw new Error("Unsupported environment");
}

export { URLClass as URL };