| 21 | // Fetches an API response and normalizes the result JSON according to schema. |
| 22 | // This makes every API response have the same shape, regardless of how nested it was. |
| 23 | const callApi = (endpoint, schema) => { |
| 24 | const fullUrl = (endpoint.indexOf(API_ROOT) === -1) ? API_ROOT + endpoint : endpoint |
| 25 | |
| 26 | return fetch(fullUrl) |
| 27 | .then(response => |
| 28 | response.json().then(json => { |
| 29 | if (!response.ok) { |
| 30 | return Promise.reject(json) |
| 31 | } |
| 32 | |
| 33 | const camelizedJson = camelizeKeys(json) |
| 34 | const nextPageUrl = getNextPageUrl(response) |
| 35 | |
| 36 | return Object.assign({}, |
| 37 | normalize(camelizedJson, schema), |
| 38 | { nextPageUrl } |
| 39 | ) |
| 40 | }) |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | // We use this Normalizr schemas to transform API responses from a nested form |
| 45 | // to a flat form where repos and users are placed in `entities`, and nested |