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.
@@ -0,0 +1,119 @@
1
+ // Cache Test Example
2
+ // This example demonstrates how caching works by making the same request multiple times
3
+ const Odesli = require('../lib/index.js');
4
+
5
+ async function cacheTest() {
6
+ console.log('๐Ÿงช Testing Cache Functionality\n');
7
+
8
+ // Initialize with caching enabled
9
+ const odesli = new Odesli({
10
+ cache: true,
11
+ timeout: 10000,
12
+ });
13
+
14
+ const testUrl = 'https://open.spotify.com/track/4iV5W9uYEdYUVa79Axb7Rh';
15
+
16
+ try {
17
+ // First request - should be a cache miss
18
+ console.log('1. First request (should be cache miss)...');
19
+ const start1 = Date.now();
20
+ const song1 = await odesli.fetch(testUrl);
21
+ const time1 = Date.now() - start1;
22
+ console.log(`โœ… Got: ${song1.title} (took ${time1}ms)`);
23
+
24
+ // Check cache stats after first request
25
+ const stats1 = odesli.getCacheStats();
26
+ console.log(
27
+ ` Cache hits: ${stats1.hitCount}, misses: ${stats1.missCount}, hit rate: ${(stats1.hitRate * 100).toFixed(1)}%\n`
28
+ );
29
+
30
+ // Second request - should be a cache hit
31
+ console.log('2. Second request (should be cache hit)...');
32
+ const start2 = Date.now();
33
+ const song2 = await odesli.fetch(testUrl);
34
+ const time2 = Date.now() - start2;
35
+ console.log(`โœ… Got: ${song2.title} (took ${time2}ms)`);
36
+
37
+ // Check cache stats after second request
38
+ const stats2 = odesli.getCacheStats();
39
+ console.log(
40
+ ` Cache hits: ${stats2.hitCount}, misses: ${stats2.missCount}, hit rate: ${(stats2.hitRate * 100).toFixed(1)}%\n`
41
+ );
42
+
43
+ // Third request - should also be a cache hit
44
+ console.log('3. Third request (should be cache hit)...');
45
+ const start3 = Date.now();
46
+ const song3 = await odesli.fetch(testUrl);
47
+ const time3 = Date.now() - start3;
48
+ console.log(`โœ… Got: ${song3.title} (took ${time3}ms)`);
49
+
50
+ // Check final cache stats
51
+ const stats3 = odesli.getCacheStats();
52
+ console.log(
53
+ ` Cache hits: ${stats3.hitCount}, misses: ${stats3.missCount}, hit rate: ${(stats3.hitRate * 100).toFixed(1)}%\n`
54
+ );
55
+
56
+ // Test with skipCache option
57
+ console.log('4. Request with skipCache=true (should be cache miss)...');
58
+ const start4 = Date.now();
59
+ const song4 = await odesli.fetch(testUrl, { skipCache: true });
60
+ const time4 = Date.now() - start4;
61
+ console.log(`โœ… Got: ${song4.title} (took ${time4}ms)`);
62
+
63
+ const stats4 = odesli.getCacheStats();
64
+ console.log(
65
+ ` Cache hits: ${stats4.hitCount}, misses: ${stats4.missCount}, hit rate: ${(stats4.hitRate * 100).toFixed(1)}%\n`
66
+ );
67
+
68
+ // Performance comparison
69
+ console.log('๐Ÿ“Š Performance Summary:');
70
+ console.log(` First request (miss): ${time1}ms`);
71
+ console.log(` Second request (hit): ${time2}ms`);
72
+ console.log(` Third request (hit): ${time3}ms`);
73
+ console.log(` Skip cache request: ${time4}ms`);
74
+
75
+ if (time2 < time1) {
76
+ console.log(
77
+ ` โœ… Cache is working! Hit was ${Math.round(time1 / time2)}x faster than miss`
78
+ );
79
+ } else {
80
+ console.log(
81
+ ` โš ๏ธ Cache hit wasn't faster (network might be very fast)`
82
+ );
83
+ }
84
+
85
+ // Clear cache and test again
86
+ console.log('\n5. Clearing cache...');
87
+ odesli.clearCache();
88
+ const statsAfterClear = odesli.getCacheStats();
89
+ console.log(
90
+ ` After clear: hits: ${statsAfterClear.hitCount}, misses: ${statsAfterClear.missCount}\n`
91
+ );
92
+
93
+ // Test after clearing cache
94
+ console.log('6. Request after clearing cache (should be cache miss)...');
95
+ const start5 = Date.now();
96
+ const song5 = await odesli.fetch(testUrl);
97
+ const time5 = Date.now() - start5;
98
+ console.log(`โœ… Got: ${song5.title} (took ${time5}ms)`);
99
+
100
+ const finalStats = odesli.getCacheStats();
101
+ console.log(
102
+ ` Final stats: hits: ${finalStats.hitCount}, misses: ${finalStats.missCount}, hit rate: ${(finalStats.hitRate * 100).toFixed(1)}%`
103
+ );
104
+ } catch (error) {
105
+ if (
106
+ error.message &&
107
+ (error.message.includes('401') || error.message.includes('429'))
108
+ ) {
109
+ console.error('Error:', error.message);
110
+ console.error(
111
+ 'You may need an API key for higher limits. See https://odesli.co/'
112
+ );
113
+ } else {
114
+ console.error('Error:', error.message);
115
+ }
116
+ }
117
+ }
118
+
119
+ cacheTest();
@@ -0,0 +1,60 @@
1
+ // Test Apple Music ID extraction improvements
2
+ const Odesli = require('../lib/index.js');
3
+
4
+ const odesli = new Odesli();
5
+
6
+ console.log('๐ŸŽ Testing Apple Music ID Extraction Improvements\n');
7
+
8
+ // Test various Apple Music URL formats
9
+ const testUrls = [
10
+ // Album URLs with track IDs
11
+ 'https://music.apple.com/us/album/blinding-lights/1493120897?i=1493120900',
12
+ 'https://music.apple.com/gb/album/watermelon-sugar/1493120897?i=1493120901',
13
+ 'https://itunes.apple.com/us/album/bad-guy/1493120897?i=1493120902',
14
+
15
+ // Direct track URLs
16
+ 'https://music.apple.com/us/album/blinding-lights/1493120897',
17
+ 'https://itunes.apple.com/us/album/watermelon-sugar/1493120898',
18
+
19
+ // Other formats
20
+ 'https://music.apple.com/us/album/123456789',
21
+ 'https://itunes.apple.com/us/album/987654321?i=123456789',
22
+
23
+ // Invalid URLs for comparison
24
+ 'https://open.spotify.com/track/4Km5HrUvYTaSUfiSGPJeQR',
25
+ 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
26
+ 'invalid-url',
27
+ ];
28
+
29
+ console.log('๐Ÿ“‹ Testing URL parsing:');
30
+ testUrls.forEach((url, index) => {
31
+ const platform = odesli.detectPlatform(url);
32
+ const extractedId = odesli.extractId(url);
33
+
34
+ console.log(`${index + 1}. ${url}`);
35
+ console.log(` Platform: ${platform || 'Unknown'}`);
36
+ console.log(` Extracted ID: ${extractedId || 'Not found'}`);
37
+
38
+ if (platform === 'appleMusic' && extractedId) {
39
+ console.log(` โœ… Successfully extracted Apple Music ID: ${extractedId}`);
40
+ } else if (platform === 'appleMusic' && !extractedId) {
41
+ console.log(` โŒ Failed to extract Apple Music ID`);
42
+ }
43
+ console.log('');
44
+ });
45
+
46
+ console.log('๐ŸŽฏ Summary:');
47
+ const appleMusicUrls = testUrls.filter(
48
+ url => odesli.detectPlatform(url) === 'appleMusic'
49
+ );
50
+ const successfulExtractions = appleMusicUrls.filter(url =>
51
+ odesli.extractId(url)
52
+ );
53
+
54
+ console.log(`Total Apple Music URLs: ${appleMusicUrls.length}`);
55
+ console.log(`Successful ID extractions: ${successfulExtractions.length}`);
56
+ console.log(
57
+ `Success rate: ${((successfulExtractions.length / appleMusicUrls.length) * 100).toFixed(1)}%`
58
+ );
59
+
60
+ console.log('\nโœจ Apple Music ID extraction test completed!');
@@ -0,0 +1,46 @@
1
+ // Example: No API key is used. You are limited to 10 requests/minute. Some requests may return 401/429 if the rate limit is exceeded.
2
+ const Odesli = require('../lib/index.js');
3
+
4
+ async function userAgentExample() {
5
+ console.log('๐ŸŒ Odesli.js User-Agent Example\n');
6
+
7
+ // Initialize the client (no API key)
8
+ const odesli = new Odesli();
9
+
10
+ // Display the User-Agent string
11
+ console.log('Current User-Agent:');
12
+ console.log(odesli.getUserAgent());
13
+ console.log();
14
+
15
+ // Show package information
16
+ console.log('Package Information:');
17
+ console.log(`Name: ${require('../package.json').name}`);
18
+ console.log(`Version: ${require('../package.json').version}`);
19
+ console.log(`Node.js: ${process.version}`);
20
+ console.log(`Platform: ${process.platform}`);
21
+ console.log();
22
+
23
+ // Example of how the User-Agent looks in requests
24
+ console.log('The User-Agent will be sent with every API request:');
25
+ console.log(`GET https://api.song.link/v1-alpha.1/links?...`);
26
+ console.log(`User-Agent: ${odesli.getUserAgent()}`);
27
+ console.log();
28
+
29
+ // Test with custom headers
30
+ console.log('With custom headers:');
31
+ const odesliWithCustomHeaders = new Odesli({
32
+ headers: {
33
+ 'X-Custom-Header': 'MyApp/1.0',
34
+ 'User-Agent': 'CustomUserAgent/1.0', // This will override the default
35
+ },
36
+ });
37
+
38
+ console.log('Custom User-Agent (overridden):');
39
+ console.log(odesliWithCustomHeaders.getUserAgent());
40
+ console.log(
41
+ 'Note: The custom User-Agent in headers will be used instead of the default one.'
42
+ );
43
+ }
44
+
45
+ // Run the example
46
+ userAgentExample().catch(console.error);
package/jest.config.js CHANGED
@@ -5,8 +5,10 @@ module.exports = {
5
5
  collectCoverageFrom: ['lib/**/*.js', '!lib/**/*.d.ts'],
6
6
  coverageDirectory: 'coverage',
7
7
  coverageReporters: ['text', 'lcov', 'html'],
8
- setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
8
+ setupFilesAfterEnv: ['./tests/setup.js'],
9
9
  testEnvironmentOptions: {
10
10
  url: 'http://localhost',
11
11
  },
12
+ testPathIgnorePatterns: ['/node_modules/', '/docs/'],
13
+ coveragePathIgnorePatterns: ['/node_modules/', '/tests/'],
12
14
  };