Files
qb-core-qbcore-fivem/html/js/utils.js
T
darknight2590 3b61e13854 feat (html): Organised html file - Darknight2590
* Rename html/app.js to html/js/app.js
* Rename html/config.js to html/js/config.js
* Rename html/drawtext.js to html/js/drawtext.js
* Rename html/testing.js to html/js/testing.js
* Rename html/utils.js to html/js/utils.js
* Rename html/style.css to html/css/style.css
* Rename html/drawtext.css to html/css/drawtext.css
* Update fxmanifest.lua
2022-02-17 19:35:01 +11:00

28 lines
938 B
JavaScript

// Returns whether we are in browser or not
export const isEnvBrowser = () => !window.invokeNative;
/**
* Real simple wrapper around fetch api for NUI focus
* it will return the mockData param if we are in browser. So we don't
* make a useless request to a hostname that doesn't exist.
* @param evName {string} - The callback event name/type
* @param data {any} - Optional `body` data JSON stringified & passed with the request
* @param mockData {any} - Mock data to return if this is running in browser
* @return Promise
**/
export const fetchNui = async (evName, data, mockData = null) => {
if (isEnvBrowser()) return mockData;
const resourceName = window.GetParentResourceName();
const rawResp = await fetch(`https://${resourceName}/${evName}`, {
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json; charset=UTF8",
},
method: "POST",
});
return await rawResp.json();
};