@vllnt/convex-music 0.1.0-canary.09eea4e

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 (301) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +169 -0
  3. package/dist/client/index.d.ts +284 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +166 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +100 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +2 -0
  10. package/dist/client/types.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +125 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +1143 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +133 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +80 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/actions.d.ts +144 -0
  28. package/dist/component/actions.d.ts.map +1 -0
  29. package/dist/component/actions.js +125 -0
  30. package/dist/component/actions.js.map +1 -0
  31. package/dist/component/catalog/browse_order.d.ts +22 -0
  32. package/dist/component/catalog/browse_order.d.ts.map +1 -0
  33. package/dist/component/catalog/browse_order.js +37 -0
  34. package/dist/component/catalog/browse_order.js.map +1 -0
  35. package/dist/component/catalog/field_source_policy.d.ts +30 -0
  36. package/dist/component/catalog/field_source_policy.d.ts.map +1 -0
  37. package/dist/component/catalog/field_source_policy.js +36 -0
  38. package/dist/component/catalog/field_source_policy.js.map +1 -0
  39. package/dist/component/catalog/merge.d.ts +58 -0
  40. package/dist/component/catalog/merge.d.ts.map +1 -0
  41. package/dist/component/catalog/merge.js +74 -0
  42. package/dist/component/catalog/merge.js.map +1 -0
  43. package/dist/component/catalog/mutations.d.ts +78 -0
  44. package/dist/component/catalog/mutations.d.ts.map +1 -0
  45. package/dist/component/catalog/mutations.js +259 -0
  46. package/dist/component/catalog/mutations.js.map +1 -0
  47. package/dist/component/catalog/promote.d.ts +21 -0
  48. package/dist/component/catalog/promote.d.ts.map +1 -0
  49. package/dist/component/catalog/promote.js +25 -0
  50. package/dist/component/catalog/promote.js.map +1 -0
  51. package/dist/component/catalog/queries.d.ts +421 -0
  52. package/dist/component/catalog/queries.d.ts.map +1 -0
  53. package/dist/component/catalog/queries.js +168 -0
  54. package/dist/component/catalog/queries.js.map +1 -0
  55. package/dist/component/config/mutations.d.ts +11 -0
  56. package/dist/component/config/mutations.d.ts.map +1 -0
  57. package/dist/component/config/mutations.js +30 -0
  58. package/dist/component/config/mutations.js.map +1 -0
  59. package/dist/component/config/queries.d.ts +9 -0
  60. package/dist/component/config/queries.d.ts.map +1 -0
  61. package/dist/component/config/queries.js +20 -0
  62. package/dist/component/config/queries.js.map +1 -0
  63. package/dist/component/convex.config.d.ts +3 -0
  64. package/dist/component/convex.config.d.ts.map +1 -0
  65. package/dist/component/convex.config.js +30 -0
  66. package/dist/component/convex.config.js.map +1 -0
  67. package/dist/component/crons.d.ts +11 -0
  68. package/dist/component/crons.d.ts.map +1 -0
  69. package/dist/component/crons.js +41 -0
  70. package/dist/component/crons.js.map +1 -0
  71. package/dist/component/imports/actions.d.ts +119 -0
  72. package/dist/component/imports/actions.d.ts.map +1 -0
  73. package/dist/component/imports/actions.js +442 -0
  74. package/dist/component/imports/actions.js.map +1 -0
  75. package/dist/component/imports/dedupe.d.ts +29 -0
  76. package/dist/component/imports/dedupe.d.ts.map +1 -0
  77. package/dist/component/imports/dedupe.js +33 -0
  78. package/dist/component/imports/dedupe.js.map +1 -0
  79. package/dist/component/imports/mutations.d.ts +59 -0
  80. package/dist/component/imports/mutations.d.ts.map +1 -0
  81. package/dist/component/imports/mutations.js +151 -0
  82. package/dist/component/imports/mutations.js.map +1 -0
  83. package/dist/component/imports/queries.d.ts +68 -0
  84. package/dist/component/imports/queries.d.ts.map +1 -0
  85. package/dist/component/imports/queries.js +22 -0
  86. package/dist/component/imports/queries.js.map +1 -0
  87. package/dist/component/imports/state.d.ts +36 -0
  88. package/dist/component/imports/state.d.ts.map +1 -0
  89. package/dist/component/imports/state.js +62 -0
  90. package/dist/component/imports/state.js.map +1 -0
  91. package/dist/component/mutations.d.ts +55 -0
  92. package/dist/component/mutations.d.ts.map +1 -0
  93. package/dist/component/mutations.js +74 -0
  94. package/dist/component/mutations.js.map +1 -0
  95. package/dist/component/providers/actions.d.ts +26 -0
  96. package/dist/component/providers/actions.d.ts.map +1 -0
  97. package/dist/component/providers/actions.js +92 -0
  98. package/dist/component/providers/actions.js.map +1 -0
  99. package/dist/component/providers/apple/impl.d.ts +52 -0
  100. package/dist/component/providers/apple/impl.d.ts.map +1 -0
  101. package/dist/component/providers/apple/impl.js +127 -0
  102. package/dist/component/providers/apple/impl.js.map +1 -0
  103. package/dist/component/providers/apple/jwt.d.ts +32 -0
  104. package/dist/component/providers/apple/jwt.d.ts.map +1 -0
  105. package/dist/component/providers/apple/jwt.js +68 -0
  106. package/dist/component/providers/apple/jwt.js.map +1 -0
  107. package/dist/component/providers/apple/mappers.d.ts +20 -0
  108. package/dist/component/providers/apple/mappers.d.ts.map +1 -0
  109. package/dist/component/providers/apple/mappers.js +68 -0
  110. package/dist/component/providers/apple/mappers.js.map +1 -0
  111. package/dist/component/providers/apple/types.d.ts +80 -0
  112. package/dist/component/providers/apple/types.d.ts.map +1 -0
  113. package/dist/component/providers/apple/types.js +7 -0
  114. package/dist/component/providers/apple/types.js.map +1 -0
  115. package/dist/component/providers/deezer/impl.d.ts +37 -0
  116. package/dist/component/providers/deezer/impl.d.ts.map +1 -0
  117. package/dist/component/providers/deezer/impl.js +99 -0
  118. package/dist/component/providers/deezer/impl.js.map +1 -0
  119. package/dist/component/providers/deezer/mappers.d.ts +18 -0
  120. package/dist/component/providers/deezer/mappers.d.ts.map +1 -0
  121. package/dist/component/providers/deezer/mappers.js +53 -0
  122. package/dist/component/providers/deezer/mappers.js.map +1 -0
  123. package/dist/component/providers/deezer/types.d.ts +59 -0
  124. package/dist/component/providers/deezer/types.d.ts.map +1 -0
  125. package/dist/component/providers/deezer/types.js +7 -0
  126. package/dist/component/providers/deezer/types.js.map +1 -0
  127. package/dist/component/providers/fetch.d.ts +69 -0
  128. package/dist/component/providers/fetch.d.ts.map +1 -0
  129. package/dist/component/providers/fetch.js +145 -0
  130. package/dist/component/providers/fetch.js.map +1 -0
  131. package/dist/component/providers/musicbrainz/impl.d.ts +36 -0
  132. package/dist/component/providers/musicbrainz/impl.d.ts.map +1 -0
  133. package/dist/component/providers/musicbrainz/impl.js +74 -0
  134. package/dist/component/providers/musicbrainz/impl.js.map +1 -0
  135. package/dist/component/providers/musicbrainz/mappers.d.ts +10 -0
  136. package/dist/component/providers/musicbrainz/mappers.d.ts.map +1 -0
  137. package/dist/component/providers/musicbrainz/mappers.js +32 -0
  138. package/dist/component/providers/musicbrainz/mappers.js.map +1 -0
  139. package/dist/component/providers/musicbrainz/types.d.ts +24 -0
  140. package/dist/component/providers/musicbrainz/types.d.ts.map +1 -0
  141. package/dist/component/providers/musicbrainz/types.js +7 -0
  142. package/dist/component/providers/musicbrainz/types.js.map +1 -0
  143. package/dist/component/providers/registry.d.ts +22 -0
  144. package/dist/component/providers/registry.d.ts.map +1 -0
  145. package/dist/component/providers/registry.js +33 -0
  146. package/dist/component/providers/registry.js.map +1 -0
  147. package/dist/component/providers/spotify/client.d.ts +25 -0
  148. package/dist/component/providers/spotify/client.d.ts.map +1 -0
  149. package/dist/component/providers/spotify/client.js +30 -0
  150. package/dist/component/providers/spotify/client.js.map +1 -0
  151. package/dist/component/providers/spotify/impl.d.ts +51 -0
  152. package/dist/component/providers/spotify/impl.d.ts.map +1 -0
  153. package/dist/component/providers/spotify/impl.js +131 -0
  154. package/dist/component/providers/spotify/impl.js.map +1 -0
  155. package/dist/component/providers/spotify/mappers.d.ts +18 -0
  156. package/dist/component/providers/spotify/mappers.d.ts.map +1 -0
  157. package/dist/component/providers/spotify/mappers.js +60 -0
  158. package/dist/component/providers/spotify/mappers.js.map +1 -0
  159. package/dist/component/providers/spotify/types.d.ts +97 -0
  160. package/dist/component/providers/spotify/types.d.ts.map +1 -0
  161. package/dist/component/providers/spotify/types.js +7 -0
  162. package/dist/component/providers/spotify/types.js.map +1 -0
  163. package/dist/component/providers/types.d.ts +84 -0
  164. package/dist/component/providers/types.d.ts.map +1 -0
  165. package/dist/component/providers/types.js +10 -0
  166. package/dist/component/providers/types.js.map +1 -0
  167. package/dist/component/providers/wikidata/impl.d.ts +34 -0
  168. package/dist/component/providers/wikidata/impl.d.ts.map +1 -0
  169. package/dist/component/providers/wikidata/impl.js +88 -0
  170. package/dist/component/providers/wikidata/impl.js.map +1 -0
  171. package/dist/component/providers/wikidata/mappers.d.ts +11 -0
  172. package/dist/component/providers/wikidata/mappers.d.ts.map +1 -0
  173. package/dist/component/providers/wikidata/mappers.js +53 -0
  174. package/dist/component/providers/wikidata/mappers.js.map +1 -0
  175. package/dist/component/providers/wikidata/types.d.ts +35 -0
  176. package/dist/component/providers/wikidata/types.d.ts.map +1 -0
  177. package/dist/component/providers/wikidata/types.js +8 -0
  178. package/dist/component/providers/wikidata/types.js.map +1 -0
  179. package/dist/component/queries.d.ts +103 -0
  180. package/dist/component/queries.d.ts.map +1 -0
  181. package/dist/component/queries.js +45 -0
  182. package/dist/component/queries.js.map +1 -0
  183. package/dist/component/schema.d.ts +566 -0
  184. package/dist/component/schema.d.ts.map +1 -0
  185. package/dist/component/schema.js +59 -0
  186. package/dist/component/schema.js.map +1 -0
  187. package/dist/component/sources/actions.d.ts +51 -0
  188. package/dist/component/sources/actions.d.ts.map +1 -0
  189. package/dist/component/sources/actions.js +164 -0
  190. package/dist/component/sources/actions.js.map +1 -0
  191. package/dist/component/sources/mutations.d.ts +25 -0
  192. package/dist/component/sources/mutations.d.ts.map +1 -0
  193. package/dist/component/sources/mutations.js +64 -0
  194. package/dist/component/sources/mutations.js.map +1 -0
  195. package/dist/component/sources/queries.d.ts +19 -0
  196. package/dist/component/sources/queries.d.ts.map +1 -0
  197. package/dist/component/sources/queries.js +20 -0
  198. package/dist/component/sources/queries.js.map +1 -0
  199. package/dist/component/sync/lifecycle.d.ts +46 -0
  200. package/dist/component/sync/lifecycle.d.ts.map +1 -0
  201. package/dist/component/sync/lifecycle.js +87 -0
  202. package/dist/component/sync/lifecycle.js.map +1 -0
  203. package/dist/component/sync/mutations.d.ts +101 -0
  204. package/dist/component/sync/mutations.d.ts.map +1 -0
  205. package/dist/component/sync/mutations.js +132 -0
  206. package/dist/component/sync/mutations.js.map +1 -0
  207. package/dist/component/sync/queries.d.ts +69 -0
  208. package/dist/component/sync/queries.d.ts.map +1 -0
  209. package/dist/component/sync/queries.js +32 -0
  210. package/dist/component/sync/queries.js.map +1 -0
  211. package/dist/component/validators.d.ts +1112 -0
  212. package/dist/component/validators.d.ts.map +1 -0
  213. package/dist/component/validators.js +317 -0
  214. package/dist/component/validators.js.map +1 -0
  215. package/dist/react/index.d.ts +42 -0
  216. package/dist/react/index.d.ts.map +1 -0
  217. package/dist/react/index.js +43 -0
  218. package/dist/react/index.js.map +1 -0
  219. package/dist/shared.d.ts +28 -0
  220. package/dist/shared.d.ts.map +1 -0
  221. package/dist/shared.js +24 -0
  222. package/dist/shared.js.map +1 -0
  223. package/package.json +126 -0
  224. package/src/client/index.ts +558 -0
  225. package/src/client/types.ts +117 -0
  226. package/src/component/_generated/api.ts +141 -0
  227. package/src/component/_generated/component.ts +1814 -0
  228. package/src/component/_generated/dataModel.ts +60 -0
  229. package/src/component/_generated/server.ts +169 -0
  230. package/src/component/actions.ts +156 -0
  231. package/src/component/catalog/browse_order.test.ts +55 -0
  232. package/src/component/catalog/browse_order.ts +43 -0
  233. package/src/component/catalog/field_source_policy.test.ts +47 -0
  234. package/src/component/catalog/field_source_policy.ts +44 -0
  235. package/src/component/catalog/merge.test.ts +187 -0
  236. package/src/component/catalog/merge.ts +134 -0
  237. package/src/component/catalog/mutations.ts +328 -0
  238. package/src/component/catalog/promote.ts +40 -0
  239. package/src/component/catalog/queries.ts +220 -0
  240. package/src/component/config/mutations.ts +29 -0
  241. package/src/component/config/queries.ts +20 -0
  242. package/src/component/convex.config.ts +32 -0
  243. package/src/component/crons.test.ts +8 -0
  244. package/src/component/crons.ts +52 -0
  245. package/src/component/imports/actions.ts +553 -0
  246. package/src/component/imports/dedupe.test.ts +66 -0
  247. package/src/component/imports/dedupe.ts +54 -0
  248. package/src/component/imports/mutations.ts +203 -0
  249. package/src/component/imports/queries.ts +25 -0
  250. package/src/component/imports/state.test.ts +60 -0
  251. package/src/component/imports/state.ts +74 -0
  252. package/src/component/mutations.ts +96 -0
  253. package/src/component/providers/actions.ts +127 -0
  254. package/src/component/providers/apple/impl.test.ts +267 -0
  255. package/src/component/providers/apple/impl.ts +224 -0
  256. package/src/component/providers/apple/jwt.test.ts +153 -0
  257. package/src/component/providers/apple/jwt.ts +99 -0
  258. package/src/component/providers/apple/mappers.test.ts +190 -0
  259. package/src/component/providers/apple/mappers.ts +100 -0
  260. package/src/component/providers/apple/types.ts +77 -0
  261. package/src/component/providers/deezer/impl.test.ts +109 -0
  262. package/src/component/providers/deezer/impl.ts +179 -0
  263. package/src/component/providers/deezer/mappers.test.ts +78 -0
  264. package/src/component/providers/deezer/mappers.ts +72 -0
  265. package/src/component/providers/deezer/types.ts +59 -0
  266. package/src/component/providers/fetch.test.ts +282 -0
  267. package/src/component/providers/fetch.ts +190 -0
  268. package/src/component/providers/musicbrainz/impl.test.ts +79 -0
  269. package/src/component/providers/musicbrainz/impl.ts +132 -0
  270. package/src/component/providers/musicbrainz/mappers.test.ts +75 -0
  271. package/src/component/providers/musicbrainz/mappers.ts +34 -0
  272. package/src/component/providers/musicbrainz/types.ts +20 -0
  273. package/src/component/providers/registry.test.ts +14 -0
  274. package/src/component/providers/registry.ts +48 -0
  275. package/src/component/providers/spotify/client.test.ts +80 -0
  276. package/src/component/providers/spotify/client.ts +57 -0
  277. package/src/component/providers/spotify/impl.test.ts +311 -0
  278. package/src/component/providers/spotify/impl.ts +230 -0
  279. package/src/component/providers/spotify/mappers.test.ts +182 -0
  280. package/src/component/providers/spotify/mappers.ts +81 -0
  281. package/src/component/providers/spotify/types.ts +101 -0
  282. package/src/component/providers/types.ts +95 -0
  283. package/src/component/providers/wikidata/impl.test.ts +81 -0
  284. package/src/component/providers/wikidata/impl.ts +139 -0
  285. package/src/component/providers/wikidata/mappers.test.ts +62 -0
  286. package/src/component/providers/wikidata/mappers.ts +66 -0
  287. package/src/component/providers/wikidata/types.ts +28 -0
  288. package/src/component/queries.ts +51 -0
  289. package/src/component/schema.ts +81 -0
  290. package/src/component/sources/actions.ts +177 -0
  291. package/src/component/sources/mutations.ts +67 -0
  292. package/src/component/sources/queries.ts +21 -0
  293. package/src/component/sync/lifecycle.test.ts +72 -0
  294. package/src/component/sync/lifecycle.ts +121 -0
  295. package/src/component/sync/mutations.ts +154 -0
  296. package/src/component/sync/queries.ts +33 -0
  297. package/src/component/validators.ts +411 -0
  298. package/src/react/index.test.ts +78 -0
  299. package/src/react/index.ts +112 -0
  300. package/src/shared.ts +32 -0
  301. package/src/test.ts +22 -0
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Spotify auth + base URL. Client-credentials OAuth: exchange the app id/secret
3
+ * for a bearer token (~1h). The token is cached by the action layer (a provider
4
+ * token table / action-cache); this module just performs the exchange.
5
+ */
6
+
7
+ import {
8
+ DEFAULT_RETRY_CONFIG,
9
+ type FetchDeps,
10
+ type RetryConfig,
11
+ defaultFetchDeps,
12
+ fetchJson,
13
+ } from "../fetch.js";
14
+ import type { SpotifyTokenResponse } from "./types.js";
15
+
16
+ /** Spotify Web API base. */
17
+ export const SPOTIFY_API_BASE = "https://api.spotify.com/v1";
18
+
19
+ /** Spotify token endpoint. */
20
+ export const SPOTIFY_TOKEN_URL = "https://accounts.spotify.com/api/token";
21
+
22
+ /** A fetched Spotify access token and its lifetime. */
23
+ export interface SpotifyToken {
24
+ accessToken: string;
25
+ expiresInSec: number;
26
+ }
27
+
28
+ /**
29
+ * Exchange client credentials for an access token.
30
+ *
31
+ * @param clientId - `SPOTIFY_CLIENT_ID`.
32
+ * @param clientSecret - `SPOTIFY_CLIENT_SECRET`.
33
+ * @param cfg - retry/timeout policy.
34
+ * @param deps - injectable fetch/sleep/random.
35
+ */
36
+ export async function fetchSpotifyToken(
37
+ clientId: string,
38
+ clientSecret: string,
39
+ cfg: RetryConfig = DEFAULT_RETRY_CONFIG,
40
+ deps: FetchDeps = defaultFetchDeps,
41
+ ): Promise<SpotifyToken> {
42
+ const res = await fetchJson<SpotifyTokenResponse>(
43
+ "spotify",
44
+ SPOTIFY_TOKEN_URL,
45
+ {
46
+ method: "POST",
47
+ headers: {
48
+ "Content-Type": "application/x-www-form-urlencoded",
49
+ Authorization: `Basic ${btoa(`${clientId}:${clientSecret}`)}`,
50
+ },
51
+ body: "grant_type=client_credentials",
52
+ },
53
+ cfg,
54
+ deps,
55
+ );
56
+ return { accessToken: res.access_token, expiresInSec: res.expires_in };
57
+ }
@@ -0,0 +1,311 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { FetchDeps } from "../fetch.js";
3
+ import {
4
+ DEFAULT_SPOTIFY_CONFIG,
5
+ type SpotifyConfig,
6
+ SpotifyProvider,
7
+ } from "./impl.js";
8
+
9
+ interface Route {
10
+ match: RegExp;
11
+ body: unknown;
12
+ status?: number;
13
+ }
14
+
15
+ /** A routed fake `fetch` returning fixtures keyed by URL pattern. */
16
+ function harness(routes: Route[]): {
17
+ deps: FetchDeps;
18
+ urls: string[];
19
+ } {
20
+ const urls: string[] = [];
21
+ return {
22
+ urls,
23
+ deps: {
24
+ fetch: (url) => {
25
+ urls.push(url);
26
+ const route = routes.find((r) => r.match.test(url));
27
+ if (route === undefined) {
28
+ return Promise.resolve(new Response("no route", { status: 404 }));
29
+ }
30
+ return Promise.resolve(
31
+ new Response(JSON.stringify(route.body), {
32
+ status: route.status ?? 200,
33
+ headers: { "content-type": "application/json" },
34
+ }),
35
+ );
36
+ },
37
+ sleep: () => Promise.resolve(),
38
+ random: () => 0,
39
+ },
40
+ };
41
+ }
42
+
43
+ function makeProvider(
44
+ routes: Route[],
45
+ cfg: SpotifyConfig = DEFAULT_SPOTIFY_CONFIG,
46
+ ): { provider: SpotifyProvider; urls: string[] } {
47
+ const { deps, urls } = harness(routes);
48
+ return {
49
+ provider: new SpotifyProvider(() => Promise.resolve("tok"), cfg, deps),
50
+ urls,
51
+ };
52
+ }
53
+
54
+ describe("SpotifyProvider", () => {
55
+ it("getArtist maps the artist (no query params)", async () => {
56
+ const { provider, urls } = makeProvider([
57
+ {
58
+ match: /\/artists\/a1$/,
59
+ body: { id: "a1", name: "Daft Punk", genres: ["house"], popularity: 80 },
60
+ },
61
+ ]);
62
+ const result = await provider.getArtist("a1");
63
+ expect(result.externalId).toBe("a1");
64
+ expect(result.value.name).toBe("Daft Punk");
65
+ expect(urls[0]).toBe("https://api.spotify.com/v1/artists/a1");
66
+ });
67
+
68
+ it("getTrack passes the market param", async () => {
69
+ const { provider, urls } = makeProvider([
70
+ {
71
+ match: /\/tracks\/t1\?/,
72
+ body: {
73
+ id: "t1",
74
+ name: "One More Time",
75
+ artists: [{ id: "a1", name: "Daft Punk" }],
76
+ external_ids: { isrc: "GBDUW0000059" },
77
+ },
78
+ },
79
+ ]);
80
+ const result = await provider.getTrack("t1");
81
+ expect(result.value.isrc).toBe("GBDUW0000059");
82
+ expect(urls[0]).toContain("market=US");
83
+ });
84
+
85
+ it("getAlbum maps album + first-page tracks", async () => {
86
+ const { provider } = makeProvider([
87
+ {
88
+ match: /\/albums\/al1\?/,
89
+ body: {
90
+ id: "al1",
91
+ name: "Discovery",
92
+ total_tracks: 2,
93
+ tracks: {
94
+ items: [
95
+ { id: "t1", name: "A", artists: [{ id: "a1", name: "Daft Punk" }] },
96
+ { id: "t2", name: "B", artists: [{ id: "a1", name: "Daft Punk" }] },
97
+ ],
98
+ total: 2,
99
+ },
100
+ },
101
+ },
102
+ ]);
103
+ const album = await provider.getAlbum("al1");
104
+ expect(album.value.title).toBe("Discovery");
105
+ expect(album.tracks.map((t) => t.externalId)).toEqual(["t1", "t2"]);
106
+ });
107
+
108
+ it("getAlbum tolerates a missing tracks object", async () => {
109
+ const { provider } = makeProvider([
110
+ { match: /\/albums\/al2\?/, body: { id: "al2", name: "Empty" } },
111
+ ]);
112
+ expect((await provider.getAlbum("al2")).tracks).toEqual([]);
113
+ });
114
+
115
+ it("getPlaylist maps metadata + filters null tracks", async () => {
116
+ const { provider } = makeProvider([
117
+ {
118
+ match: /\/playlists\/p1\?/,
119
+ body: {
120
+ id: "p1",
121
+ name: "Top",
122
+ description: "d",
123
+ owner: { display_name: "Spotify" },
124
+ tracks: {
125
+ items: [
126
+ {
127
+ track: {
128
+ id: "t1",
129
+ name: "A",
130
+ artists: [{ id: "a1", name: "X" }],
131
+ },
132
+ },
133
+ { track: null },
134
+ ],
135
+ },
136
+ },
137
+ },
138
+ ]);
139
+ const playlist = await provider.getPlaylist("p1");
140
+ expect(playlist.value.owner).toBe("Spotify");
141
+ expect(playlist.tracks).toHaveLength(1);
142
+ });
143
+
144
+ it("getPlaylist tolerates a missing tracks object", async () => {
145
+ const { provider } = makeProvider([
146
+ { match: /\/playlists\/p2\?/, body: { id: "p2", name: "Bare" } },
147
+ ]);
148
+ const playlist = await provider.getPlaylist("p2");
149
+ expect(playlist.tracks).toEqual([]);
150
+ expect(playlist.value.title).toBe("Bare");
151
+ });
152
+
153
+ it("getArtistTopTracks maps every track", async () => {
154
+ const { provider } = makeProvider([
155
+ {
156
+ match: /\/artists\/a1\/top-tracks\?/,
157
+ body: {
158
+ tracks: [
159
+ { id: "t1", name: "A", artists: [{ id: "a1", name: "X" }] },
160
+ { id: "t2", name: "B", artists: [{ id: "a1", name: "X" }] },
161
+ ],
162
+ },
163
+ },
164
+ ]);
165
+ expect(await provider.getArtistTopTracks("a1")).toHaveLength(2);
166
+ });
167
+
168
+ it("getArtistAlbums slices to the cap + flags partial", async () => {
169
+ const cfg: SpotifyConfig = { ...DEFAULT_SPOTIFY_CONFIG, maxAlbumsPerArtist: 1 };
170
+ const { provider } = makeProvider(
171
+ [
172
+ {
173
+ match: /\/artists\/a1\/albums\?/,
174
+ body: {
175
+ total: 2,
176
+ items: [
177
+ { id: "al1", name: "One" },
178
+ { id: "al2", name: "Two" },
179
+ ],
180
+ },
181
+ },
182
+ { match: /\/albums\/al1\?/, body: { id: "al1", name: "One" } },
183
+ { match: /\/albums\/al2\?/, body: { id: "al2", name: "Two" } },
184
+ ],
185
+ cfg,
186
+ );
187
+ const result = await provider.getArtistAlbums("a1");
188
+ expect(result.isPartial).toBe(true);
189
+ expect(result.albums).toHaveLength(1);
190
+ expect(result.albums[0]?.externalId).toBe("al1");
191
+ });
192
+
193
+ it("getArtistAlbums returns all when under the cap", async () => {
194
+ const { provider } = makeProvider([
195
+ {
196
+ match: /\/artists\/a2\/albums\?/,
197
+ body: { total: 1, items: [{ id: "al9", name: "Solo" }] },
198
+ },
199
+ { match: /\/albums\/al9\?/, body: { id: "al9", name: "Solo" } },
200
+ ]);
201
+ const result = await provider.getArtistAlbums("a2");
202
+ expect(result.isPartial).toBe(false);
203
+ expect(result.albums).toHaveLength(1);
204
+ });
205
+
206
+ it("search(artist) maps hits", async () => {
207
+ const { provider } = makeProvider([
208
+ {
209
+ match: /\/search\?/,
210
+ body: { artists: { items: [{ id: "a1", name: "X", genres: [] }] } },
211
+ },
212
+ ]);
213
+ const results = await provider.search("x", "artist");
214
+ expect(results).toEqual([
215
+ {
216
+ type: "artist",
217
+ data: {
218
+ externalId: "a1",
219
+ value: {
220
+ name: "X",
221
+ genres: [],
222
+ popularity: undefined,
223
+ imageUrl: undefined,
224
+ url: undefined,
225
+ },
226
+ },
227
+ },
228
+ ]);
229
+ });
230
+
231
+ it("search(artist) tolerates a missing artists field", async () => {
232
+ const { provider } = makeProvider([{ match: /\/search\?/, body: {} }]);
233
+ expect(await provider.search("x", "artist")).toEqual([]);
234
+ });
235
+
236
+ it("search(track) maps hits", async () => {
237
+ const { provider } = makeProvider([
238
+ {
239
+ match: /\/search\?/,
240
+ body: {
241
+ tracks: {
242
+ items: [{ id: "t1", name: "A", artists: [{ id: "a1", name: "X" }] }],
243
+ },
244
+ },
245
+ },
246
+ ]);
247
+ const results = await provider.search("a", "track");
248
+ expect(results[0]?.type).toBe("track");
249
+ });
250
+
251
+ it("search(track) tolerates a missing tracks field", async () => {
252
+ const { provider } = makeProvider([{ match: /\/search\?/, body: {} }]);
253
+ expect(await provider.search("x", "track")).toEqual([]);
254
+ });
255
+
256
+ it("searchByIsrc maps + tolerates empties", async () => {
257
+ const hit = makeProvider([
258
+ {
259
+ match: /\/search\?/,
260
+ body: {
261
+ tracks: {
262
+ items: [
263
+ {
264
+ id: "t1",
265
+ name: "A",
266
+ artists: [{ id: "a1", name: "X" }],
267
+ external_ids: { isrc: "X" },
268
+ },
269
+ ],
270
+ },
271
+ },
272
+ },
273
+ ]);
274
+ expect(await hit.provider.searchByIsrc("X")).toHaveLength(1);
275
+ const miss = makeProvider([{ match: /\/search\?/, body: {} }]);
276
+ expect(await miss.provider.searchByIsrc("Y")).toEqual([]);
277
+ });
278
+
279
+ it("getSeveralTracks chunks ids + filters market-restricted nulls", async () => {
280
+ const cfg: SpotifyConfig = { ...DEFAULT_SPOTIFY_CONFIG, batchSize: 2 };
281
+ const { provider, urls } = makeProvider(
282
+ [
283
+ {
284
+ match: /ids=t1%2Ct2/,
285
+ body: {
286
+ tracks: [
287
+ { id: "t1", name: "A", artists: [{ id: "a", name: "X" }] },
288
+ null,
289
+ ],
290
+ },
291
+ },
292
+ {
293
+ match: /ids=t3(&|$)/,
294
+ body: {
295
+ tracks: [{ id: "t3", name: "C", artists: [{ id: "a", name: "X" }] }],
296
+ },
297
+ },
298
+ ],
299
+ cfg,
300
+ );
301
+ const tracks = await provider.getSeveralTracks(["t1", "t2", "t3"]);
302
+ expect(tracks.map((t) => t.externalId)).toEqual(["t1", "t3"]);
303
+ expect(urls).toHaveLength(2);
304
+ });
305
+
306
+ it("getSeveralTracks returns empty for no ids", async () => {
307
+ const { provider, urls } = makeProvider([]);
308
+ expect(await provider.getSeveralTracks([])).toEqual([]);
309
+ expect(urls).toHaveLength(0);
310
+ });
311
+ });
@@ -0,0 +1,230 @@
1
+ /**
2
+ * Spotify adapter. Implements {@link MusicProvider} over the Web API. Token
3
+ * acquisition is injected (`getToken`) so the class stays pure + testable; the
4
+ * action layer wires it to the cached client-credentials token. Album/playlist
5
+ * listings take the first page (bounded by `pageLimit`); deep pagination is a
6
+ * follow-up (`read-through-fetch` / catalog import enrich ISRC separately).
7
+ */
8
+
9
+ import {
10
+ DEFAULT_RETRY_CONFIG,
11
+ type FetchDeps,
12
+ type RetryConfig,
13
+ defaultFetchDeps,
14
+ fetchJson,
15
+ mapWithConcurrency,
16
+ } from "../fetch.js";
17
+ import type {
18
+ ArtistAlbumsResult,
19
+ MusicProvider,
20
+ ProviderAlbum,
21
+ ProviderArtist,
22
+ ProviderPlaylist,
23
+ ProviderSearchResult,
24
+ ProviderTrack,
25
+ } from "../types.js";
26
+ import { SPOTIFY_API_BASE } from "./client.js";
27
+ import { mapAlbum, mapArtist, mapPlaylist, mapTrack } from "./mappers.js";
28
+ import type {
29
+ SpotifyAlbumDetail,
30
+ SpotifyArtist,
31
+ SpotifyPagedAlbums,
32
+ SpotifyPlaylistResponse,
33
+ SpotifySearchResponse,
34
+ SpotifySeveralTracksResponse,
35
+ SpotifyTopTracksResponse,
36
+ SpotifyTrack,
37
+ } from "./types.js";
38
+
39
+ /** Tunable Spotify limits (songtrivia-derived defaults; all overridable). */
40
+ export interface SpotifyConfig {
41
+ /** Market for catalog availability + previews. */
42
+ market: string;
43
+ /** Retry/timeout policy for each call. */
44
+ retry: RetryConfig;
45
+ /** Max results per free-text search. */
46
+ searchLimit: number;
47
+ /** Max results per ISRC search. */
48
+ isrcSearchLimit: number;
49
+ /** Ids per batch `GET /tracks` request. */
50
+ batchSize: number;
51
+ /** Cap on albums fetched per artist. */
52
+ maxAlbumsPerArtist: number;
53
+ /** Page size for album/playlist/artist-album listings. */
54
+ pageLimit: number;
55
+ /** Concurrent album fetches per artist. */
56
+ albumConcurrency: number;
57
+ /** `include_groups` filter for artist albums. */
58
+ albumGroups: string;
59
+ }
60
+
61
+ /** Default Spotify limits. */
62
+ export const DEFAULT_SPOTIFY_CONFIG: SpotifyConfig = {
63
+ market: "US",
64
+ retry: DEFAULT_RETRY_CONFIG,
65
+ searchLimit: 10,
66
+ isrcSearchLimit: 5,
67
+ batchSize: 50,
68
+ maxAlbumsPerArtist: 30,
69
+ pageLimit: 50,
70
+ albumConcurrency: 5,
71
+ albumGroups: "album,single",
72
+ };
73
+
74
+ export class SpotifyProvider implements MusicProvider {
75
+ readonly id = "spotify" as const;
76
+
77
+ constructor(
78
+ private readonly getToken: () => Promise<string>,
79
+ private readonly cfg: SpotifyConfig = DEFAULT_SPOTIFY_CONFIG,
80
+ private readonly deps: FetchDeps = defaultFetchDeps,
81
+ ) {}
82
+
83
+ private async api<T>(
84
+ path: string,
85
+ params: Record<string, string> = {},
86
+ ): Promise<T> {
87
+ const token = await this.getToken();
88
+ const url = new URL(SPOTIFY_API_BASE + path);
89
+ for (const [key, value] of Object.entries(params)) {
90
+ url.searchParams.set(key, value);
91
+ }
92
+ return fetchJson<T>(
93
+ "spotify",
94
+ url.toString(),
95
+ { headers: { Authorization: `Bearer ${token}` } },
96
+ this.cfg.retry,
97
+ this.deps,
98
+ );
99
+ }
100
+
101
+ private toTrack(raw: SpotifyTrack): ProviderTrack {
102
+ return { externalId: raw.id, value: mapTrack(raw) };
103
+ }
104
+
105
+ async getArtist(externalId: string): Promise<ProviderArtist> {
106
+ const raw = await this.api<SpotifyArtist>(
107
+ `/artists/${encodeURIComponent(externalId)}`,
108
+ );
109
+ return { externalId: raw.id, value: mapArtist(raw) };
110
+ }
111
+
112
+ async getTrack(externalId: string): Promise<ProviderTrack> {
113
+ const raw = await this.api<SpotifyTrack>(
114
+ `/tracks/${encodeURIComponent(externalId)}`,
115
+ { market: this.cfg.market },
116
+ );
117
+ return this.toTrack(raw);
118
+ }
119
+
120
+ async getAlbum(externalId: string): Promise<ProviderAlbum> {
121
+ const raw = await this.api<SpotifyAlbumDetail>(
122
+ `/albums/${encodeURIComponent(externalId)}`,
123
+ { market: this.cfg.market, limit: String(this.cfg.pageLimit) },
124
+ );
125
+ const items = raw.tracks?.items ?? [];
126
+ return {
127
+ externalId: raw.id,
128
+ value: mapAlbum(raw),
129
+ tracks: items.map((track) => this.toTrack(track)),
130
+ };
131
+ }
132
+
133
+ async getPlaylist(externalId: string): Promise<ProviderPlaylist> {
134
+ const raw = await this.api<SpotifyPlaylistResponse>(
135
+ `/playlists/${encodeURIComponent(externalId)}`,
136
+ { market: this.cfg.market },
137
+ );
138
+ const tracks: ProviderTrack[] = [];
139
+ for (const item of raw.tracks?.items ?? []) {
140
+ if (item.track !== null) tracks.push(this.toTrack(item.track));
141
+ }
142
+ return { externalId: raw.id, value: mapPlaylist(raw), tracks };
143
+ }
144
+
145
+ async getArtistTopTracks(externalId: string): Promise<ProviderTrack[]> {
146
+ const raw = await this.api<SpotifyTopTracksResponse>(
147
+ `/artists/${encodeURIComponent(externalId)}/top-tracks`,
148
+ { market: this.cfg.market },
149
+ );
150
+ return raw.tracks.map((track) => this.toTrack(track));
151
+ }
152
+
153
+ async getArtistAlbums(externalId: string): Promise<ArtistAlbumsResult> {
154
+ const raw = await this.api<SpotifyPagedAlbums>(
155
+ `/artists/${encodeURIComponent(externalId)}/albums`,
156
+ {
157
+ market: this.cfg.market,
158
+ limit: String(this.cfg.pageLimit),
159
+ include_groups: this.cfg.albumGroups,
160
+ },
161
+ );
162
+ const isPartial = raw.total > this.cfg.maxAlbumsPerArtist;
163
+ const slice = raw.items.slice(0, this.cfg.maxAlbumsPerArtist);
164
+ const albums = await mapWithConcurrency(
165
+ slice,
166
+ this.cfg.albumConcurrency,
167
+ (album) => this.getAlbum(album.id),
168
+ );
169
+ return { albums, isPartial };
170
+ }
171
+
172
+ async search(
173
+ query: string,
174
+ type: "artist" | "track",
175
+ ): Promise<ProviderSearchResult[]> {
176
+ const raw = await this.api<SpotifySearchResponse>("/search", {
177
+ q: query,
178
+ type,
179
+ limit: String(this.cfg.searchLimit),
180
+ market: this.cfg.market,
181
+ });
182
+ if (type === "artist") {
183
+ return (raw.artists?.items ?? []).map(
184
+ (artist): ProviderSearchResult => ({
185
+ type: "artist",
186
+ data: { externalId: artist.id, value: mapArtist(artist) },
187
+ }),
188
+ );
189
+ }
190
+ return (raw.tracks?.items ?? []).map(
191
+ (track): ProviderSearchResult => ({
192
+ type: "track",
193
+ data: this.toTrack(track),
194
+ }),
195
+ );
196
+ }
197
+
198
+ async searchByIsrc(isrc: string): Promise<ProviderTrack[]> {
199
+ const raw = await this.api<SpotifySearchResponse>("/search", {
200
+ q: `isrc:${isrc}`,
201
+ type: "track",
202
+ limit: String(this.cfg.isrcSearchLimit),
203
+ market: this.cfg.market,
204
+ });
205
+ return (raw.tracks?.items ?? []).map((track) => this.toTrack(track));
206
+ }
207
+
208
+ async getSeveralTracks(externalIds: string[]): Promise<ProviderTrack[]> {
209
+ const chunks: string[][] = [];
210
+ for (let i = 0; i < externalIds.length; i += this.cfg.batchSize) {
211
+ chunks.push(externalIds.slice(i, i + this.cfg.batchSize));
212
+ }
213
+ const pages = await mapWithConcurrency(
214
+ chunks,
215
+ this.cfg.albumConcurrency,
216
+ (chunk) =>
217
+ this.api<SpotifySeveralTracksResponse>("/tracks", {
218
+ ids: chunk.join(","),
219
+ market: this.cfg.market,
220
+ }),
221
+ );
222
+ const out: ProviderTrack[] = [];
223
+ for (const page of pages) {
224
+ for (const track of page.tracks) {
225
+ if (track !== null) out.push(this.toTrack(track));
226
+ }
227
+ }
228
+ return out;
229
+ }
230
+ }