-
Notifications
You must be signed in to change notification settings - Fork 15
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
+34
−30
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c1990e
feat: support browser environment
alantoa 222aa8b
code format
alantoa 353be0b
support react native env
alantoa e48c141
improvements
alantoa 0d2402a
resolve feedback
alantoa 8427dca
replace universal-url.ts with URL
alantoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 (viawindow
) and Node.js (viaglobalThis
), so we can't drop the typecheck code ofsrc/universal-url.ts
, saving some bytes 🙂There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 globalURL
class is available in all modern JS environments.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.