@warpfx/client 0.2.1 → 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 CHANGED
@@ -1,181 +1,115 @@
1
1
  /**
2
- * @warp/client — Typed SDK for Warp client-side module development.
2
+ * @warpfx/client — Client-side SDK for Warp module development.
3
3
  *
4
- * Provides type-safe access to the Warp client API. Under the hood,
5
- * all calls go through FiveM's resource exports to the 'warp' resource.
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 { onReady, getEvents, registerKeybind, sendNUI } from '@warp/client';
8
+ * import { accounts } from './db'; // auto-generated reactive store
9
+ * import { action, call } from '@warpfx/client';
9
10
  *
10
- * onReady(() => {
11
- * const events = getEvents();
12
- * events.onServer('economy:balance', (data) => { ... });
13
- *
14
- * registerKeybind('open_shop', {
15
- * description: 'Open Shop',
16
- * key: 'E',
17
- * onPress: () => showNUI('shop'),
18
- * });
19
- * });
11
+ * accounts.watch((data) => console.log('Balance:', data[0].balance));
12
+ * action('deposit', { amount: 100 });
13
+ * const { balance } = await call('getBalance', { targetId: 'xyz' });
20
14
  */
21
15
  /** Typed event bus for client-side and network communication. */
22
16
  interface ClientEventBus<TMap extends Record<string, any> = Record<string, any>> {
23
- /** Subscribe to a local event. Returns an unsubscribe function. */
24
17
  on<K extends string & keyof TMap>(event: K, handler: (data: TMap[K]) => void): () => void;
25
- /** Unsubscribe from a local event. */
26
18
  off<K extends string & keyof TMap>(event: K, handler: (data: TMap[K]) => void): void;
27
- /** Emit a local event to all subscribers. */
28
19
  emit<K extends string & keyof TMap>(event: K, data: TMap[K]): void;
29
- /** Send an event to the server. */
30
20
  emitServer<K extends string & keyof TMap>(event: K, data: TMap[K]): void;
31
- /** Listen for events from the server. Returns an unsubscribe function. */
32
21
  onServer<K extends string & keyof TMap>(event: K, handler: (data: TMap[K]) => void): () => void;
33
- /** Unsubscribe from a server event. */
34
22
  offServer<K extends string & keyof TMap>(event: K, handler: (data: TMap[K]) => void): void;
35
23
  }
24
+ /** Reactive handle to synced state from the server. */
25
+ interface SyncRef<T = any> {
26
+ /** Current value of the synced state. */
27
+ readonly value: T;
28
+ /** React to state changes. Returns an unsubscribe function. */
29
+ watch(handler: (newState: T, oldState: T) => void): () => void;
30
+ }
31
+ /** Action error received from the server. */
32
+ interface ActionError {
33
+ action: string;
34
+ code: string;
35
+ message: string;
36
+ }
36
37
  /** Key binding configuration. */
37
38
  interface KeybindOptions {
38
- /** Description shown in FiveM key mapping settings. */
39
39
  description: string;
40
- /** Default key (e.g. 'T', 'E', 'F5'). */
41
40
  key: string;
42
- /** Input device (defaults to 'keyboard'). */
43
41
  device?: "keyboard" | "mouse_button" | "pad_analogbutton";
44
- /** Callback when the key is pressed. */
45
42
  onPress: () => void;
46
- /** Callback when the key is released (optional). */
47
43
  onRelease?: () => void;
48
44
  }
49
45
  /** Options for showNUI(). */
50
46
  interface ShowNUIOptions {
51
- /** Whether to show the mouse cursor. Defaults to true. */
52
47
  cursor?: boolean;
53
48
  }
54
49
  /**
55
- * Register a callback to run when Warp client is ready.
56
- * If already ready, the callback fires immediately.
50
+ * Dispatch an action to the server. Fire-and-forget state syncs back
51
+ * automatically via reactive stores.
52
+ *
53
+ * @example
54
+ * action('deposit', { amount: 100 });
55
+ * action('transfer', { targetId: 'xyz', amount: 50 });
57
56
  */
