@xmoxmo/bncr 0.2.2 → 0.2.3

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/src/core/types.ts CHANGED
@@ -26,7 +26,21 @@ export type OutboxEntry = {
26
26
  accountId: string;
27
27
  sessionKey: string;
28
28
  route: BncrRoute;
29
- payload: Record<string, unknown>;
29
+ payload: Record<string, unknown> & {
30
+ _meta?: {
31
+ kind?: 'message' | 'file-transfer';
32
+ retryCount?: number;
33
+ nextAttemptAt?: number;
34
+ mediaUrl?: string;
35
+ mediaLocalRoots?: string[];
36
+ text?: string;
37
+ asVoice?: boolean;
38
+ audioAsVoice?: boolean;
39
+ replyToId?: string;
40
+ finalEvent?: string;
41
+ [key: string]: unknown;
42
+ };
43
+ };
30
44
  createdAt: number;
31
45
  retryCount: number;
32
46
  nextAttemptAt: number;
@@ -0,0 +1,115 @@
1
+ import { randomUUID } from 'node:crypto';
2
+
3
+ type MinimalBncrSendInput = {
4
+ channel?: string;
5
+ action?: string;
6
+ idempotencyKey?: string;
7
+ accountId?: string;
8
+ to?: string;
9
+ message?: string;
10
+ caption?: string;
11
+ path?: string;
12
+ media?: string;
13
+ filePath?: string;
14
+ mediaUrl?: string;
15
+ asVoice?: boolean;
16
+ audioAsVoice?: boolean;
17
+ params?: Record<string, unknown>;
18
+ };
19
+
20
+ type BuiltBncrMessageAction = {
21
+ channel: string;
22
+ action: string;
23
+ idempotencyKey: string;
24
+ accountId?: string;
25
+ params: Record<string, unknown>;
26
+ };
27
+
28
+ function asString(v: unknown, fallback = ''): string {
29
+ if (typeof v === 'string') return v;
30
+ if (v == null) return fallback;
31
+ return String(v);
32
+ }
33
+
34
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
35
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
36
+ }
37
+
38
+ function pickFirstString(...values: unknown[]): string | undefined {
39
+ for (const value of values) {
40
+ if (typeof value !== 'string') continue;
41
+ if (value.length === 0) return '';
42
+ return value;
43
+ }
44
+ return undefined;
45
+ }
46
+
47
+ function pickFirstBoolean(...values: unknown[]): boolean | undefined {
48
+ for (const value of values) {
49
+ if (typeof value === 'boolean') return value;
50
+ }
51
+ return undefined;
52
+ }
53
+
54
+ export function buildBncrMessageAction(input: MinimalBncrSendInput): BuiltBncrMessageAction {
55
+ const paramsObj = isPlainObject(input.params) ? input.params : {};
56
+
57
+ const channel = asString(input.channel || 'bncr').trim() || 'bncr';
58
+ const action = asString(input.action || 'send').trim() || 'send';
59
+ const idempotencyKey =
60
+ asString(input.idempotencyKey || '').trim() || `bncr-${randomUUID()}`;
61
+ const accountId =
62
+ asString(pickFirstString(paramsObj.accountId, input.accountId) || '').trim() || undefined;
63
+
64
+ const to = asString(pickFirstString(paramsObj.to, input.to) || '').trim();
65
+ if (!to) throw new Error('bncr send requires to');
66
+
67
+ const mediaPath = pickFirstString(
68
+ paramsObj.media,
69
+ paramsObj.path,
70
+ paramsObj.filePath,
71
+ paramsObj.mediaUrl,
72
+ input.media,
73
+ input.path,
74
+ input.filePath,
75
+ input.mediaUrl,
76
+ );
77
+
78
+ const message = pickFirstString(paramsObj.message, input.message) ?? '';
79
+ const explicitCaption = pickFirstString(paramsObj.caption, input.caption) ?? '';
80
+ const asVoice = pickFirstBoolean(paramsObj.asVoice, input.asVoice);
81
+ const audioAsVoice = pickFirstBoolean(paramsObj.audioAsVoice, input.audioAsVoice);
82
+
83
+ if ((asVoice === true || audioAsVoice === true) && !mediaPath) {
84
+ throw new Error('bncr voice send requires media path');
85
+ }
86
+
87
+ const normalizedParams: Record<string, unknown> = {
88
+ ...paramsObj,
89
+ to,
90
+ };
91
+
92
+ if (mediaPath) {
93
+ normalizedParams.path = mediaPath;
94
+ const finalCaption = explicitCaption || message;
95
+ if (finalCaption) normalizedParams.caption = finalCaption;
96
+ delete normalizedParams.message;
97
+ } else {
98
+ const finalMessage = message || explicitCaption;
99
+ if (!finalMessage.trim()) throw new Error('bncr send requires message or media');
100
+ normalizedParams.message = finalMessage;
101
+ delete normalizedParams.caption;
102
+ }
103
+
104
+ if (asVoice === true) normalizedParams.asVoice = true;
105
+ if (audioAsVoice === true) normalizedParams.audioAsVoice = true;
106
+ if (accountId) normalizedParams.accountId = accountId;
107
+
108
+ return {
109
+ channel,
110
+ action,
111
+ idempotencyKey,
112
+ ...(accountId ? { accountId } : {}),
113
+ params: normalizedParams,
114
+ };
115
+ }