MCPcopy
hub / github.com/Hashnode/mern-starter / callApi

Function callApi

client/util/apiCaller.js:8–26  ·  view source on GitHub ↗
(endpoint, method = 'get', body)

Source from the content-addressed store, hash-verified

6 '/api';
7
8export default function callApi(endpoint, method = 'get', body) {
9 return fetch(`${API_URL}/${endpoint}`, {
10 headers: { 'content-type': 'application/json' },
11 method,
12 body: JSON.stringify(body),
13 })
14 .then(response => response.json().then(json => ({ json, response })))
15 .then(({ json, response }) => {
16 if (!response.ok) {
17 return Promise.reject(json);
18 }
19
20 return json;
21 })
22 .then(
23 response => response,
24 error => error
25 );
26}

Callers 5

addPostRequestFunction · 0.85
fetchPostsFunction · 0.85
fetchPostFunction · 0.85
deletePostRequestFunction · 0.85
apiCaller.spec.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected