Add useNuiEventCallback hook (#173)

* Add useNuiEventCallback hook
This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-03-19 11:54:26 -03:00
1 parent 769f5a3d21
commit 97c308f550
3 files changed
+136 -8

No files matched your search

+29 -6
View File
@@ -1,13 +1,36 @@
// eslint-disable-next-line
export default {
async send(event: any, data = {}) {
/* eslint-disable no-unreachable */
return fetch(`https://new-phone-who-dis/${event}`, {
export interface IAbortableFetch {
abort: () => void;
promise: Promise<Response>;
}
function abortableFetch(request, opts): IAbortableFetch {
const controller = new AbortController();
const signal = controller.signal;
return {
abort: () => controller.abort(),
promise: fetch(request, { ...opts, signal }),
};
}
function getParams(event, data): [RequestInfo, RequestInit] {
return [
`https://new-phone-who-dis/${event}`,
{
method: 'post',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify(data),
});
},
];
}
export default {
async send(event: any, data = {}) {
return fetch(...getParams(event, data));
},
sendAbortable(event: any, data = {}): IAbortableFetch {
return abortableFetch(...getParams(event, data));
},
};