58
- declare function onReady(cb: () => void): void;
59
- /** Check if Warp client has finished initializing. */
60
- declare function isReady(): boolean;
61
- /** Get the shared client event bus instance. */
62
- declare function getEvents<TMap extends Record<string, any> = Record<string, any>>(): ClientEventBus<TMap>;
63
- /** Reactive handle to synced state. */
64
- interface SyncHandle<T = any> {
65
- /** Current value of the synced state. */
66
- readonly value: T;
67
- /** Register a callback for when state changes. Returns unsubscribe function. */
68
- onChange(handler: (newState: T, oldState: T) => void): () => void;
69
- }
70
- /** Action error received from the server. */
71
- interface ActionError {
72
- action: string;
73
- code: string;
74
- message: string;
75
- }
57
+ declare function action(name: string, payload?: any): void;
76
58
  /**
77
- * Subscribe to synced state from the server.
78
- *
79
- * Returns a handle with a `.value` getter and `.onChange()` method.
80
- * State updates automatically when the server pushes changes.
59
+ * Call a server function and await the result.
81
60
  *
82
61
  * @example
83
- * const inventory = useSync<InventoryItem[]>('inventory', []);
84
- * console.log(inventory.value); // current items
85
- * inventory.onChange((items) => console.log('Updated:', items.length));
62
+ * const { balance } = await call<{ balance: number }>('getBalance');
63
+ * const price = await call('getPrice', { itemId: 'bread' });
86
64
  */
87
- declare function useSync<T = any>(key: string, defaultValue?: T): SyncHandle<T>;
65
+ declare function call<T = any>(name: string, payload?: any, timeout?: number): Promise<T>;
88
66
  /**
89
- * Create an action dispatcher to call server-side action handlers.
67
+ * Create a reactive sync reference for a state key.
90
68
  *
91
- * Returns a function that sends the action to the server.
92
- * State updates flow back automatically via the sync engine.
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.
93
71
  *
94
72
  * @example
95
- * const moveItem = useAction<{ itemId: string; toSlot: number }>('inventory:moveItem');
96
- * moveItem({ itemId: 'abc', toSlot: 5 });
73
+ * const accounts = sync<Account[]>('accounts', []);
74
+ * accounts.watch((data) => console.log('Updated:', data));
75
+ * console.log(accounts.value);
97
76
  */
98
- declare function useAction<P = any>(name: string, opts?: {
99
- onError?: (error: ActionError) => void;
100
- }): (payload: P) => void;
77
+ declare function sync<T = any>(key: string, defaultValue?: T): SyncRef<T>;
101
78
  /**
102
79
  * Register a global error handler for all action errors.
103
80
  * Returns an unsubscribe function.
104
81
  *
105
82
  * @example
106
- * useErrorHandler((error) => {
107
- * console.log(`Action ${error.action} failed: ${error.message}`);
108
- * });
83
+ * onError((err) => console.log(`${err.action} failed: ${err.message}`));
109
84
  */
110
- declare function useErrorHandler(handler: (error: ActionError) => void): () => void;
85
+ declare function onError(handler: (error: ActionError) => void): () => void;
111
86
  /**
112
- * Get the current synced state for a key.
113
- * Unlike useSync(), this is a one-shot read with no change tracking.
114
- */
115
- declare function getSyncState<T = any>(key: string): T | undefined;
116
- /**
117
- * Call a server function and await the result.
118
- *
119
- * Unlike actions (fire-and-forget), this returns a Promise that resolves
120
- * with the server's return value. Use for request/response patterns.
121
- *
122
- * @param name The registered function name.
123
- * @param payload Optional data to send to the server.
124
- * @param timeout Timeout in ms (default: 10000).
125
- *
126
- * @example
127
- * const { balance } = await callServer<{ balance: number }>('economy:getBalance');
128
- * const result = await callServer('shop:getPrice', { itemId: 'bread' });
87
+ * Register a callback to run when Warp client is ready.
88
+ * If already ready, the callback fires immediately.
129
89
  */
130
- declare function callServer<T = any>(name: string, payload?: any, timeout?: number): Promise<T>;
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>;
131
95
  /**
132
96
  * Show the NUI overlay with focus.
133
- * Optionally specify a panel name and cursor control.
134
97
  *
135
98
  * @example
136
- * showNUI('inventory'); // panel + keyboard + cursor
137
- * showNUI('chat', { cursor: false }); // panel + keyboard only
99
+ * showNUI('inventory');
100
+ * showNUI('chat', { cursor: false });
138
101
  */
139
102
  declare function showNUI(panel?: string, opts?: ShowNUIOptions): void;
140
103
  /** Hide the NUI overlay and release all focus. */
141
104
  declare function hideNUI(): void;
142
- /**
143
- * Set NUI focus directly without sending visibility events.
144
- * Use this for custom focus control (e.g. keyboard-only overlays).
145
- *
146
- * @param hasFocus Whether NUI should capture keyboard input.
147
- * @param hasCursor Whether to show the mouse cursor. Defaults to match hasFocus.
148
- *
149
- * @example
150
- * setNUIFocus(true, false); // keyboard only, no cursor
151
- * setNUIFocus(false); // release all focus
152
- */
105
+ /** Set NUI focus directly without visibility events. */
153
106
  declare function setNUIFocus(hasFocus: boolean, hasCursor?: boolean): void;
