refactor request

This commit is contained in:
Sayem Chowdhury 2021-04-13 13:45:07 +06:00
parent 257e4f0f67
commit e93df0e0a8
2 changed files with 30 additions and 25 deletions

View File

@ -1,5 +1,5 @@
import axios from '../lib/request'; import axios from '../lib/request';
import {request, requestPublicApi} from './request'; import {request, requestGet, requestPublicApi} from './request';
import type { import type {
albumType, albumType,
trackType, trackType,
@ -118,18 +118,7 @@ export const searchMusic = (query: string, types: searchTypesProp[] = ['TRACK'],
/** /**
* Get details about current user * Get details about current user
*/ */
export const getUser = async (): Promise<userType> => { export const getUser = async (): Promise<userType> => requestGet('user_getInfo');
const {
data: {error, results},
} = await axios.get('/gateway.php', {params: {method: 'user_getInfo'}});
if (Object.keys(results).length > 0) {
return results;
}
const errorMessage = Object.entries(error).join(', ');
throw new Error(errorMessage);
};
/** /**
* Get list of channles * Get list of channles
@ -234,14 +223,6 @@ export const getPlaylistChannel = async (name?: string): Promise<playlistChannel
lang: 'en', lang: 'en',
timezone_offset: '6', timezone_offset: '6',
}; };
const {
data: {error, results},
} = await axios.get('/gateway.php', {params: {method: 'app_page_get', gateway_input}});
if (Object.keys(results).length > 0) { return await requestGet('app_page_get', {gateway_input});
return results;
}
const errorMessage = Object.entries(error).join(', ');
throw new Error(errorMessage);
}; };

View File

@ -2,7 +2,7 @@ import axios from '../lib/request';
import lru from './cache'; import lru from './cache';
/** /**
* Make post requests to deezer api * Make POST requests to deezer api
* @param {Object} body post body * @param {Object} body post body
* @param {String} method request method * @param {String} method request method
*/ */
@ -27,9 +27,33 @@ export const request = async (body: object, method: string) => {
}; };
/** /**
* Make post requests to deezer public api * Make GET requests to deezer public api
* @param {Object} body post body
* @param {String} method request method * @param {String} method request method
* @param {Object} params request parameters
*/
export const requestGet = async (method: string, params?: object) => {
const cacheKey = `${method}:${params ? Object.entries(params).join(':') : 'get_request'}`;
const cache = lru.get(cacheKey);
if (cache) {
return cache;
}
const {
data: {error, results},
} = await axios.get('/gateway.php', {params: {method, ...params}});
if (Object.keys(results).length > 0) {
lru.set(cacheKey, results);
return results;
}
const errorMessage = Object.entries(error).join(', ');
throw new Error(errorMessage);
};
/**
* Make GET requests to deezer public api
* @param {String} slug endpoint
*/ */
export const requestPublicApi = async (slug: string) => { export const requestPublicApi = async (slug: string) => {
const cache = lru.get(slug); const cache = lru.get(slug);