@wwdrew/expo-spotify-sdk 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -9
- package/build/ExpoSpotifySDK.types.d.ts +2 -0
- package/build/ExpoSpotifySDK.types.d.ts.map +1 -1
- package/build/ExpoSpotifySDK.types.js.map +1 -1
- package/ios/ExpoSpotifySDKModule.swift +22 -16
- package/ios/ExpoSpotifySessionManager+SessionManagerDelegate.swift +14 -4
- package/ios/ExpoSpotifySessionManager.swift +19 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,9 +24,7 @@ Include the `expo-spotify-sdk` plugin in your `app.json/app.config.js` file with
|
|
|
24
24
|
["@wwdrew/expo-spotify-sdk", {
|
|
25
25
|
"clientID": "<your-spotify-client-id>",
|
|
26
26
|
"scheme": "expo-spotify-sdk-example",
|
|
27
|
-
"host": "authenticate"
|
|
28
|
-
"tokenSwapURL": "http://192.168.0.5:3000/swap",
|
|
29
|
-
"tokenRefreshURL": "http://192.168.0.5:3000/swap"
|
|
27
|
+
"host": "authenticate"
|
|
30
28
|
}]
|
|
31
29
|
],
|
|
32
30
|
...
|
|
@@ -37,23 +35,27 @@ Required:
|
|
|
37
35
|
- `clientID`: <string> the Spotify Client ID for your application
|
|
38
36
|
- `scheme`: <string> the [URL scheme](https://docs.expo.dev/versions/latest/config/app/#scheme) to link into your app as part of the redirect URI
|
|
39
37
|
- `host`: <string> the path of the redirect URI
|
|
40
|
-
- `tokenSwapURL` (optional): <string> The URL to use for attempting to swap an authorization code for an access token
|
|
41
|
-
- `tokenRefreshURL` (optional): <string> The URL to use for attempting to renew an access token with a refresh token
|
|
42
38
|
|
|
43
39
|
## API Reference
|
|
44
40
|
|
|
45
|
-
|
|
41
|
+
```typescript
|
|
42
|
+
isAvailable(): boolean`
|
|
43
|
+
```
|
|
46
44
|
|
|
47
45
|
Determines if the Spotify app is installed on the target device.
|
|
48
46
|
|
|
49
47
|
---
|
|
50
48
|
|
|
51
|
-
|
|
49
|
+
```typescript
|
|
50
|
+
authenticateAsync(config: SpotifyConfig): Promise<SpotifySession>
|
|
51
|
+
```
|
|
52
52
|
|
|
53
53
|
Starts the authentication process. Requires an array of OAuth scopes. If the Spotify app is installed on the target device it will interact directly with it, otherwise it will open a web view to authenticate with the Spotify website.
|
|
54
54
|
|
|
55
55
|
### Parameters
|
|
56
56
|
|
|
57
|
+
- `tokenSwapURL` (optional): <string> The URL to use for attempting to swap an authorization code for an access token
|
|
58
|
+
- `tokenRefreshURL` (optional): <string> The URL to use for attempting to renew an access token with a refresh token
|
|
57
59
|
- `scopes`: An array of OAuth scopes that declare how your app wants to access a user's account. See [Spotify Scopes](https://developer.spotify.com/web-api/using-scopes/) for more information.
|
|
58
60
|
|
|
59
61
|
**Note**: The following scopes are not available to Expo Spotify SDK:
|
|
@@ -68,11 +70,18 @@ Starts the authentication process. Requires an array of OAuth scopes. If the Spo
|
|
|
68
70
|
### Types
|
|
69
71
|
|
|
70
72
|
```typescript
|
|
73
|
+
interface SpotifyConfig {
|
|
74
|
+
scopes: SpotifyScope[];
|
|
75
|
+
tokenSwapURL?: string;
|
|
76
|
+
tokenRefreshURL?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
71
79
|
interface SpotifySession {
|
|
72
80
|
accessToken: string;
|
|
73
81
|
refreshToken: string;
|
|
74
82
|
expirationDate: number;
|
|
75
83
|
isExpired: boolean;
|
|
84
|
+
scopes: SpotifyScopes[];
|
|
76
85
|
}
|
|
77
86
|
|
|
78
87
|
type SpotifyScopes =
|
|
@@ -94,12 +103,11 @@ type SpotifyScopes =
|
|
|
94
103
|
| "user-library-read"
|
|
95
104
|
| "user-read-email"
|
|
96
105
|
| "user-read-private";
|
|
97
|
-
|
|
98
106
|
```
|
|
99
107
|
|
|
100
108
|
## Acknowledgments
|
|
101
109
|
|
|
102
|
-
This project has been heavily inspired by the following projects
|
|
110
|
+
This project has been heavily inspired by the following projects:
|
|
103
111
|
|
|
104
112
|
* [react-native-spotify-remote](https://github.com/cjam/react-native-spotify-remote)
|
|
105
113
|
* [expo-spotify](https://github.com/kvbalib/expo-spotify)
|
|
@@ -3,11 +3,13 @@ export interface SpotifySession {
|
|
|
3
3
|
refreshToken: string;
|
|
4
4
|
expirationDate: number;
|
|
5
5
|
isExpired: boolean;
|
|
6
|
+
scopes: SpotifyScope[];
|
|
6
7
|
}
|
|
7
8
|
export interface SpotifyConfig {
|
|
8
9
|
scopes: SpotifyScope[];
|
|
9
10
|
tokenSwapURL?: string;
|
|
10
11
|
tokenRefreshURL?: string;
|
|
12
|
+
shouldRequestAccessToken?: boolean;
|
|
11
13
|
}
|
|
12
14
|
export type SpotifyScope = "ugc-image-upload" | "user-read-playback-state" | "user-modify-playback-state" | "user-read-currently-playing" | "app-remote-control" | "streaming" | "playlist-read-private" | "playlist-read-collaborative" | "playlist-modify-private" | "playlist-modify-public" | "user-follow-modify" | "user-follow-read" | "user-top-read" | "user-read-recently-played" | "user-library-modify" | "user-library-read" | "user-read-email" | "user-read-private";
|
|
13
15
|
//# sourceMappingURL=ExpoSpotifySDK.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpoSpotifySDK.types.d.ts","sourceRoot":"","sources":["../src/ExpoSpotifySDK.types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"ExpoSpotifySDK.types.d.ts","sourceRoot":"","sources":["../src/ExpoSpotifySDK.types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,YAAY,EAAE,CAAC;CACxB;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,YAAY,EAAE,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACpC;AAED,MAAM,MAAM,YAAY,GACpB,kBAAkB,GAClB,0BAA0B,GAC1B,4BAA4B,GAC5B,6BAA6B,GAC7B,oBAAoB,GACpB,WAAW,GACX,uBAAuB,GACvB,6BAA6B,GAC7B,yBAAyB,GACzB,wBAAwB,GACxB,oBAAoB,GACpB,kBAAkB,GAClB,eAAe,GACf,2BAA2B,GAC3B,qBAAqB,GACrB,mBAAmB,GACnB,iBAAiB,GACjB,mBAAmB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpoSpotifySDK.types.js","sourceRoot":"","sources":["../src/ExpoSpotifySDK.types.ts"],"names":[],"mappings":"","sourcesContent":["export interface SpotifySession {\n accessToken: string;\n refreshToken: string;\n expirationDate: number;\n isExpired: boolean;\n}\n\nexport interface SpotifyConfig {\n scopes: SpotifyScope[];\n tokenSwapURL?: string;\n tokenRefreshURL?: string;\n}\n\nexport type SpotifyScope =\n | \"ugc-image-upload\"\n | \"user-read-playback-state\"\n | \"user-modify-playback-state\"\n | \"user-read-currently-playing\"\n | \"app-remote-control\"\n | \"streaming\"\n | \"playlist-read-private\"\n | \"playlist-read-collaborative\"\n | \"playlist-modify-private\"\n | \"playlist-modify-public\"\n | \"user-follow-modify\"\n | \"user-follow-read\"\n | \"user-top-read\"\n | \"user-read-recently-played\"\n | \"user-library-modify\"\n | \"user-library-read\"\n | \"user-read-email\"\n | \"user-read-private\";\n"]}
|
|
1
|
+
{"version":3,"file":"ExpoSpotifySDK.types.js","sourceRoot":"","sources":["../src/ExpoSpotifySDK.types.ts"],"names":[],"mappings":"","sourcesContent":["export interface SpotifySession {\n accessToken: string;\n refreshToken: string;\n expirationDate: number;\n isExpired: boolean;\n scopes: SpotifyScope[];\n}\n\nexport interface SpotifyConfig {\n scopes: SpotifyScope[];\n tokenSwapURL?: string;\n tokenRefreshURL?: string;\n shouldRequestAccessToken?: boolean;\n}\n\nexport type SpotifyScope =\n | \"ugc-image-upload\"\n | \"user-read-playback-state\"\n | \"user-modify-playback-state\"\n | \"user-read-currently-playing\"\n | \"app-remote-control\"\n | \"streaming\"\n | \"playlist-read-private\"\n | \"playlist-read-collaborative\"\n | \"playlist-modify-private\"\n | \"playlist-modify-public\"\n | \"user-follow-modify\"\n | \"user-follow-read\"\n | \"user-top-read\"\n | \"user-read-recently-played\"\n | \"user-library-modify\"\n | \"user-library-read\"\n | \"user-read-email\"\n | \"user-read-private\";\n"]}
|
|
@@ -2,38 +2,44 @@ import ExpoModulesCore
|
|
|
2
2
|
import SpotifyiOS
|
|
3
3
|
|
|
4
4
|
public class ExpoSpotifySDKModule: Module {
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
public func definition() -> ModuleDefinition {
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
let spotifySession = ExpoSpotifySessionManager.shared
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
Name("ExpoSpotifySDK")
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
Function("isAvailable") {
|
|
13
13
|
return spotifySession.spotifyAppInstalled()
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
AsyncFunction("authenticateAsync") { (config: [String: Any], promise: Promise) in
|
|
17
17
|
guard let scopes = config["scopes"] as? [String] else {
|
|
18
18
|
promise.reject("INVALID_CONFIG", "Invalid SpotifyConfig object")
|
|
19
19
|
return
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
let tokenSwapURL = config["tokenSwapURL"] as? String
|
|
23
23
|
let tokenRefreshURL = config["tokenRefreshURL"] as? String
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
let shouldRequestAccessToken = config["shouldRequestAccessToken"] as? Bool ?? true
|
|
25
|
+
|
|
26
|
+
spotifySession.authenticate(scopes: scopes, tokenSwapURL: tokenSwapURL, tokenRefreshURL: tokenRefreshURL, shouldRequestAccessToken: shouldRequestAccessToken).done { result in
|
|
27
|
+
switch result {
|
|
28
|
+
case .session(let session):
|
|
29
|
+
promise.resolve([
|
|
30
|
+
"accessToken": session.accessToken,
|
|
31
|
+
"refreshToken": session.refreshToken,
|
|
32
|
+
"expirationDate": Int(session.expirationDate.timeIntervalSince1970 * 1000),
|
|
33
|
+
"scopes": SPTScopeSerializer.serializeScopes(session.scope),
|
|
34
|
+
"isExpired": session.isExpired
|
|
35
|
+
])
|
|
36
|
+
case .authorizationCode(let code):
|
|
37
|
+
promise.resolve(["code": code])
|
|
38
|
+
}
|
|
34
39
|
}.catch { error in
|
|
35
40
|
promise.reject(error)
|
|
36
41
|
}
|
|
37
42
|
}
|
|
43
|
+
|
|
38
44
|
}
|
|
39
45
|
}
|
|
@@ -3,14 +3,24 @@ import SpotifyiOS
|
|
|
3
3
|
|
|
4
4
|
extension ExpoSpotifySessionManager: SPTSessionManagerDelegate {
|
|
5
5
|
public func sessionManager(manager _: SPTSessionManager, didInitiate session: SPTSession) {
|
|
6
|
-
|
|
6
|
+
if shouldRequestAccessToken {
|
|
7
|
+
authPromiseSeal?.fulfill(AuthenticationResult.session(session))
|
|
8
|
+
}
|
|
7
9
|
}
|
|
8
|
-
|
|
10
|
+
|
|
9
11
|
public func sessionManager(manager _: SPTSessionManager, didFailWith error: Error) {
|
|
10
12
|
authPromiseSeal?.reject(error)
|
|
11
13
|
}
|
|
12
|
-
|
|
14
|
+
|
|
13
15
|
public func sessionManager(manager _: SPTSessionManager, didRenew session: SPTSession) {
|
|
14
|
-
authPromiseSeal?.fulfill(session)
|
|
16
|
+
authPromiseSeal?.fulfill(AuthenticationResult.session(session))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public func sessionManager(manager: SPTSessionManager, shouldRequestAccessTokenWith code: String) -> Bool {
|
|
20
|
+
if !shouldRequestAccessToken {
|
|
21
|
+
authPromiseSeal?.fulfill(AuthenticationResult.authorizationCode(code))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return shouldRequestAccessToken
|
|
15
25
|
}
|
|
16
26
|
}
|
|
@@ -2,6 +2,11 @@ import ExpoModulesCore
|
|
|
2
2
|
import SpotifyiOS
|
|
3
3
|
import PromiseKit
|
|
4
4
|
|
|
5
|
+
enum AuthenticationResult {
|
|
6
|
+
case session(SPTSession)
|
|
7
|
+
case authorizationCode(String)
|
|
8
|
+
}
|
|
9
|
+
|
|
5
10
|
enum SessionManagerError: Error {
|
|
6
11
|
case notInitialized
|
|
7
12
|
case invalidConfiguration
|
|
@@ -9,7 +14,9 @@ enum SessionManagerError: Error {
|
|
|
9
14
|
|
|
10
15
|
final class ExpoSpotifySessionManager: NSObject {
|
|
11
16
|
weak var module: ExpoSpotifySDKModule?
|
|
12
|
-
var authPromiseSeal: Resolver<
|
|
17
|
+
var authPromiseSeal: Resolver<AuthenticationResult>?
|
|
18
|
+
|
|
19
|
+
var shouldRequestAccessToken: Bool = true
|
|
13
20
|
|
|
14
21
|
static let shared = ExpoSpotifySessionManager()
|
|
15
22
|
|
|
@@ -44,7 +51,7 @@ final class ExpoSpotifySessionManager: NSObject {
|
|
|
44
51
|
}()
|
|
45
52
|
|
|
46
53
|
|
|
47
|
-
func authenticate(scopes: [String], tokenSwapURL: String?, tokenRefreshURL: String
|
|
54
|
+
func authenticate(scopes: [String], tokenSwapURL: String?, tokenRefreshURL: String?, shouldRequestAccessToken: Bool = true) -> PromiseKit.Promise<AuthenticationResult> {
|
|
48
55
|
return Promise { seal in
|
|
49
56
|
guard let clientID = expoSpotifyConfiguration?.clientID,
|
|
50
57
|
let redirectURL = expoSpotifyConfiguration?.redirectURL else {
|
|
@@ -53,10 +60,17 @@ final class ExpoSpotifySessionManager: NSObject {
|
|
|
53
60
|
return
|
|
54
61
|
}
|
|
55
62
|
let configuration = SPTConfiguration(clientID: clientID, redirectURL: redirectURL)
|
|
56
|
-
|
|
57
|
-
configuration.tokenSwapURL = URL(string: tokenSwapURL ?? "")
|
|
58
|
-
configuration.tokenRefreshURL = URL(string: tokenRefreshURL ?? "")
|
|
59
63
|
|
|
64
|
+
if (tokenSwapURL != nil) {
|
|
65
|
+
configuration.tokenSwapURL = URL(string: tokenSwapURL ?? "")
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (tokenRefreshURL != nil) {
|
|
69
|
+
configuration.tokenRefreshURL = URL(string: tokenRefreshURL ?? "")
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
self.shouldRequestAccessToken = shouldRequestAccessToken
|
|
73
|
+
self.configuration = configuration
|
|
60
74
|
self.sessionManager = SPTSessionManager(configuration: configuration, delegate: self)
|
|
61
75
|
|
|
62
76
|
self.authPromiseSeal = seal
|