diff --git a/src/api.ts b/src/api/api.ts similarity index 82% rename from src/api.ts rename to src/api/api.ts index 5953351..843f3f5 100644 --- a/src/api.ts +++ b/src/api/api.ts @@ -1,5 +1,5 @@ -import axios from './lib/request'; -import FastLRU from './lib/fast-lru'; +import axios from '../lib/request'; +import {request, requestPublicApi} from './request'; import type { albumType, trackType, @@ -15,60 +15,7 @@ import type { trackTypePublicApi, albumTypePublicApi, userType, -} from './types'; - -// expire cache in 60 minutes -const lru = new FastLRU({ - maxSize: 1000, - ttl: 60 * 60000, -}); - -/** - * Make post requests to deezer api - * @param {Object} body post body - * @param {String} method request method - */ -export const request = async (body: object, method: string) => { - const cacheKey = method + ':' + Object.entries(body).join(':'); - const cache = lru.get(cacheKey); - if (cache) { - return cache; - } - - const { - data: {error, results}, - } = await axios.post('/gateway.php', body, {params: {method}}); - - if (Object.keys(results).length > 0) { - lru.set(cacheKey, results); - return results; - } - - const errorMessage = Object.entries(error).join(', '); - throw new Error(errorMessage); -}; - -/** - * Make post requests to deezer public api - * @param {Object} body post body - * @param {String} method request method - */ -export const requestPublicApi = async (slug: string) => { - const cache = lru.get(slug); - if (cache) { - return cache; - } - - const {data} = await axios.get('https://api.deezer.com' + slug); - - if (data.error) { - const errorMessage = Object.entries(data.error).join(', '); - throw new Error(errorMessage); - } - - lru.set(slug, data); - return data; -}; +} from '../types'; /** * @param {String} sng_id song id diff --git a/src/api/cache.ts b/src/api/cache.ts new file mode 100644 index 0000000..52d02c9 --- /dev/null +++ b/src/api/cache.ts @@ -0,0 +1,9 @@ +import FastLRU from '../lib/fast-lru'; + +// Expire cache in 60 minutes +const lru = new FastLRU({ + maxSize: 1000, + ttl: 60 * 60000, +}); + +export default lru; diff --git a/src/api/index.ts b/src/api/index.ts new file mode 100644 index 0000000..d88e3c4 --- /dev/null +++ b/src/api/index.ts @@ -0,0 +1,2 @@ +export * from './api'; +export * from './request'; diff --git a/src/api/request.ts b/src/api/request.ts new file mode 100644 index 0000000..858029c --- /dev/null +++ b/src/api/request.ts @@ -0,0 +1,49 @@ +import axios from '../lib/request'; +import lru from './cache'; + +/** + * Make post requests to deezer api + * @param {Object} body post body + * @param {String} method request method + */ +export const request = async (body: object, method: string) => { + const cacheKey = method + ':' + Object.entries(body).join(':'); + const cache = lru.get(cacheKey); + if (cache) { + return cache; + } + + const { + data: {error, results}, + } = await axios.post('/gateway.php', body, {params: {method}}); + + if (Object.keys(results).length > 0) { + lru.set(cacheKey, results); + return results; + } + + const errorMessage = Object.entries(error).join(', '); + throw new Error(errorMessage); +}; + +/** + * Make post requests to deezer public api + * @param {Object} body post body + * @param {String} method request method + */ +export const requestPublicApi = async (slug: string) => { + const cache = lru.get(slug); + if (cache) { + return cache; + } + + const {data} = await axios.get('https://api.deezer.com' + slug); + + if (data.error) { + const errorMessage = Object.entries(data.error).join(', '); + throw new Error(errorMessage); + } + + lru.set(slug, data); + return data; +};