URL filter functionality.

This commit is contained in:
Mike Cao 2020-09-25 22:31:18 -07:00
parent 6bc371352c
commit 4fded49b03
27 changed files with 251 additions and 117 deletions

View file

@ -1,3 +1,5 @@
import { getQueryString } from './url';
export const apiRequest = (method, url, body) =>
fetch(url, {
method,
@ -20,19 +22,9 @@ export const apiRequest = (method, url, body) =>
return null;
});
const parseQuery = (url, params = {}) => {
const query = Object.keys(params).reduce((values, key) => {
if (params[key] !== undefined) {
return values.concat(`${key}=${encodeURIComponent(params[key])}`);
}
return values;
}, []);
return query.length ? `${url}?${query.join('&')}` : url;
};
export const get = (url, params) => apiRequest('get', `${url}${getQueryString(params)}`);
export const get = (url, params) => apiRequest('get', parseQuery(url, params));
export const del = (url, params) => apiRequest('delete', parseQuery(url, params));
export const del = (url, params) => apiRequest('delete', `${url}${getQueryString(params)}`);
export const post = (url, params) => apiRequest('post', url, JSON.stringify(params));