@wwdrew/expo-spotify-sdk 1.0.0 → 2.1.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.
Files changed (61) hide show
  1. package/README.md +92 -350
  2. package/android/build.gradle +2 -2
  3. package/android/src/main/java/expo/modules/spotifysdk/ExpoSpotifySDKModule.kt +3 -5
  4. package/android/src/main/java/expo/modules/spotifysdk/SpotifyAppRemoteCoordinator.kt +6 -64
  5. package/android/src/main/java/expo/modules/spotifysdk/SpotifyAppRemoteErrorMapping.kt +106 -0
  6. package/android/src/main/java/expo/modules/spotifysdk/SpotifyTokenSwapClient.kt +2 -7
  7. package/app.plugin.js +1 -1
  8. package/build/app-remote/index.d.ts +2 -2
  9. package/build/app-remote/index.d.ts.map +1 -1
  10. package/build/app-remote/index.js +11 -24
  11. package/build/app-remote/index.js.map +1 -1
  12. package/build/auth/index.d.ts +2 -2
  13. package/build/auth/index.d.ts.map +1 -1
  14. package/build/auth/index.js +17 -34
  15. package/build/auth/index.js.map +1 -1
  16. package/build/content/index.d.ts.map +1 -1
  17. package/build/content/index.js +12 -25
  18. package/build/content/index.js.map +1 -1
  19. package/build/hooks/index.d.ts +4 -4
  20. package/build/hooks/index.d.ts.map +1 -1
  21. package/build/hooks/index.js +38 -115
  22. package/build/hooks/index.js.map +1 -1
  23. package/build/images/index.d.ts +2 -2
  24. package/build/images/index.d.ts.map +1 -1
  25. package/build/images/index.js +12 -25
  26. package/build/images/index.js.map +1 -1
  27. package/build/index.d.ts +0 -18
  28. package/build/index.d.ts.map +1 -1
  29. package/build/index.js +0 -27
  30. package/build/index.js.map +1 -1
  31. package/build/internal/native-errors.d.ts +14 -0
  32. package/build/internal/native-errors.d.ts.map +1 -0
  33. package/build/internal/native-errors.js +29 -0
  34. package/build/internal/native-errors.js.map +1 -0
  35. package/build/internal/sync-external-store.d.ts +14 -0
  36. package/build/internal/sync-external-store.d.ts.map +1 -0
  37. package/build/internal/sync-external-store.js +36 -0
  38. package/build/internal/sync-external-store.js.map +1 -0
  39. package/build/player/index.d.ts +1 -1
  40. package/build/player/index.d.ts.map +1 -1
  41. package/build/player/index.js +15 -27
  42. package/build/player/index.js.map +1 -1
  43. package/build/uri/index.js.map +1 -1
  44. package/build/user/index.d.ts +2 -2
  45. package/build/user/index.d.ts.map +1 -1
  46. package/build/user/index.js +16 -26
  47. package/build/user/index.js.map +1 -1
  48. package/ios/ExpoSpotifySDK.podspec +1 -1
  49. package/ios/ExpoSpotifySDKModule.swift +1 -1
  50. package/ios/SpotifyAppRemoteCoordinator.swift +29 -404
  51. package/ios/SpotifyAppRemoteErrorMapping.swift +110 -0
  52. package/ios/SpotifyAppRemoteErrors.swift +221 -0
  53. package/ios/SpotifyAppRemoteMappers.swift +88 -0
  54. package/package.json +28 -14
  55. package/plugin/build/index.d.ts +20 -3
  56. package/plugin/build/index.js +22 -25
  57. package/plugin/build/types.d.ts +17 -1
  58. package/plugin/build/withSpotifySdkConfig.d.ts +4 -0
  59. package/plugin/build/withSpotifySdkConfig.js +27 -0
  60. package/plugin/index.d.ts +2 -0
  61. package/plugin/index.js +1 -0
@@ -1,227 +1,7 @@
1
- import ExpoModulesCore
2
1
  import Foundation
3
2
  import SpotifyiOS
4
3
  import UIKit
5
4
 
