@open-wa/schema 5.0.0 → 5.1.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/generated/index.cjs +8 -2
- package/dist/generated/index.cjs.map +1 -1
- package/dist/generated/index.d.cts +715 -1
- package/dist/generated/index.d.cts.map +1 -1
- package/dist/generated/index.d.mts +715 -1
- package/dist/generated/index.d.mts.map +1 -1
- package/dist/generated/index.mjs +8 -2
- package/dist/generated/index.mjs.map +1 -1
- package/dist/{index-ChYFQM1K.d.cts → index-DTztyYhw.d.cts} +893 -3
- package/dist/index-DTztyYhw.d.cts.map +1 -0
- package/dist/{index-BHcd0h7x.d.mts → index-nVKR0RmZ.d.mts} +893 -3
- package/dist/index-nVKR0RmZ.d.mts.map +1 -0
- package/dist/index.cjs +51 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +24 -2
- package/dist/index.mjs.map +1 -1
- package/dist/methods/index.cjs +5 -1
- package/dist/methods/index.d.cts +2 -2
- package/dist/methods/index.d.mts +2 -2
- package/dist/methods/index.mjs +2 -2
- package/dist/{methods-CZqeUhKy.mjs → methods-BSv5O3bS.mjs} +317 -2
- package/dist/methods-BSv5O3bS.mjs.map +1 -0
- package/dist/{methods-Dizm7Fn9.cjs → methods-DO0JONvB.cjs} +472 -1
- package/dist/methods-DO0JONvB.cjs.map +1 -0
- package/package.json +4 -3
- package/src/__tests__/generators.contract.test.ts +181 -0
- package/src/__tests__/registry.contract.test.ts +81 -0
- package/src/client-config.ts +30 -0
- package/src/codecs.test.ts +41 -0
- package/src/codecs.ts +191 -0
- package/src/common-types.ts +280 -0
- package/src/config.ts +47 -0
- package/src/enums.ts +63 -0
- package/src/events/groups.ts +33 -0
- package/src/events/index.ts +4 -0
- package/src/events/messaging.ts +86 -0
- package/src/events/registry.ts +124 -0
- package/src/events/state.ts +59 -0
- package/src/generated/AliasMap.ts +216 -0
- package/src/generated/BaseClient.ts +878 -0
- package/src/generated/BaseNamespacedClient.ts +214 -0
- package/src/generated/index.ts +5 -0
- package/src/generated/openapi.json +8949 -0
- package/src/generated/types.ts +383 -0
- package/src/http-manifest.ts +97 -0
- package/src/implementor.test.ts +88 -0
- package/src/implementor.ts +75 -0
- package/src/index.ts +44 -0
- package/src/interactive.ts +151 -0
- package/src/methods/business.ts +51 -0
- package/src/methods/chats.ts +263 -0
- package/src/methods/communities.ts +119 -0
- package/src/methods/contacts.ts +158 -0
- package/src/methods/groups.ts +376 -0
- package/src/methods/index.ts +34 -0
- package/src/methods/interactive.ts +21 -0
- package/src/methods/labels.ts +86 -0
- package/src/methods/media.ts +36 -0
- package/src/methods/messaging.ts +828 -0
- package/src/methods/session.ts +261 -0
- package/src/methods/status.ts +120 -0
- package/src/parameters.ts +237 -0
- package/src/registry.test.ts +30 -0
- package/src/registry.ts +601 -0
- package/dist/index-BHcd0h7x.d.mts.map +0 -1
- package/dist/index-ChYFQM1K.d.cts.map +0 -1
- package/dist/methods-CZqeUhKy.mjs.map +0 -1
- package/dist/methods-Dizm7Fn9.cjs.map +0 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { clientRegistry } from '../registry';
|
|
2
|
+
import '../methods'; // Trigger registration
|
|
3
|
+
|
|
4
|
+
describe('clientRegistry contract', () => {
|
|
5
|
+
describe('registration', () => {
|
|
6
|
+
it('should have registered methods', () => {
|
|
7
|
+
const methods = clientRegistry.getAll();
|
|
8
|
+
expect(methods.length).toBeGreaterThan(100); // We have 121 methods
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should have unique method names', () => {
|
|
12
|
+
const names = clientRegistry.getNames();
|
|
13
|
+
const uniqueNames = new Set(names);
|
|
14
|
+
expect(uniqueNames.size).toBe(names.length);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should have required metadata for each method', () => {
|
|
18
|
+
const methods = clientRegistry.getAll();
|
|
19
|
+
|
|
20
|
+
methods.forEach((def) => {
|
|
21
|
+
expect(def.meta.functionName).toBeTruthy();
|
|
22
|
+
expect(def.meta.parameterOrder).toBeInstanceOf(Array);
|
|
23
|
+
expect(def.meta.inputSchema).toBeDefined();
|
|
24
|
+
expect(def.meta.outputSchema).toBeDefined();
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('lookup', () => {
|
|
30
|
+
it('should find method by name', () => {
|
|
31
|
+
const sendText = clientRegistry.get('sendText');
|
|
32
|
+
expect(sendText).toBeDefined();
|
|
33
|
+
expect(sendText?.meta.functionName).toBe('sendText');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should find method by schema', () => {
|
|
37
|
+
const sendTextDef = clientRegistry.get('sendText');
|
|
38
|
+
expect(sendTextDef).toBeDefined();
|
|
39
|
+
|
|
40
|
+
const foundBySchema = clientRegistry.getBySchema(sendTextDef!.schema);
|
|
41
|
+
expect(foundBySchema?.meta.functionName).toBe('sendText');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return undefined for non-existent method', () => {
|
|
45
|
+
const notFound = clientRegistry.get('thisMethodDoesNotExist');
|
|
46
|
+
expect(notFound).toBeUndefined();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('namespaces', () => {
|
|
51
|
+
it('should have methods in messaging namespace', () => {
|
|
52
|
+
const messagingMethods = clientRegistry.getByNamespace('messages');
|
|
53
|
+
expect(messagingMethods.length).toBeGreaterThan(0);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should have methods in chats namespace', () => {
|
|
57
|
+
const chatMethods = clientRegistry.getByNamespace('chats');
|
|
58
|
+
expect(chatMethods.length).toBeGreaterThan(0);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('critical methods exist', () => {
|
|
63
|
+
const criticalMethods = [
|
|
64
|
+
'sendText',
|
|
65
|
+
'sendImage',
|
|
66
|
+
'sendFile',
|
|
67
|
+
'getChat',
|
|
68
|
+
'getAllChats',
|
|
69
|
+
'getContact',
|
|
70
|
+
'getAllContacts',
|
|
71
|
+
'getMe',
|
|
72
|
+
'getConnectionState',
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
criticalMethods.forEach((name) => {
|
|
76
|
+
it(`should have ${name} registered`, () => {
|
|
77
|
+
expect(clientRegistry.has(name)).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client configuration types - re-exported from @open-wa/config
|
|
3
|
+
*
|
|
4
|
+
* @deprecated Import from '@open-wa/config' instead
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export { ConfigSchema as ClientConfigSchema, type Config as ClientConfig } from '@open-wa/config';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Migrates a v4 configuration object to the v5 schema format.
|
|
11
|
+
* Currently, most v4 options are compatible with v5.
|
|
12
|
+
* This function serves as a placeholder for any future transformations.
|
|
13
|
+
*
|
|
14
|
+
* @deprecated This is a legacy compatibility function. Use @open-wa/config directly.
|
|
15
|
+
*/
|
|
16
|
+
export function migrateV4Config(oldConfig: Record<string, unknown>): Record<string, unknown> {
|
|
17
|
+
// Deep clone to avoid mutating original
|
|
18
|
+
const config = JSON.parse(JSON.stringify(oldConfig));
|
|
19
|
+
|
|
20
|
+
// Example migration: if 'authTimeout' was string in some legacy version (unlikely but example)
|
|
21
|
+
if (typeof config.authTimeout === 'string') {
|
|
22
|
+
config.authTimeout = parseInt(config.authTimeout, 10);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (config.apiLifecycle === undefined) {
|
|
26
|
+
config.apiLifecycle = 'hybrid';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return config;
|
|
30
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { chatIdCodec, contactIdCodec, groupIdCodec } from './codecs';
|
|
2
|
+
|
|
3
|
+
describe('Zod Codecs', () => {
|
|
4
|
+
describe('chatIdCodec', () => {
|
|
5
|
+
it('should convert regular phone number to @c.us', () => {
|
|
6
|
+
expect(chatIdCodec.decode('447123456789')).toBe('447123456789@c.us');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('should convert 18-digit number to @g.us', () => {
|
|
10
|
+
expect(chatIdCodec.decode('123456789012345678')).toBe('123456789012345678@g.us');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should convert 14-digit non-Indonesian to @lid', () => {
|
|
14
|
+
expect(chatIdCodec.decode('12345678901234')).toBe('12345678901234@lid');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should convert 14-digit Indonesian to @c.us', () => {
|
|
18
|
+
expect(chatIdCodec.decode('62812345678901')).toBe('62812345678901@c.us');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should pass through already formatted IDs', () => {
|
|
22
|
+
expect(chatIdCodec.decode('447123456789@c.us')).toBe('447123456789@c.us');
|
|
23
|
+
expect(chatIdCodec.decode('123456789012345678@g.us')).toBe('123456789012345678@g.us');
|
|
24
|
+
expect(chatIdCodec.decode('12345678901234@lid')).toBe('12345678901234@lid');
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('contactIdCodec', () => {
|
|
29
|
+
it('should always convert to @c.us', () => {
|
|
30
|
+
expect(contactIdCodec.decode('447123456789')).toBe('447123456789@c.us');
|
|
31
|
+
expect(contactIdCodec.decode('447123456789@c.us')).toBe('447123456789@c.us');
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('groupIdCodec', () => {
|
|
36
|
+
it('should always convert to @g.us', () => {
|
|
37
|
+
expect(groupIdCodec.decode('123456789012345678')).toBe('123456789012345678@g.us');
|
|
38
|
+
expect(groupIdCodec.decode('123456789012345678@g.us')).toBe('123456789012345678@g.us');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
package/src/codecs.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { assertFile, getDUrl } from '@open-wa/utils';
|
|
3
|
+
|
|
4
|
+
const chatIdPattern = /^\d+(-\d+)?@(c|g)\.us$|^\d+@lid$/;
|
|
5
|
+
const contactIdPattern = /^\d+@c\.us$/;
|
|
6
|
+
const groupIdPattern = /^\d+(-\d+)?@g\.us$/;
|
|
7
|
+
const messageIdPattern = /^(true|false)_.+_.+$/;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Chat ID Codec
|
|
11
|
+
* Intelligently converts phone numbers to proper WhatsApp chat IDs.
|
|
12
|
+
*/
|
|
13
|
+
export const chatIdCodec = {
|
|
14
|
+
decode: (input: string): string => {
|
|
15
|
+
if (input.includes('@')) {
|
|
16
|
+
return input;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const digits = input.replace(/\D/g, '');
|
|
20
|
+
const digitCount = digits.length;
|
|
21
|
+
|
|
22
|
+
if (digitCount === 18) {
|
|
23
|
+
return `${digits}@g.us`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (digitCount === 14 && !digits.startsWith('62')) {
|
|
27
|
+
return `${digits}@lid`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return `${digits}@c.us`;
|
|
31
|
+
},
|
|
32
|
+
encode: (chatId: string): string => chatId,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Contact ID Codec - always formats as @c.us
|
|
37
|
+
*/
|
|
38
|
+
export const contactIdCodec = {
|
|
39
|
+
decode: (input: string): string => {
|
|
40
|
+
if (input.endsWith('@c.us')) {
|
|
41
|
+
return input;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return `${input.replace(/\D/g, '')}@c.us`;
|
|
45
|
+
},
|
|
46
|
+
encode: (contactId: string): string => contactId,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Group ID Codec - always formats as @g.us
|
|
51
|
+
*/
|
|
52
|
+
export const groupIdCodec = {
|
|
53
|
+
decode: (input: string): string => {
|
|
54
|
+
if (input.endsWith('@g.us')) {
|
|
55
|
+
return input;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return `${input.replace(/\D/g, '')}@g.us`;
|
|
59
|
+
},
|
|
60
|
+
encode: (groupId: string): string => groupId,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Message ID Codec - validates format only
|
|
65
|
+
*/
|
|
66
|
+
export const messageIdCodec = {
|
|
67
|
+
decode: (input: string): string => input,
|
|
68
|
+
encode: (messageId: string): string => messageId,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* File Codec Factory
|
|
73
|
+
*/
|
|
74
|
+
export const createFileCodec = (outputType: string) => ({
|
|
75
|
+
decode: async (input: unknown): Promise<unknown> => assertFile(input, outputType),
|
|
76
|
+
encode: (output: unknown) => output,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
export const fileToDataUrlCodec = createFileCodec('DATA_URL');
|
|
80
|
+
export const fileToBase64Codec = createFileCodec('BASE_64');
|
|
81
|
+
export const fileToBufferCodec = createFileCodec('BUFFER');
|
|
82
|
+
export const fileToTempPathCodec = createFileCodec('TEMP_FILE_PATH');
|
|
83
|
+
export const fileToStreamCodec = createFileCodec('READ_STREAM');
|
|
84
|
+
|
|
85
|
+
export const base64ToDataUrlCodec = {
|
|
86
|
+
decode: (base64: string): string => {
|
|
87
|
+
if (base64.startsWith('data:')) {
|
|
88
|
+
return base64;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return `data:application/octet-stream;base64,${base64}`;
|
|
92
|
+
},
|
|
93
|
+
encode: (dataUrl: string): string => dataUrl.split(',')[1] || dataUrl,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const urlToDataUrlCodec = {
|
|
97
|
+
decode: async (url: string): Promise<string> => getDUrl(url),
|
|
98
|
+
encode: (_dataUrl: string): string => {
|
|
99
|
+
throw new Error('Cannot encode DataURL back to URL');
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const isoDatetimeToDate = {
|
|
104
|
+
decode: (isoString: string): Date => new Date(isoString),
|
|
105
|
+
encode: (date: Date): string => date.toISOString(),
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const unixSecondsToDate = {
|
|
109
|
+
decode: (seconds: number): Date => new Date(seconds * 1000),
|
|
110
|
+
encode: (date: Date): number => Math.floor(date.getTime() / 1000),
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const unixMillisToDate = {
|
|
114
|
+
decode: (millis: number): Date => new Date(millis),
|
|
115
|
+
encode: (date: Date): number => date.getTime(),
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const hexColorCodec = {
|
|
119
|
+
decode: (input: string): string => input.replace('#', '').toUpperCase(),
|
|
120
|
+
encode: (hex: string): string => `#${hex}`,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const jsonStringCodec = <T>(schema: z.ZodType<T>) =>
|
|
124
|
+
z.codec(z.string(), schema, {
|
|
125
|
+
decode: (jsonString: string): T => schema.parse(JSON.parse(jsonString)),
|
|
126
|
+
encode: (value: T): string => JSON.stringify(value),
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
export const ChatIdSchema = z.codec(
|
|
130
|
+
z.string(),
|
|
131
|
+
z.string().regex(chatIdPattern).brand('ChatId'),
|
|
132
|
+
chatIdCodec
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
export const ContactIdSchema = z.codec(
|
|
136
|
+
z.string(),
|
|
137
|
+
z.string().regex(contactIdPattern).brand('ContactId'),
|
|
138
|
+
contactIdCodec
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
export const GroupIdSchema = z.codec(
|
|
142
|
+
z.string(),
|
|
143
|
+
z.string().regex(groupIdPattern).brand('GroupChatId'),
|
|
144
|
+
groupIdCodec
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
export const MessageIdSchema = z.codec(
|
|
148
|
+
z.string(),
|
|
149
|
+
z.string().regex(messageIdPattern).brand('MessageId'),
|
|
150
|
+
messageIdCodec
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
export const ChatIdCodecSchema = ChatIdSchema;
|
|
154
|
+
export const ContactIdCodecSchema = ContactIdSchema;
|
|
155
|
+
export const GroupIdCodecSchema = GroupIdSchema;
|
|
156
|
+
export const MessageIdCodecSchema = MessageIdSchema;
|
|
157
|
+
|
|
158
|
+
export const FileToDataUrlSchema = z
|
|
159
|
+
.union([z.string(), z.instanceof(Buffer)])
|
|
160
|
+
.transform(async (input) => fileToDataUrlCodec.decode(input))
|
|
161
|
+
.pipe(z.string().regex(/^data:.+;base64,.+$/).brand('DataURL'));
|
|
162
|
+
|
|
163
|
+
export const Base64ToDataUrlSchema = z.codec(
|
|
164
|
+
z.string(),
|
|
165
|
+
z.string().regex(/^data:.+;base64,.+$/).brand('DataURL'),
|
|
166
|
+
base64ToDataUrlCodec
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
export const HexColorSchema = z.codec(
|
|
170
|
+
z.string(),
|
|
171
|
+
z.string().regex(/^[0-9A-F]+$/),
|
|
172
|
+
hexColorCodec
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
export const IsoDatetimeToDateSchema = z.codec(
|
|
176
|
+
z.iso.datetime(),
|
|
177
|
+
z.date(),
|
|
178
|
+
isoDatetimeToDate
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
export const UnixSecondsToDateSchema = z.codec(
|
|
182
|
+
z.number().int().min(0),
|
|
183
|
+
z.date(),
|
|
184
|
+
unixSecondsToDate
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
export const UnixMillisToDateSchema = z.codec(
|
|
188
|
+
z.number().int().min(0),
|
|
189
|
+
z.date(),
|
|
190
|
+
unixMillisToDate
|
|
191
|
+
);
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// Aliases
|
|
4
|
+
export const ContactIdSchema = z.string().brand('ContactId');
|
|
5
|
+
export type ContactId = z.infer<typeof ContactIdSchema>;
|
|
6
|
+
|
|
7
|
+
export const ChatIdSchema = z.string().brand('ChatId');
|
|
8
|
+
export type ChatId = z.infer<typeof ChatIdSchema>;
|
|
9
|
+
|
|
10
|
+
export const MessageIdSchema = z.string().brand('MessageId');
|
|
11
|
+
export type MessageId = z.infer<typeof MessageIdSchema>;
|
|
12
|
+
|
|
13
|
+
export const GroupChatIdSchema = z.string().brand('GroupChatId');
|
|
14
|
+
export type GroupChatId = z.infer<typeof GroupChatIdSchema>;
|
|
15
|
+
|
|
16
|
+
export const GroupIdSchema = z.string().brand('GroupId');
|
|
17
|
+
export type GroupId = z.infer<typeof GroupIdSchema>;
|
|
18
|
+
|
|
19
|
+
export type DataURL = `data:${string};base64,${string}`;
|
|
20
|
+
export type Base64 = string;
|
|
21
|
+
export type Content = string | DataURL | Base64 | Buffer;
|
|
22
|
+
|
|
23
|
+
export const GroupMetadataSchema = z.object({
|
|
24
|
+
id: GroupIdSchema,
|
|
25
|
+
subject: z.string(),
|
|
26
|
+
creation: z.number(),
|
|
27
|
+
owner: ContactIdSchema.optional(),
|
|
28
|
+
desc: z.string().optional(),
|
|
29
|
+
descTime: z.number().optional(),
|
|
30
|
+
descOwner: ContactIdSchema.optional(),
|
|
31
|
+
restrict: z.boolean().optional(),
|
|
32
|
+
announce: z.boolean().optional(),
|
|
33
|
+
participants: z.array(z.object({
|
|
34
|
+
id: ContactIdSchema,
|
|
35
|
+
isAdmin: z.boolean(),
|
|
36
|
+
isSuperAdmin: z.boolean().optional(),
|
|
37
|
+
})),
|
|
38
|
+
}).passthrough();
|
|
39
|
+
export type GroupMetadata = z.infer<typeof GroupMetadataSchema>;
|
|
40
|
+
|
|
41
|
+
// Id
|
|
42
|
+
export const IdSchema = z.object({
|
|
43
|
+
server: z.string(),
|
|
44
|
+
user: z.string(),
|
|
45
|
+
_serialized: z.string(),
|
|
46
|
+
});
|
|
47
|
+
export type Id = z.infer<typeof IdSchema>;
|
|
48
|
+
|
|
49
|
+
// Enums
|
|
50
|
+
export enum MessageTypes {
|
|
51
|
+
TEXT = 'chat',
|
|
52
|
+
AUDIO = 'audio',
|
|
53
|
+
VOICE = 'ptt',
|
|
54
|
+
IMAGE = 'image',
|
|
55
|
+
VIDEO = 'video',
|
|
56
|
+
DOCUMENT = 'document',
|
|
57
|
+
STICKER = 'sticker',
|
|
58
|
+
LOCATION = 'location',
|
|
59
|
+
CONTACT_CARD = 'vcard',
|
|
60
|
+
CONTACT_CARD_MULTI = 'multi_vcard',
|
|
61
|
+
REVOKED = 'revoked',
|
|
62
|
+
ORDER = 'order',
|
|
63
|
+
BUTTONS_RESPONSE = 'buttons_response',
|
|
64
|
+
LIST_RESPONSE = "list_response",
|
|
65
|
+
UNKNOWN = 'unknown'
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export enum MessageAck {
|
|
69
|
+
ACK_ERROR = -1,
|
|
70
|
+
ACK_PENDING = 0,
|
|
71
|
+
ACK_SERVER = 1,
|
|
72
|
+
ACK_DEVICE = 2,
|
|
73
|
+
ACK_READ = 3,
|
|
74
|
+
ACK_PLAYED = 4,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* An enum of all the "simple listeners". A simple listener is a listener that just takes one parameter which is the callback function to handle the event.
|
|
79
|
+
*/
|
|
80
|
+
export enum SimpleListener {
|
|
81
|
+
/**
|
|
82
|
+
* Represents [[onMessage]]
|
|
83
|
+
*/
|
|
84
|
+
Message = 'onMessage',
|
|
85
|
+
/**
|
|
86
|
+
* Represents [[onAnyMessage]]
|
|
87
|
+
*/
|
|
88
|
+
AnyMessage = 'onAnyMessage',
|
|
89
|
+
/**
|
|
90
|
+
* Represents [[onMessageDeleted]]
|
|
91
|
+
*/
|
|
92
|
+
MessageDeleted = 'onMessageDeleted',
|
|
93
|
+
/**
|
|
94
|
+
* Represents [[onAck]]
|
|
95
|
+
*/
|
|
96
|
+
Ack = 'onAck',
|
|
97
|
+
/**
|
|
98
|
+
* Represents [[onAddedToGroup]]
|
|
99
|
+
*/
|
|
100
|
+
AddedToGroup = 'onAddedToGroup',
|
|
101
|
+
/**
|
|
102
|
+
* Represents [[onChatDeleted]]
|
|
103
|
+
*/
|
|
104
|
+
ChatDeleted = 'onChatDeleted',
|
|
105
|
+
/**
|
|
106
|
+
* Represents [[onBattery]]
|
|
107
|
+
*/
|
|
108
|
+
Battery = 'onBattery',
|
|
109
|
+
/**
|
|
110
|
+
* Represents [[onChatOpened]]
|
|
111
|
+
*/
|
|
112
|
+
ChatOpened = 'onChatOpened',
|
|
113
|
+
/**
|
|
114
|
+
* Represents [[onIncomingCall]]
|
|
115
|
+
*/
|
|
116
|
+
IncomingCall = 'onIncomingCall',
|
|
117
|
+
/**
|
|
118
|
+
* Represents [[onGlobalParticipantsChanged]]
|
|
119
|
+
*/
|
|
120
|
+
GlobalParticipantsChanged = 'onGlobalParticipantsChanged',
|
|
121
|
+
/**
|
|
122
|
+
* Represents [[onChatState]]
|
|
123
|
+
*/
|
|
124
|
+
ChatState = 'onChatState',
|
|
125
|
+
/**
|
|
126
|
+
* Represents [[onLogout]]
|
|
127
|
+
*/
|
|
128
|
+
Logout = 'onLogout',
|
|
129
|
+
/**
|
|
130
|
+
* Represents [[onPlugged]]
|
|
131
|
+
*/
|
|
132
|
+
Plugged = 'onPlugged',
|
|
133
|
+
/**
|
|
134
|
+
* Represents [[onStateChanged]]
|
|
135
|
+
*/
|
|
136
|
+
StateChanged = 'onStateChanged',
|
|
137
|
+
/**
|
|
138
|
+
* Represents [[onButton]]
|
|
139
|
+
*/
|
|
140
|
+
Button = 'onButton',
|
|
141
|
+
/**
|
|
142
|
+
* Requires licence
|
|
143
|
+
* Represents [[onStory]]
|
|
144
|
+
*/
|
|
145
|
+
Story = 'onStory',
|
|
146
|
+
/**
|
|
147
|
+
* Requires licence
|
|
148
|
+
* Represents [[onRemovedFromGroup]]
|
|
149
|
+
*/
|
|
150
|
+
RemovedFromGroup = 'onRemovedFromGroup',
|
|
151
|
+
/**
|
|
152
|
+
* Requires licence
|
|
153
|
+
* Represents [[onContactAdded]]
|
|
154
|
+
*/
|
|
155
|
+
ContactAdded = 'onContactAdded',
|
|
156
|
+
/**
|
|
157
|
+
* Represents [[onOrder]]
|
|
158
|
+
*/
|
|
159
|
+
Order = 'onOrder',
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Forward declarations
|
|
163
|
+
const MessageSchemaBase = z.object({
|
|
164
|
+
id: MessageIdSchema,
|
|
165
|
+
body: z.string(),
|
|
166
|
+
type: z.nativeEnum(MessageTypes),
|
|
167
|
+
t: z.number(),
|
|
168
|
+
notifyName: z.string().optional(),
|
|
169
|
+
from: ChatIdSchema,
|
|
170
|
+
to: ChatIdSchema,
|
|
171
|
+
self: z.enum(['in', 'out']),
|
|
172
|
+
ack: z.nativeEnum(MessageAck),
|
|
173
|
+
invis: z.boolean().optional(),
|
|
174
|
+
isNewMsg: z.boolean().optional(),
|
|
175
|
+
star: z.boolean().optional(),
|
|
176
|
+
recvFresh: z.boolean().optional(),
|
|
177
|
+
broadcast: z.boolean().optional(),
|
|
178
|
+
isForwarded: z.boolean().optional(),
|
|
179
|
+
labels: z.array(z.string()).optional(),
|
|
180
|
+
mentionedJidList: z.array(ContactIdSchema).optional(),
|
|
181
|
+
caption: z.string().optional(),
|
|
182
|
+
sender: z.any(), // Circular reference handled later/lazy
|
|
183
|
+
timestamp: z.number(),
|
|
184
|
+
content: z.string(),
|
|
185
|
+
isGroupMsg: z.boolean(),
|
|
186
|
+
isMMS: z.boolean().optional(),
|
|
187
|
+
isMedia: z.boolean(),
|
|
188
|
+
isNotification: z.boolean(),
|
|
189
|
+
isPSA: z.boolean().optional(),
|
|
190
|
+
fromMe: z.boolean(),
|
|
191
|
+
chat: z.any(), // Circular reference handled later/lazy
|
|
192
|
+
chatId: ChatIdSchema,
|
|
193
|
+
author: z.string().optional(),
|
|
194
|
+
clientUrl: z.string().optional(),
|
|
195
|
+
deprecatedMms3Url: z.string().optional(),
|
|
196
|
+
isQuotedMsgAvailable: z.boolean(),
|
|
197
|
+
quotedMsg: z.any().optional(), // z.lazy(() => MessageSchema.optional()),
|
|
198
|
+
quotedMsgObj: z.any().optional(), // z.lazy(() => MessageSchema.optional()),
|
|
199
|
+
senderId: z.string().optional(),
|
|
200
|
+
}).passthrough();
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
// Contact
|
|
204
|
+
export const ContactSchema = z.object({
|
|
205
|
+
id: ContactIdSchema,
|
|
206
|
+
name: z.string().optional(),
|
|
207
|
+
shortName: z.string().optional(),
|
|
208
|
+
pushname: z.string().optional(),
|
|
209
|
+
formattedName: z.string().optional(),
|
|
210
|
+
isBusiness: z.boolean().optional(),
|
|
211
|
+
isEnterprise: z.boolean().optional(),
|
|
212
|
+
isMe: z.boolean().optional(),
|
|
213
|
+
isMyContact: z.boolean().optional(),
|
|
214
|
+
isPSA: z.boolean().optional(),
|
|
215
|
+
isUser: z.boolean().optional(),
|
|
216
|
+
isWAContact: z.boolean().optional(),
|
|
217
|
+
labels: z.array(z.string()).optional(),
|
|
218
|
+
msgs: z.array(z.any()).optional(), // Avoid infinite recursion
|
|
219
|
+
profilePicThumbObj: z.object({
|
|
220
|
+
eurl: z.string().optional(),
|
|
221
|
+
id: IdSchema.optional(),
|
|
222
|
+
img: z.string().optional(),
|
|
223
|
+
imgFull: z.string().optional(),
|
|
224
|
+
tag: z.string().optional(),
|
|
225
|
+
}).optional(),
|
|
226
|
+
statusMute: z.boolean().optional(),
|
|
227
|
+
type: z.string().optional(),
|
|
228
|
+
verifiedLevel: z.number().optional(),
|
|
229
|
+
verifiedName: z.string().optional(),
|
|
230
|
+
isOnline: z.boolean().optional(),
|
|
231
|
+
lastSeen: z.number().optional(),
|
|
232
|
+
}).passthrough();
|
|
233
|
+
|
|
234
|
+
export type Contact = z.infer<typeof ContactSchema>;
|
|
235
|
+
|
|
236
|
+
// Location
|
|
237
|
+
export const LocationSchema = z.object({
|
|
238
|
+
lat: z.number().describe('Latitude coordinate'),
|
|
239
|
+
lng: z.number().describe('Longitude coordinate'),
|
|
240
|
+
name: z.string().optional().describe('Location name'),
|
|
241
|
+
address: z.string().optional().describe('Formatted address'),
|
|
242
|
+
url: z.string().optional().describe('WhatsApp location URL'),
|
|
243
|
+
}).passthrough();
|
|
244
|
+
|
|
245
|
+
export type Location = z.infer<typeof LocationSchema>;
|
|
246
|
+
|
|
247
|
+
// Chat
|
|
248
|
+
export const ChatSchema = z.object({
|
|
249
|
+
id: ContactIdSchema.or(GroupChatIdSchema),
|
|
250
|
+
name: z.string().optional(),
|
|
251
|
+
formattedTitle: z.string().optional(),
|
|
252
|
+
isGroup: z.boolean(),
|
|
253
|
+
contact: ContactSchema,
|
|
254
|
+
groupMetadata: z.any().optional(), // Metadata
|
|
255
|
+
presence: z.any().optional(),
|
|
256
|
+
t: z.number().optional(),
|
|
257
|
+
unreadCount: z.number().optional(),
|
|
258
|
+
lastReceivedKey: z.any().optional(),
|
|
259
|
+
msgs: z.array(z.any()).optional(), // Avoid infinite recursion
|
|
260
|
+
isReadOnly: z.boolean().optional(),
|
|
261
|
+
muteExpiration: z.number().optional(),
|
|
262
|
+
notSpam: z.boolean().optional(),
|
|
263
|
+
pin: z.number().optional(),
|
|
264
|
+
ack: z.any().optional(),
|
|
265
|
+
}).passthrough();
|
|
266
|
+
|
|
267
|
+
export type Chat = z.infer<typeof ChatSchema>;
|
|
268
|
+
|
|
269
|
+
// Full Message Schema with implementations
|
|
270
|
+
export const MessageSchema = MessageSchemaBase.extend({
|
|
271
|
+
sender: ContactSchema,
|
|
272
|
+
chat: ChatSchema,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
export type Message = z.infer<typeof MessageSchema>;
|
|
276
|
+
|
|
277
|
+
// Additional return types
|
|
278
|
+
export const MessageIdReturnSchema = z.object({
|
|
279
|
+
_serialized: z.string(),
|
|
280
|
+
}).passthrough();
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration schema and types - re-exported from @open-wa/config
|
|
3
|
+
*
|
|
4
|
+
* This file provides backward compatibility for code that imports from @open-wa/schema.
|
|
5
|
+
* New code should import directly from @open-wa/config.
|
|
6
|
+
*
|
|
7
|
+
* @deprecated Import from '@open-wa/config' instead
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// Re-export everything from @open-wa/config
|
|
11
|
+
export {
|
|
12
|
+
// Enums
|
|
13
|
+
QRFormat,
|
|
14
|
+
CLOUD_PROVIDERS,
|
|
15
|
+
DIRECTORY_STRATEGY,
|
|
16
|
+
NotificationLanguage,
|
|
17
|
+
OnError,
|
|
18
|
+
LicenseType,
|
|
19
|
+
// Sub-schemas
|
|
20
|
+
SessionDataSchema,
|
|
21
|
+
DevToolsSchema,
|
|
22
|
+
ProxyServerCredentialsSchema,
|
|
23
|
+
CloudUploadOptionsSchema,
|
|
24
|
+
ViewportSchema,
|
|
25
|
+
S3SyncSchema,
|
|
26
|
+
LoggingTransportSchema,
|
|
27
|
+
// Main schema
|
|
28
|
+
ConfigSchema,
|
|
29
|
+
PartialConfigSchema,
|
|
30
|
+
StrictConfigSchema,
|
|
31
|
+
// Types
|
|
32
|
+
type Config,
|
|
33
|
+
type PartialConfig,
|
|
34
|
+
type DeepPartialConfig,
|
|
35
|
+
type StrictConfig,
|
|
36
|
+
type SessionData,
|
|
37
|
+
type DevTools,
|
|
38
|
+
type ProxyServerCredentials,
|
|
39
|
+
type CloudUploadOptions,
|
|
40
|
+
type Viewport,
|
|
41
|
+
type S3Sync,
|
|
42
|
+
type LoggingTransport,
|
|
43
|
+
// Validation helpers
|
|
44
|
+
validateConfig,
|
|
45
|
+
parseConfig,
|
|
46
|
+
getDefaultConfig,
|
|
47
|
+
} from '@open-wa/config';
|