-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.d.ts
143 lines (128 loc) · 4.51 KB
/
index.d.ts
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
type ToBuffer = (buffer: Buffer, value: unknown, index: number) => Buffer;
type FromBuffer = (buffer: Buffer, index: number, returnLength: boolean) => unknown;
declare module "@athombv/data-types" {
interface DataTypeInterface {
id: number;
shortName: string;
length: number;
toBuffer: ToBuffer;
fromBuffer: FromBuffer;
args: unknown[];
defaultValue: unknown;
isAnalog: () => boolean;
inspect: () => string;
}
interface DataTypeConstructor {
new (
id: number,
shortName: string,
length: number,
toBuf: ToBuffer,
fromBuf: FromBuffer,
...args: unknown[]
): DataTypeInterface;
}
interface DataTypeFunctionConstructor {
(...args: unknown[]): DataTypeConstructor;
}
type DataTypeItem =
| DataTypeConstructor
| DataTypeFunctionConstructor
| { [name: string]: DataTypeItem }; // This OR is needed for Structs with a second level of DataTypes
type GenericMap<T> = {
[K in keyof T]: DataTypeItem;
};
interface StructTypeInterface<T> {
toJSON: () => T;
toBuffer: (buffer?: Buffer, index?: number) => Buffer;
}
export interface StructInstance<StructType> {
fromBuffer: (buffer: Buffer) => StructType & StructTypeInterface<StructType>;
toBuffer: (buffer: Buffer, object: StructType, index?: number) => Buffer;
fields: GenericMap<StructType>;
name: string;
length: number;
fromJSON: (object: StructType) => StructType & StructTypeInterface<StructType>;
fromArgs: (...args: unknown[]) => StructType & StructTypeInterface<StructType>;
}
export const DataTypes: {
noData: DataTypeConstructor;
data8: DataTypeConstructor;
data16: DataTypeConstructor;
data24: DataTypeConstructor;
data32: DataTypeConstructor;
data40: DataTypeConstructor;
data48: DataTypeConstructor;
data56: DataTypeConstructor;
data64: DataTypeConstructor;
bool: DataTypeConstructor;
map8: DataTypeFunctionConstructor;
map16: DataTypeFunctionConstructor;
map24: DataTypeFunctionConstructor;
map32: DataTypeFunctionConstructor;
map40: DataTypeFunctionConstructor;
map48: DataTypeFunctionConstructor;
map56: DataTypeFunctionConstructor;
map64: DataTypeFunctionConstructor;
uint8: DataTypeConstructor;
uint16: DataTypeConstructor;
uint24: DataTypeConstructor;
uint32: DataTypeConstructor;
uint40: DataTypeConstructor;
uint48: DataTypeConstructor;
int8: DataTypeConstructor;
int16: DataTypeConstructor;
int24: DataTypeConstructor;
int32: DataTypeConstructor;
int40: DataTypeConstructor;
int48: DataTypeConstructor;
enum8: DataTypeFunctionConstructor;
enum16: DataTypeFunctionConstructor;
enum32: DataTypeFunctionConstructor;
single: DataTypeConstructor;
double: DataTypeConstructor;
octstr: DataTypeConstructor;
string: DataTypeConstructor;
EUI48: DataTypeConstructor;
EUI64: DataTypeConstructor;
key128: DataTypeConstructor;
uint4: DataTypeConstructor;
enum4: DataTypeFunctionConstructor;
map4: DataTypeFunctionConstructor;
buffer: DataTypeConstructor;
buffer8: DataTypeConstructor;
buffer16: DataTypeConstructor;
Array0: DataTypeFunctionConstructor;
Array8: DataTypeFunctionConstructor;
FixedString: DataTypeFunctionConstructor;
};
export const DataType: DataTypeInterface;
export function Struct<StructType>(
name: string,
objectDefinition: GenericMap<StructType>
): StructInstance<StructType>;
}
/*
How to use @athombv/data-types in TypeScript:
// Create a type that represents the Struct data
type ZdoEndDeviceAnnounceIndication = {
srcAddr: number;
IEEEAddr: string;
};
// Create a Struct instance with generic type ZdoEndDeviceAnnounceIndication
const ZdoEndDeviceAnnounceIndicationStruct =
Struct<ZdoEndDeviceAnnounceIndication>("ZdoEndDeviceAnnounceIndication", {
srcAddr: DataTypes.uint16,
IEEEAddr: DataTypes.EUI64,
});
// Create ZdoEndDeviceAnnounceIndication object
const ZdoEndDeviceAnnounceObject = ZdoEndDeviceAnnounceIndicationStruct.fromBuffer(
Buffer.from([0, 1, 2, 3])
);
ZdoEndDeviceAnnounceObject.srcAddr.trim(); // This errors, srcAddr is not a string
// Create Buffer instance from ZdoEndDeviceAnnounceObject
const ZdoEndDeviceAnnounceBuffer = ZdoEndDeviceAnnounceIndicationStruct.toBuffer({ srcAddr: 1, IEEAddr: 'abc' }); // This errors due to typo in IEEEAddr name
Known limitations:
- Structs in Structs are considered a no-go by these definitions.
- DataTypes have no related JS type, so a few unknowns are used.
*/