forked from Simnation/Main
edit
This commit is contained in:
parent
be02d05ba8
commit
fc7ea910e9
35 changed files with 11992 additions and 1 deletions
30
resources/[tools]/mt_lib/web/src/utils/debugData.ts
Normal file
30
resources/[tools]/mt_lib/web/src/utils/debugData.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { isEnvBrowser } from "./misc";
|
||||
|
||||
interface DebugEvent<T = unknown> {
|
||||
action: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emulates dispatching an event using SendNuiMessage in the lua scripts.
|
||||
* This is used when developing in browser
|
||||
*
|
||||
* @param events - The event you want to cover
|
||||
* @param timer - How long until it should trigger (ms)
|
||||
*/
|
||||
export const debugData = <P>(events: DebugEvent<P>[], timer = 1000): void => {
|
||||
if (import.meta.env.MODE === "development" && isEnvBrowser()) {
|
||||
for (const event of events) {
|
||||
setTimeout(() => {
|
||||
window.dispatchEvent(
|
||||
new MessageEvent("message", {
|
||||
data: {
|
||||
action: event.action,
|
||||
data: event.data,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}, timer);
|
||||
}
|
||||
}
|
||||
};
|
26
resources/[tools]/mt_lib/web/src/utils/fetchNui.ts
Normal file
26
resources/[tools]/mt_lib/web/src/utils/fetchNui.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import { isEnvBrowser } from "./misc"
|
||||
|
||||
export async function fetchNui<T = unknown>(
|
||||
eventName: string,
|
||||
data?: unknown,
|
||||
mockData?: T,
|
||||
): Promise<T> {
|
||||
const options = {
|
||||
method: "post",
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=UTF-8",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
};
|
||||
|
||||
if (isEnvBrowser() && mockData) return mockData
|
||||
|
||||
const resourceName = (window as any).GetParentResourceName
|
||||
? (window as any).GetParentResourceName() : "nui-frame-app"
|
||||
|
||||
const resp = await fetch(`https://${resourceName}/${eventName}`, options)
|
||||
|
||||
const respFormatted = await resp.json()
|
||||
|
||||
return respFormatted
|
||||
}
|
3
resources/[tools]/mt_lib/web/src/utils/misc.ts
Normal file
3
resources/[tools]/mt_lib/web/src/utils/misc.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export const isEnvBrowser = (): boolean => !(window as any).invokeNative
|
||||
|
||||
export const noop = () => {}
|
Loading…
Add table
Add a link
Reference in a new issue