react-native-dropdown-autocomplete
Version:
[npm-badge]: https://img.shields.io/npm/v/react-native-dropdown-autocomplete.svg?colorA=6b7c93&colorB=5ab1b8&style=flat-square [npm-url]: https://www.npmjs.com/package/react-native-dropdown-autocomplete [npm-downloads]: https://img.shields.io/npm/dt/react
44 lines (35 loc) • 983 B
JavaScript
import queryString from "query-string";
export const opts = {
mode: "cors",
cache: "no-cache",
headers: {
"Content-Type": "application/json; charset=utf-8",
"Access-Control-Request-Headers": "Content-Type",
},
};
export const fetchError = () => {
throw new Error("There was an error fetching your data");
};
const throwErrorResponse = resp => {
throw resp;
};
const qs = params =>
Object.entries(params).length ? `?${queryString.stringify(params)}` : "";
const processResponse = async resp => {
try {
const body = await resp.json();
if (!body.data) {
return resp.ok ? body : throwErrorResponse(body);
}
return resp.ok && body.success ? body.data : throwErrorResponse(body.error);
} catch (e) {
return fetchError(e);
}
};
export const get = async (url, params = {}) => {
const response = await fetch(`${url}${qs(params)}`, {
...opts,
method: "GET",
}).catch(fetchError);
return processResponse(response);
};