@wwdrew/expo-spotify-sdk
Version:
Expo module wrapping the native Spotify iOS (v5) and Android (v4) SDKs for OAuth authentication and App Remote playback control
57 lines • 1.79 kB
JavaScript
const URI_RE = /^spotify:(track|album|playlist|artist|show|episode):([A-Za-z0-9]+)$/;
/**
* Helpers for constructing, validating and decomposing {@link SpotifyURI}
* values.
*
* @example
* ```ts
* const uri = SpotifyURI.from("spotify:track:4uLU6hMCjMI75M1A2tKUQC");
* const { type, id } = SpotifyURI.parse(uri);
* const rebuilt = SpotifyURI.build("track", id);
* ```
*/
export const SpotifyURI = {
/**
* Cast `uri` to {@link SpotifyURI} after validation.
* Throws a plain `Error` if the URI does not match the `spotify:<type>:<id>`
* format — use at app-code call sites where you want early feedback.
*/
from(uri) {
if (!SpotifyURI.isValid(uri)) {
throw new Error(`Invalid Spotify URI: "${uri}". Expected spotify:<type>:<id> where ` +
`<type> is one of: track, album, playlist, artist, show, episode.`);
}
return uri;
},
/**
* Cast `uri` to {@link SpotifyURI} without validation.
* Use only for URIs that originate from the Spotify SDK itself (i.e. already
* known-valid values coming back over the bridge).
*/
unsafe(uri) {
return uri;
},
/**
* Decompose a {@link SpotifyURI} into its `{ type, id }` parts.
*/
parse(uri) {
const m = uri.match(URI_RE);
if (!m) {
throw new Error(`Cannot parse Spotify URI: "${uri}"`);
}
return { type: m[1], id: m[2] };
},
/**
* Build a {@link SpotifyURI} from a resource type and ID.
*/
build(type, id) {
return `spotify:${type}:${id}`;
},
/**
* Type-guard: returns `true` if `uri` is a valid Spotify URI string.
*/
isValid(uri) {
return URI_RE.test(uri);
},
};
//# sourceMappingURL=index.js.map