xmr-remote-nodes/frontend/src/routes/remote-nodes/api-handler.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

import { apiUri } from '$lib/utils/common';
/**
* @typedef {import('@vincjo/datatables/remote').State} State
* @param {State} state - The state object from the data table.
*/
export const loadData = async (state) => {
const response = await fetch(apiUri(`/api/v1/nodes?${getParams(state)}`));
const json = await response.json();
state.setTotalRows(json.data.total_rows ?? 0);
return json.data.items ?? [];
};
2024-05-06 13:59:33 +07:00
export const loadCountries = async () => {
2024-05-28 10:03:29 +07:00
const response = await fetch(apiUri('/api/v1/countries'));
const json = await response.json();
return json.data ?? [];
2024-05-06 13:59:33 +07:00
};
export const loadFees = async () => {
const response = await fetch(apiUri('/api/v1/fees'));
const json = await response.json();
return json.data ?? [];
};
/** @param {State} state - The state object from the data table. */
const getParams = ({ pageNumber, rowsPerPage, sort, filters }) => {
let params = `page=${pageNumber}&limit=${rowsPerPage}`;
if (sort) {
params += `&sort_by=${sort.orderBy}&sort_direction=${sort.direction}`;
}
if (filters) {
params += filters.map(({ filterBy, value }) => `&${filterBy}=${value}`).join('');
}
return params;
};