@nativewrappers/common 0.0.52 → 0.0.53

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/Kvp.d.ts CHANGED
@@ -1,58 +1,50 @@
1
- interface Schema {
2
- [key: string]: "string" | "number" | "float" | object;
3
- }
4
- export declare class Kvp<T extends Schema> {
5
- private schema;
6
- constructor(schema: T);
7
- /**
8
- * Returns the type associated with a schema key.
9
- */
10
- getType(key: keyof T): "string" | "number" | "float" | "object";
1
+ type KvpSchema = Record<string, string | number | object>;
2
+ type KvpObject<T> = T extends `${infer Prefix}.${infer Rest}` ? Rest extends `${string}.${string}` ? never : Prefix : never;
3
+ type ValidJsonKey<Schema> = {
4
+ [K in keyof Schema]: K extends string ? `${K}.${string}` : never;
5
+ }[keyof Schema];
6
+ export declare class Kvp<Schema extends KvpSchema> {
11
7
  /**
12
8
  * Returns the value associated with a key as a number.
13
9
  */
14
- getNumber(key: string): number;
10
+ getNumber<K extends string & keyof Schema>(key: K): number;
15
11
  /**
16
12
  * Returns the value associated with a key as a float.
17
13
  */
18
- getFloat(key: string): number;
14
+ getFloat<K extends string & keyof Schema>(key: K): number;
19
15
  /**
20
16
  * Returns the value associated with a key as a string.
21
17
  */
22
- getString(key: string): string | null;
18
+ getString<K extends string & keyof Schema>(key: K): string | null;
23
19
  /**
24
20
  * Returns the value associated with a key as a parsed JSON string.
25
21
  */
26
- getJson<T>(key: string): T | null;
22
+ getJson<K extends string, O = KvpObject<K>>(key: K extends ValidJsonKey<O> ? K : never): O extends string ? Schema[O] : null;
27
23
  /**
28
24
  * Sets the value associated with a key as a number.
29
25
  * @param async set the value using an async operation.
30
26
  */
31
- setNumber(key: string, value: number, async?: boolean): void;
27
+ setNumber<K extends string & keyof Schema>(key: K, value: number, async?: boolean): void;
32
28
  /**
33
29
  * Sets the value associated with a key as a float.
34
30
  * @param async set the value using an async operation.
35
31
  */
36
- setFloat(key: string, value: number, async?: boolean): void;
32
+ setFloat<K extends string & keyof Schema>(key: K, value: number, async?: boolean): void;
37
33
  /**
38
34
  * Sets the value associated with a key as a string.
39
35
  * @param async set the value using an async operation.
40
36
  */
41
- setString(key: string, value: string, async?: boolean): void;
37
+ setString<K extends string & keyof Schema>(key: K, value: string, async?: boolean): void;
42
38
  /**
43
39
  * Sets the value associated with a key as a JSON string.
44
40
  * @param async set the value using an async operation.
45
41
  */
46
- setJson(key: string, value: object, async?: boolean): void;
42
+ setJson<K extends string, O = KvpObject<K>>(key: K extends ValidJsonKey<O> ? K : never, value: O extends string ? Schema[O] : never, async?: boolean): void;
47
43
  /**
48
- * Returns the value associated with a key, determining the type using the declared Kvp schema.
49
- */
50
- get<K extends keyof T>(key: K): T[K] extends "number" | "float" ? number : T[K] extends object ? T[K] | null : string | null;
51
- /**
52
- * Sets the value associated with a key as a value, using its type from the declared Kvp stricture.
44
+ * Sets the value associated with a key as a JSON string.
53
45
  * @param async set the value using an async operation.
54
46
  */
55
- set<K extends string>(key: K extends keyof T ? K : never, value: T[K] extends "number" | "float" ? number : T[K] extends object ? T[K] | null : string | null, async?: boolean): void;
47
+ set<K extends string, O = KvpObject<K>>(key: K extends keyof Schema ? K : O extends string ? K : never, value: K extends keyof Schema ? Schema[K] : O extends string ? Schema[O] : never, async?: boolean): void;
56
48
  /**
57
49
  * Deletes the specified value for key.
58
50
  * @param async remove the value using an async operation
@@ -64,14 +56,14 @@ export declare class Kvp<T extends Schema> {
64
56
  * Should be called after calling set methods using the async flag.
65
57
  */
66
58
  flush(): void;
67
- private getAllKeys;
59
+ getAllKeys(prefix: string): (keyof Schema)[];
68
60
  /**
69
61
  * Returns an array of keys which match or contain the given keys.
70
62
  */
71
- getKeys(prefix: string | string[]): string[];
63
+ getKeys<K extends (string & keyof Schema) | string[]>(prefix: K): (keyof Schema)[];
72
64
  /**
73
65
  * Get all values from keys in an array as the specified type.
74
66
  */
75
- getValuesAsType(prefix: string[], type: "string" | "number" | "float" | "object"): unknown[];
67
+ getValuesAsType<K extends (string & keyof Schema) | (string & keyof Schema)[]>(prefix: K, type: any): (string | number | Schema[string] | null)[];
76
68
  }
77
69
  export {};
package/Kvp.js CHANGED
@@ -1,18 +1,4 @@
1
1
  export class Kvp {
2
- schema;
3
- constructor(schema) {
4
- this.schema = { ...schema };
5
- for (const key in this.schema) {
6
- if (typeof this.schema[key] === "object")
7
- this.schema[key] = Object;
8
- }
9
- }
10
- /**
11
- * Returns the type associated with a schema key.
12
- */
13
- getType(key) {
14
- return typeof this.schema[key] === "object" ? "object" : this.schema[key];
15
- }
16
2
  /**
17
3
  * Returns the value associated with a key as a number.
18
4
  */
@@ -74,38 +60,26 @@ export class Kvp {
74
60
  return async ? SetResourceKvpNoSync(key, str) : SetResourceKvp(key, str);
75
61
  }
76
62
  /**
77
- * Returns the value associated with a key, determining the type using the declared Kvp schema.
78
- */
79
- get(key) {
80
- const type = this.getType(key);
81
- switch (type) {
82
- case "number":
83
- return this.getNumber(key);
84
- case "float":
85
- return this.getFloat(key);
86
- case "object":
87
- return this.getJson(key);
88
- default:
89
- return this.getString(key);
90
- }
91
- }
92
- /**
93
- * Sets the value associated with a key as a value, using its type from the declared Kvp stricture.
63
+ * Sets the value associated with a key as a JSON string.
94
64
  * @param async set the value using an async operation.
95
65
  */
96
66
  set(key, value, async = false) {
97
- const type = this.getType(key);
98
- const valueType = typeof value;
99
- if (valueType !== type && type !== "float" && valueType === "number")
100
- throw new Error(`Expected '${key}' to be type '${type}' but received '${valueType}'`);
101
- switch (type) {
102
- case "number":
103
- return this.setNumber(key, value, async);
104
- case "float":
105
- return this.setFloat(key, value, async);
67
+ switch (typeof value) {
68
+ case "function":
69
+ case "symbol":
70
+ throw new Error(`Failed to set Kvp for invalid type '${typeof value}'`);
71
+ case "undefined":
72
+ return this.delete(key, async);
106
73
  case "object":
107
74
  return this.setJson(key, value, async);
75
+ case "boolean":
76
+ value = value ? 1 : 0;
77
+ case "number":
78
+ return Number.isInteger(value)
79
+ ? this.setNumber(key, value, async)
80
+ : this.setFloat(key, value, async);
108
81
  default:
82
+ value = String(value);
109
83
  return this.setString(key, value, async);
110
84
  }
111
85
  }
@@ -157,10 +131,10 @@ export class Kvp {
157
131
  return this.getNumber(key);
158
132
  case "float":
159
133
  return this.getFloat(key);
160
- case "object":
161
- return this.getJson(key);
162
- default:
134
+ case "string":
163
135
  return this.getString(key);
136
+ default:
137
+ return this.getJson(key);
164
138
  }
165
139
  });
166
140
  }
