forked from qbcore-framework/qb-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
27 lines (23 loc) · 938 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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();
};