The API provides means for adding, editing, removing, and querying transactions in the form of HTTP methods defined on a fetch call. To simplify the process of performing these actions, we'll write some API wrapper functions. Create a file in the /src/store folder named api.js and populate it with the following contents:
// Paste your jsonstore.io endpoint here (no ending slash):
const API_URL = '[JSONSTORE.IO ENDPOINT]';
/**
* Wrapper for performing API calls. We don't want to call response.json()
* each time we make a fetch call.
* @param {string} endpoint Endpoint (e.g. "/transactions" to make API call to
* @param {Object} init Fetch options object containing any custom settings
* @returns {Promise<*>}
* @see https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
*/
const performApiFetch = (endpoint = '', init = {}) =>
fetch(`${API_URL}${endpoint}`, {
headers: {
'Content-type': 'application/json'
},
...init
}).then(response => response.json());
export const apiFetchTransactions = () =>
performApiFetch('/transactions').then(({ result }) =>
/*
* The response object looks like this:
* {
* "result": {
* "1": {
* "category": "Sales Revenue",
* ...
* },
* "2": {
* "category": "Hotels",
* ...
* },
* ...
* }
* }
* We need the "1" and "2" values for deleting or editing existing
* records, so we store that in the transaction record as "apiId".
*/
Object.keys(result).map(apiId => ({
...result[apiId],
apiId
}))
);
export const apiEditTransaction = transaction =>
performApiFetch(`/transactions/${transaction.apiId}`, {
method: 'POST',
body: JSON.stringify(transaction)
});
export const apiRemoveTransaction = transaction =>
performApiFetch(`/transactions/${transaction.apiId}`, {
method: 'DELETE'
});
export const apiAddTransaction = transaction =>
performApiFetch(`/transactions/${transaction.apiId}`, {
method: 'POST',
body: JSON.stringify(transaction)
});
You'll need the jsonstore.io endpoint you created in the Setting up the project section in order to interact with the API. Replace [JSONSTORE.IO ENDPOINT] with your jsonstore.io endpoint. Ensure the endpoint doesn't end with a forward slash or the word transactions.