package/decors/Events.js CHANGED
@@ -34,7 +34,18 @@ export function Event(eventName) {
34
34
  context.addInitializer(function () {
35
35
  const t = this;
36
36
  on(eventName, (...args) => {
37
- return originalMethod.call(t, ...args);
37
+ try {
38
+ return originalMethod.call(t, ...args);
39
+ }
40
+ catch (e) {
41
+ REMOVE_EVENT_LOG: {
42
+ console.error(`------- EVENT ERROR --------`);
43
+ console.error(`Call to ${eventName} errored`);
44
+ console.error(`Data: ${JSON.stringify(args)}`);
45
+ console.error(`Error: ${e}`);
46
+ console.error(`------- END EVENT ERROR --------`);
47
+ }
48
+ }
38
49
  });
39
50
  });
40
51
  };
@@ -50,12 +61,25 @@ export function NetEvent(eventName, remoteOnly = false) {
50
61
  context.addInitializer(function () {
51
62
  const t = this;
52
63
  onNet(eventName, (...args) => {
53
- CLIENT: {
54
- if (remoteOnly && source !== 65535) {
55
- return;
64
+ const src = source;
65
+ try {
66
+ CLIENT: {
67
+ if (remoteOnly && source !== 65535) {
68
+ return;
69
+ }
70
+ }
71
+ return originalMethod.call(t, ...args);
72
+ }
73
+ catch (e) {
74
+ REMOVE_NET_EVENT_LOG: {
75
+ console.error(`------- NET EVENT ERROR --------`);
76
+ console.error(`Call to ${eventName} errored`);
77
+ console.error(`Caller: ${src}`);
78
+ console.error(`Data: ${JSON.stringify(args)}`);
79
+ console.error(`Error: ${e}`);
80
+ console.error(`------- END NET EVENT ERROR --------`);
56
81
  }
57
82
  }
58
- return originalMethod.call(t, ...args);
59
83
  });
60
84
  });
61
85
  };
package/package.json CHANGED
@@ -4,13 +4,13 @@
4
4
  "author": "Remco Troost <d0p3t>",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
- "version": "0.0.52",
7
+ "version": "0.0.53",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/nativewrappers/fivem.git"
10
+ "url": "https://github.com/nativewrappers/nativewrappers.git"
11
11
  },
12
12
  "bugs": {
13
- "url": "https://github.com/nativewrappers/fivem/issues"
13
+ "url": "https://github.com/nativewrappers/nativewrappers/issues"
14
14
  },
15
15
  "homepage": "https://fivemjs.avarian.dev/",
16
16
  "keywords": [