@kyyinfinite/zapo-message 0.0.0-stage → 1.0.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.
Files changed (191) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/LICENSE +20 -0
  3. package/README.md +532 -2
  4. package/build.config.json +28 -0
  5. package/dist/airich/airich.d.ts +180 -0
  6. package/dist/airich/airich.js +548 -0
  7. package/dist/airich/context.d.ts +24 -0
  8. package/dist/airich/context.js +48 -0
  9. package/dist/airich/encode.d.ts +3 -0
  10. package/dist/airich/encode.js +16 -0
  11. package/dist/airich/index.d.ts +20 -0
  12. package/dist/airich/index.js +11 -0
  13. package/dist/airich/inline.d.ts +45 -0
  14. package/dist/airich/inline.js +152 -0
  15. package/dist/airich/nodes.d.ts +30 -0
  16. package/dist/airich/nodes.js +121 -0
  17. package/dist/airich/primitives.d.ts +144 -0
  18. package/dist/airich/primitives.js +230 -0
  19. package/dist/airich/protocol.d.ts +35 -0
  20. package/dist/airich/protocol.js +65 -0
  21. package/dist/airich/table.d.ts +22 -0
  22. package/dist/airich/table.js +35 -0
  23. package/dist/airich/tokenizer.d.ts +13 -0
  24. package/dist/airich/tokenizer.js +717 -0
  25. package/dist/airich/transport.d.ts +15 -0
  26. package/dist/airich/transport.js +56 -0
  27. package/dist/branding.d.ts +5 -0
  28. package/dist/branding.js +9 -0
  29. package/dist/btn/builders.d.ts +246 -0
  30. package/dist/btn/builders.js +710 -0
  31. package/dist/btn/media-slot.d.ts +28 -0
  32. package/dist/btn/media-slot.js +323 -0
  33. package/dist/btn/media-source.d.ts +38 -0
  34. package/dist/btn/media-source.js +116 -0
  35. package/dist/btn/sharp-loader.d.ts +5 -0
  36. package/dist/btn/sharp-loader.js +18 -0
  37. package/dist/btn/thumbnail.d.ts +21 -0
  38. package/dist/btn/thumbnail.js +124 -0
  39. package/dist/btn/validators.d.ts +40 -0
  40. package/dist/btn/validators.js +135 -0
  41. package/dist/btn.d.ts +1 -0
  42. package/dist/btn.js +1 -0
  43. package/dist/core/async-send.d.ts +20 -0
  44. package/dist/core/async-send.js +1 -0
  45. package/dist/core/base-builder.d.ts +20 -0
  46. package/dist/core/base-builder.js +67 -0
  47. package/dist/core/errors.d.ts +30 -0
  48. package/dist/core/errors.js +61 -0
  49. package/dist/download.d.ts +24 -0
  50. package/dist/download.js +123 -0
  51. package/dist/index.d.ts +38 -0
  52. package/dist/index.js +22 -0
  53. package/dist/integration/augment.d.ts +97 -0
  54. package/dist/integration/augment.js +1 -0
  55. package/dist/integration/auto.d.ts +8 -0
  56. package/dist/integration/auto.js +79 -0
  57. package/dist/integration/index.d.ts +4 -0
  58. package/dist/integration/index.js +2 -0
  59. package/dist/integration/methods.d.ts +25 -0
  60. package/dist/integration/methods.js +24 -0
  61. package/dist/interactive.d.ts +24 -0
  62. package/dist/interactive.js +121 -0
  63. package/dist/media-gate.d.ts +99 -0
  64. package/dist/media-gate.js +322 -0
  65. package/dist/message/album.d.ts +33 -0
  66. package/dist/message/album.js +107 -0
  67. package/dist/message/contact.d.ts +29 -0
  68. package/dist/message/contact.js +123 -0
  69. package/dist/message/forward.d.ts +23 -0
  70. package/dist/message/forward.js +77 -0
  71. package/dist/message/index.d.ts +26 -0
  72. package/dist/message/index.js +13 -0
  73. package/dist/message/link-preview.d.ts +28 -0
  74. package/dist/message/link-preview.js +98 -0
  75. package/dist/message/location.d.ts +24 -0
  76. package/dist/message/location.js +94 -0
  77. package/dist/message/media.d.ts +90 -0
  78. package/dist/message/media.js +123 -0
  79. package/dist/message/poll.d.ts +24 -0
  80. package/dist/message/poll.js +98 -0
  81. package/dist/message/reaction.d.ts +20 -0
  82. package/dist/message/reaction.js +71 -0
  83. package/dist/message/sticker.d.ts +29 -0
  84. package/dist/message/sticker.js +44 -0
  85. package/dist/message/target.d.ts +18 -0
  86. package/dist/message/target.js +24 -0
  87. package/dist/message/text.d.ts +17 -0
  88. package/dist/message/text.js +29 -0
  89. package/dist/message/vcard.d.ts +9 -0
  90. package/dist/message/vcard.js +23 -0
  91. package/dist/message/webp.d.ts +6 -0
  92. package/dist/message/webp.js +57 -0
  93. package/dist/payloads.d.ts +180 -0
  94. package/dist/payloads.js +1 -0
  95. package/dist/serialize.d.ts +41 -0
  96. package/dist/serialize.js +93 -0
  97. package/dist/status.d.ts +3 -0
  98. package/dist/status.js +21 -0
  99. package/dist/zapo-client.d.ts +44 -0
  100. package/dist/zapo-client.js +1 -0
  101. package/examples/airich.ts +59 -0
  102. package/examples/buttons.ts +48 -0
  103. package/examples/client-methods.ts +58 -0
  104. package/examples/hybrid.ts +133 -0
  105. package/examples/serialize.ts +61 -0
  106. package/package.json +271 -4
  107. package/scripts/check-no-comments.mjs +101 -0
  108. package/scripts/compare-golden.mjs +461 -0
  109. package/scripts/lib-config.mjs +9 -0
  110. package/scripts/smoke-auto.mjs +75 -0
  111. package/scripts/test-external-consumer.mjs +316 -0
  112. package/scripts/verify-package.mjs +78 -0
  113. package/src/airich/airich.ts +970 -0
  114. package/src/airich/context.ts +96 -0
  115. package/src/airich/encode.ts +25 -0
  116. package/src/airich/index.ts +98 -0
  117. package/src/airich/inline.ts +272 -0
  118. package/src/airich/nodes.ts +174 -0
  119. package/src/airich/primitives.ts +474 -0
  120. package/src/airich/protocol.ts +133 -0
  121. package/src/airich/table.ts +70 -0
  122. package/src/airich/tokenizer.ts +799 -0
  123. package/src/airich/transport.ts +106 -0
  124. package/src/branding.ts +23 -0
  125. package/src/btn/builders.ts +1073 -0
  126. package/src/btn/media-slot.ts +486 -0
  127. package/src/btn/media-source.ts +189 -0
  128. package/src/btn/sharp-loader.ts +23 -0
  129. package/src/btn/thumbnail.ts +182 -0
  130. package/src/btn/validators.ts +237 -0
  131. package/src/btn.ts +8 -0
  132. package/src/core/async-send.ts +28 -0
  133. package/src/core/base-builder.ts +104 -0
  134. package/src/core/errors.ts +82 -0
  135. package/src/download.ts +212 -0
  136. package/src/index.ts +229 -0
  137. package/src/integration/augment.ts +262 -0
  138. package/src/integration/auto.ts +103 -0
  139. package/src/integration/index.ts +8 -0
  140. package/src/integration/methods.ts +45 -0
  141. package/src/interactive.ts +188 -0
  142. package/src/media-gate.ts +499 -0
  143. package/src/message/album.ts +187 -0
  144. package/src/message/contact.ts +170 -0
  145. package/src/message/forward.ts +123 -0
  146. package/src/message/index.ts +83 -0
  147. package/src/message/link-preview.ts +148 -0
  148. package/src/message/location.ts +150 -0
  149. package/src/message/media.ts +323 -0
  150. package/src/message/poll.ts +158 -0
  151. package/src/message/reaction.ts +133 -0
  152. package/src/message/sticker.ts +100 -0
  153. package/src/message/target.ts +38 -0
  154. package/src/message/text.ts +61 -0
  155. package/src/message/vcard.ts +36 -0
  156. package/src/message/webp.ts +78 -0
  157. package/src/payloads.ts +204 -0
  158. package/src/serialize.ts +180 -0
  159. package/src/status.ts +29 -0
  160. package/src/zapo-client.ts +84 -0
  161. package/test/airich-inline.test.ts +200 -0
  162. package/test/airich-primitives.test.ts +226 -0
  163. package/test/airich-tokenizer.test.ts +145 -0
  164. package/test/airich.golden.test.ts +406 -0
  165. package/test/airich.test.ts +1006 -0
  166. package/test/btn-button.test.ts +416 -0
  167. package/test/btn-header.test.ts +646 -0
  168. package/test/btn-legacy.test.ts +342 -0
  169. package/test/btn-lifecycle.test.ts +295 -0
  170. package/test/btn-list.test.ts +261 -0
  171. package/test/btn-media-source.test.ts +150 -0
  172. package/test/btn-thumbnail.test.ts +160 -0
  173. package/test/builder-compat.test.ts +264 -0
  174. package/test/helpers.ts +27 -0
  175. package/test/hybrid-airich.test.ts +345 -0
  176. package/test/hybrid-client.test.ts +239 -0
  177. package/test/hybrid-errors.test.ts +557 -0
  178. package/test/hybrid-interactive.test.ts +455 -0
  179. package/test/hybrid.golden.test.ts +349 -0
  180. package/test/hybrid.test.ts +479 -0
  181. package/test/integration-auto.test.ts +159 -0
  182. package/test/integration.fakeserver.test.ts +440 -0
  183. package/test/media-gate.test.ts +215 -0
  184. package/test/message-album.test.ts +145 -0
  185. package/test/message-builders.test.ts +401 -0
  186. package/test/message-media.test.ts +303 -0
  187. package/test/message.fakeserver.test.ts +284 -0
  188. package/test/message.golden.test.ts +254 -0
  189. package/test/serializer.test.ts +448 -0
  190. package/tsconfig.json +26 -0
  191. package/tsconfig.test.json +10 -0
