Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions app/helperFunctions/fetchData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import axios from 'axios';

type fetchParams = {
url: string;
method?: 'get' | 'patch' | 'post' | 'delete';
params?: any | null;
data?: null | any;
headers?: null | object;
options?: object | null;
};

/**
* Used for network calls
* @param url {String} - API Endpoint URL
* @param method {String} - API Call Method (GET, POST etc.)
* @param params {Object} - Query Params for the API call
* @param data {Object} - Body to be sent
* @param headers {Object} - Headers to be sent
* @param options {Object} - Options to be sent via axios
*/

const fetch = ({
url,
method = 'get',
params = null,
data = null,
headers = null,
options = {},
}: fetchParams): { requestPromise: Promise<any>; cancelApi: () => void } => {
const { CancelToken } = axios;
const source = CancelToken.source();
const requestPromise = axios({
method,
url,
params,
data,
headers: {
'Content-type': 'application/json',
...headers,
},
withCredentials: true,
cancelToken: source.token,
...options,
});
return {
requestPromise,
cancelApi: source.cancel,
};
};

export default fetch;