@warpfx/client 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +77 -55
- package/dist/index.js +40 -20
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,48 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @
|
|
2
|
+
* @warpfx/client — Client-side SDK for Warp module development.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Clean, minimal API for building FiveM client features with Warp.
|
|
5
|
+
* All calls delegate to the Warp core via FiveM resource exports.
|
|
6
6
|
*
|
|
7
7
|
* Usage:
|
|
8
|
-
* import {
|
|
8
|
+
* import { accounts } from './db'; // auto-generated reactive store
|
|
9
|
+
* import { action, call } from '@warpfx/client';
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* events.emitServer('economy:request-balance', {});
|
|
14
|
-
* });
|
|
11
|
+
* accounts.watch((data) => console.log('Balance:', data[0].balance));
|
|
12
|
+
* action('deposit', { amount: 100 });
|
|
13
|
+
* const { balance } = await call('getBalance', { targetId: 'xyz' });
|
|
15
14
|
*/
|
|
16
15
|
/** Typed event bus for client-side and network communication. */
|
|
17
16
|
interface ClientEventBus<TMap extends Record<string, any> = Record<string, any>> {
|
|
18
|
-
/** Subscribe to a local event. Returns an unsubscribe function. */
|
|
19
17
|
on<K extends string & keyof TMap>(event: K, handler: (data: TMap[K]) => void): () => void;
|
|
20
|
-
/** Unsubscribe from a local event. */
|
|
21
18
|
off<K extends string & keyof TMap>(event: K, handler: (data: TMap[K]) => void): void;
|
|
22
|
-
/** Emit a local event to all subscribers. */
|
|
23
19
|
emit<K extends string & keyof TMap>(event: K, data: TMap[K]): void;
|
|
24
|
-
/** Send an event to the server. */
|
|
25
20
|
emitServer<K extends string & keyof TMap>(event: K, data: TMap[K]): void;
|
|
26
|
-
/** Listen for events from the server. Returns an unsubscribe function. */
|
|
27
21
|
onServer<K extends string & keyof TMap>(event: K, handler: (data: TMap[K]) => void): () => void;
|
|
28
|
-
/** Unsubscribe from a server event. */
|
|
29
22
|
offServer<K extends string & keyof TMap>(event: K, handler: (data: TMap[K]) => void): void;
|
|
30
23
|
}
|
|
31
|
-
/**
|
|
32
|
-
|
|
33
|
-
* If already ready, the callback fires immediately.
|
|
34
|
-
*/
|
|
35
|
-
declare function onReady(cb: () => void): void;
|
|
36
|
-
/** Check if Warp client has finished initializing. */
|
|
37
|
-
declare function isReady(): boolean;
|
|
38
|
-
/** Get the shared client event bus instance. */
|
|
39
|
-
declare function getEvents<TMap extends Record<string, any> = Record<string, any>>(): ClientEventBus<TMap>;
|
|
40
|
-
/** Reactive handle to synced state. */
|
|
41
|
-
interface SyncHandle<T = any> {
|
|
24
|
+
/** Reactive handle to synced state from the server. */
|
|
25
|
+
interface SyncRef<T = any> {
|
|
42
26
|
/** Current value of the synced state. */
|
|
43
27
|
readonly value: T;
|
|
44
|
-
/**
|
|
45
|
-
|
|
28
|
+
/** React to state changes. Returns an unsubscribe function. */
|
|
29
|
+
watch(handler: (newState: T, oldState: T) => void): () => void;
|
|
46
30
|
}
|
|
47
31
|
/** Action error received from the server. */
|
|
48
32
|
interface ActionError {
|
|
@@ -50,52 +34,90 @@ interface ActionError {
|
|
|
50
34
|
code: string;
|
|
51
35
|
message: string;
|
|
52
36
|
}
|
|
37
|
+
/** Key binding configuration. */
|
|
38
|
+
interface KeybindOptions {
|
|
39
|
+
description: string;
|
|
40
|
+
key: string;
|
|
41
|
+
device?: "keyboard" | "mouse_button" | "pad_analogbutton";
|
|
42
|
+
onPress: () => void;
|
|
43
|
+
onRelease?: () => void;
|
|
44
|
+
}
|
|
45
|
+
/** Options for showNUI(). */
|
|
46
|
+
interface ShowNUIOptions {
|
|
47
|
+
cursor?: boolean;
|
|
48
|
+
}
|
|
53
49
|
/**
|
|
54
|
-
*
|
|
50
|
+
* Dispatch an action to the server. Fire-and-forget — state syncs back
|
|
51
|
+
* automatically via reactive stores.
|
|
55
52
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
53
|
+
* @example
|
|
54
|
+
* action('deposit', { amount: 100 });
|
|
55
|
+
* action('transfer', { targetId: 'xyz', amount: 50 });
|
|
56
|
+
*/
|
|
57
|
+
declare function action(name: string, payload?: any): void;
|
|
58
|
+
/**
|
|
59
|
+
* Call a server function and await the result.
|
|
58
60
|
*
|
|
59
61
|
* @example
|
|
60
|
-
* const
|
|
61
|
-
*
|
|
62
|
-
* inventory.onChange((items) => console.log('Updated:', items.length));
|
|
62
|
+
* const { balance } = await call<{ balance: number }>('getBalance');
|
|
63
|
+
* const price = await call('getPrice', { itemId: 'bread' });
|
|
63
64
|
*/
|
|
64
|
-
declare function
|
|
65
|
+
declare function call<T = any>(name: string, payload?: any, timeout?: number): Promise<T>;
|
|
65
66
|
/**
|
|
66
|
-
* Create
|
|
67
|
+
* Create a reactive sync reference for a state key.
|
|
67
68
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
69
|
+
* The returned ref updates automatically when the server pushes changes.
|
|
70
|
+
* Use this in auto-generated `db.ts` files or for manual state subscriptions.
|
|
70
71
|
*
|
|
71
72
|
* @example
|
|
72
|
-
* const
|
|
73
|
-
*
|
|
73
|
+
* const accounts = sync<Account[]>('accounts', []);
|
|
74
|
+
* accounts.watch((data) => console.log('Updated:', data));
|
|
75
|
+
* console.log(accounts.value);
|
|
74
76
|
*/
|
|
75
|
-
declare function
|
|
76
|
-
onError?: (error: ActionError) => void;
|
|
77
|
-
}): (payload: P) => void;
|
|
77
|
+
declare function sync<T = any>(key: string, defaultValue?: T): SyncRef<T>;
|
|
78
78
|
/**
|
|
79
79
|
* Register a global error handler for all action errors.
|
|
80
80
|
* Returns an unsubscribe function.
|
|
81
81
|
*
|
|
82
82
|
* @example
|
|
83
|
-
*
|
|
84
|
-
* console.log(`Action ${error.action} failed: ${error.message}`);
|
|
85
|
-
* });
|
|
83
|
+
* onError((err) => console.log(`${err.action} failed: ${err.message}`));
|
|
86
84
|
*/
|
|
87
|
-
declare function
|
|
85
|
+
declare function onError(handler: (error: ActionError) => void): () => void;
|
|
88
86
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
87
|
+
* Register a callback to run when Warp client is ready.
|
|
88
|
+
* If already ready, the callback fires immediately.
|
|
91
89
|
*/
|
|
92
|
-
declare function
|
|
90
|
+
declare function onReady(cb: () => void): void;
|
|
91
|
+
/** Check if Warp client has finished initializing. */
|
|
92
|
+
declare function isReady(): boolean;
|
|
93
|
+
/** Get the shared client event bus instance. */
|
|
94
|
+
declare function getEvents<TMap extends Record<string, any> = Record<string, any>>(): ClientEventBus<TMap>;
|
|
93
95
|
/**
|
|
94
|
-
* Show the NUI overlay with
|
|
95
|
-
*
|
|
96
|
+
* Show the NUI overlay with focus.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* showNUI('inventory');
|
|
100
|
+
* showNUI('chat', { cursor: false });
|
|
96
101
|
*/
|
|
97
|
-
declare function showNUI(panel?: string): void;
|
|
98
|
-
/** Hide the NUI overlay and release
|
|
102
|
+
declare function showNUI(panel?: string, opts?: ShowNUIOptions): void;
|
|
103
|
+
/** Hide the NUI overlay and release all focus. */
|
|
99
104
|
declare function hideNUI(): void;
|
|
105
|
+
/** Set NUI focus directly without visibility events. */
|
|
106
|
+
declare function setNUIFocus(hasFocus: boolean, hasCursor?: boolean): void;
|
|
107
|
+
/** Send a message to the NUI (browser) layer. */
|
|
108
|
+
declare function sendNUI(type: string, payload?: Record<string, any>): void;
|
|
109
|
+
/** Register a handler for NUI callbacks (browser → client). */
|
|
110
|
+
declare function onNUI<T = any>(name: string, handler: (data: T) => any): void;
|
|
111
|
+
/**
|
|
112
|
+
* Register a key binding with FiveM's key mapping system.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* registerKeybind('open_inventory', {
|
|
116
|
+
* description: 'Open Inventory',
|
|
117
|
+
* key: 'I',
|
|
118
|
+
* onPress: () => showNUI('inventory'),
|
|
119
|
+
* });
|
|
120
|
+
*/
|
|
121
|
+
declare function registerKeybind(name: string, options: KeybindOptions): void;
|
|
100
122
|
|
|
101
|
-
export { type ActionError, type ClientEventBus, type
|
|
123
|
+
export { type ActionError, type ClientEventBus, type KeybindOptions, type ShowNUIOptions, type SyncRef, action, call, getEvents, hideNUI, isReady, onError, onNUI, onReady, registerKeybind, sendNUI, setNUIFocus, showNUI, sync };
|
package/dist/index.js
CHANGED
|
@@ -20,20 +20,36 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
action: () => action,
|
|
24
|
+
call: () => call,
|
|
23
25
|
getEvents: () => getEvents,
|
|
24
|
-
getSyncState: () => getSyncState,
|
|
25
26
|
hideNUI: () => hideNUI,
|
|
26
27
|
isReady: () => isReady,
|
|
28
|
+
onError: () => onError,
|
|
29
|
+
onNUI: () => onNUI,
|
|
27
30
|
onReady: () => onReady,
|
|
31
|
+
registerKeybind: () => registerKeybind,
|
|
32
|
+
sendNUI: () => sendNUI,
|
|
33
|
+
setNUIFocus: () => setNUIFocus,
|
|
28
34
|
showNUI: () => showNUI,
|
|
29
|
-
|
|
30
|
-
useErrorHandler: () => useErrorHandler,
|
|
31
|
-
useSync: () => useSync
|
|
35
|
+
sync: () => sync
|
|
32
36
|
});
|
|
33
37
|
module.exports = __toCommonJS(index_exports);
|
|
34
38
|
function warp() {
|
|
35
39
|
return globalThis.exports["warp"];
|
|
36
40
|
}
|
|
41
|
+
function action(name, payload) {
|
|
42
|
+
warp().action(name, payload);
|
|
43
|
+
}
|
|
44
|
+
function call(name, payload, timeout) {
|
|
45
|
+
return warp().call(name, payload, timeout);
|
|
46
|
+
}
|
|
47
|
+
function sync(key, defaultValue) {
|
|
48
|
+
return warp().sync(key, defaultValue);
|
|
49
|
+
}
|
|
50
|
+
function onError(handler) {
|
|
51
|
+
return warp().onError(handler);
|
|
52
|
+
}
|
|
37
53
|
function onReady(cb) {
|
|
38
54
|
warp().onReady(cb);
|
|
39
55
|
}
|
|
@@ -43,33 +59,37 @@ function isReady() {
|
|
|
43
59
|
function getEvents() {
|
|
44
60
|
return warp().getEvents();
|
|
45
61
|
}
|
|
46
|
-
function
|
|
47
|
-
|
|
62
|
+
function showNUI(panel, opts) {
|
|
63
|
+
warp().showNUI(panel, opts);
|
|
48
64
|
}
|
|
49
|
-
function
|
|
50
|
-
|
|
65
|
+
function hideNUI() {
|
|
66
|
+
warp().hideNUI();
|
|
51
67
|
}
|
|
52
|
-
function
|
|
53
|
-
|
|
68
|
+
function setNUIFocus(hasFocus, hasCursor) {
|
|
69
|
+
warp().setNUIFocus(hasFocus, hasCursor);
|
|
54
70
|
}
|
|
55
|
-
function
|
|
56
|
-
|
|
71
|
+
function sendNUI(type, payload) {
|
|
72
|
+
warp().sendNUI(type, payload);
|
|
57
73
|
}
|
|
58
|
-
function
|
|
59
|
-
warp().
|
|
74
|
+
function onNUI(name, handler) {
|
|
75
|
+
warp().onNUI(name, handler);
|
|
60
76
|
}
|
|
61
|
-
function
|
|
62
|
-
warp().
|
|
77
|
+
function registerKeybind(name, options) {
|
|
78
|
+
warp().registerKeybind(name, options);
|
|
63
79
|
}
|
|
64
80
|
// Annotate the CommonJS export names for ESM import in node:
|
|
65
81
|
0 && (module.exports = {
|
|
82
|
+
action,
|
|
83
|
+
call,
|
|
66
84
|
getEvents,
|
|
67
|
-
getSyncState,
|
|
68
85
|
hideNUI,
|
|
69
86
|
isReady,
|
|
87
|
+
onError,
|
|
88
|
+
onNUI,
|
|
70
89
|
onReady,
|
|
90
|
+
registerKeybind,
|
|
91
|
+
sendNUI,
|
|
92
|
+
setNUIFocus,
|
|
71
93
|
showNUI,
|
|
72
|
-
|
|
73
|
-
useErrorHandler,
|
|
74
|
-
useSync
|
|
94
|
+
sync
|
|
75
95
|
});
|