odesli.js 1.1.1 → 1.3.1
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/.eslintrc.js +0 -1
- package/.github/workflows/ci.yml +3 -11
- package/.github/workflows/publish-github-packages.yml +46 -0
- package/.github/workflows/{publish.yml → publish-npm.yml} +11 -2
- package/.prettierignore +30 -30
- package/API_IMPROVEMENTS.md +318 -0
- package/CHANGELOG.md +96 -33
- package/LICENSE +14 -14
- package/README.md +212 -144
- package/examples/README.md +197 -26
- package/examples/advanced-features-example.js +205 -0
- package/examples/advanced-usage.js +217 -36
- package/examples/basic-usage.js +92 -17
- package/examples/cache-test.js +119 -0
- package/examples/test-apple-music-extraction.js +60 -0
- package/examples/user-agent-example.js +46 -0
- package/jest.config.js +3 -1
- package/lib/index.d.ts +663 -484
- package/lib/index.js +1274 -390
- package/lib/index.mjs +3 -0
- package/lib/metrics.js +390 -0
- package/lib/metrics.mjs +3 -0
- package/lib/plugin-system.js +382 -0
- package/lib/plugin-system.mjs +3 -0
- package/lib/rate-limiter.js +148 -0
- package/lib/rate-limiter.mjs +3 -0
- package/package.json +2 -1
- package/tests/advanced-features.test.js +423 -0
- package/tests/odesli.test.js +246 -92
- package/tests/setup.js +2 -0
package/tests/odesli.test.js
CHANGED
|
@@ -73,7 +73,7 @@ describe('Odesli', () => {
|
|
|
73
73
|
expect.objectContaining({
|
|
74
74
|
signal: expect.any(AbortSignal),
|
|
75
75
|
headers: expect.objectContaining({
|
|
76
|
-
'User-Agent':
|
|
76
|
+
'User-Agent': expect.any(String),
|
|
77
77
|
Accept: 'application/json',
|
|
78
78
|
}),
|
|
79
79
|
})
|
|
@@ -117,16 +117,14 @@ describe('Odesli', () => {
|
|
|
117
117
|
});
|
|
118
118
|
|
|
119
119
|
test('should handle unexpected API response', async () => {
|
|
120
|
-
fetch.
|
|
121
|
-
new Error('Unexpected token < in JSON at position 0')
|
|
122
|
-
);
|
|
120
|
+
fetch.mockResponseOnce('invalid json');
|
|
123
121
|
await expect(odesli._request('test-path')).rejects.toThrow(
|
|
124
|
-
'
|
|
122
|
+
'invalid json response body'
|
|
125
123
|
);
|
|
126
124
|
});
|
|
127
125
|
|
|
128
126
|
test('should handle network errors', async () => {
|
|
129
|
-
fetch.
|
|
127
|
+
fetch.mockReject(new Error('Network error'));
|
|
130
128
|
await expect(odesli._request('test-path')).rejects.toThrow(
|
|
131
129
|
'Network error'
|
|
132
130
|
);
|
|
@@ -175,8 +173,20 @@ describe('Odesli', () => {
|
|
|
175
173
|
expect(result.thumbnail).toBe('https://example.com/thumb.jpg');
|
|
176
174
|
});
|
|
177
175
|
|
|
178
|
-
test('should fetch
|
|
179
|
-
const
|
|
176
|
+
test('should fetch multiple songs by array of URLs', async () => {
|
|
177
|
+
const mockResponse1 = {
|
|
178
|
+
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
179
|
+
entitiesByUniqueId: {
|
|
180
|
+
'SPOTIFY_SONG::123': {
|
|
181
|
+
id: '123',
|
|
182
|
+
title: 'Test Song 1',
|
|
183
|
+
artistName: 'Test Artist 1',
|
|
184
|
+
type: 'song',
|
|
185
|
+
thumbnailUrl: 'https://example.com/thumb1.jpg',
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
const mockResponse2 = {
|
|
180
190
|
entityUniqueId: 'SPOTIFY_SONG::456',
|
|
181
191
|
entitiesByUniqueId: {
|
|
182
192
|
'SPOTIFY_SONG::456': {
|
|
@@ -188,95 +198,120 @@ describe('Odesli', () => {
|
|
|
188
198
|
},
|
|
189
199
|
},
|
|
190
200
|
};
|
|
191
|
-
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
192
|
-
await odesli.fetch('https://open.spotify.com/track/456', 'GB');
|
|
193
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
194
|
-
'https://api.song.link/v1-alpha.1/links?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F456&userCountry=GB',
|
|
195
|
-
expect.any(Object)
|
|
196
|
-
);
|
|
197
|
-
});
|
|
198
201
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
'No URL was provided to odesli.fetch()'
|
|
202
|
-
);
|
|
203
|
-
});
|
|
202
|
+
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse1));
|
|
203
|
+
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse2));
|
|
204
204
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
'
|
|
208
|
-
|
|
205
|
+
const urls = [
|
|
206
|
+
'https://open.spotify.com/track/123',
|
|
207
|
+
'https://open.spotify.com/track/456',
|
|
208
|
+
];
|
|
209
|
+
|
|
210
|
+
const results = await odesli.fetch(urls);
|
|
211
|
+
|
|
212
|
+
expect(Array.isArray(results)).toBe(true);
|
|
213
|
+
expect(results).toHaveLength(2);
|
|
214
|
+
expect(results[0].title).toBe('Test Song 1');
|
|
215
|
+
expect(results[1].title).toBe('Test Song 2');
|
|
216
|
+
expect(fetch).toHaveBeenCalledTimes(2);
|
|
209
217
|
});
|
|
210
218
|
|
|
211
|
-
test('should
|
|
212
|
-
|
|
213
|
-
'
|
|
219
|
+
test('should handle errors in batch fetch', async () => {
|
|
220
|
+
const urls = [
|
|
221
|
+
'https://open.spotify.com/track/123',
|
|
222
|
+
'https://open.spotify.com/track/invalid',
|
|
223
|
+
];
|
|
224
|
+
fetch.mockResponseOnce(
|
|
225
|
+
JSON.stringify({
|
|
226
|
+
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
227
|
+
entitiesByUniqueId: {
|
|
228
|
+
'SPOTIFY_SONG::123': { title: 'Test Song' },
|
|
229
|
+
},
|
|
230
|
+
})
|
|
214
231
|
);
|
|
232
|
+
fetch.mockRejectOnce(new Error('Network error'));
|
|
233
|
+
|
|
234
|
+
const results = await odesli.fetch(urls);
|
|
235
|
+
|
|
236
|
+
expect(Array.isArray(results)).toBe(true);
|
|
237
|
+
expect(results).toHaveLength(2);
|
|
238
|
+
expect(results[0].title).toBe('Test Song');
|
|
239
|
+
expect(results[1].error).toBe('Network error');
|
|
215
240
|
});
|
|
216
241
|
|
|
217
|
-
test('should
|
|
242
|
+
test('should fetch song with custom country', async () => {
|
|
218
243
|
const mockResponse = {
|
|
219
|
-
entityUniqueId: 'SPOTIFY_SONG::
|
|
244
|
+
entityUniqueId: 'SPOTIFY_SONG::456',
|
|
220
245
|
entitiesByUniqueId: {
|
|
221
|
-
'SPOTIFY_SONG::
|
|
222
|
-
id: '
|
|
223
|
-
title: '
|
|
224
|
-
artistName: '
|
|
246
|
+
'SPOTIFY_SONG::456': {
|
|
247
|
+
id: '456',
|
|
248
|
+
title: 'Test Song 2',
|
|
249
|
+
artistName: 'Test Artist 2',
|
|
225
250
|
type: 'song',
|
|
226
|
-
thumbnailUrl: 'https://example.com/
|
|
251
|
+
thumbnailUrl: 'https://example.com/thumb2.jpg',
|
|
227
252
|
},
|
|
228
253
|
},
|
|
229
254
|
};
|
|
230
255
|
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
231
|
-
|
|
232
|
-
expect(
|
|
256
|
+
await odesli.fetch('https://open.spotify.com/track/456', 'GB');
|
|
257
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
258
|
+
'https://api.song.link/v1-alpha.1/links?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F456&userCountry=GB',
|
|
259
|
+
expect.any(Object)
|
|
260
|
+
);
|
|
233
261
|
});
|
|
234
262
|
|
|
235
|
-
test('should
|
|
263
|
+
test('should fetch batch with options', async () => {
|
|
236
264
|
const mockResponse = {
|
|
237
|
-
entityUniqueId: 'SPOTIFY_SONG::
|
|
265
|
+
entityUniqueId: 'SPOTIFY_SONG::789',
|
|
238
266
|
entitiesByUniqueId: {
|
|
239
|
-
'SPOTIFY_SONG::
|
|
240
|
-
id: '
|
|
241
|
-
title: '
|
|
267
|
+
'SPOTIFY_SONG::789': {
|
|
268
|
+
id: '789',
|
|
269
|
+
title: 'Test Song 3',
|
|
270
|
+
artistName: 'Test Artist 3',
|
|
242
271
|
type: 'song',
|
|
243
|
-
thumbnailUrl: 'https://example.com/
|
|
272
|
+
thumbnailUrl: 'https://example.com/thumb3.jpg',
|
|
244
273
|
},
|
|
245
274
|
},
|
|
246
275
|
};
|
|
247
276
|
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
248
|
-
|
|
249
|
-
|
|
277
|
+
|
|
278
|
+
const urls = ['https://open.spotify.com/track/789'];
|
|
279
|
+
const results = await odesli.fetch(urls, {
|
|
280
|
+
country: 'CA',
|
|
281
|
+
concurrency: 1,
|
|
282
|
+
skipCache: true,
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
expect(Array.isArray(results)).toBe(true);
|
|
286
|
+
expect(results[0].title).toBe('Test Song 3');
|
|
287
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
288
|
+
'https://api.song.link/v1-alpha.1/links?url=https%3A%2F%2Fopen.spotify.com%2Ftrack%2F789&userCountry=CA',
|
|
289
|
+
expect.any(Object)
|
|
290
|
+
);
|
|
250
291
|
});
|
|
251
292
|
|
|
252
|
-
test('should
|
|
253
|
-
|
|
254
|
-
'
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
'https://tidal.com/track/204',
|
|
258
|
-
];
|
|
293
|
+
test('should throw error when no URL provided', async () => {
|
|
294
|
+
await expect(odesli.fetch()).rejects.toThrow(
|
|
295
|
+
'No URL was provided to odesli.fetch()'
|
|
296
|
+
);
|
|
297
|
+
});
|
|
259
298
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
`https://api.song.link/v1-alpha.1/links?url=${encodeURIComponent(urls[i])}&userCountry=US`,
|
|
277
|
-
expect.any(Object)
|
|
278
|
-
);
|
|
279
|
-
}
|
|
299
|
+
test('should throw error when URL is null', async () => {
|
|
300
|
+
await expect(odesli.fetch(null)).rejects.toThrow(
|
|
301
|
+
'No URL was provided to odesli.fetch()'
|
|
302
|
+
);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test('should return empty array when URLs array is empty', async () => {
|
|
306
|
+
const results = await odesli.fetch([]);
|
|
307
|
+
expect(Array.isArray(results)).toBe(true);
|
|
308
|
+
expect(results).toHaveLength(0);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test('should throw error when URLs is not an array', async () => {
|
|
312
|
+
await expect(odesli.fetch('not-an-array')).rejects.toThrow(
|
|
313
|
+
'Invalid URL format provided to odesli.fetch()'
|
|
314
|
+
);
|
|
280
315
|
});
|
|
281
316
|
});
|
|
282
317
|
|
|
@@ -344,28 +379,24 @@ describe('Odesli', () => {
|
|
|
344
379
|
});
|
|
345
380
|
|
|
346
381
|
test('should handle different platforms', async () => {
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
artistName: 'Platform Artist',
|
|
357
|
-
type: 'song',
|
|
358
|
-
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
359
|
-
},
|
|
382
|
+
const mockResponse = {
|
|
383
|
+
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
384
|
+
entitiesByUniqueId: {
|
|
385
|
+
'SPOTIFY_SONG::123': {
|
|
386
|
+
id: '123',
|
|
387
|
+
title: 'Test Song',
|
|
388
|
+
artistName: 'Test Artist',
|
|
389
|
+
type: 'song',
|
|
390
|
+
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
360
391
|
},
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
)
|
|
368
|
-
|
|
392
|
+
},
|
|
393
|
+
};
|
|
394
|
+
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
395
|
+
await odesli.getByParams('spotify', 'song', '123');
|
|
396
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
397
|
+
'https://api.song.link/v1-alpha.1/links?platform=spotify&type=song&id=123&userCountry=US',
|
|
398
|
+
expect.any(Object)
|
|
399
|
+
);
|
|
369
400
|
});
|
|
370
401
|
|
|
371
402
|
test('should handle different types', async () => {
|
|
@@ -585,4 +616,127 @@ describe('Odesli', () => {
|
|
|
585
616
|
expect(result.title).toBeUndefined();
|
|
586
617
|
});
|
|
587
618
|
});
|
|
619
|
+
|
|
620
|
+
describe('getCountryOptions method', () => {
|
|
621
|
+
test('should return array of country options', () => {
|
|
622
|
+
const countryOptions = Odesli.getCountryOptions();
|
|
623
|
+
expect(Array.isArray(countryOptions)).toBe(true);
|
|
624
|
+
expect(countryOptions.length).toBeGreaterThan(0);
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
test('should return objects with code and name properties', () => {
|
|
628
|
+
const countryOptions = Odesli.getCountryOptions();
|
|
629
|
+
const firstCountry = countryOptions[0];
|
|
630
|
+
expect(firstCountry).toHaveProperty('code');
|
|
631
|
+
expect(firstCountry).toHaveProperty('name');
|
|
632
|
+
expect(typeof firstCountry.code).toBe('string');
|
|
633
|
+
expect(typeof firstCountry.name).toBe('string');
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
test('should include popular countries', () => {
|
|
637
|
+
const countryOptions = Odesli.getCountryOptions();
|
|
638
|
+
const codes = countryOptions.map(c => c.code);
|
|
639
|
+
expect(codes).toContain('US');
|
|
640
|
+
expect(codes).toContain('GB');
|
|
641
|
+
expect(codes).toContain('CA');
|
|
642
|
+
expect(codes).toContain('AU');
|
|
643
|
+
expect(codes).toContain('DE');
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
test('should have valid ISO 3166-1 alpha-2 codes', () => {
|
|
647
|
+
const countryOptions = Odesli.getCountryOptions();
|
|
648
|
+
countryOptions.forEach(country => {
|
|
649
|
+
expect(country.code).toMatch(/^[A-Z]{2}$/);
|
|
650
|
+
});
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
test('should have unique country codes', () => {
|
|
654
|
+
const countryOptions = Odesli.getCountryOptions();
|
|
655
|
+
const codes = countryOptions.map(c => c.code);
|
|
656
|
+
const uniqueCodes = [...new Set(codes)];
|
|
657
|
+
expect(codes).toHaveLength(uniqueCodes.length);
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
test('should have non-empty country names', () => {
|
|
661
|
+
const countryOptions = Odesli.getCountryOptions();
|
|
662
|
+
countryOptions.forEach(country => {
|
|
663
|
+
expect(country.name.trim().length).toBeGreaterThan(0);
|
|
664
|
+
});
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
describe('Country code validation', () => {
|
|
669
|
+
test('should accept valid country codes', async () => {
|
|
670
|
+
const mockResponse = {
|
|
671
|
+
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
672
|
+
entitiesByUniqueId: {
|
|
673
|
+
'SPOTIFY_SONG::123': {
|
|
674
|
+
id: '123',
|
|
675
|
+
title: 'Test Song',
|
|
676
|
+
artistName: 'Test Artist',
|
|
677
|
+
type: 'song',
|
|
678
|
+
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
679
|
+
},
|
|
680
|
+
},
|
|
681
|
+
};
|
|
682
|
+
fetch.mockResolvedValueOnce(mockFetchResponse(mockResponse));
|
|
683
|
+
|
|
684
|
+
const result = await odesli.fetch('https://open.spotify.com/track/123', {
|
|
685
|
+
country: 'US',
|
|
686
|
+
});
|
|
687
|
+
expect(result.title).toBe('Test Song');
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
test('should accept multiple valid country codes', async () => {
|
|
691
|
+
const mockResponse = {
|
|
692
|
+
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
693
|
+
entitiesByUniqueId: {
|
|
694
|
+
'SPOTIFY_SONG::123': {
|
|
695
|
+
id: '123',
|
|
696
|
+
title: 'Test Song',
|
|
697
|
+
artistName: 'Test Artist',
|
|
698
|
+
type: 'song',
|
|
699
|
+
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
700
|
+
},
|
|
701
|
+
},
|
|
702
|
+
};
|
|
703
|
+
fetch.mockResolvedValue(mockFetchResponse(mockResponse));
|
|
704
|
+
|
|
705
|
+
const validCountries = ['US', 'GB', 'CA', 'AU', 'DE', 'FR', 'JP'];
|
|
706
|
+
|
|
707
|
+
for (const country of validCountries) {
|
|
708
|
+
await odesli.fetch('https://open.spotify.com/track/123', {
|
|
709
|
+
country,
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// Should have made requests for each country
|
|
714
|
+
expect(fetch).toHaveBeenCalledTimes(validCountries.length);
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
test('should handle batch requests with country codes', async () => {
|
|
718
|
+
const mockResponse = {
|
|
719
|
+
entityUniqueId: 'SPOTIFY_SONG::123',
|
|
720
|
+
entitiesByUniqueId: {
|
|
721
|
+
'SPOTIFY_SONG::123': {
|
|
722
|
+
id: '123',
|
|
723
|
+
title: 'Test Song',
|
|
724
|
+
artistName: 'Test Artist',
|
|
725
|
+
type: 'song',
|
|
726
|
+
thumbnailUrl: 'https://example.com/thumb.jpg',
|
|
727
|
+
},
|
|
728
|
+
},
|
|
729
|
+
};
|
|
730
|
+
fetch.mockResolvedValue(mockFetchResponse(mockResponse));
|
|
731
|
+
|
|
732
|
+
const urls = [
|
|
733
|
+
'https://open.spotify.com/track/123',
|
|
734
|
+
'https://music.apple.com/us/album/test/456?i=789',
|
|
735
|
+
];
|
|
736
|
+
|
|
737
|
+
const results = await odesli.fetch(urls, { country: 'GB' });
|
|
738
|
+
expect(results).toHaveLength(2);
|
|
739
|
+
expect(fetch).toHaveBeenCalledTimes(2);
|
|
740
|
+
});
|
|
741
|
+
});
|
|
588
742
|
});
|