2022-03-13 22:30:57 +01:00
|
|
|
/**
|
|
|
|
|
* Simple wrapper around fetch API tailored for CEF/NUI use. This abstraction
|
|
|
|
|
* can be extended to include AbortController if needed or if the response isn't
|
|
|
|
|
* JSON. Tailor it to your needs.
|
|
|
|
|
*
|
|
|
|
|
* @param eventName - The endpoint eventname to target
|
|
|
|
|
* @param data - Data you wish to send in the NUI Callback
|
|
|
|
|
*
|
|
|
|
|
* @return returnData - A promise for the data sent back by the NuiCallbacks CB argument
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export async function fetchNui<T = any>(eventName: string, data?: any): Promise<T> {
|
|
|
|
|
const options = {
|
|
|
|
|
method: 'post',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
const resourceName = (window as any).GetParentResourceName
|
|
|
|
|
? (window as any).GetParentResourceName()
|
|
|
|
|
: 'nui-frame-app';
|
2022-03-13 22:30:57 +01:00
|
|
|
|
|
|
|
|
const resp = await fetch(`https://${resourceName}/${eventName}`, options);
|
|
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
const respFormatted = await resp.json();
|
2022-03-13 22:30:57 +01:00
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
return respFormatted;
|
2022-03-13 22:30:57 +01:00
|
|
|
}
|