@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
108 lines • 4.26 kB
JavaScript
import ExpoSpotifySDKModule from "../ExpoSpotifySDKModule";
import { AppRemoteError } from "./error";
import { createNativeErrorRethrow } from "../internal/native-errors";
export { AppRemoteError } from "./error";
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
const rethrowAsAppRemoteError = createNativeErrorRethrow({
ErrorClass: AppRemoteError,
unknownCode: "UNKNOWN",
validCodes: new Set([
"CONNECTION_FAILED",
"CONNECTION_LOST",
"NOT_CONNECTED",
"UNKNOWN",
]),
});
// ---------------------------------------------------------------------------
// AppRemote namespace
// ---------------------------------------------------------------------------
/**
* Spotify App Remote namespace. Manages the IPC connection to the running
* Spotify app. All `Player`, `User`, `Content`, and `Images` calls require an
* active connection established via `AppRemote.connect()`.
*
* @example
* ```ts
* import { AppRemote } from "@wwdrew/expo-spotify-sdk";
*
* // Connect using the access token from Auth.authenticate()
* await AppRemote.connect(session.accessToken);
*
* const sub = AppRemote.addListener("connectionStateChange", ({ state }) => {
* console.log("connection state:", state);
* });
*
* // later:
* await AppRemote.disconnect();
* sub.remove();
* ```
*/
export const AppRemote = {
/**
* Opens a connection to the running Spotify app using the provided access
* token. Resolves when the connection is established; rejects with an
* {@link AppRemoteError} if the connection fails.
*
* **Android note:** The access token is accepted for API parity with iOS but
* the Android App Remote SDK does not accept it directly — it uses the
* session cached in the Spotify app from your earlier `Auth.authenticate()`
* call. Ensure `Auth.authenticate()` has succeeded before calling `connect()`.
*
* Calling `connect()` while already connected is a no-op.
*/
connect(accessToken) {
return ExpoSpotifySDKModule.appRemoteConnect(accessToken).catch(rethrowAsAppRemoteError);
},
/**
* Wakes Spotify (launching if suspended), starts or resumes playback, and
* connects. Use when {@link connect} may fail because Spotify is not running.
* Always foregrounds Spotify and starts audio — see package api-reference.
*
* @param accessToken Access token from `Auth.authenticate()`.
* @param uri Spotify URI to play, or empty (default) to resume last/contextual track.
*/
authorizeAndPlay(accessToken, uri = "") {
return ExpoSpotifySDKModule.appRemoteAuthorizeAndPlay(accessToken, uri).catch(rethrowAsAppRemoteError);
},
/**
* Disconnects from the Spotify app. Safe to call when already disconnected.
* Resolves once the disconnection is complete.
*/
disconnect() {
return ExpoSpotifySDKModule.appRemoteDisconnect();
},
/**
* Returns `true` if currently connected to the Spotify app.
* This is a synchronous snapshot — subscribe to `"connectionStateChange"`
* for reactive updates.
*/
isConnected() {
return ExpoSpotifySDKModule.appRemoteIsConnected();
},
/**
* Returns the current {@link ConnectionState} synchronously.
* Subscribe to `"connectionStateChange"` for reactive updates.
*/
getConnectionState() {
return ExpoSpotifySDKModule.appRemoteGetConnectionState();
},
/**
* Subscribes to connection lifecycle events.
*
* | Event | Payload | When |
* |---|---|---|
* | `"connectionStateChange"` | `{ state: ConnectionState }` | State transitions |
* | `"connectionError"` | `{ code, message }` | Connection failures and drops |
*
* Returns a `Subscription` — call `.remove()` to unsubscribe.
*/
addListener(event, listener) {
const nativeEvent = event === "connectionStateChange"
? "onConnectionStateChange"
: "onConnectionError";
return ExpoSpotifySDKModule.addListener(nativeEvent, listener);
},
};
//# sourceMappingURL=index.js.map