154
- /**
155
- * Send a message to the NUI (browser) layer.
156
- * The payload fields are spread into the message alongside the type.
157
- *
158
- * @example
159
- * sendNUI('inventory:open', { tab: 'weapons' });
160
- * // NUI receives: { type: 'inventory:open', tab: 'weapons' }
161
- */
107
+ /** Send a message to the NUI (browser) layer. */
162
108
  declare function sendNUI(type: string, payload?: Record<string, any>): void;
163
- /**
164
- * Register a handler for NUI callbacks (browser → client).
165
- *
166
- * When the NUI calls `fetch('https://warp/{name}', ...)`, this handler
167
- * fires. Return a value to send it back to the NUI, or return nothing
168
- * for the default `{ ok: true }` response.
169
- *
170
- * @example
171
- * onNUI('inventoryDrop', (data) => {
172
- * console.log('Dropped item:', data.itemId);
173
- * });
174
- */
109
+ /** Register a handler for NUI callbacks (browser → client). */
175
110
  declare function onNUI<T = any>(name: string, handler: (data: T) => any): void;
176
111
  /**
177
112
  * Register a key binding with FiveM's key mapping system.
178
- * Players can rebind the key in their FiveM settings.
179
113
  *
180
114
  * @example
181
115
  * registerKeybind('open_inventory', {
@@ -183,14 +117,7 @@ declare function onNUI<T = any>(name: string, handler: (data: T) => any): void;
183
117
  * key: 'I',
184
118
  * onPress: () => showNUI('inventory'),
185
119
  * });
186
- *
187
- * registerKeybind('sprint_boost', {
188
- * description: 'Sprint Boost',
189
- * key: 'LSHIFT',
190
- * onPress: () => startBoost(),
191
- * onRelease: () => stopBoost(),
192
- * });
193
120
  */
194
121
  declare function registerKeybind(name: string, options: KeybindOptions): void;
195
122
 
196
- export { type ActionError, type ClientEventBus, type KeybindOptions, type ShowNUIOptions, type SyncHandle, callServer, getEvents, getSyncState, hideNUI, isReady, onNUI, onReady, registerKeybind, sendNUI, setNUIFocus, showNUI, useAction, useErrorHandler, useSync };
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,25 +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
- callServer: () => callServer,
23
+ action: () => action,
24
+ call: () => call,
24
25
  getEvents: () => getEvents,
25
- getSyncState: () => getSyncState,
26
26
  hideNUI: () => hideNUI,
27
27
  isReady: () => isReady,
28
+ onError: () => onError,
28
29
  onNUI: () => onNUI,
29
30
  onReady: () => onReady,
30
31
  registerKeybind: () => registerKeybind,
31
32
  sendNUI: () => sendNUI,
32
33
  setNUIFocus: () => setNUIFocus,
33
34
  showNUI: () => showNUI,
34
- useAction: () => useAction,
35
- useErrorHandler: () => useErrorHandler,
36
- useSync: () => useSync
35
+ sync: () => sync
37
36
  });
38
37
  module.exports = __toCommonJS(index_exports);
39
38
  function warp() {
40
39
  return globalThis.exports["warp"];
41
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
+ }
42
53
  function onReady(cb) {
43
54
  warp().onReady(cb);
44
55
  }
@@ -48,21 +59,6 @@ function isReady() {
48
59
  function getEvents() {
49
60
  return warp().getEvents();
50
61
  }
51
- function useSync(key, defaultValue) {
52
- return warp().useSync(key, defaultValue);
53
- }
54
- function useAction(name, opts) {
55
- return warp().useAction(name, opts);
56
- }
57
- function useErrorHandler(handler) {
58
- return warp().useErrorHandler(handler);
59
- }
60
- function getSyncState(key) {
61
- return warp().getSyncState(key);
62
- }
63
- function callServer(name, payload, timeout) {
64
- return warp().callServer(name, payload, timeout);
65
- }
66
62
  function showNUI(panel, opts) {
67
63
  warp().showNUI(panel, opts);
68
64
  }
@@ -83,18 +79,17 @@ function registerKeybind(name, options) {
83
79
  }
84
80
  // Annotate the CommonJS export names for ESM import in node:
85
81
  0 && (module.exports = {
86
- callServer,
82
+ action,
83
+ call,
87
84
  getEvents,
88
- getSyncState,
89
85
  hideNUI,
90
86
  isReady,
87
+ onError,
91
88
  onNUI,
92
89
  onReady,
93
90
  registerKeybind,
94
91
  sendNUI,
95
92
  setNUIFocus,
96
93
  showNUI,
97
- useAction,
98
- useErrorHandler,
99
- useSync
94
+ sync
100
95
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warpfx/client",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Warp Framework SDK for client-side FiveM module development",
5
5
  "keywords": [
6
6
  "client",