Files
Mckay Wrigley 6500db9c1c MAJOR REFACTOR (#494)
* move index to home folder, create state and context files and barrell folder

* Sanity Check Commit:  reducer added to home.tsx manual QA all working

* WIP: promptBar

* fix missing json parse on folders and prompts

* split context and add promptbar context

* add context to nested prompt componets and componetize Folder componet

* remove log

* Create buttons folder and componetize sidebar action button

* tidy up prompt handlers

* componetized sidebar

* added back chatbar componet to left side sidebar

* monster commit: Componetized the common code between chatbar and promptbar into new componet Sidebar and added context to both bars

* add useFetch service

* added prettier import sort to keep imports ordered and easier to indentify

* added react query and useFetch to work with RQ

* added apiService, errorService and reactQuery

* add callback and tidy up error service

* refactor chat and child componets to useContext

* fix extra calls and bad calls to mel endpoint

* minor import cleanup

---------

Co-authored-by: jc.durbin <jc.durbin@ardanis.com>
2023-04-10 21:10:18 -06:00

89 lines
2.5 KiB
TypeScript

export type RequestModel = {
params?: object;
headers?: object;
signal?: AbortSignal;
};
export type RequestWithBodyModel = RequestModel & {
body?: object | FormData;
};
export const useFetch = () => {
const handleFetch = async (
url: string,
request: any,
signal?: AbortSignal,
) => {
const requestUrl = request?.params ? `${url}${request.params}` : url;
const requestBody = request?.body
? request.body instanceof FormData
? { ...request, body: request.body }
: { ...request, body: JSON.stringify(request.body) }
: request;
const headers = {
...(request?.headers
? request.headers
: request?.body && request.body instanceof FormData
? {}
: { 'Content-type': 'application/json' }),
};
return fetch(requestUrl, { ...requestBody, headers, signal })
.then((response) => {
if (!response.ok) throw response;
const contentType = response.headers.get('content-type');
const contentDisposition = response.headers.get('content-disposition');
const headers = response.headers;
const result =
contentType &&
(contentType?.indexOf('application/json') !== -1 ||
contentType?.indexOf('text/plain') !== -1)
? response.json()
: contentDisposition?.indexOf('attachment') !== -1
? response.blob()
: response;
return result;
})
.catch(async (err) => {
const contentType = err.headers.get('content-type');
const errResult =
contentType && contentType?.indexOf('application/problem+json') !== -1
? await err.json()
: err;
throw errResult;
});
};
return {
get: async <T>(url: string, request?: RequestModel): Promise<T> => {
return handleFetch(url, { ...request, method: 'get' });
},
post: async <T>(
url: string,
request?: RequestWithBodyModel,
): Promise<T> => {
return handleFetch(url, { ...request, method: 'post' });
},
put: async <T>(url: string, request?: RequestWithBodyModel): Promise<T> => {
return handleFetch(url, { ...request, method: 'put' });
},
patch: async <T>(
url: string,
request?: RequestWithBodyModel,
): Promise<T> => {
return handleFetch(url, { ...request, method: 'patch' });
},
delete: async <T>(url: string, request?: RequestModel): Promise<T> => {
return handleFetch(url, { ...request, method: 'delete' });
},
};
};