6
- // MARK: — App Remote error types
7
-
8
- enum AppRemoteError: Error {
9
- case connectionFailed(String)
10
- case connectionLost(String)
11
- case notConnected(String)
12
- case unknown(String)
13
-
14
- var code: String {
15
- switch self {
16
- case .connectionFailed: return "CONNECTION_FAILED"
17
- case .connectionLost: return "CONNECTION_LOST"
18
- case .notConnected: return "NOT_CONNECTED"
19
- case .unknown: return "UNKNOWN"
20
- }
21
- }
22
-
23
- var message: String {
24
- switch self {
25
- case .connectionFailed(let m): return m
26
- case .connectionLost(let m): return m
27
- case .notConnected(let m): return m
28
- case .unknown(let m): return m
29
- }
30
- }
31
- }
32
-
33
- /// Bridges an `AppRemoteError` through expo-modules-core's exception system so
34
- /// JS callers receive a structured `code` and `reason`, not "undefined reason".
35
- final class AppRemoteException: Exception, @unchecked Sendable {
36
- private let appRemoteCode: String
37
- private let appRemoteMessage: String
38
-
39
- init(_ error: AppRemoteError, file: String = #fileID, line: UInt = #line, function: String = #function) {
40
- self.appRemoteCode = error.code
41
- self.appRemoteMessage = error.message
42
- super.init(file: file, line: line, function: function)
43
- }
44
-
45
- override var code: String { appRemoteCode }
46
- override var reason: String { appRemoteMessage }
47
- }
48
-
49
- // MARK: — Player error types
50
-
51
- enum NativePlayerError: Error {
52
- case notConnected(String)
53
- case connectionLost(String)
54
- case premiumRequired(String)
55
- case invalidURI(String)
56
- case invalidParameter(String)
57
- case operationNotAllowed(String)
58
- case unknown(String)
59
-
60
- var code: String {
61
- switch self {
62
- case .notConnected: return "NOT_CONNECTED"
63
- case .connectionLost: return "CONNECTION_LOST"
64
- case .premiumRequired: return "PREMIUM_REQUIRED"
65
- case .invalidURI: return "INVALID_URI"
66
- case .invalidParameter: return "INVALID_PARAMETER"
67
- case .operationNotAllowed: return "OPERATION_NOT_ALLOWED"
68
- case .unknown: return "UNKNOWN"
69
- }
70
- }
71
-
72
- var message: String {
73
- switch self {
74
- case .notConnected(let m): return m
75
- case .connectionLost(let m): return m
76
- case .premiumRequired(let m): return m
77
- case .invalidURI(let m): return m
78
- case .invalidParameter(let m): return m
79
- case .operationNotAllowed(let m): return m
80
- case .unknown(let m): return m
81
- }
82
- }
83
- }
84
-
85
- final class PlayerException: Exception, @unchecked Sendable {
86
- private let playerCode: String
87
- private let playerMessage: String
88
-
89
- init(_ error: NativePlayerError, file: String = #fileID, line: UInt = #line, function: String = #function) {
90
- self.playerCode = error.code
91
- self.playerMessage = error.message
92
- super.init(file: file, line: line, function: function)
93
- }
94
-
95
- override var code: String { playerCode }
96
- override var reason: String { playerMessage }
97
- }
98
-
99
- // MARK: — User error types
100
-
101
- enum NativeUserError: Error {
102
- case notConnected(String)
103
- case connectionLost(String)
104
- case invalidURI(String)
105
- case operationNotAllowed(String)
106
- case unknown(String)
107
-
108
- var code: String {
109
- switch self {
110
- case .notConnected: return "NOT_CONNECTED"
111
- case .connectionLost: return "CONNECTION_LOST"
112
- case .invalidURI: return "INVALID_URI"
113
- case .operationNotAllowed: return "OPERATION_NOT_ALLOWED"
114
- case .unknown: return "UNKNOWN"
115
- }
116
- }
117
-
118
- var message: String {
119
- switch self {
120
- case .notConnected(let m): return m
121
- case .connectionLost(let m): return m
122
- case .invalidURI(let m): return m
123
- case .operationNotAllowed(let m): return m
124
- case .unknown(let m): return m
125
- }
126
- }
127
- }
128
-
129
- final class UserException: Exception, @unchecked Sendable {
130
- private let userCode: String
131
- private let userMessage: String
132
-
133
- init(_ error: NativeUserError, file: String = #fileID, line: UInt = #line, function: String = #function) {
134
- self.userCode = error.code
135
- self.userMessage = error.message
136
- super.init(file: file, line: line, function: function)
137
- }
138
-
139
- override var code: String { userCode }
140
- override var reason: String { userMessage }
141
- }
142
-
143
- // MARK: — Content error types
144
-
145
- enum NativeContentError: Error {
146
- case notConnected(String)
147
- case connectionLost(String)
148
- case contentAPIUnavailable(String)
149
- case unknown(String)
150
-
151
- var code: String {
152
- switch self {
153
- case .notConnected: return "NOT_CONNECTED"
154
- case .connectionLost: return "CONNECTION_LOST"
155
- case .contentAPIUnavailable: return "CONTENT_API_UNAVAILABLE"
156
- case .unknown: return "UNKNOWN"
157
- }
158
- }
159
-
160
- var message: String {
161
- switch self {
162
- case .notConnected(let m): return m
163
- case .connectionLost(let m): return m
164
- case .contentAPIUnavailable(let m): return m
165
- case .unknown(let m): return m
166
- }
167
- }
168
- }
169
-
170
- final class ContentException: Exception, @unchecked Sendable {
171
- private let contentCode: String
172
- private let contentMessage: String
173
-
174
- init(_ error: NativeContentError, file: String = #fileID, line: UInt = #line, function: String = #function) {
175
- self.contentCode = error.code
176
- self.contentMessage = error.message
177
- super.init(file: file, line: line, function: function)
178
- }
179
-
180
- override var code: String { contentCode }
181
- override var reason: String { contentMessage }
182
- }
183
-
184
- // MARK: — Images error types
185
-
186
- enum NativeImagesError: Error {
187
- case notConnected(String)
188
- case invalidURI(String)
189
- case imageLoadFailed(String)
190
- case unknown(String)
191
-
192
- var code: String {
193
- switch self {
194
- case .notConnected: return "NOT_CONNECTED"
195
- case .invalidURI: return "INVALID_URI"
196
- case .imageLoadFailed: return "IMAGE_LOAD_FAILED"
197
- case .unknown: return "UNKNOWN"
198
- }
199
- }
200
-
201
- var message: String {
202
- switch self {
203
- case .notConnected(let m): return m
204
- case .invalidURI(let m): return m
205
- case .imageLoadFailed(let m): return m
206
- case .unknown(let m): return m
207
- }
208
- }
209
- }
210
-
211
- final class ImagesException: Exception, @unchecked Sendable {
212
- private let imagesCode: String
213
- private let imagesMessage: String
214
-
215
- init(_ error: NativeImagesError, file: String = #fileID, line: UInt = #line, function: String = #function) {
216
- self.imagesCode = error.code
217
- self.imagesMessage = error.message
218
- super.init(file: file, line: line, function: function)
219
- }
220
-
221
- override var code: String { imagesCode }
222
- override var reason: String { imagesMessage }
223
- }
224
-
225
5
  // MARK: — Coordinator