@@ -0,0 +1,103 @@
1
+ import { createRequire } from "node:module";
2
+ import { WaClient } from "zapo-js";
3
+ import { ZAPO_MESSAGE_METHOD_NAMES, zapoMessageMethods } from "./methods.js";
4
+ import { isClient } from "../core/base-builder.js";
5
+ import type { ZapoClient } from "../zapo-client.js";
6
+
7
+ const INSTALLED_MARKER = Symbol.for(
8
+ "@kyyinfinite/zapo-message.methods-installed",
9
+ );
10
+
11
+ type PrototypeLike = Record<PropertyKey, unknown>;
12
+
13
+ const defineMarker = (target: PrototypeLike): void => {
14
+ Object.defineProperty(target, INSTALLED_MARKER, {
15
+ value: true,
16
+ enumerable: false,
17
+ writable: false,
18
+ configurable: false,
19
+ });
20
+ };
21
+
22
+ const hasMarker = (target: object): boolean =>
23
+ (INSTALLED_MARKER as symbol) in target ||
24
+ Object.prototype.hasOwnProperty.call(target, INSTALLED_MARKER);
25
+
26
+ export const installOnPrototype = (prototype: object): boolean => {
27
+ if (!prototype || typeof prototype !== "object") return false;
28
+ if (hasMarker(prototype)) return false;
29
+ if (!Object.isExtensible(prototype)) return false;
30
+
31
+ const target = prototype as PrototypeLike;
32
+ defineMarker(target);
33
+ for (const name of ZAPO_MESSAGE_METHOD_NAMES) {
34
+ if (name in prototype) continue;
35
+ const factory = zapoMessageMethods[name] as unknown as (
36
+ client: ZapoClient,
37
+ ...args: unknown[]
38
+ ) => unknown;
39
+ Object.defineProperty(prototype, name, {
40
+ value(this: unknown, ...args: unknown[]) {
41
+ return factory(this as ZapoClient, ...args);
42
+ },
43
+ enumerable: false,
44
+ writable: true,
45
+ configurable: true,
46
+ });
47
+ }
48
+ return true;
49
+ };
50
+
51
+ export const installZapoMessage = (client: ZapoClient): ZapoClient => {
52
+ if (!isClient(client)) {
53
+ throw new TypeError(
54
+ "installZapoMessage() butuh WaClient zapo-js (punya client.message.send)",
55
+ );
56
+ }
57
+ const target = client as unknown as PrototypeLike;
58
+ if (hasMarker(client)) return client;
59
+ defineMarker(target);
60
+ for (const name of ZAPO_MESSAGE_METHOD_NAMES) {
61
+ if (name in client) continue;
62
+ const factory = zapoMessageMethods[name] as unknown as (
63
+ client: ZapoClient,
64
+ ...args: unknown[]
65
+ ) => unknown;
66
+ Object.defineProperty(client, name, {
67
+ value(...args: unknown[]) {
68
+ return factory(client, ...args);
69
+ },
70
+ enumerable: false,
71
+ writable: true,
72
+ configurable: true,
73
+ });
74
+ }
75
+ return client;
76
+ };
77
+
78
+ const esmPrototype = (WaClient as unknown as { prototype?: object }).prototype;
79
+ const esmInstalled = esmPrototype ? installOnPrototype(esmPrototype) : false;
80
+
81
+ let cjsInstalled = false;
82
+ try {
83
+ const require = createRequire(import.meta.url);
84
+ const cjs = require("zapo-js") as {
85
+ WaClient?: { prototype?: object };
86
+ };
87
+ const cjsPrototype = cjs?.WaClient?.prototype;
88
+ if (
89
+ cjsPrototype &&
90
+ cjsPrototype !== esmPrototype &&
91
+ installOnPrototype(cjsPrototype)
92
+ ) {
93
+ cjsInstalled = true;
94
+ }
95
+ } catch {
96
+ cjsInstalled = false;
97
+ }
98
+
99
+ export const autoInstallStatus = (): {
100
+ esm: boolean;
101
+ cjs: boolean;
102
+ marker: symbol;
103
+ } => ({ esm: esmInstalled, cjs: cjsInstalled, marker: INSTALLED_MARKER });
@@ -0,0 +1,8 @@
1
+ export {
2
+ installZapoMessage,
3
+ installOnPrototype,
4
+ autoInstallStatus,
5
+ } from "./auto.js";
6
+ export { zapoMessageMethods, ZAPO_MESSAGE_METHOD_NAMES } from "./methods.js";
7
+ export type { ZapoMessageMethodName } from "./methods.js";
8
+ export type { ZapoMessageClientMethods } from "./augment.js";
@@ -0,0 +1,45 @@
1
+ import { button, legacyButton, listMessage } from "../btn/builders.js";
2
+ import { airich } from "../airich/airich.js";
3
+ import {
4
+ text,
5
+ linkPreview,
6
+ reaction,
7
+ forward,
8
+ contact,
9
+ poll,
10
+ media,
11
+ image,
12
+ video,
13
+ audio,
14
+ document,
15
+ sticker,
16
+ album,
17
+ location,
18
+ } from "../message/index.js";
19
+
20
+ export const zapoMessageMethods = {
21
+ button,
22
+ legacyButton,
23
+ listMessage,
24
+ airich,
25
+ text,
26
+ reaction,
27
+ media,
28
+ image,
29
+ video,
30
+ audio,
31
+ document,
32
+ sticker,
33
+ contact,
34
+ poll,
35
+ album,
36
+ linkPreview,
37
+ forward,
38
+ location,
39
+ } as const;
40
+
41
+ export type ZapoMessageMethodName = keyof typeof zapoMessageMethods;
42
+
43
+ export const ZAPO_MESSAGE_METHOD_NAMES = Object.keys(
44
+ zapoMessageMethods,
45
+ ) as ZapoMessageMethodName[];
@@ -0,0 +1,188 @@
1
+ import { unwrapMessage } from "zapo-js";
2
+ import type { Proto } from "zapo-js";
3
+
4
+ const NATIVE_FLOW_ID_KEYS = [
5
+ "id",
6
+ "selectedId",
7
+ "selected_id",
8
+ "rowId",
9
+ "row_id",
10
+ ] as const;
11
+
12
+ const safeParseJsonParams = (
13
+ paramsJson: unknown,
14
+ ): Record<string, unknown> | undefined => {
15
+ if (typeof paramsJson !== "string" || !paramsJson) return undefined;
16
+ try {
17
+ const parsed = JSON.parse(paramsJson) as unknown;
18
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
19
+ return undefined;
20
+ return parsed as Record<string, unknown>;
21
+ } catch {
22
+ return undefined;
23
+ }
24
+ };
25
+
26
+ const nonEmptyString = (value: unknown): string | undefined => {
27
+ if (typeof value !== "string") return undefined;
28
+ const trimmed = value.trim();
29
+ return trimmed ? trimmed : undefined;
30
+ };
31
+
32
+ export interface ExtractedInteractiveResponse {
33
+ id: string;
34
+ displayText: string | undefined;
35
+ flowName: string | undefined;
36
+ content:
37
+ | "buttonsResponseMessage"
38
+ | "listResponseMessage"
39
+ | "templateButtonReplyMessage"
40
+ | "interactiveResponseMessage";
41
+ contextInfo: Proto.ContextInfo.$Properties | undefined;
42
+ }
43
+
44
+ export const extractInteractiveResponse = (
45
+ message: unknown,
46
+ ): ExtractedInteractiveResponse | undefined => {
47
+ if (!message || typeof message !== "object") return undefined;
48
+
49
+ const msg = unwrapMessage(message as Proto.IMessage) as Record<
50
+ string,
51
+ unknown
52
+ >;
53
+
54
+ const buttonsResponse = msg.buttonsResponseMessage as
55
+ Record<string, unknown> | undefined;
56
+ if (buttonsResponse) {
57
+ const id = nonEmptyString(buttonsResponse.selectedButtonId);
58
+ if (id) {
59
+ return {
60
+ id,
61
+ displayText: nonEmptyString(buttonsResponse.selectedDisplayText),
62
+ flowName: undefined,
63
+ content: "buttonsResponseMessage",
64
+ contextInfo: buttonsResponse.contextInfo as
65
+ Proto.ContextInfo.$Properties | undefined,
66
+ };
67
+ }
68
+ return undefined;
69
+ }
70
+
71
+ const listResponse = msg.listResponseMessage as
72
+ Record<string, unknown> | undefined;
73
+ if (listResponse) {
74
+ const singleSelectReply = listResponse.singleSelectReply as
75
+ Record<string, unknown> | undefined;
76
+ const id = nonEmptyString(singleSelectReply?.selectedRowId);
77
+ if (id) {
78
+ return {
79
+ id,
80
+ displayText: nonEmptyString(listResponse.title),
81
+ flowName: undefined,
82
+ content: "listResponseMessage",
83
+ contextInfo: listResponse.contextInfo as
84
+ Proto.ContextInfo.$Properties | undefined,
85
+ };
86
+ }
87
+ return undefined;
88
+ }
89
+
90
+ const templateReply = msg.templateButtonReplyMessage as
91
+ Record<string, unknown> | undefined;
92
+ if (templateReply) {
93
+ const id = nonEmptyString(templateReply.selectedId);
94
+ if (id) {
95
+ return {
96
+ id,
97
+ displayText: nonEmptyString(templateReply.selectedDisplayText),
98
+ flowName: undefined,
99
+ content: "templateButtonReplyMessage",
100
+ contextInfo: templateReply.contextInfo as
101
+ Proto.ContextInfo.$Properties | undefined,
102
+ };
103
+ }
104
+ return undefined;
105
+ }
106
+
107
+ const interactiveResponse = msg.interactiveResponseMessage as
108
+ Record<string, unknown> | undefined;
109
+ if (interactiveResponse) {
110
+ const native = interactiveResponse.nativeFlowResponseMessage as
111
+ Record<string, unknown> | undefined;
112
+ if (native) {
113
+ const params = safeParseJsonParams(native.paramsJson);
114
+ if (params) {
115
+ for (const key of NATIVE_FLOW_ID_KEYS) {
116
+ const id = nonEmptyString(params[key]);
117
+ if (id) {
118
+ return {
119
+ id,
120
+ displayText: nonEmptyString(
121
+ params.display_text ?? params.displayText,
122
+ ),
123
+ flowName: nonEmptyString(native.name),
124
+ content: "interactiveResponseMessage",
125
+ contextInfo: interactiveResponse.contextInfo as
126
+ Proto.ContextInfo.$Properties | undefined,
127
+ };
128
+ }
129
+ }
130
+ }
131
+ return undefined;
132
+ }
133
+
134
+ return undefined;
135
+ }
136
+
137
+ return undefined;
138
+ };
139
+
140
+ export interface InteractiveContextMeta {
141
+ hasContext: boolean;
142
+ quotedStanzaId: string | undefined;
143
+ quotedParticipant: string | undefined;
144
+ quotedRemoteJid: string | undefined;
145
+ senderMatches: boolean;
146
+ }
147
+
148
+ export interface InspectInteractiveContextOptions {
149
+ botJids?: string[];
150
+ }
151
+
152
+ export const inspectInteractiveContext = (
153
+ event: { key?: Record<string, unknown> } | null | undefined,
154
+ response: { contextInfo?: Record<string, unknown> } | null | undefined,
155
+ { botJids = [] }: InspectInteractiveContextOptions = {},
156
+ ): InteractiveContextMeta => {
157
+ const contextInfo = response?.contextInfo;
158
+ const key = event?.key as Record<string, unknown> | undefined;
159
+ const senderCandidates = [
160
+ key?.participantAlt,
161
+ key?.participant,
162
+ key?.remoteJidAlt,
163
+ key?.remoteJid,
164
+ ].filter(Boolean) as string[];
165
+
166
+ const meta: InteractiveContextMeta = {
167
+ hasContext: !!contextInfo,
168
+ quotedStanzaId: nonEmptyString(
169
+ (contextInfo as Record<string, unknown> | undefined)?.stanzaId,
170
+ ),
171
+ quotedParticipant: nonEmptyString(
172
+ (contextInfo as Record<string, unknown> | undefined)?.participant,
173
+ ),
174
+ quotedRemoteJid: nonEmptyString(
175
+ (contextInfo as Record<string, unknown> | undefined)?.remoteJid,
176
+ ),
177
+ senderMatches: true,
178
+ };
179
+
180
+ if (meta.quotedParticipant) {
181
+ const knownJids = [...botJids, key?.remoteJid, key?.remoteJidAlt].filter(
182
+ Boolean,
183
+ ) as string[];
184
+ meta.senderMatches = knownJids.includes(meta.quotedParticipant);
185
+ }
186
+
187
+ return meta;
188
+ };