@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.
- package/LICENSE +21 -0
- package/README.md +169 -0
- package/dist/client/index.d.ts +284 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +166 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +100 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +125 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +1143 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/actions.d.ts +144 -0
- package/dist/component/actions.d.ts.map +1 -0
- package/dist/component/actions.js +125 -0
- package/dist/component/actions.js.map +1 -0
- package/dist/component/catalog/browse_order.d.ts +22 -0
- package/dist/component/catalog/browse_order.d.ts.map +1 -0
- package/dist/component/catalog/browse_order.js +37 -0
- package/dist/component/catalog/browse_order.js.map +1 -0
- package/dist/component/catalog/field_source_policy.d.ts +30 -0
- package/dist/component/catalog/field_source_policy.d.ts.map +1 -0
- package/dist/component/catalog/field_source_policy.js +36 -0
- package/dist/component/catalog/field_source_policy.js.map +1 -0
- package/dist/component/catalog/merge.d.ts +58 -0
- package/dist/component/catalog/merge.d.ts.map +1 -0
- package/dist/component/catalog/merge.js +74 -0
- package/dist/component/catalog/merge.js.map +1 -0
- package/dist/component/catalog/mutations.d.ts +78 -0
- package/dist/component/catalog/mutations.d.ts.map +1 -0
- package/dist/component/catalog/mutations.js +259 -0
- package/dist/component/catalog/mutations.js.map +1 -0
- package/dist/component/catalog/promote.d.ts +21 -0
- package/dist/component/catalog/promote.d.ts.map +1 -0
- package/dist/component/catalog/promote.js +25 -0
- package/dist/component/catalog/promote.js.map +1 -0
- package/dist/component/catalog/queries.d.ts +421 -0
- package/dist/component/catalog/queries.d.ts.map +1 -0
- package/dist/component/catalog/queries.js +168 -0
- package/dist/component/catalog/queries.js.map +1 -0
- package/dist/component/config/mutations.d.ts +11 -0
- package/dist/component/config/mutations.d.ts.map +1 -0
- package/dist/component/config/mutations.js +30 -0
- package/dist/component/config/mutations.js.map +1 -0
- package/dist/component/config/queries.d.ts +9 -0
- package/dist/component/config/queries.d.ts.map +1 -0
- package/dist/component/config/queries.js +20 -0
- package/dist/component/config/queries.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +30 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +11 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +41 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/imports/actions.d.ts +119 -0
- package/dist/component/imports/actions.d.ts.map +1 -0
- package/dist/component/imports/actions.js +442 -0
- package/dist/component/imports/actions.js.map +1 -0
- package/dist/component/imports/dedupe.d.ts +29 -0
- package/dist/component/imports/dedupe.d.ts.map +1 -0
- package/dist/component/imports/dedupe.js +33 -0
- package/dist/component/imports/dedupe.js.map +1 -0
- package/dist/component/imports/mutations.d.ts +59 -0
- package/dist/component/imports/mutations.d.ts.map +1 -0
- package/dist/component/imports/mutations.js +151 -0
- package/dist/component/imports/mutations.js.map +1 -0
- package/dist/component/imports/queries.d.ts +68 -0
- package/dist/component/imports/queries.d.ts.map +1 -0
- package/dist/component/imports/queries.js +22 -0
- package/dist/component/imports/queries.js.map +1 -0
- package/dist/component/imports/state.d.ts +36 -0
- package/dist/component/imports/state.d.ts.map +1 -0
- package/dist/component/imports/state.js +62 -0
- package/dist/component/imports/state.js.map +1 -0
- package/dist/component/mutations.d.ts +55 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +74 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/providers/actions.d.ts +26 -0
- package/dist/component/providers/actions.d.ts.map +1 -0
- package/dist/component/providers/actions.js +92 -0
- package/dist/component/providers/actions.js.map +1 -0
- package/dist/component/providers/apple/impl.d.ts +52 -0
- package/dist/component/providers/apple/impl.d.ts.map +1 -0
- package/dist/component/providers/apple/impl.js +127 -0
- package/dist/component/providers/apple/impl.js.map +1 -0
- package/dist/component/providers/apple/jwt.d.ts +32 -0
- package/dist/component/providers/apple/jwt.d.ts.map +1 -0
- package/dist/component/providers/apple/jwt.js +68 -0
- package/dist/component/providers/apple/jwt.js.map +1 -0
- package/dist/component/providers/apple/mappers.d.ts +20 -0
- package/dist/component/providers/apple/mappers.d.ts.map +1 -0
- package/dist/component/providers/apple/mappers.js +68 -0
- package/dist/component/providers/apple/mappers.js.map +1 -0
- package/dist/component/providers/apple/types.d.ts +80 -0
- package/dist/component/providers/apple/types.d.ts.map +1 -0
- package/dist/component/providers/apple/types.js +7 -0
- package/dist/component/providers/apple/types.js.map +1 -0
- package/dist/component/providers/deezer/impl.d.ts +37 -0
- package/dist/component/providers/deezer/impl.d.ts.map +1 -0
- package/dist/component/providers/deezer/impl.js +99 -0
- package/dist/component/providers/deezer/impl.js.map +1 -0
- package/dist/component/providers/deezer/mappers.d.ts +18 -0
- package/dist/component/providers/deezer/mappers.d.ts.map +1 -0
- package/dist/component/providers/deezer/mappers.js +53 -0
- package/dist/component/providers/deezer/mappers.js.map +1 -0
- package/dist/component/providers/deezer/types.d.ts +59 -0
- package/dist/component/providers/deezer/types.d.ts.map +1 -0
- package/dist/component/providers/deezer/types.js +7 -0
- package/dist/component/providers/deezer/types.js.map +1 -0
- package/dist/component/providers/fetch.d.ts +69 -0
- package/dist/component/providers/fetch.d.ts.map +1 -0
- package/dist/component/providers/fetch.js +145 -0
- package/dist/component/providers/fetch.js.map +1 -0
- package/dist/component/providers/musicbrainz/impl.d.ts +36 -0
- package/dist/component/providers/musicbrainz/impl.d.ts.map +1 -0
- package/dist/component/providers/musicbrainz/impl.js +74 -0
- package/dist/component/providers/musicbrainz/impl.js.map +1 -0
- package/dist/component/providers/musicbrainz/mappers.d.ts +10 -0
- package/dist/component/providers/musicbrainz/mappers.d.ts.map +1 -0
- package/dist/component/providers/musicbrainz/mappers.js +32 -0
- package/dist/component/providers/musicbrainz/mappers.js.map +1 -0
- package/dist/component/providers/musicbrainz/types.d.ts +24 -0
- package/dist/component/providers/musicbrainz/types.d.ts.map +1 -0
- package/dist/component/providers/musicbrainz/types.js +7 -0
- package/dist/component/providers/musicbrainz/types.js.map +1 -0
- package/dist/component/providers/registry.d.ts +22 -0
- package/dist/component/providers/registry.d.ts.map +1 -0
- package/dist/component/providers/registry.js +33 -0
- package/dist/component/providers/registry.js.map +1 -0
- package/dist/component/providers/spotify/client.d.ts +25 -0
- package/dist/component/providers/spotify/client.d.ts.map +1 -0
- package/dist/component/providers/spotify/client.js +30 -0
- package/dist/component/providers/spotify/client.js.map +1 -0
- package/dist/component/providers/spotify/impl.d.ts +51 -0
- package/dist/component/providers/spotify/impl.d.ts.map +1 -0
- package/dist/component/providers/spotify/impl.js +131 -0
- package/dist/component/providers/spotify/impl.js.map +1 -0
- package/dist/component/providers/spotify/mappers.d.ts +18 -0
- package/dist/component/providers/spotify/mappers.d.ts.map +1 -0
- package/dist/component/providers/spotify/mappers.js +60 -0
- package/dist/component/providers/spotify/mappers.js.map +1 -0
- package/dist/component/providers/spotify/types.d.ts +97 -0
- package/dist/component/providers/spotify/types.d.ts.map +1 -0
- package/dist/component/providers/spotify/types.js +7 -0
- package/dist/component/providers/spotify/types.js.map +1 -0
- package/dist/component/providers/types.d.ts +84 -0
- package/dist/component/providers/types.d.ts.map +1 -0
- package/dist/component/providers/types.js +10 -0
- package/dist/component/providers/types.js.map +1 -0
- package/dist/component/providers/wikidata/impl.d.ts +34 -0
- package/dist/component/providers/wikidata/impl.d.ts.map +1 -0
- package/dist/component/providers/wikidata/impl.js +88 -0
- package/dist/component/providers/wikidata/impl.js.map +1 -0
- package/dist/component/providers/wikidata/mappers.d.ts +11 -0
- package/dist/component/providers/wikidata/mappers.d.ts.map +1 -0
- package/dist/component/providers/wikidata/mappers.js +53 -0
- package/dist/component/providers/wikidata/mappers.js.map +1 -0
- package/dist/component/providers/wikidata/types.d.ts +35 -0
- package/dist/component/providers/wikidata/types.d.ts.map +1 -0
- package/dist/component/providers/wikidata/types.js +8 -0
- package/dist/component/providers/wikidata/types.js.map +1 -0
- package/dist/component/queries.d.ts +103 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +45 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +566 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +59 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/sources/actions.d.ts +51 -0
- package/dist/component/sources/actions.d.ts.map +1 -0
- package/dist/component/sources/actions.js +164 -0
- package/dist/component/sources/actions.js.map +1 -0
- package/dist/component/sources/mutations.d.ts +25 -0
- package/dist/component/sources/mutations.d.ts.map +1 -0
- package/dist/component/sources/mutations.js +64 -0
- package/dist/component/sources/mutations.js.map +1 -0
- package/dist/component/sources/queries.d.ts +19 -0
- package/dist/component/sources/queries.d.ts.map +1 -0
- package/dist/component/sources/queries.js +20 -0
- package/dist/component/sources/queries.js.map +1 -0
- package/dist/component/sync/lifecycle.d.ts +46 -0
- package/dist/component/sync/lifecycle.d.ts.map +1 -0
- package/dist/component/sync/lifecycle.js +87 -0
- package/dist/component/sync/lifecycle.js.map +1 -0
- package/dist/component/sync/mutations.d.ts +101 -0
- package/dist/component/sync/mutations.d.ts.map +1 -0
- package/dist/component/sync/mutations.js +132 -0
- package/dist/component/sync/mutations.js.map +1 -0
- package/dist/component/sync/queries.d.ts +69 -0
- package/dist/component/sync/queries.d.ts.map +1 -0
- package/dist/component/sync/queries.js +32 -0
- package/dist/component/sync/queries.js.map +1 -0
- package/dist/component/validators.d.ts +1112 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +317 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/react/index.d.ts +42 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +43 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +28 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +24 -0
- package/dist/shared.js.map +1 -0
- package/package.json +126 -0
- package/src/client/index.ts +558 -0
- package/src/client/types.ts +117 -0
- package/src/component/_generated/api.ts +141 -0
- package/src/component/_generated/component.ts +1814 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/actions.ts +156 -0
- package/src/component/catalog/browse_order.test.ts +55 -0
- package/src/component/catalog/browse_order.ts +43 -0
- package/src/component/catalog/field_source_policy.test.ts +47 -0
- package/src/component/catalog/field_source_policy.ts +44 -0
- package/src/component/catalog/merge.test.ts +187 -0
- package/src/component/catalog/merge.ts +134 -0
- package/src/component/catalog/mutations.ts +328 -0
- package/src/component/catalog/promote.ts +40 -0
- package/src/component/catalog/queries.ts +220 -0
- package/src/component/config/mutations.ts +29 -0
- package/src/component/config/queries.ts +20 -0
- package/src/component/convex.config.ts +32 -0
- package/src/component/crons.test.ts +8 -0
- package/src/component/crons.ts +52 -0
- package/src/component/imports/actions.ts +553 -0
- package/src/component/imports/dedupe.test.ts +66 -0
- package/src/component/imports/dedupe.ts +54 -0
- package/src/component/imports/mutations.ts +203 -0
- package/src/component/imports/queries.ts +25 -0
- package/src/component/imports/state.test.ts +60 -0
- package/src/component/imports/state.ts +74 -0
- package/src/component/mutations.ts +96 -0
- package/src/component/providers/actions.ts +127 -0
- package/src/component/providers/apple/impl.test.ts +267 -0
- package/src/component/providers/apple/impl.ts +224 -0
- package/src/component/providers/apple/jwt.test.ts +153 -0
- package/src/component/providers/apple/jwt.ts +99 -0
- package/src/component/providers/apple/mappers.test.ts +190 -0
- package/src/component/providers/apple/mappers.ts +100 -0
- package/src/component/providers/apple/types.ts +77 -0
- package/src/component/providers/deezer/impl.test.ts +109 -0
- package/src/component/providers/deezer/impl.ts +179 -0
- package/src/component/providers/deezer/mappers.test.ts +78 -0
- package/src/component/providers/deezer/mappers.ts +72 -0
- package/src/component/providers/deezer/types.ts +59 -0
- package/src/component/providers/fetch.test.ts +282 -0
- package/src/component/providers/fetch.ts +190 -0
- package/src/component/providers/musicbrainz/impl.test.ts +79 -0
- package/src/component/providers/musicbrainz/impl.ts +132 -0
- package/src/component/providers/musicbrainz/mappers.test.ts +75 -0
- package/src/component/providers/musicbrainz/mappers.ts +34 -0
- package/src/component/providers/musicbrainz/types.ts +20 -0
- package/src/component/providers/registry.test.ts +14 -0
- package/src/component/providers/registry.ts +48 -0
- package/src/component/providers/spotify/client.test.ts +80 -0
- package/src/component/providers/spotify/client.ts +57 -0
- package/src/component/providers/spotify/impl.test.ts +311 -0
- package/src/component/providers/spotify/impl.ts +230 -0
- package/src/component/providers/spotify/mappers.test.ts +182 -0
- package/src/component/providers/spotify/mappers.ts +81 -0
- package/src/component/providers/spotify/types.ts +101 -0
- package/src/component/providers/types.ts +95 -0
- package/src/component/providers/wikidata/impl.test.ts +81 -0
- package/src/component/providers/wikidata/impl.ts +139 -0
- package/src/component/providers/wikidata/mappers.test.ts +62 -0
- package/src/component/providers/wikidata/mappers.ts +66 -0
- package/src/component/providers/wikidata/types.ts +28 -0
- package/src/component/queries.ts +51 -0
- package/src/component/schema.ts +81 -0
- package/src/component/sources/actions.ts +177 -0
- package/src/component/sources/mutations.ts +67 -0
- package/src/component/sources/queries.ts +21 -0
- package/src/component/sync/lifecycle.test.ts +72 -0
- package/src/component/sync/lifecycle.ts +121 -0
- package/src/component/sync/mutations.ts +154 -0
- package/src/component/sync/queries.ts +33 -0
- package/src/component/validators.ts +411 -0
- package/src/react/index.test.ts +78 -0
- package/src/react/index.ts +112 -0
- package/src/shared.ts +32 -0
- package/src/test.ts +22 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
mapAlbum,
|
|
4
|
+
mapArtist,
|
|
5
|
+
mapArtistRef,
|
|
6
|
+
mapPlaylist,
|
|
7
|
+
mapTrack,
|
|
8
|
+
} from "./mappers.js";
|
|
9
|
+
import type {
|
|
10
|
+
SpotifyAlbum,
|
|
11
|
+
SpotifyArtist,
|
|
12
|
+
SpotifyPlaylistResponse,
|
|
13
|
+
SpotifyTrack,
|
|
14
|
+
} from "./types.js";
|
|
15
|
+
|
|
16
|
+
describe("mapArtistRef", () => {
|
|
17
|
+
it("carries name + provider id", () => {
|
|
18
|
+
expect(mapArtistRef({ id: "a1", name: "Daft Punk" })).toEqual({
|
|
19
|
+
name: "Daft Punk",
|
|
20
|
+
externalId: "a1",
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("mapArtist", () => {
|
|
26
|
+
it("maps a full artist", () => {
|
|
27
|
+
const raw: SpotifyArtist = {
|
|
28
|
+
id: "a1",
|
|
29
|
+
name: "Daft Punk",
|
|
30
|
+
genres: ["french house"],
|
|
31
|
+
popularity: 82,
|
|
32
|
+
images: [{ url: "https://img/1" }, { url: "https://img/2" }],
|
|
33
|
+
external_urls: { spotify: "https://open.spotify.com/artist/a1" },
|
|
34
|
+
};
|
|
35
|
+
expect(mapArtist(raw)).toEqual({
|
|
36
|
+
name: "Daft Punk",
|
|
37
|
+
genres: ["french house"],
|
|
38
|
+
popularity: 82,
|
|
39
|
+
imageUrl: "https://img/1",
|
|
40
|
+
url: "https://open.spotify.com/artist/a1",
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("defaults genres + omits missing image/url", () => {
|
|
45
|
+
expect(mapArtist({ id: "a2", name: "Unknown" })).toEqual({
|
|
46
|
+
name: "Unknown",
|
|
47
|
+
genres: [],
|
|
48
|
+
popularity: undefined,
|
|
49
|
+
imageUrl: undefined,
|
|
50
|
+
url: undefined,
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("treats an empty image array as no image", () => {
|
|
55
|
+
expect(mapArtist({ id: "a3", name: "NoArt", images: [] }).imageUrl).toBe(
|
|
56
|
+
undefined,
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("mapTrack", () => {
|
|
62
|
+
it("maps a full track with ISRC, preview, cover, url", () => {
|
|
63
|
+
const raw: SpotifyTrack = {
|
|
64
|
+
id: "t1",
|
|
65
|
+
name: "One More Time",
|
|
66
|
+
popularity: 88,
|
|
67
|
+
duration_ms: 320_000,
|
|
68
|
+
preview_url: "https://preview/1",
|
|
69
|
+
external_urls: { spotify: "https://open.spotify.com/track/t1" },
|
|
70
|
+
external_ids: { isrc: "GBDUW0000059" },
|
|
71
|
+
artists: [{ id: "a1", name: "Daft Punk" }],
|
|
72
|
+
album: { id: "al1", name: "Discovery", images: [{ url: "https://cov/1" }] },
|
|
73
|
+
};
|
|
74
|
+
expect(mapTrack(raw)).toEqual({
|
|
75
|
+
title: "One More Time",
|
|
76
|
+
artists: [{ name: "Daft Punk", externalId: "a1" }],
|
|
77
|
+
isrc: "GBDUW0000059",
|
|
78
|
+
genres: [],
|
|
79
|
+
popularity: 88,
|
|
80
|
+
durationMs: 320_000,
|
|
81
|
+
previewUrl: "https://preview/1",
|
|
82
|
+
coverUrl: "https://cov/1",
|
|
83
|
+
url: "https://open.spotify.com/track/t1",
|
|
84
|
+
albumId: "al1",
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("maps a reduced track (no isrc, null preview, no album)", () => {
|
|
89
|
+
const raw: SpotifyTrack = {
|
|
90
|
+
id: "t2",
|
|
91
|
+
name: "Reduced",
|
|
92
|
+
preview_url: null,
|
|
93
|
+
artists: [{ id: "a1", name: "Daft Punk" }],
|
|
94
|
+
};
|
|
95
|
+
expect(mapTrack(raw)).toEqual({
|
|
96
|
+
title: "Reduced",
|
|
97
|
+
artists: [{ name: "Daft Punk", externalId: "a1" }],
|
|
98
|
+
isrc: undefined,
|
|
99
|
+
genres: [],
|
|
100
|
+
popularity: undefined,
|
|
101
|
+
durationMs: undefined,
|
|
102
|
+
previewUrl: undefined,
|
|
103
|
+
coverUrl: undefined,
|
|
104
|
+
url: undefined,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("reads ISRC from external_ids when present but empty", () => {
|
|
109
|
+
const raw: SpotifyTrack = {
|
|
110
|
+
id: "t3",
|
|
111
|
+
name: "NoIsrcField",
|
|
112
|
+
external_ids: {},
|
|
113
|
+
artists: [{ id: "a1", name: "X" }],
|
|
114
|
+
};
|
|
115
|
+
expect(mapTrack(raw).isrc).toBe(undefined);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe("mapAlbum", () => {
|
|
120
|
+
it("maps a full album", () => {
|
|
121
|
+
const raw: SpotifyAlbum = {
|
|
122
|
+
id: "al1",
|
|
123
|
+
name: "Discovery",
|
|
124
|
+
images: [{ url: "https://cov/1" }],
|
|
125
|
+
release_date: "2001-03-12",
|
|
126
|
+
external_urls: { spotify: "https://open.spotify.com/album/al1" },
|
|
127
|
+
total_tracks: 14,
|
|
128
|
+
artists: [{ id: "a1", name: "Daft Punk" }],
|
|
129
|
+
};
|
|
130
|
+
expect(mapAlbum(raw)).toEqual({
|
|
131
|
+
title: "Discovery",
|
|
132
|
+
artists: [{ name: "Daft Punk", externalId: "a1" }],
|
|
133
|
+
releaseDate: "2001-03-12",
|
|
134
|
+
coverUrl: "https://cov/1",
|
|
135
|
+
url: "https://open.spotify.com/album/al1",
|
|
136
|
+
trackCount: 14,
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("defaults artists + omits missing fields", () => {
|
|
141
|
+
expect(mapAlbum({ id: "al2", name: "Bare" })).toEqual({
|
|
142
|
+
title: "Bare",
|
|
143
|
+
artists: [],
|
|
144
|
+
releaseDate: undefined,
|
|
145
|
+
coverUrl: undefined,
|
|
146
|
+
url: undefined,
|
|
147
|
+
trackCount: undefined,
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("mapPlaylist", () => {
|
|
153
|
+
it("maps full playlist metadata", () => {
|
|
154
|
+
const raw: SpotifyPlaylistResponse = {
|
|
155
|
+
id: "p1",
|
|
156
|
+
name: "Top Hits",
|
|
157
|
+
description: "the best",
|
|
158
|
+
images: [{ url: "https://cov/p1" }],
|
|
159
|
+
external_urls: { spotify: "https://open.spotify.com/playlist/p1" },
|
|
160
|
+
owner: { display_name: "Spotify" },
|
|
161
|
+
};
|
|
162
|
+
expect(mapPlaylist(raw)).toEqual({
|
|
163
|
+
title: "Top Hits",
|
|
164
|
+
description: "the best",
|
|
165
|
+
coverUrl: "https://cov/p1",
|
|
166
|
+
url: "https://open.spotify.com/playlist/p1",
|
|
167
|
+
owner: "Spotify",
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("nulls a missing description + owner", () => {
|
|
172
|
+
expect(
|
|
173
|
+
mapPlaylist({ id: "p2", name: "Bare", description: null }),
|
|
174
|
+
).toEqual({
|
|
175
|
+
title: "Bare",
|
|
176
|
+
description: undefined,
|
|
177
|
+
coverUrl: undefined,
|
|
178
|
+
url: undefined,
|
|
179
|
+
owner: undefined,
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map raw Spotify responses to the component's normalized schema. Pure functions;
|
|
3
|
+
* the only Spotify-specific knowledge in the adapter beyond endpoint paths.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
ArtistRef,
|
|
8
|
+
NormalizedAlbum,
|
|
9
|
+
NormalizedArtist,
|
|
10
|
+
NormalizedTrack,
|
|
11
|
+
} from "../../../client/types.js";
|
|
12
|
+
import type { NormalizedPlaylist } from "../types.js";
|
|
13
|
+
import type {
|
|
14
|
+
SpotifyAlbum,
|
|
15
|
+
SpotifyArtist,
|
|
16
|
+
SpotifyArtistRef,
|
|
17
|
+
SpotifyImage,
|
|
18
|
+
SpotifyPlaylistResponse,
|
|
19
|
+
SpotifyTrack,
|
|
20
|
+
} from "./types.js";
|
|
21
|
+
|
|
22
|
+
/** First image url, if any. */
|
|
23
|
+
function firstImage(images: SpotifyImage[] | undefined): string | undefined {
|
|
24
|
+
const first = images?.[0];
|
|
25
|
+
return first?.url;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Reduced track/album artist → credited artist ref. */
|
|
29
|
+
export function mapArtistRef(raw: SpotifyArtistRef): ArtistRef {
|
|
30
|
+
return { name: raw.name, externalId: raw.id };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Full Spotify artist → normalized artist. */
|
|
34
|
+
export function mapArtist(raw: SpotifyArtist): NormalizedArtist {
|
|
35
|
+
return {
|
|
36
|
+
name: raw.name,
|
|
37
|
+
genres: raw.genres ?? [],
|
|
38
|
+
popularity: raw.popularity,
|
|
39
|
+
imageUrl: firstImage(raw.images),
|
|
40
|
+
url: raw.external_urls?.spotify,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Spotify track → normalized track. ISRC lives in `external_ids` (full objects only). */
|
|
45
|
+
export function mapTrack(raw: SpotifyTrack): NormalizedTrack {
|
|
46
|
+
return {
|
|
47
|
+
title: raw.name,
|
|
48
|
+
artists: raw.artists.map(mapArtistRef),
|
|
49
|
+
isrc: raw.external_ids?.isrc,
|
|
50
|
+
genres: [],
|
|
51
|
+
popularity: raw.popularity,
|
|
52
|
+
durationMs: raw.duration_ms,
|
|
53
|
+
previewUrl: raw.preview_url ?? undefined,
|
|
54
|
+
coverUrl: firstImage(raw.album?.images),
|
|
55
|
+
url: raw.external_urls?.spotify,
|
|
56
|
+
albumId: raw.album?.id,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Spotify album → normalized album. */
|
|
61
|
+
export function mapAlbum(raw: SpotifyAlbum): NormalizedAlbum {
|
|
62
|
+
return {
|
|
63
|
+
title: raw.name,
|
|
64
|
+
artists: (raw.artists ?? []).map(mapArtistRef),
|
|
65
|
+
releaseDate: raw.release_date,
|
|
66
|
+
coverUrl: firstImage(raw.images),
|
|
67
|
+
url: raw.external_urls?.spotify,
|
|
68
|
+
trackCount: raw.total_tracks,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Spotify playlist → normalized playlist metadata (membership is separate). */
|
|
73
|
+
export function mapPlaylist(raw: SpotifyPlaylistResponse): NormalizedPlaylist {
|
|
74
|
+
return {
|
|
75
|
+
title: raw.name,
|
|
76
|
+
description: raw.description ?? undefined,
|
|
77
|
+
coverUrl: firstImage(raw.images),
|
|
78
|
+
url: raw.external_urls?.spotify,
|
|
79
|
+
owner: raw.owner?.display_name,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Raw Spotify Web API response shapes — private to the Spotify adapter. Only the
|
|
3
|
+
* fields the mappers consume are modeled; the public contract is the normalized
|
|
4
|
+
* schema, never these.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export interface SpotifyImage {
|
|
8
|
+
url: string;
|
|
9
|
+
height?: number | null;
|
|
10
|
+
width?: number | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SpotifyExternalUrls {
|
|
14
|
+
spotify?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SpotifyArtist {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
genres?: string[];
|
|
21
|
+
popularity?: number;
|
|
22
|
+
images?: SpotifyImage[];
|
|
23
|
+
external_urls?: SpotifyExternalUrls;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The reduced artist object embedded in a track/album. */
|
|
27
|
+
export interface SpotifyArtistRef {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SpotifyAlbum {
|
|
33
|
+
id: string;
|
|
34
|
+
name: string;
|
|
35
|
+
images?: SpotifyImage[];
|
|
36
|
+
release_date?: string;
|
|
37
|
+
external_urls?: SpotifyExternalUrls;
|
|
38
|
+
total_tracks?: number;
|
|
39
|
+
artists?: SpotifyArtistRef[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface SpotifyTrack {
|
|
43
|
+
id: string;
|
|
44
|
+
name: string;
|
|
45
|
+
popularity?: number;
|
|
46
|
+
duration_ms?: number;
|
|
47
|
+
preview_url?: string | null;
|
|
48
|
+
external_urls?: SpotifyExternalUrls;
|
|
49
|
+
external_ids?: { isrc?: string };
|
|
50
|
+
artists: SpotifyArtistRef[];
|
|
51
|
+
album?: SpotifyAlbum;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SpotifyTokenResponse {
|
|
55
|
+
access_token: string;
|
|
56
|
+
token_type: string;
|
|
57
|
+
expires_in: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface SpotifySearchResponse {
|
|
61
|
+
artists?: { items: SpotifyArtist[] };
|
|
62
|
+
tracks?: { items: SpotifyTrack[] };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SpotifySeveralTracksResponse {
|
|
66
|
+
tracks: Array<SpotifyTrack | null>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface SpotifyTopTracksResponse {
|
|
70
|
+
tracks: SpotifyTrack[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface SpotifyPagedAlbums {
|
|
74
|
+
items: SpotifyAlbum[];
|
|
75
|
+
total: number;
|
|
76
|
+
next?: string | null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface SpotifyPagedTracks {
|
|
80
|
+
items: SpotifyTrack[];
|
|
81
|
+
total: number;
|
|
82
|
+
next?: string | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface SpotifyAlbumDetail extends SpotifyAlbum {
|
|
86
|
+
tracks?: SpotifyPagedTracks;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface SpotifyPlaylistItem {
|
|
90
|
+
track: SpotifyTrack | null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface SpotifyPlaylistResponse {
|
|
94
|
+
id: string;
|
|
95
|
+
name: string;
|
|
96
|
+
description?: string | null;
|
|
97
|
+
images?: SpotifyImage[];
|
|
98
|
+
external_urls?: SpotifyExternalUrls;
|
|
99
|
+
owner?: { display_name?: string };
|
|
100
|
+
tracks?: { items: SpotifyPlaylistItem[] };
|
|
101
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider adapter contract. Every provider (Spotify, Apple, …) implements
|
|
3
|
+
* {@link MusicProvider}, mapping its raw API responses to the component's single
|
|
4
|
+
* normalized schema (`NormalizedTrack` / `NormalizedArtist` / `NormalizedAlbum`
|
|
5
|
+
* from the client types — the one public contract). Each returned entity carries
|
|
6
|
+
* its provider `externalId` so the cache + catalog can key and unify it. Adding a
|
|
7
|
+
* provider is a new folder + a registry entry — no edits to this contract.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
NormalizedAlbum,
|
|
12
|
+
NormalizedArtist,
|
|
13
|
+
NormalizedTrack,
|
|
14
|
+
} from "../../client/types.js";
|
|
15
|
+
import type { Provider } from "../../shared.js";
|
|
16
|
+
|
|
17
|
+
/** A provider's artist, tagged with its provider id. */
|
|
18
|
+
export interface ProviderArtist {
|
|
19
|
+
externalId: string;
|
|
20
|
+
value: NormalizedArtist;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A provider's track, tagged with its provider id. */
|
|
24
|
+
export interface ProviderTrack {
|
|
25
|
+
externalId: string;
|
|
26
|
+
value: NormalizedTrack;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A provider's album plus its (normalized) track listing. */
|
|
30
|
+
export interface ProviderAlbum {
|
|
31
|
+
externalId: string;
|
|
32
|
+
value: NormalizedAlbum;
|
|
33
|
+
tracks: ProviderTrack[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Normalized playlist metadata (membership is carried separately). */
|
|
37
|
+
export interface NormalizedPlaylist {
|
|
38
|
+
title: string;
|
|
39
|
+
description?: string;
|
|
40
|
+
coverUrl?: string;
|
|
41
|
+
url?: string;
|
|
42
|
+
owner?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A provider's playlist plus its ordered (normalized) track membership. */
|
|
46
|
+
export interface ProviderPlaylist {
|
|
47
|
+
externalId: string;
|
|
48
|
+
value: NormalizedPlaylist;
|
|
49
|
+
tracks: ProviderTrack[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A single search hit, discriminated by entity kind. */
|
|
53
|
+
export type ProviderSearchResult =
|
|
54
|
+
| { type: "artist"; data: ProviderArtist }
|
|
55
|
+
| { type: "track"; data: ProviderTrack };
|
|
56
|
+
|
|
57
|
+
/** An artist's albums, flagged when the listing was truncated. */
|
|
58
|
+
export interface ArtistAlbumsResult {
|
|
59
|
+
albums: ProviderAlbum[];
|
|
60
|
+
isPartial: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* One music provider behind a uniform interface. Methods take/return the
|
|
65
|
+
* provider's own external ids; values are already normalized.
|
|
66
|
+
*/
|
|
67
|
+
export interface MusicProvider {
|
|
68
|
+
/** The provider id this adapter serves. */
|
|
69
|
+
readonly id: Provider;
|
|
70
|
+
/** Fetch one artist by provider id. */
|
|
71
|
+
getArtist(externalId: string): Promise<ProviderArtist>;
|
|
72
|
+
/** Fetch one track by provider id. */
|
|
73
|
+
getTrack(externalId: string): Promise<ProviderTrack>;
|
|
74
|
+
/** Fetch one album (with its tracks) by provider id. */
|
|
75
|
+
getAlbum(externalId: string): Promise<ProviderAlbum>;
|
|
76
|
+
/** Fetch one playlist (with its track membership) by provider id. */
|
|
77
|
+
getPlaylist(externalId: string): Promise<ProviderPlaylist>;
|
|
78
|
+
/** An artist's top tracks. */
|
|
79
|
+
getArtistTopTracks(externalId: string): Promise<ProviderTrack[]>;
|
|
80
|
+
/** An artist's albums (bounded; `isPartial` when truncated). */
|
|
81
|
+
getArtistAlbums(externalId: string): Promise<ArtistAlbumsResult>;
|
|
82
|
+
/** Free-text search for artists or tracks. */
|
|
83
|
+
search(
|
|
84
|
+
query: string,
|
|
85
|
+
type: "artist" | "track",
|
|
86
|
+
): Promise<ProviderSearchResult[]>;
|
|
87
|
+
/** Resolve tracks by ISRC (cross-provider identity). */
|
|
88
|
+
searchByIsrc(isrc: string): Promise<ProviderTrack[]>;
|
|
89
|
+
/**
|
|
90
|
+
* Batch-fetch full track objects by provider id — used to enrich tracks whose
|
|
91
|
+
* ISRC was omitted by a playlist/album listing. Optional: only providers whose
|
|
92
|
+
* listings drop ISRC (Spotify) implement it.
|
|
93
|
+
*/
|
|
94
|
+
getSeveralTracks?(externalIds: string[]): Promise<ProviderTrack[]>;
|
|
95
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type { FetchDeps } from "../fetch.js";
|
|
3
|
+
import { WikidataProvider } from "./impl.js";
|
|
4
|
+
|
|
5
|
+
interface Route {
|
|
6
|
+
match: RegExp;
|
|
7
|
+
body: unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function makeProvider(routes: Route[]): WikidataProvider {
|
|
11
|
+
const deps: FetchDeps = {
|
|
12
|
+
fetch: (url) => {
|
|
13
|
+
const route = routes.find((r) => r.match.test(url));
|
|
14
|
+
return Promise.resolve(
|
|
15
|
+
new Response(JSON.stringify(route?.body ?? {}), {
|
|
16
|
+
status: route === undefined ? 404 : 200,
|
|
17
|
+
headers: { "content-type": "application/json" },
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
20
|
+
},
|
|
21
|
+
sleep: () => Promise.resolve(),
|
|
22
|
+
random: () => 0,
|
|
23
|
+
};
|
|
24
|
+
return new WikidataProvider(undefined, deps);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("WikidataProvider", () => {
|
|
28
|
+
it("getArtist resolves facts from claims", async () => {
|
|
29
|
+
const p = makeProvider([
|
|
30
|
+
{
|
|
31
|
+
match: /wbgetentities/,
|
|
32
|
+
body: {
|
|
33
|
+
entities: {
|
|
34
|
+
Q1: {
|
|
35
|
+
labels: { en: { value: "Daft Punk" } },
|
|
36
|
+
claims: { P31: [{ mainsnak: { datavalue: { value: { id: "Q215380" } } } }] },
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
]);
|
|
42
|
+
const artist = await p.getArtist("Q1");
|
|
43
|
+
expect(artist.value.name).toBe("Daft Punk");
|
|
44
|
+
expect(artist.value.members).toBe("group");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("getArtist throws for a missing entity", async () => {
|
|
48
|
+
const p = makeProvider([{ match: /wbgetentities/, body: { entities: {} } }]);
|
|
49
|
+
await expect(p.getArtist("Q404")).rejects.toThrow(/not found/);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("search(artist) maps hits (label or blank) + tolerates an empty body", async () => {
|
|
53
|
+
const p = makeProvider([
|
|
54
|
+
{
|
|
55
|
+
match: /wbsearchentities/,
|
|
56
|
+
body: { search: [{ id: "Q1", label: "Daft Punk" }, { id: "Q2" }] },
|
|
57
|
+
},
|
|
58
|
+
]);
|
|
59
|
+
const results = await p.search("daft", "artist");
|
|
60
|
+
expect(results[0]).toMatchObject({ type: "artist", data: { externalId: "Q1" } });
|
|
61
|
+
expect(results[1]).toMatchObject({ data: { value: { name: "" } } });
|
|
62
|
+
|
|
63
|
+
const empty = makeProvider([{ match: /wbsearchentities/, body: {} }]);
|
|
64
|
+
expect(await empty.search("none", "artist")).toEqual([]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("search(track) + searchByIsrc return empty", async () => {
|
|
68
|
+
const p = makeProvider([]);
|
|
69
|
+
expect(await p.search("x", "track")).toEqual([]);
|
|
70
|
+
expect(await p.searchByIsrc()).toEqual([]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("rejects the track/album/playlist methods it does not serve", async () => {
|
|
74
|
+
const p = makeProvider([]);
|
|
75
|
+
await expect(p.getTrack()).rejects.toThrow(/not supported/);
|
|
76
|
+
await expect(p.getAlbum()).rejects.toThrow(/not supported/);
|
|
77
|
+
await expect(p.getPlaylist()).rejects.toThrow(/not supported/);
|
|
78
|
+
await expect(p.getArtistTopTracks()).rejects.toThrow(/not supported/);
|
|
79
|
+
await expect(p.getArtistAlbums()).rejects.toThrow(/not supported/);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wikidata adapter — a no-auth, facts-only provider (an alternative facts source
|
|
3
|
+
* to MusicBrainz; the field-source policy chooses between them per field). Serves
|
|
4
|
+
* artist facts via `wbgetentities` + artist discovery via `wbsearchentities`; it
|
|
5
|
+
* does not serve tracks/albums/playlists (those methods reject). Requests carry a
|
|
6
|
+
* `User-Agent` per Wikimedia policy.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
DEFAULT_RETRY_CONFIG,
|
|
11
|
+
type FetchDeps,
|
|
12
|
+
type RetryConfig,
|
|
13
|
+
defaultFetchDeps,
|
|
14
|
+
fetchJson,
|
|
15
|
+
} from "../fetch.js";
|
|
16
|
+
import type {
|
|
17
|
+
ArtistAlbumsResult,
|
|
18
|
+
MusicProvider,
|
|
19
|
+
ProviderAlbum,
|
|
20
|
+
ProviderArtist,
|
|
21
|
+
ProviderPlaylist,
|
|
22
|
+
ProviderSearchResult,
|
|
23
|
+
ProviderTrack,
|
|
24
|
+
} from "../types.js";
|
|
25
|
+
import { mapWikidataArtist } from "./mappers.js";
|
|
26
|
+
import type {
|
|
27
|
+
WikidataEntitiesResponse,
|
|
28
|
+
WikidataSearchResponse,
|
|
29
|
+
} from "./types.js";
|
|
30
|
+
|
|
31
|
+
/** Tunable Wikidata settings. */
|
|
32
|
+
export interface WikidataConfig {
|
|
33
|
+
apiUrl: string;
|
|
34
|
+
userAgent: string;
|
|
35
|
+
retry: RetryConfig;
|
|
36
|
+
searchLimit: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Default Wikidata settings. */
|
|
40
|
+
export const DEFAULT_WIKIDATA_CONFIG: WikidataConfig = {
|
|
41
|
+
apiUrl: "https://www.wikidata.org/w/api.php",
|
|
42
|
+
userAgent: "vllnt-convex-music/0.1 (https://github.com/vllnt/convex-music)",
|
|
43
|
+
retry: DEFAULT_RETRY_CONFIG,
|
|
44
|
+
searchLimit: 10,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** A method Wikidata (facts-only) does not serve. */
|
|
48
|
+
function unsupported<T>(method: string): Promise<T> {
|
|
49
|
+
return Promise.reject(
|
|
50
|
+
new Error(`wikidata: ${method} not supported (facts-only provider)`),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class WikidataProvider implements MusicProvider {
|
|
55
|
+
readonly id = "wikidata" as const;
|
|
56
|
+
|
|
57
|
+
constructor(
|
|
58
|
+
private readonly cfg: WikidataConfig = DEFAULT_WIKIDATA_CONFIG,
|
|
59
|
+
private readonly deps: FetchDeps = defaultFetchDeps,
|
|
60
|
+
) {}
|
|
61
|
+
|
|
62
|
+
private async api<T>(params: Record<string, string>): Promise<T> {
|
|
63
|
+
const url = new URL(this.cfg.apiUrl);
|
|
64
|
+
url.searchParams.set("format", "json");
|
|
65
|
+
for (const [key, value] of Object.entries(params)) {
|
|
66
|
+
url.searchParams.set(key, value);
|
|
67
|
+
}
|
|
68
|
+
return fetchJson<T>(
|
|
69
|
+
"wikidata",
|
|
70
|
+
url.toString(),
|
|
71
|
+
{ headers: { "User-Agent": this.cfg.userAgent } },
|
|
72
|
+
this.cfg.retry,
|
|
73
|
+
this.deps,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async getArtist(externalId: string): Promise<ProviderArtist> {
|
|
78
|
+
const res = await this.api<WikidataEntitiesResponse>({
|
|
79
|
+
action: "wbgetentities",
|
|
80
|
+
ids: externalId,
|
|
81
|
+
props: "labels|claims",
|
|
82
|
+
languages: "en",
|
|
83
|
+
});
|
|
84
|
+
const entity = res.entities?.[externalId];
|
|
85
|
+
if (entity === undefined) {
|
|
86
|
+
throw new Error(`wikidata: entity "${externalId}" not found`);
|
|
87
|
+
}
|
|
88
|
+
return { externalId, value: mapWikidataArtist(entity) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async search(
|
|
92
|
+
query: string,
|
|
93
|
+
type: "artist" | "track",
|
|
94
|
+
): Promise<ProviderSearchResult[]> {
|
|
95
|
+
if (type === "artist") {
|
|
96
|
+
const res = await this.api<WikidataSearchResponse>({
|
|
97
|
+
action: "wbsearchentities",
|
|
98
|
+
search: query,
|
|
99
|
+
language: "en",
|
|
100
|
+
type: "item",
|
|
101
|
+
limit: String(this.cfg.searchLimit),
|
|
102
|
+
});
|
|
103
|
+
return (res.search ?? []).map(
|
|
104
|
+
(hit): ProviderSearchResult => ({
|
|
105
|
+
type: "artist",
|
|
106
|
+
data: {
|
|
107
|
+
externalId: hit.id,
|
|
108
|
+
value: { name: hit.label ?? "", genres: [] },
|
|
109
|
+
},
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
searchByIsrc(): Promise<ProviderTrack[]> {
|
|
117
|
+
return Promise.resolve([]);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getTrack(): Promise<ProviderTrack> {
|
|
121
|
+
return unsupported("getTrack");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
getAlbum(): Promise<ProviderAlbum> {
|
|
125
|
+
return unsupported("getAlbum");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
getPlaylist(): Promise<ProviderPlaylist> {
|
|
129
|
+
return unsupported("getPlaylist");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
getArtistTopTracks(): Promise<ProviderTrack[]> {
|
|
133
|
+
return unsupported("getArtistTopTracks");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
getArtistAlbums(): Promise<ArtistAlbumsResult> {
|
|
137
|
+
return unsupported("getArtistAlbums");
|
|
138
|
+
}
|
|
139
|
+
}
|