226
6
 
227
7
  /// Manages the `SPTAppRemote` singleton, the IPC connection lifecycle, and all
@@ -266,9 +46,12 @@ actor SpotifyAppRemoteCoordinator {
266
46
 
267
47
  private init(sptConfiguration: SPTConfiguration) {
268
48
  self.sptConfiguration = sptConfiguration
269
- self.connectionBridge = SpotifyAppRemoteDelegateBridge()
270
- self.playerStateBridge = SpotifyPlayerStateDelegateBridge()
271
- self.userCapabilitiesBridge = SpotifyUserCapabilitiesDelegateBridge()
49
+ let connectionBridge = SpotifyAppRemoteDelegateBridge()
50
+ let playerStateBridge = SpotifyPlayerStateDelegateBridge()
51
+ let userCapabilitiesBridge = SpotifyUserCapabilitiesDelegateBridge()
52
+ self.connectionBridge = connectionBridge
53
+ self.playerStateBridge = playerStateBridge
54
+ self.userCapabilitiesBridge = userCapabilitiesBridge
272
55
  connectionBridge.coordinator = self
273
56
  playerStateBridge.coordinator = self
274
57
  userCapabilitiesBridge.coordinator = self
@@ -381,7 +164,7 @@ actor SpotifyAppRemoteCoordinator {
381
164
 
382
165
  /// Called by `SpotifyPlayerStateDelegateBridge` when a state update arrives.
383
166
  func playerStateDidChange(_ state: any SPTAppRemotePlayerState) {
384
- onPlayerStateChange?(Self.playerStateToMap(state))
167
+ onPlayerStateChange?(SpotifyAppRemoteMappers.playerStateToMap(state))
385
168
  }
386
169
 
387
170
  // MARK: — User subscription
@@ -400,7 +183,7 @@ actor SpotifyAppRemoteCoordinator {
400
183
 
401
184
  /// Called by `SpotifyUserCapabilitiesDelegateBridge` on each capabilities update.
402
185
  func userCapabilitiesDidChange(_ capabilities: any SPTAppRemoteUserCapabilities) {
403
- onCapabilitiesChange?(Self.capabilitiesToMap(capabilities))
186
+ onCapabilitiesChange?(SpotifyAppRemoteMappers.capabilitiesToMap(capabilities))
404
187
  }
405
188
 
406
189
  // MARK: — Player transport
@@ -458,9 +241,9 @@ actor SpotifyAppRemoteCoordinator {
458
241
  return try await withCheckedThrowingContinuation { cont in
459
242
  playerAPI.getPlayerState { result, error in
460
243
  if let error = error {
461
- cont.resume(throwing: Self.normalizePlayerError(error as NSError, callsite: "Player.getPlayerState"))
244
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapPlayerError(error as NSError, callsite: "Player.getPlayerState"))
462
245
  } else if let state = result as? any SPTAppRemotePlayerState {
463
- cont.resume(returning: Self.playerStateToMap(state))
246
+ cont.resume(returning: SpotifyAppRemoteMappers.playerStateToMap(state))
464
247
  } else {
465
248
  cont.resume(throwing: NativePlayerError.unknown("Player.getPlayerState: unexpected result type"))
466
249
  }
@@ -473,7 +256,7 @@ actor SpotifyAppRemoteCoordinator {
473
256
  return try await withCheckedThrowingContinuation { cont in
474
257
  playerAPI.getCrossfadeState { result, error in
475
258
  if let error = error {
476
- cont.resume(throwing: Self.normalizePlayerError(error as NSError, callsite: "Player.getCrossfadeState"))
259
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapPlayerError(error as NSError, callsite: "Player.getCrossfadeState"))
477
260
  } else if let state = result as? any SPTAppRemoteCrossfadeState {
478
261
  cont.resume(returning: ["isEnabled": state.isEnabled, "duration": state.duration])
479
262
  } else {
@@ -489,7 +272,7 @@ actor SpotifyAppRemoteCoordinator {
489
272
  let speeds: [any SPTAppRemotePodcastPlaybackSpeed] = try await withCheckedThrowingContinuation { cont in
490
273
  playerAPI.getAvailablePodcastPlaybackSpeeds { result, error in
491
274
  if let error = error {
492
- cont.resume(throwing: Self.normalizePlayerError(error as NSError, callsite: "Player.setPodcastPlaybackSpeed"))
275
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapPlayerError(error as NSError, callsite: "Player.setPodcastPlaybackSpeed"))
493
276
  } else if let speeds = result as? [any SPTAppRemotePodcastPlaybackSpeed] {
494
277
  cont.resume(returning: speeds)
495
278
  } else {
@@ -515,9 +298,9 @@ actor SpotifyAppRemoteCoordinator {
515
298
  return try await withCheckedThrowingContinuation { cont in
516
299
  userAPI.fetchCapabilities { result, error in
517
300
  if let error = error {
518
- cont.resume(throwing: Self.normalizeUserError(error as NSError, callsite: "User.getCapabilities"))
301
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapUserError(error as NSError, callsite: "User.getCapabilities"))
519
302
  } else if let capabilities = result as? any SPTAppRemoteUserCapabilities {
520
- cont.resume(returning: Self.capabilitiesToMap(capabilities))
303
+ cont.resume(returning: SpotifyAppRemoteMappers.capabilitiesToMap(capabilities))
521
304
  } else {
522
305
  cont.resume(throwing: NativeUserError.unknown("User.getCapabilities: unexpected result type"))
523
306
  }
@@ -530,9 +313,9 @@ actor SpotifyAppRemoteCoordinator {
530
313
  return try await withCheckedThrowingContinuation { cont in
531
314
  userAPI.fetchLibraryState(forURI: uri) { result, error in
532
315
  if let error = error {
533
- cont.resume(throwing: Self.normalizeUserError(error as NSError, callsite: "User.getLibraryState"))
316
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapUserError(error as NSError, callsite: "User.getLibraryState"))
534
317
  } else if let state = result as? any SPTAppRemoteLibraryState {
535
- cont.resume(returning: Self.libraryStateToMap(state))
318
+ cont.resume(returning: SpotifyAppRemoteMappers.libraryStateToMap(state))
536
319
  } else {
537
320
  cont.resume(throwing: NativeUserError.unknown("User.getLibraryState: unexpected result type"))
538
321
  }
@@ -545,9 +328,9 @@ actor SpotifyAppRemoteCoordinator {
545
328
  return try await withCheckedThrowingContinuation { cont in
546
329
  userAPI.addItemToLibrary(withURI: uri) { result, error in
547
330
  if let error = error {
548
- cont.resume(throwing: Self.normalizeUserError(error as NSError, callsite: "User.addToLibrary"))
331
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapUserError(error as NSError, callsite: "User.addToLibrary"))
549
332
  } else if let state = result as? any SPTAppRemoteLibraryState {
550
- cont.resume(returning: Self.libraryStateToMap(state))
333
+ cont.resume(returning: SpotifyAppRemoteMappers.libraryStateToMap(state))
551
334
  } else {
552
335
  cont.resume(throwing: NativeUserError.unknown("User.addToLibrary: unexpected result type"))
553
336
  }
@@ -560,9 +343,9 @@ actor SpotifyAppRemoteCoordinator {
560
343
  return try await withCheckedThrowingContinuation { cont in
561
344
  userAPI.removeItemFromLibrary(withURI: uri) { result, error in
562
345
  if let error = error {
563
- cont.resume(throwing: Self.normalizeUserError(error as NSError, callsite: "User.removeFromLibrary"))
346
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapUserError(error as NSError, callsite: "User.removeFromLibrary"))
564
347
  } else if let state = result as? any SPTAppRemoteLibraryState {
565
- cont.resume(returning: Self.libraryStateToMap(state))
348
+ cont.resume(returning: SpotifyAppRemoteMappers.libraryStateToMap(state))
566
349
  } else {
567
350
  cont.resume(throwing: NativeUserError.unknown("User.removeFromLibrary: unexpected result type"))
568
351
  }
@@ -575,11 +358,11 @@ actor SpotifyAppRemoteCoordinator {
575
358
  func contentGetRecommendedContentItems(type: String) async throws -> [[String: Any]] {
576
359
  let contentAPI = try requireContentAPI(callsite: "Content.getRecommendedContentItems")
577
360
  return try await withCheckedThrowingContinuation { cont in
578
- contentAPI.fetchRecommendedContentItems(forType: Self.mapContentType(type), flattenContainers: false) { result, error in
361
+ contentAPI.fetchRecommendedContentItems(forType: SpotifyAppRemoteMappers.mapContentType(type), flattenContainers: false) { result, error in
579
362
  if let error = error {
580
- cont.resume(throwing: Self.normalizeContentError(error as NSError, callsite: "Content.getRecommendedContentItems"))
363
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapContentError(error as NSError, callsite: "Content.getRecommendedContentItems"))
581
364
  } else if let list = result as? [any SPTAppRemoteContentItem] {
582
- cont.resume(returning: list.map(Self.contentItemToMap))
365
+ cont.resume(returning: list.map(SpotifyAppRemoteMappers.contentItemToMap))
583
366
  } else {
584
367
  cont.resume(throwing: NativeContentError.unknown("Content.getRecommendedContentItems: unexpected result type"))
585
368
  }
@@ -596,7 +379,7 @@ actor SpotifyAppRemoteCoordinator {
596
379
  let contentItem: any SPTAppRemoteContentItem = try await withCheckedThrowingContinuation { cont in
597
380
  contentAPI.fetchContentItem(forURI: uri) { result, error in
598
381
  if let error = error {
599
- cont.resume(throwing: Self.normalizeContentError(error as NSError, callsite: "Content.getChildren"))
382
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapContentError(error as NSError, callsite: "Content.getChildren"))
600
383
  } else if let item = result as? any SPTAppRemoteContentItem {
601
384
  cont.resume(returning: item)
602
385
  } else {
@@ -608,9 +391,9 @@ actor SpotifyAppRemoteCoordinator {
608
391
  return try await withCheckedThrowingContinuation { cont in
609
392
  contentAPI.fetchChildren(of: contentItem, callback: { result, error in
610
393
  if let error = error {
611
- cont.resume(throwing: Self.normalizeContentError(error as NSError, callsite: "Content.getChildren"))
394
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapContentError(error as NSError, callsite: "Content.getChildren"))
612
395
  } else if let children = result as? [any SPTAppRemoteContentItem] {
613
- cont.resume(returning: children.map(Self.contentItemToMap))
396
+ cont.resume(returning: children.map(SpotifyAppRemoteMappers.contentItemToMap))
614
397
  } else {
615
398
  cont.resume(throwing: NativeContentError.unknown("Content.getChildren: unexpected result type"))
616
399
  }
@@ -626,12 +409,12 @@ actor SpotifyAppRemoteCoordinator {
626
409
  }
627
410
  let imageAPI = try requireImageAPI(callsite: "Images.load")
628
411
  let representable = LocalImageRepresentable(imageIdentifier: imageIdentifier)
629
- let targetSize = Self.mapImageSize(size)
412
+ let targetSize = SpotifyAppRemoteMappers.mapImageSize(size)
630
413
 
631
414
  let image: UIImage = try await withCheckedThrowingContinuation { cont in
632
415
  imageAPI.fetchImage(forItem: representable, with: targetSize) { result, error in
633
416
  if let error = error {
634
- cont.resume(throwing: Self.normalizeImagesError(error as NSError, callsite: "Images.load"))
417
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapImagesError(error as NSError, callsite: "Images.load"))
635
418
  } else if let image = result as? UIImage {
636
419
  cont.resume(returning: image)
637
420
  } else {
@@ -699,7 +482,7 @@ actor SpotifyAppRemoteCoordinator {
699
482
  try await withCheckedThrowingContinuation { (cont: CheckedContinuation<Void, Error>) in
700
483
  block { _, error in
701
484
  if let error = error {
702
- cont.resume(throwing: Self.normalizePlayerError(error as NSError, callsite: callsite))
485
+ cont.resume(throwing: SpotifyAppRemoteErrorMapping.mapPlayerError(error as NSError, callsite: callsite))
703
486
  } else {
704
487
  cont.resume()
705
488
  }
@@ -707,164 +490,6 @@ actor SpotifyAppRemoteCoordinator {
707
490
  }
708
491
  }
709
492
 
710
- private static func normalizePlayerError(_ error: NSError, callsite: String) -> NativePlayerError {
711
- guard error.domain == SPTAppRemoteErrorDomain else {
712
- return .unknown(error.localizedDescription)
713
- }
714
- switch error.code {
715
- case SPTAppRemoteErrorCode.connectionTerminatedError.rawValue:
716
- return .connectionLost("\(callsite): connection to Spotify app was terminated")
717
- case SPTAppRemoteErrorCode.invalidArgumentsError.rawValue:
718
- return .invalidParameter(error.localizedDescription)
719
- case SPTAppRemoteErrorCode.requestFailedError.rawValue:
720
- let desc = error.localizedDescription.lowercased()
721
- if desc.contains("premium") {
722
- return .premiumRequired("\(callsite): Spotify Premium is required for on-demand playback")
723
- }
724
- if desc.contains("not allowed") || desc.contains("restriction") {
725
- return .operationNotAllowed("\(callsite): \(error.localizedDescription)")
726
- }
727
- return .unknown(error.localizedDescription)
728
- default:
729
- return .unknown(error.localizedDescription)
730
- }
731
- }
732
-
733
- private static func playerStateToMap(_ state: any SPTAppRemotePlayerState) -> [String: Any] {
734
- let track = state.track
735
- let restrictions = state.playbackRestrictions
736
- let options = state.playbackOptions
737
- return [
738
- "track": [
739
- "uri": track.uri,
740
- "name": track.name,
741
- "imageIdentifier": track.imageIdentifier,
742
- "duration": track.duration,
743
- "artist": ["name": track.artist.name, "uri": track.artist.uri],
744
- "album": ["name": track.album.name, "uri": track.album.uri],
745
- "isSaved": track.isSaved,
746
- "isEpisode": track.isEpisode,
747
- "isPodcast": track.isPodcast,
748
- "isAdvertisement": track.isAdvertisement,
749
- ] as [String: Any],
750
- "playbackPosition": state.playbackPosition,
751
- "playbackSpeed": state.playbackSpeed,
752
- "isPaused": state.isPaused,
753
- "playbackOptions": [
754
- "isShuffling": options.isShuffling,
755
- "repeatMode": options.repeatMode.rawValue,
756
- ] as [String: Any],
757
- "playbackRestrictions": [
758
- "canSkipNext": restrictions.canSkipNext,
759
- "canSkipPrevious": restrictions.canSkipPrevious,
760
- "canRepeatTrack": restrictions.canRepeatTrack,
761
- "canRepeatContext": restrictions.canRepeatContext,
762
- "canToggleShuffle": restrictions.canToggleShuffle,
763
- "canSeek": restrictions.canSeek,
764
- ] as [String: Any],
765
- "contextTitle": state.contextTitle,
766
- "contextUri": state.contextURI.absoluteString,
767
- ]
768
- }
769
-
770
- private static func capabilitiesToMap(_ capabilities: any SPTAppRemoteUserCapabilities) -> [String: Any] {
771
- ["canPlayOnDemand": capabilities.canPlayOnDemand]
772
- }
773
-
774
- private static func libraryStateToMap(_ state: any SPTAppRemoteLibraryState) -> [String: Any] {
775
- ["uri": state.uri, "isAdded": state.isAdded, "canAdd": state.canAdd]
776
- }
777
-
778
- private static func normalizeUserError(_ error: NSError, callsite: String) -> NativeUserError {
779
- guard error.domain == SPTAppRemoteErrorDomain else {
780
- return .unknown(error.localizedDescription)
781
- }
782
- switch error.code {
783
- case SPTAppRemoteErrorCode.connectionTerminatedError.rawValue:
784
- return .connectionLost("\(callsite): connection to Spotify app was terminated")
785
- case SPTAppRemoteErrorCode.invalidArgumentsError.rawValue:
786
- return .invalidURI("\(callsite): \(error.localizedDescription)")
787
- case SPTAppRemoteErrorCode.requestFailedError.rawValue:
788
- let desc = error.localizedDescription.lowercased()
789
- if desc.contains("not allowed") || desc.contains("restriction") {
790
- return .operationNotAllowed("\(callsite): \(error.localizedDescription)")
791
- }
792
- return .unknown(error.localizedDescription)
793
- default:
794
- return .unknown(error.localizedDescription)
795
- }
796
- }
797
-
798
- private static func normalizeContentError(_ error: NSError, callsite: String) -> NativeContentError {
799
- guard error.domain == SPTAppRemoteErrorDomain else {
800
- return .unknown(error.localizedDescription)
801
- }
802
- switch error.code {
803
- case SPTAppRemoteErrorCode.connectionTerminatedError.rawValue:
804
- return .connectionLost("\(callsite): connection to Spotify app was terminated")
805
- case SPTAppRemoteErrorCode.requestFailedError.rawValue:
806
- let desc = error.localizedDescription.lowercased()
807
- if desc.contains("not supported") || desc.contains("unsupported") {
808
- return .contentAPIUnavailable("\(callsite): content API is unavailable on this Spotify app version")
809
- }
810
- return .unknown(error.localizedDescription)
811
- default:
812
- return .unknown(error.localizedDescription)
813
- }
814
- }
815
-
816
- private static func normalizeImagesError(_ error: NSError, callsite: String) -> NativeImagesError {
817
- guard error.domain == SPTAppRemoteErrorDomain else {
818
- return .unknown(error.localizedDescription)
819
- }
820
- switch error.code {
821
- case SPTAppRemoteErrorCode.connectionTerminatedError.rawValue:
822
- return .notConnected("\(callsite): connection to Spotify app was terminated")
823
- case SPTAppRemoteErrorCode.invalidArgumentsError.rawValue:
824
- return .invalidURI("\(callsite): invalid image identifier")
825
- case SPTAppRemoteErrorCode.requestFailedError.rawValue:
826
- return .imageLoadFailed("\(callsite): Spotify rejected image request")
827
- default:
828
- return .unknown(error.localizedDescription)
829
- }
830
- }
831
-
832
- private static func mapContentType(_ type: String) -> String {
833
- switch type {
834
- case "navigation": return SPTAppRemoteContentTypeNavigation
835
- case "fitness": return SPTAppRemoteContentTypeFitness
836
- case "gaming": return SPTAppRemoteContentTypeGaming
837
- default: return SPTAppRemoteContentTypeDefault
838
- }
839
- }
840
-
841
- private static func mapImageSize(_ size: String) -> CGSize {
842
- switch size {
843
- case "small": return CGSize(width: 64, height: 64)
844
- case "medium": return CGSize(width: 300, height: 300)
845
- default: return CGSize(width: 640, height: 640)
846
- }
847
- }
848
-
849
- private static func contentItemToMap(_ item: any SPTAppRemoteContentItem) -> [String: Any] {
850
- var map: [String: Any] = [
851
- "title": item.title as Any,
852
- "subtitle": item.subtitle as Any,
853
- "contentDescription": item.contentDescription as Any,
854
- "identifier": item.identifier,
855
- "uri": item.uri,
856
- "imageIdentifier": item.imageIdentifier,
857
- "isAvailableOffline": item.isAvailableOffline,
858
- "isPlayable": item.isPlayable,
859
- "isContainer": item.isContainer,
860
- "isPinned": item.isPinned,
861
- ]
862
- if let children = item.children {
863
- map["children"] = children.map(Self.contentItemToMap)
864
- }
865
- return map
866
- }
867
-
868
493
  private func transitionState(_ state: String) {
869
494
  connectionStateString = state
870
495
  onConnectionStateChange?(state)
@@ -0,0 +1,110 @@
1
+ import Foundation
2
+ import SpotifyiOS
3
+
4
+ /// Maps `SPTAppRemote` NSError payloads into per-namespace native error enums.
5
+ /// Keep aligned with [docs/app-remote-error-mapping.md](../docs/app-remote-error-mapping.md).
6
+ enum SpotifyAppRemoteErrorMapping {
7
+ private static func isSPTError(_ error: NSError) -> Bool {
8
+ error.domain == SPTAppRemoteErrorDomain
9
+ }
10
+
11
+ private static func isConnectionTerminated(_ error: NSError) -> Bool {
12
+ isSPTError(error) && error.code == SPTAppRemoteErrorCode.connectionTerminatedError.rawValue
13
+ }
14
+
15
+ private static func isInvalidArguments(_ error: NSError) -> Bool {
16
+ isSPTError(error) && error.code == SPTAppRemoteErrorCode.invalidArgumentsError.rawValue
17
+ }
18
+
19
+ private static func isRequestFailed(_ error: NSError) -> Bool {
20
+ isSPTError(error) && error.code == SPTAppRemoteErrorCode.requestFailedError.rawValue
21
+ }
22
+
23
+ private static func connectionLostMessage(callsite: String) -> String {
24
+ "\(callsite): connection to Spotify app was terminated"
25
+ }
26
+
27
+ private static func containsRestriction(_ description: String) -> Bool {
28
+ let desc = description.lowercased()
29
+ return desc.contains("not allowed") || desc.contains("restriction")
30
+ }
31
+
32
+ static func mapPlayerError(_ error: NSError, callsite: String) -> NativePlayerError {
33
+ guard isSPTError(error) else {
34
+ return .unknown(error.localizedDescription)
35
+ }
36
+ if isConnectionTerminated(error) {
37
+ return .connectionLost(connectionLostMessage(callsite: callsite))
38
+ }
39
+ if isInvalidArguments(error) {
40
+ let desc = error.localizedDescription.lowercased()
41
+ if desc.contains("uri") {
42
+ return .invalidURI("\(callsite): \(error.localizedDescription)")
43
+ }
44
+ return .invalidParameter(error.localizedDescription)
45
+ }
46
+ if isRequestFailed(error) {
47
+ let desc = error.localizedDescription.lowercased()
48
+ if desc.contains("premium") {
49
+ return .premiumRequired("\(callsite): Spotify Premium is required for on-demand playback")
50
+ }
51
+ if containsRestriction(error.localizedDescription) {
52
+ return .operationNotAllowed("\(callsite): \(error.localizedDescription)")
53
+ }
54
+ return .unknown(error.localizedDescription)
55
+ }
56
+ return .unknown(error.localizedDescription)
57
+ }
58
+
59
+ static func mapUserError(_ error: NSError, callsite: String) -> NativeUserError {
60
+ guard isSPTError(error) else {
61
+ return .unknown(error.localizedDescription)
62
+ }
63
+ if isConnectionTerminated(error) {
64
+ return .connectionLost(connectionLostMessage(callsite: callsite))
65
+ }
66
+ if isInvalidArguments(error) {
67
+ return .invalidURI("\(callsite): \(error.localizedDescription)")
68
+ }
69
+ if isRequestFailed(error) {
70
+ if containsRestriction(error.localizedDescription) {
71
+ return .operationNotAllowed("\(callsite): \(error.localizedDescription)")
72
+ }
73
+ return .unknown(error.localizedDescription)
74
+ }
75
+ return .unknown(error.localizedDescription)
76
+ }
77
+
78
+ static func mapContentError(_ error: NSError, callsite: String) -> NativeContentError {
79
+ guard isSPTError(error) else {
80
+ return .unknown(error.localizedDescription)
81
+ }
82
+ if isConnectionTerminated(error) {
83
+ return .connectionLost(connectionLostMessage(callsite: callsite))
84
+ }
85
+ if isRequestFailed(error) {
86
+ let desc = error.localizedDescription.lowercased()
87
+ if desc.contains("not supported") || desc.contains("unsupported") {
88
+ return .contentAPIUnavailable("\(callsite): content API is unavailable on this Spotify app version")
89
+ }
90
+ return .unknown(error.localizedDescription)
91
+ }
92
+ return .unknown(error.localizedDescription)
93
+ }
94
+
95
+ static func mapImagesError(_ error: NSError, callsite: String) -> NativeImagesError {
96
+ guard isSPTError(error) else {
97
+ return .unknown(error.localizedDescription)
98
+ }
99
+ if isConnectionTerminated(error) {
100
+ return .notConnected(connectionLostMessage(callsite: callsite))
101
+ }
102
+ if isInvalidArguments(error) {
103
+ return .invalidURI("\(callsite): invalid image identifier")
104
+ }
105
+ if isRequestFailed(error) {
106
+ return .imageLoadFailed("\(callsite): Spotify rejected image request")
107
+ }
108
+ return .unknown(error.localizedDescription)
109
+ }
110
+ }