@open-wa/schema 5.0.0 → 5.2.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 +7868 -994
- package/dist/generated/index.d.cts.map +1 -1
- package/dist/generated/index.d.mts +7868 -994
- 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-C9Ad0lEr.d.cts +4729 -0
- package/dist/index-C9Ad0lEr.d.cts.map +1 -0
- package/dist/index-D0QN2PaW.d.mts +4729 -0
- package/dist/index-D0QN2PaW.d.mts.map +1 -0
- package/dist/index.cjs +109 -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 +53 -2
- package/dist/index.mjs.map +1 -1
- package/dist/methods/index.cjs +6 -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-Dizm7Fn9.cjs → methods-BGmdzjd3.cjs} +904 -76
- package/dist/methods-BGmdzjd3.cjs.map +1 -0
- package/dist/{methods-CZqeUhKy.mjs → methods-BhLsQNtL.mjs} +587 -77
- package/dist/methods-BhLsQNtL.mjs.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 +287 -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 +30049 -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 +46 -0
- package/src/interactive.ts +151 -0
- package/src/methods/business.ts +52 -0
- package/src/methods/chats.ts +264 -0
- package/src/methods/communities.ts +121 -0
- package/src/methods/contacts.ts +160 -0
- package/src/methods/groups.ts +378 -0
- package/src/methods/index.ts +34 -0
- package/src/methods/interactive.ts +21 -0
- package/src/methods/labels.ts +88 -0
- package/src/methods/media.ts +36 -0
- package/src/methods/messaging.ts +836 -0
- package/src/methods/session.ts +262 -0
- package/src/methods/status.ts +122 -0
- package/src/output-json-schema.ts +27 -0
- package/src/parameters.ts +237 -0
- package/src/registry.test.ts +30 -0
- package/src/registry.ts +601 -0
- package/src/return-types.ts +125 -0
- package/dist/index-BHcd0h7x.d.mts +0 -1265
- package/dist/index-BHcd0h7x.d.mts.map +0 -1
- package/dist/index-ChYFQM1K.d.cts +0 -1265
- 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,181 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { execFile } from 'node:child_process';
|
|
4
|
+
import { promisify } from 'node:util';
|
|
5
|
+
import { clientRegistry } from '../registry';
|
|
6
|
+
import '../methods';
|
|
7
|
+
|
|
8
|
+
const generatedDir = path.join(__dirname, '../generated');
|
|
9
|
+
const clientDocsDir = path.join(__dirname, '../../../../apps/docs/content/docs/reference/client');
|
|
10
|
+
const execFileAsync = promisify(execFile);
|
|
11
|
+
|
|
12
|
+
describe('generator outputs', () => {
|
|
13
|
+
describe('openapi.json', () => {
|
|
14
|
+
const openapiPath = path.join(generatedDir, 'openapi.json');
|
|
15
|
+
|
|
16
|
+
it('should exist', () => {
|
|
17
|
+
expect(fs.existsSync(openapiPath)).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should be valid JSON', () => {
|
|
21
|
+
const content = fs.readFileSync(openapiPath, 'utf-8');
|
|
22
|
+
expect(() => JSON.parse(content)).not.toThrow();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should have paths for registered methods', () => {
|
|
26
|
+
const content = JSON.parse(fs.readFileSync(openapiPath, 'utf-8'));
|
|
27
|
+
const pathCount = Object.keys(content.paths || {}).length;
|
|
28
|
+
const methodCount = clientRegistry.getAll().length;
|
|
29
|
+
|
|
30
|
+
expect(pathCount).toBeGreaterThan(methodCount * 0.9);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should have valid OpenAPI structure', () => {
|
|
34
|
+
const content = JSON.parse(fs.readFileSync(openapiPath, 'utf-8'));
|
|
35
|
+
|
|
36
|
+
expect(content.openapi).toBe('3.0.3');
|
|
37
|
+
expect(content.info).toBeDefined();
|
|
38
|
+
expect(content.info.title).toBeTruthy();
|
|
39
|
+
expect(content.paths).toBeDefined();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('types.ts', () => {
|
|
44
|
+
const typesPath = path.join(generatedDir, 'types.ts');
|
|
45
|
+
|
|
46
|
+
it('should exist', () => {
|
|
47
|
+
expect(fs.existsSync(typesPath)).toBe(true);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should have type exports for methods', () => {
|
|
51
|
+
const content = fs.readFileSync(typesPath, 'utf-8');
|
|
52
|
+
|
|
53
|
+
expect(content).toContain('SendTextInput');
|
|
54
|
+
expect(content).toContain('SendTextOutput');
|
|
55
|
+
expect(content).toContain('export type');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it.skip('should not have TypeScript errors', async () => {
|
|
59
|
+
if (!process.env.TS_CHECK) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
await expect(execFileAsync('npx', ['tsc', '--noEmit', typesPath])).resolves.toBeDefined();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('BaseClient.ts', () => {
|
|
68
|
+
const baseClientPath = path.join(generatedDir, 'BaseClient.ts');
|
|
69
|
+
const generatedIndexPath = path.join(generatedDir, 'index.ts');
|
|
70
|
+
|
|
71
|
+
it('should exist', () => {
|
|
72
|
+
expect(fs.existsSync(baseClientPath)).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should have method implementations', () => {
|
|
76
|
+
const content = fs.readFileSync(baseClientPath, 'utf-8');
|
|
77
|
+
|
|
78
|
+
expect(content).toContain('sendText = implementMethod');
|
|
79
|
+
expect(content).toContain('public sendText');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should have at least expected method count', () => {
|
|
83
|
+
const content = fs.readFileSync(baseClientPath, 'utf-8');
|
|
84
|
+
const methodCount = clientRegistry.getAll().length;
|
|
85
|
+
|
|
86
|
+
const matches = content.match(/implementMethod/g) || [];
|
|
87
|
+
expect(matches.length).toBeGreaterThanOrEqual(methodCount);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should create a generated package index', () => {
|
|
91
|
+
expect(fs.existsSync(generatedIndexPath)).toBe(true);
|
|
92
|
+
const content = fs.readFileSync(generatedIndexPath, 'utf-8');
|
|
93
|
+
expect(content).toContain("export * from './BaseClient'");
|
|
94
|
+
expect(content).toContain("export * from './types'");
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('Client API reference docs', () => {
|
|
99
|
+
const indexPath = path.join(clientDocsDir, 'index.mdx');
|
|
100
|
+
const metaPath = path.join(clientDocsDir, 'meta.json');
|
|
101
|
+
const messagesPath = path.join(clientDocsDir, 'messages.mdx');
|
|
102
|
+
|
|
103
|
+
it('should create client docs index and navigation metadata', () => {
|
|
104
|
+
expect(fs.existsSync(indexPath)).toBe(true);
|
|
105
|
+
expect(fs.existsSync(metaPath)).toBe(true);
|
|
106
|
+
|
|
107
|
+
const indexContent = fs.readFileSync(indexPath, 'utf-8');
|
|
108
|
+
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf-8'));
|
|
109
|
+
|
|
110
|
+
expect(indexContent).toContain('Generated file warning');
|
|
111
|
+
expect(indexContent).toContain('packages/schema/scripts/gen-client-reference-docs.ts');
|
|
112
|
+
expect(indexContent).toContain('<Cards>');
|
|
113
|
+
expect(indexContent).toContain('<Card title="Messages" href="./messages">');
|
|
114
|
+
expect(indexContent).toContain('client methods');
|
|
115
|
+
expect(meta.title).toBe('Client API');
|
|
116
|
+
expect(meta.icon).toBe('BookOpen');
|
|
117
|
+
expect(meta.pages).toContain('messages');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should create a generated MDX page for every namespace', () => {
|
|
121
|
+
const namespaces = Array.from(new Set(clientRegistry.getAll().map((def) => def.meta.namespace || 'core'))).sort();
|
|
122
|
+
const mdxFiles = fs
|
|
123
|
+
.readdirSync(clientDocsDir)
|
|
124
|
+
.filter((fileName) => fileName.endsWith('.mdx') && fileName !== 'index.mdx' && fileName !== 'client.mdx')
|
|
125
|
+
.sort();
|
|
126
|
+
|
|
127
|
+
expect(mdxFiles).toEqual(expect.arrayContaining(namespaces.map((namespace) => `${namespace.toLowerCase()}.mdx`)));
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should include sendText method details from the schema registry', () => {
|
|
131
|
+
expect(fs.existsSync(messagesPath)).toBe(true);
|
|
132
|
+
const content = fs.readFileSync(messagesPath, 'utf-8');
|
|
133
|
+
|
|
134
|
+
expect(content).toContain('Generated file warning');
|
|
135
|
+
expect(content).toContain('packages/schema/scripts/gen-client-reference-docs.ts');
|
|
136
|
+
expect(content).toContain('## `sendText`');
|
|
137
|
+
expect(content).toContain('### Overview');
|
|
138
|
+
expect(content).toContain('| Prop | Value |');
|
|
139
|
+
expect(content).toContain('| Namespace | `messages` |');
|
|
140
|
+
expect(content).toContain('| Action | `send` |');
|
|
141
|
+
expect(content).toContain('| Positional parameter order | `to`, `content`, `options` |');
|
|
142
|
+
expect(content).toContain('| Aliases | `messages.sendText` |');
|
|
143
|
+
expect(content).toContain('### Routes');
|
|
144
|
+
expect(content).toContain('| Type | Method | Path | Name | Status |');
|
|
145
|
+
expect(content).toContain('| Primary | `POST` | `/api/messages/sendText` | `sendText` | Active |');
|
|
146
|
+
expect(content).toContain('| Alias | `POST` | `/api/sendText` | `sendText` | Active |');
|
|
147
|
+
expect(content).toContain('### Usage');
|
|
148
|
+
expect(content).toContain('<InterfaceTabs>');
|
|
149
|
+
expect(content).toContain('<InterfaceTab value="Embedded">');
|
|
150
|
+
expect(content).toContain('const result = await client.sendText({');
|
|
151
|
+
expect(content).toContain('// namespaced form: client.messages.sendText(...)');
|
|
152
|
+
expect(content).toContain('<InterfaceTab value="SocketClient">');
|
|
153
|
+
expect(content).toContain('<InterfaceTab value="Easy API">');
|
|
154
|
+
expect(content).toContain('curl -X POST "http://localhost:8080/api/messages/sendText"');
|
|
155
|
+
expect(content).toContain('### Parameters');
|
|
156
|
+
expect(content).toContain('<AutoTypeTable path="./generated-method-params.ts" name="SendTextParams" />');
|
|
157
|
+
expect(content).toContain('### Output');
|
|
158
|
+
expect(content).toContain('| Return type | `object { _serialized } \\| boolean \\| string` |');
|
|
159
|
+
expect(content).not.toContain('Returns: `');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should render licensed methods with callouts and visible heading indicators', () => {
|
|
163
|
+
expect(fs.existsSync(messagesPath)).toBe(true);
|
|
164
|
+
const content = fs.readFileSync(messagesPath, 'utf-8');
|
|
165
|
+
|
|
166
|
+
expect(content).toContain('## `sendButtons` - insiders');
|
|
167
|
+
expect(content).toContain('<LicensedFeatureCallout tier="insiders" />');
|
|
168
|
+
expect(content).toContain('| License | `insiders` |');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should visibly label deprecated alias routes in route tables', () => {
|
|
172
|
+
const contactsPath = path.join(clientDocsDir, 'contacts.mdx');
|
|
173
|
+
expect(fs.existsSync(contactsPath)).toBe(true);
|
|
174
|
+
const content = fs.readFileSync(contactsPath, 'utf-8');
|
|
175
|
+
|
|
176
|
+
expect(content).toContain('## `blockContact`');
|
|
177
|
+
expect(content).toContain('| Deprecated alias | `PUT` | `/api/contactBlock` | `contactBlock` | **Deprecated** |');
|
|
178
|
+
expect(content).toContain('| Deprecated alias | `PUT` | `/api/contacts/contactBlock` | `contacts.contactBlock` | **Deprecated** |');
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
});
|
|
@@ -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
|
+
);
|