/* * Endpoint chill_api_single_country__index * method GET, get Country Object * @returns {Promise} a promise containing all Country object */ const fetchCountries = () => { console.log('<<< fetching countries'); const url = `/api/1.0/main/country.json?item_per_page=1000`; return fetch(url) .then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; /* * Endpoint chill_api_single_postal_code__index * method GET, get Country Object * @returns {Promise} a promise containing all Postal Code objects filtered with country */ const fetchCities = (country) => { console.log('<<< fetching cities for', country); const url = `/api/1.0/main/postal-code.json?item_per_page=1000&country=${country.id}`; return fetch(url) .then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; /* * Endpoint chill_api_single_address_reference__index * method GET, get AddressReference Object * @returns {Promise} a promise containing all AddressReference objects filtered with postal code */ const fetchReferenceAddresses = (postalCode) => { console.log('<<< fetching references addresses for', postalCode); //TODO deal with huge number of addresses... we should do suggestion... const url = `/api/1.0/main/address-reference.json?item_per_page=1000&postal_code=${postalCode.id}`; return fetch(url) .then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; /* * Endpoint chill_api_single_address__entity__create * method POST, post Address Object * @returns {Promise} */ const postAddress = (address) => { console.log(address); const url = `/api/1.0/main/address.json?`; const body = address; return fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json;charset=utf-8' }, body: JSON.stringify(body) }).then(response => { if (response.ok) { return response.json(); } throw Error('Error with request resource response'); }); }; export { fetchCountries, fetchCities, fetchReferenceAddresses, postAddress };