refactor api

This commit is contained in:
Sayem Chowdhury 2021-04-07 16:47:11 +06:00
parent 66d220ab23
commit 2847a406e5
4 changed files with 63 additions and 56 deletions

View File

@ -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

9
src/api/cache.ts Normal file
View File

@ -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;

2
src/api/index.ts Normal file
View File

@ -0,0 +1,2 @@
export * from './api';
export * from './request';

49
src/api/request.ts Normal file
View File

@ -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;
};