meta-cloud-api 0.0.1-beta.0 → 0.0.2

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 (284) hide show
  1. package/dist/api/base.d.mts +12 -0
  2. package/dist/api/base.d.ts +12 -0
  3. package/dist/api/base.js +2 -0
  4. package/dist/api/base.js.map +1 -0
  5. package/dist/api/base.mjs +2 -0
  6. package/dist/api/base.mjs.map +1 -0
  7. package/dist/api/encryption.d.mts +15 -0
  8. package/dist/api/encryption.d.ts +15 -0
  9. package/dist/api/encryption.js +2 -0
  10. package/dist/api/encryption.js.map +1 -0
  11. package/dist/api/encryption.mjs +2 -0
  12. package/dist/api/encryption.mjs.map +1 -0
  13. package/dist/api/flow.d.mts +114 -0
  14. package/dist/api/flow.d.ts +114 -0
  15. package/dist/api/flow.js +2 -0
  16. package/dist/api/flow.js.map +1 -0
  17. package/dist/api/flow.mjs +2 -0
  18. package/dist/api/flow.mjs.map +1 -0
  19. package/dist/api/index.d.mts +25 -0
  20. package/dist/api/index.d.ts +25 -0
  21. package/dist/api/index.js +2 -0
  22. package/dist/api/index.js.map +1 -0
  23. package/dist/api/index.mjs +2 -0
  24. package/dist/api/index.mjs.map +1 -0
  25. package/dist/api/media.d.mts +17 -0
  26. package/dist/api/media.d.ts +17 -0
  27. package/dist/api/media.js +2 -0
  28. package/dist/api/media.js.map +1 -0
  29. package/dist/api/media.mjs +2 -0
  30. package/dist/api/media.mjs.map +1 -0
  31. package/dist/api/messages.d.mts +27 -0
  32. package/dist/api/messages.d.ts +27 -0
  33. package/dist/api/messages.js +2 -0
  34. package/dist/api/messages.js.map +1 -0
  35. package/dist/api/messages.mjs +2 -0
  36. package/dist/api/messages.mjs.map +1 -0
  37. package/dist/api/phoneNumber.d.mts +17 -0
  38. package/dist/api/phoneNumber.d.ts +17 -0
  39. package/dist/api/phoneNumber.js +2 -0
  40. package/dist/api/phoneNumber.js.map +1 -0
  41. package/dist/api/phoneNumber.mjs +2 -0
  42. package/dist/api/phoneNumber.mjs.map +1 -0
  43. package/dist/api/qrCode.d.mts +18 -0
  44. package/dist/api/qrCode.d.ts +18 -0
  45. package/dist/api/qrCode.js +2 -0
  46. package/dist/api/qrCode.js.map +1 -0
  47. package/dist/api/qrCode.mjs +2 -0
  48. package/dist/api/qrCode.mjs.map +1 -0
  49. package/dist/api/registration.d.mts +14 -0
  50. package/dist/api/registration.d.ts +14 -0
  51. package/dist/api/registration.js +2 -0
  52. package/dist/api/registration.js.map +1 -0
  53. package/dist/api/registration.mjs +2 -0
  54. package/dist/api/registration.mjs.map +1 -0
  55. package/dist/api/template.d.mts +18 -0
  56. package/dist/api/template.d.ts +18 -0
  57. package/dist/api/template.js +2 -0
  58. package/dist/api/template.js.map +1 -0
  59. package/dist/api/template.mjs +2 -0
  60. package/dist/api/template.mjs.map +1 -0
  61. package/dist/api/twoStepVerification.d.mts +13 -0
  62. package/dist/api/twoStepVerification.d.ts +13 -0
  63. package/dist/api/twoStepVerification.js +2 -0
  64. package/dist/api/twoStepVerification.js.map +1 -0
  65. package/dist/api/twoStepVerification.mjs +2 -0
  66. package/dist/api/twoStepVerification.mjs.map +1 -0
  67. package/dist/api/waba.d.mts +16 -0
  68. package/dist/api/waba.d.ts +16 -0
  69. package/dist/api/waba.js +2 -0
  70. package/dist/api/waba.js.map +1 -0
  71. package/dist/api/waba.mjs +2 -0
  72. package/dist/api/waba.mjs.map +1 -0
  73. package/dist/enums-D6WRUWH9.d.mts +585 -0
  74. package/dist/enums-D6WRUWH9.d.ts +585 -0
  75. package/dist/httpsClient.d.mts +22 -0
  76. package/dist/httpsClient.d.ts +22 -0
  77. package/dist/httpsClient.js +2 -0
  78. package/dist/httpsClient.js.map +1 -0
  79. package/dist/httpsClient.mjs +2 -0
  80. package/dist/httpsClient.mjs.map +1 -0
  81. package/dist/index.d.mts +35 -0
  82. package/dist/index.d.ts +35 -0
  83. package/dist/index.js +2 -0
  84. package/dist/index.js.map +1 -0
  85. package/dist/index.mjs +2 -0
  86. package/dist/index.mjs.map +1 -0
  87. package/dist/request.d.mts +22 -0
  88. package/dist/request.d.ts +22 -0
  89. package/dist/request.js +2 -0
  90. package/dist/request.js.map +1 -0
  91. package/dist/request.mjs +2 -0
  92. package/dist/request.mjs.map +1 -0
  93. package/dist/types/base.d.mts +8 -0
  94. package/dist/types/base.d.ts +8 -0
  95. package/dist/types/base.js +2 -0
  96. package/dist/types/base.js.map +1 -0
  97. package/dist/types/base.mjs +1 -0
  98. package/dist/types/base.mjs.map +1 -0
  99. package/dist/types/config.d.mts +59 -0
  100. package/dist/types/config.d.ts +59 -0
  101. package/dist/types/config.js +2 -0
  102. package/dist/types/config.js.map +1 -0
  103. package/dist/types/config.mjs +1 -0
  104. package/dist/types/config.mjs.map +1 -0
  105. package/dist/types/encryption.d.mts +13 -0
  106. package/dist/types/encryption.d.ts +13 -0
  107. package/dist/types/encryption.js +2 -0
  108. package/dist/types/encryption.js.map +1 -0
  109. package/dist/types/encryption.mjs +1 -0
  110. package/dist/types/encryption.mjs.map +1 -0
  111. package/dist/types/enums.d.mts +1 -0
  112. package/dist/types/enums.d.ts +1 -0
  113. package/dist/types/enums.js +2 -0
  114. package/dist/types/enums.js.map +1 -0
  115. package/dist/types/enums.mjs +2 -0
  116. package/dist/types/enums.mjs.map +1 -0
  117. package/dist/types/flow.d.mts +245 -0
  118. package/dist/types/flow.d.ts +245 -0
  119. package/dist/types/flow.js +2 -0
  120. package/dist/types/flow.js.map +1 -0
  121. package/dist/types/flow.mjs +2 -0
  122. package/dist/types/flow.mjs.map +1 -0
  123. package/dist/types/httpsClient.d.mts +19 -0
  124. package/dist/types/httpsClient.d.ts +19 -0
  125. package/dist/types/httpsClient.js +2 -0
  126. package/dist/types/httpsClient.js.map +1 -0
  127. package/dist/types/httpsClient.mjs +1 -0
  128. package/dist/types/httpsClient.mjs.map +1 -0
  129. package/dist/types/index.d.mts +16 -0
  130. package/dist/types/index.d.ts +16 -0
  131. package/dist/types/index.js +2 -0
  132. package/dist/types/index.js.map +1 -0
  133. package/dist/types/index.mjs +2 -0
  134. package/dist/types/index.mjs.map +1 -0
  135. package/dist/types/logger.d.mts +9 -0
  136. package/dist/types/logger.d.ts +9 -0
  137. package/dist/types/logger.js +2 -0
  138. package/dist/types/logger.js.map +1 -0
  139. package/dist/types/logger.mjs +1 -0
  140. package/dist/types/logger.mjs.map +1 -0
  141. package/dist/types/media.d.mts +31 -0
  142. package/dist/types/media.d.ts +31 -0
  143. package/dist/types/media.js +2 -0
  144. package/dist/types/media.js.map +1 -0
  145. package/dist/types/media.mjs +1 -0
  146. package/dist/types/media.mjs.map +1 -0
  147. package/dist/types/messages.d.mts +322 -0
  148. package/dist/types/messages.d.ts +322 -0
  149. package/dist/types/messages.js +2 -0
  150. package/dist/types/messages.js.map +1 -0
  151. package/dist/types/messages.mjs +1 -0
  152. package/dist/types/messages.mjs.map +1 -0
  153. package/dist/types/phoneNumber.d.mts +82 -0
  154. package/dist/types/phoneNumber.d.ts +82 -0
  155. package/dist/types/phoneNumber.js +2 -0
  156. package/dist/types/phoneNumber.js.map +1 -0
  157. package/dist/types/phoneNumber.mjs +1 -0
  158. package/dist/types/phoneNumber.mjs.map +1 -0
  159. package/dist/types/qrCode.d.mts +27 -0
  160. package/dist/types/qrCode.d.ts +27 -0
  161. package/dist/types/qrCode.js +2 -0
  162. package/dist/types/qrCode.js.map +1 -0
  163. package/dist/types/qrCode.mjs +1 -0
  164. package/dist/types/qrCode.mjs.map +1 -0
  165. package/dist/types/registration.d.mts +14 -0
  166. package/dist/types/registration.d.ts +14 -0
  167. package/dist/types/registration.js +2 -0
  168. package/dist/types/registration.js.map +1 -0
  169. package/dist/types/registration.mjs +1 -0
  170. package/dist/types/registration.mjs.map +1 -0
  171. package/dist/types/request.d.mts +47 -0
  172. package/dist/types/request.d.ts +47 -0
  173. package/dist/types/request.js +2 -0
  174. package/dist/types/request.js.map +1 -0
  175. package/dist/types/request.mjs +1 -0
  176. package/dist/types/request.mjs.map +1 -0
  177. package/dist/types/template.d.mts +106 -0
  178. package/dist/types/template.d.ts +106 -0
  179. package/dist/types/template.js +2 -0
  180. package/dist/types/template.js.map +1 -0
  181. package/dist/types/template.mjs +1 -0
  182. package/dist/types/template.mjs.map +1 -0
  183. package/dist/types/twoStepVerification.d.mts +11 -0
  184. package/dist/types/twoStepVerification.d.ts +11 -0
  185. package/dist/types/twoStepVerification.js +2 -0
  186. package/dist/types/twoStepVerification.js.map +1 -0
  187. package/dist/types/twoStepVerification.mjs +1 -0
  188. package/dist/types/twoStepVerification.mjs.map +1 -0
  189. package/dist/types/waba.d.mts +19 -0
  190. package/dist/types/waba.d.ts +19 -0
  191. package/dist/types/waba.js +2 -0
  192. package/dist/types/waba.js.map +1 -0
  193. package/dist/types/waba.mjs +1 -0
  194. package/dist/types/waba.mjs.map +1 -0
  195. package/dist/types/whatsapp.d.mts +31 -0
  196. package/dist/types/whatsapp.d.ts +31 -0
  197. package/dist/types/whatsapp.js +2 -0
  198. package/dist/types/whatsapp.js.map +1 -0
  199. package/dist/types/whatsapp.mjs +1 -0
  200. package/dist/types/whatsapp.mjs.map +1 -0
  201. package/dist/utils/generateXHub256Sig.d.mts +3 -0
  202. package/dist/utils/generateXHub256Sig.d.ts +3 -0
  203. package/dist/utils/generateXHub256Sig.js +2 -0
  204. package/dist/utils/generateXHub256Sig.js.map +1 -0
  205. package/dist/utils/generateXHub256Sig.mjs +2 -0
  206. package/dist/utils/generateXHub256Sig.mjs.map +1 -0
  207. package/dist/utils/importConfig.d.mts +7 -0
  208. package/dist/utils/importConfig.d.ts +7 -0
  209. package/dist/utils/importConfig.js +2 -0
  210. package/dist/utils/importConfig.js.map +1 -0
  211. package/dist/utils/importConfig.mjs +2 -0
  212. package/dist/utils/importConfig.mjs.map +1 -0
  213. package/dist/utils/index.d.mts +5 -0
  214. package/dist/utils/index.d.ts +5 -0
  215. package/dist/utils/index.js +2 -0
  216. package/dist/utils/index.js.map +1 -0
  217. package/dist/utils/index.mjs +2 -0
  218. package/dist/utils/index.mjs.map +1 -0
  219. package/dist/utils/isMetaError.d.mts +21 -0
  220. package/dist/utils/isMetaError.d.ts +21 -0
  221. package/dist/utils/isMetaError.js +2 -0
  222. package/dist/utils/isMetaError.js.map +1 -0
  223. package/dist/utils/isMetaError.mjs +2 -0
  224. package/dist/utils/isMetaError.mjs.map +1 -0
  225. package/dist/utils/logger.d.mts +13 -0
  226. package/dist/utils/logger.d.ts +13 -0
  227. package/dist/utils/logger.js +2 -0
  228. package/dist/utils/logger.js.map +1 -0
  229. package/dist/utils/logger.mjs +2 -0
  230. package/dist/utils/logger.mjs.map +1 -0
  231. package/dist/utils/objectToQueryString.d.mts +3 -0
  232. package/dist/utils/objectToQueryString.d.ts +3 -0
  233. package/dist/utils/objectToQueryString.js +2 -0
  234. package/dist/utils/objectToQueryString.js.map +1 -0
  235. package/dist/utils/objectToQueryString.mjs +2 -0
  236. package/dist/utils/objectToQueryString.mjs.map +1 -0
  237. package/dist/whatsapp.d.mts +54 -0
  238. package/dist/whatsapp.d.ts +54 -0
  239. package/dist/whatsapp.js +2 -0
  240. package/dist/whatsapp.js.map +1 -0
  241. package/dist/whatsapp.mjs +2 -0
  242. package/dist/whatsapp.mjs.map +1 -0
  243. package/package.json +51 -7
  244. package/src/api/base.ts +19 -0
  245. package/src/api/encryption.ts +34 -0
  246. package/src/api/flow.ts +253 -0
  247. package/src/api/index.ts +24 -0
  248. package/src/api/media.ts +52 -0
  249. package/src/api/messages.ts +160 -0
  250. package/src/api/phoneNumber.ts +59 -0
  251. package/src/api/qrCode.ts +64 -0
  252. package/src/api/registration.ts +38 -0
  253. package/src/api/template.ts +70 -0
  254. package/src/api/twoStepVerification.ts +24 -0
  255. package/src/api/waba.ts +55 -0
  256. package/src/httpsClient.ts +83 -0
  257. package/src/index.ts +3 -0
  258. package/src/request.ts +104 -0
  259. package/src/types/base.ts +5 -0
  260. package/src/types/config.ts +68 -0
  261. package/src/types/encryption.ts +11 -0
  262. package/src/types/enums.ts +563 -0
  263. package/src/types/flow.ts +274 -0
  264. package/src/types/httpsClient.ts +28 -0
  265. package/src/types/index.ts +16 -0
  266. package/src/types/logger.ts +7 -0
  267. package/src/types/media.ts +31 -0
  268. package/src/types/messages.ts +453 -0
  269. package/src/types/phoneNumber.ts +111 -0
  270. package/src/types/qrCode.ts +30 -0
  271. package/src/types/registration.ts +17 -0
  272. package/src/types/request.ts +66 -0
  273. package/src/types/template.ts +134 -0
  274. package/src/types/twoStepVerification.ts +9 -0
  275. package/src/types/waba.ts +18 -0
  276. package/src/types/whatsapp.ts +31 -0
  277. package/src/utils/.gitkeep +0 -0
  278. package/src/utils/generateXHub256Sig.ts +5 -0
  279. package/src/utils/importConfig.ts +55 -0
  280. package/src/utils/index.ts +2 -0
  281. package/src/utils/isMetaError.ts +44 -0
  282. package/src/utils/logger.ts +46 -0
  283. package/src/utils/objectToQueryString.ts +16 -0
  284. package/src/whatsapp.ts +96 -0
@@ -0,0 +1,70 @@
1
+ import { objectToQueryString } from 'src/utils/objectToQueryString';
2
+ import type { WabaConfigType } from '../types/config';
3
+ import { HttpMethodsEnum, WabaConfigEnum } from '../types/enums';
4
+ import type { RequesterClass, RequesterResponseInterface, ResponsePagination, ResponseSuccess } from '../types/request';
5
+ import type {
6
+ TemplateClass,
7
+ TemplateDeleteParams,
8
+ TemplateGetParams,
9
+ TemplateRequestBody,
10
+ TemplateResponse,
11
+ } from '../types/template';
12
+ import BaseAPI from './base';
13
+
14
+ export default class TemplateApi extends BaseAPI implements TemplateClass {
15
+ private readonly endpoint = 'message_templates';
16
+
17
+ constructor(config: WabaConfigType, client: RequesterClass) {
18
+ super(config, client);
19
+ }
20
+
21
+ async getTemplate(templateId: string): Promise<RequesterResponseInterface<TemplateResponse>> {
22
+ return this.client.sendRequest(
23
+ HttpMethodsEnum.Get,
24
+ `${templateId}`,
25
+ this.config[WabaConfigEnum.RequestTimeout],
26
+ null,
27
+ );
28
+ }
29
+
30
+ async updateTemplate(
31
+ templateId: string,
32
+ template: Partial<TemplateRequestBody>,
33
+ ): Promise<RequesterResponseInterface<ResponseSuccess>> {
34
+ return this.client.sendRequest(
35
+ HttpMethodsEnum.Post,
36
+ `${templateId}`,
37
+ this.config[WabaConfigEnum.RequestTimeout],
38
+ JSON.stringify(template),
39
+ );
40
+ }
41
+
42
+ async getTemplates(
43
+ params: TemplateGetParams,
44
+ ): Promise<RequesterResponseInterface<ResponsePagination<TemplateResponse>>> {
45
+ return this.client.sendRequest(
46
+ HttpMethodsEnum.Get,
47
+ `${this.config[WabaConfigEnum.BusinessAcctId]}/${this.endpoint}${objectToQueryString(params)}`,
48
+ this.config[WabaConfigEnum.RequestTimeout],
49
+ null,
50
+ );
51
+ }
52
+
53
+ async createTemplate(template: TemplateRequestBody): Promise<RequesterResponseInterface<TemplateResponse>> {
54
+ return this.client.sendRequest(
55
+ HttpMethodsEnum.Post,
56
+ `${this.config[WabaConfigEnum.BusinessAcctId]}/${this.endpoint}`,
57
+ this.config[WabaConfigEnum.RequestTimeout],
58
+ JSON.stringify(template),
59
+ );
60
+ }
61
+
62
+ async deleteTemplate(params: TemplateDeleteParams): Promise<RequesterResponseInterface<ResponseSuccess>> {
63
+ return this.client.sendRequest(
64
+ HttpMethodsEnum.Delete,
65
+ `${this.config[WabaConfigEnum.BusinessAcctId]}/${this.endpoint}${objectToQueryString(params)}`,
66
+ this.config[WabaConfigEnum.RequestTimeout],
67
+ null,
68
+ );
69
+ }
70
+ }
@@ -0,0 +1,24 @@
1
+ import { TwoStepVerificationClass } from '../types/twoStepVerification';
2
+ import type { WabaConfigType } from '../types/config';
3
+ import { HttpMethodsEnum, WabaConfigEnum } from '../types/enums';
4
+ import type { RequesterClass, RequesterResponseInterface, ResponseSuccess } from '../types/request';
5
+ import BaseAPI from './base';
6
+
7
+ export default class TwoStepVerificationAPI extends BaseAPI implements TwoStepVerificationClass {
8
+ constructor(config: WabaConfigType, client: RequesterClass) {
9
+ super(config, client);
10
+ }
11
+
12
+ async setTwoStepVerificationCode(pin: string): Promise<RequesterResponseInterface<ResponseSuccess>> {
13
+ const body = {
14
+ pin,
15
+ };
16
+
17
+ return this.client.sendRequest(
18
+ HttpMethodsEnum.Post,
19
+ `${this.config[WabaConfigEnum.PhoneNumberId]}`,
20
+ this.config[WabaConfigEnum.RequestTimeout],
21
+ JSON.stringify(body),
22
+ );
23
+ }
24
+ }
@@ -0,0 +1,55 @@
1
+ import { HttpMethodsEnum, WabaConfigEnum } from '../types/enums';
2
+ import type { WabaConfigType } from '../types/config';
3
+ import type { RequesterClass, RequesterResponseInterface, ResponseSuccess } from '../types/request';
4
+ import BaseAPI from './base';
5
+ import { WABAClass, WabaSubscriptions } from '../types/waba';
6
+
7
+ export default class WabaAPI extends BaseAPI implements WABAClass {
8
+ constructor(config: WabaConfigType, client: RequesterClass) {
9
+ super(config, client);
10
+ }
11
+
12
+ async getAllWabaSubscriptions(): Promise<RequesterResponseInterface<WabaSubscriptions>> {
13
+ return this.client.sendRequest(
14
+ HttpMethodsEnum.Get,
15
+ `${this.config[WabaConfigEnum.BusinessAcctId]}/subscribed_apps`,
16
+ this.config[WabaConfigEnum.RequestTimeout],
17
+ null,
18
+ );
19
+ }
20
+
21
+ async subscribeToWaba(): Promise<RequesterResponseInterface<ResponseSuccess>> {
22
+ return this.client.sendRequest(
23
+ HttpMethodsEnum.Post,
24
+ `${this.config[WabaConfigEnum.BusinessAcctId]}/subscribed_apps`,
25
+ this.config[WabaConfigEnum.RequestTimeout],
26
+ null,
27
+ );
28
+ }
29
+
30
+ async unsubscribeFromWaba(): Promise<RequesterResponseInterface<ResponseSuccess>> {
31
+ return this.client.sendRequest(
32
+ HttpMethodsEnum.Delete,
33
+ `${this.config[WabaConfigEnum.BusinessAcctId]}/subscribed_apps`,
34
+ this.config[WabaConfigEnum.RequestTimeout],
35
+ null,
36
+ );
37
+ }
38
+
39
+ async overrideWabaWebhook(
40
+ webhookUrl: string,
41
+ verifyToken: string,
42
+ ): Promise<RequesterResponseInterface<ResponseSuccess>> {
43
+ const body = {
44
+ url: webhookUrl,
45
+ verify_token: verifyToken,
46
+ };
47
+
48
+ return this.client.sendRequest(
49
+ HttpMethodsEnum.Post,
50
+ `${this.config[WabaConfigEnum.BusinessAcctId]}/webhooks`,
51
+ this.config[WabaConfigEnum.RequestTimeout],
52
+ JSON.stringify(body),
53
+ );
54
+ }
55
+ }
@@ -0,0 +1,83 @@
1
+ import { Agent } from 'https';
2
+
3
+ import Logger from './utils/logger';
4
+ import { HttpMethodsEnum } from './types/enums';
5
+ import { HttpsClientClass, HttpsClientResponseClass, ResponseHeaders, ResponseJSONBody } from './types/httpsClient';
6
+
7
+ const LIB_NAME = 'HttpsClient';
8
+ const LOG_LOCAL = false;
9
+ const LOGGER = new Logger(LIB_NAME, process.env.DEBUG === 'true' || LOG_LOCAL);
10
+
11
+ export default class HttpsClient implements HttpsClientClass {
12
+ agent: Agent;
13
+
14
+ constructor() {
15
+ this.agent = new Agent({ keepAlive: true });
16
+ }
17
+
18
+ clearSockets(): boolean {
19
+ this.agent.destroy();
20
+ return true;
21
+ }
22
+
23
+ async sendRequest(
24
+ hostname: string,
25
+ path: string,
26
+ method: HttpMethodsEnum,
27
+ headers: HeadersInit,
28
+ timeout: number,
29
+ body?: BodyInit | null,
30
+ ): Promise<HttpsClientResponseClass> {
31
+ const url = `https://${hostname}/${path}`;
32
+ const controller = new AbortController();
33
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
34
+
35
+ try {
36
+ const response = await fetch(url, {
37
+ method,
38
+ headers,
39
+ body,
40
+ signal: controller.signal,
41
+ });
42
+ LOGGER.log(`${method} : ${url} - ${JSON.stringify(response)}`);
43
+
44
+ clearTimeout(timeoutId);
45
+ return new HttpsClientResponse(response);
46
+ } catch (error) {
47
+ throw error;
48
+ }
49
+ }
50
+ }
51
+
52
+ export class HttpsClientResponse implements HttpsClientResponseClass {
53
+ res: Response;
54
+ respStatusCode: number;
55
+ respHeaders: ResponseHeaders;
56
+
57
+ constructor(resp: Response) {
58
+ this.res = resp;
59
+ this.respStatusCode = resp.status;
60
+ this.respHeaders = Object.fromEntries(resp.headers.entries());
61
+ }
62
+
63
+ statusCode(): number {
64
+ return this.respStatusCode;
65
+ }
66
+
67
+ headers(): ResponseHeaders {
68
+ return this.respHeaders;
69
+ }
70
+
71
+ rawResponse(): Response {
72
+ return this.res;
73
+ }
74
+
75
+ async json(): Promise<ResponseJSONBody> {
76
+ try {
77
+ return (await this.res.json()) as ResponseJSONBody;
78
+ } catch (err) {
79
+ // TODO Error Handling
80
+ throw new Error('Failed to parse response body to JSON: ' + (err as Error).message);
81
+ }
82
+ }
83
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import WhatsApp from './whatsapp';
2
+
3
+ export default WhatsApp;
package/src/request.ts ADDED
@@ -0,0 +1,104 @@
1
+ import HttpsClient from './httpsClient';
2
+ import Logger from './utils/logger';
3
+ import type { HttpMethodsEnum } from './types/enums';
4
+ import type { RequesterClass } from './types/request';
5
+ import { MetaError } from './utils/isMetaError';
6
+ import { isMetaError } from './utils/isMetaError';
7
+
8
+ const LIB_NAME = 'REQUESTER';
9
+ const LOG_LOCAL = false;
10
+ const LOGGER = new Logger(LIB_NAME, process.env.DEBUG === 'true' || LOG_LOCAL);
11
+
12
+ export default class Requester implements RequesterClass {
13
+ client: Readonly<HttpsClient>;
14
+ accessToken: Readonly<string>;
15
+ phoneNumberId: Readonly<number>;
16
+ businessAcctId: Readonly<string>;
17
+ apiVersion: Readonly<string>;
18
+ userAgent: Readonly<string>;
19
+ host: Readonly<string>;
20
+ protocol: Readonly<string> = 'https:';
21
+
22
+ constructor(
23
+ host: string,
24
+ apiVersion: string,
25
+ phoneNumberId: number,
26
+ accessToken: string,
27
+ businessAcctId: string,
28
+ userAgent: string,
29
+ ) {
30
+ this.client = new HttpsClient();
31
+ this.host = host;
32
+ this.apiVersion = apiVersion;
33
+ this.phoneNumberId = phoneNumberId;
34
+ this.accessToken = accessToken;
35
+ this.businessAcctId = businessAcctId;
36
+ this.userAgent = userAgent;
37
+ }
38
+
39
+ buildHeader(contentType: string): HeadersInit {
40
+ const headers: HeadersInit = {
41
+ 'Content-Type': contentType,
42
+ Authorization: `Bearer ${this.accessToken}`,
43
+ 'User-Agent': this.userAgent,
44
+ };
45
+ return headers;
46
+ }
47
+
48
+ buildCAPIPath(endpoint: string): string {
49
+ return `v${this.apiVersion}.0/${endpoint}`;
50
+ }
51
+
52
+ async sendRequest(method: HttpMethodsEnum, endpoint: string, timeout: number, body?: any) {
53
+ const contentType = 'application/json';
54
+ const path = `${this.protocol.toLowerCase()}//${this.host}/${this.buildCAPIPath(endpoint)}`;
55
+
56
+ LOGGER.log(`${method} : ${path}`);
57
+
58
+ try {
59
+ const response = await this.client.sendRequest(
60
+ this.host,
61
+ this.buildCAPIPath(endpoint),
62
+ method,
63
+ this.buildHeader(contentType),
64
+ timeout,
65
+ method === 'POST' || method === 'PUT' ? body : undefined,
66
+ );
67
+
68
+ if (!response.rawResponse().ok) {
69
+ const errorData = await response.json();
70
+ if (isMetaError(errorData)) {
71
+ throw errorData;
72
+ }
73
+ // If the error doesn't match Meta's format, create a generic MetaError
74
+ throw {
75
+ name: 'MetaError',
76
+ message: 'Unknown error occurred',
77
+ error: {
78
+ message: errorData.message || 'Unknown error occurred',
79
+ type: 'UnknownError',
80
+ code: response.statusCode(),
81
+ fbtrace_id: '',
82
+ },
83
+ } as MetaError;
84
+ }
85
+
86
+ return response;
87
+ } catch (error) {
88
+ if (isMetaError(error)) {
89
+ throw error;
90
+ }
91
+ // Handle network errors or other unexpected errors
92
+ throw {
93
+ name: 'MetaError',
94
+ message: error instanceof Error ? error.message : 'Network error occurred',
95
+ error: {
96
+ message: error instanceof Error ? error.message : 'Network error occurred',
97
+ type: 'NetworkError',
98
+ code: 500,
99
+ fbtrace_id: '',
100
+ },
101
+ } as MetaError;
102
+ }
103
+ }
104
+ }
@@ -0,0 +1,5 @@
1
+ import type { WabaConfigType } from './config';
2
+
3
+ export declare class BaseClass {
4
+ constructor(config: WabaConfigType);
5
+ }
@@ -0,0 +1,68 @@
1
+ import { WabaConfigEnum } from './enums';
2
+
3
+ export type WabaConfigType = {
4
+ /**
5
+ * That base URL of the Cloud API, hosted by Meta.
6
+ * @default 'graph.facebook.com/'
7
+ */
8
+ [WabaConfigEnum.BaseURL]: string;
9
+
10
+ /**
11
+ * The Meta for Developers business application Id for this registered application.
12
+ */
13
+ [WabaConfigEnum.AppId]: string;
14
+
15
+ /**
16
+ * The Meta for Developers business application secret for this registered application.
17
+ */
18
+ [WabaConfigEnum.AppSecret]: string;
19
+
20
+ /**
21
+ * The Meta for Developers phone number id used by the registered business.
22
+ */
23
+ [WabaConfigEnum.PhoneNumberId]: number;
24
+
25
+ /**
26
+ * The Meta for Developers business id for the registered business.
27
+ */
28
+ [WabaConfigEnum.BusinessAcctId]: string;
29
+ /**
30
+ * The version of the Cloud API being used. Starts with a "v" and follows the major number.
31
+ */
32
+ [WabaConfigEnum.APIVersion]: string;
33
+
34
+ /**
35
+ * The access token to make calls on behalf of the signed in Meta for Developers account or business.
36
+ */
37
+ [WabaConfigEnum.AccessToken]: string;
38
+
39
+ /**
40
+ * The endpoint path (e.g. if the value here is webhook, the webhook URL would look like http/https://{host}/webhook).
41
+ */
42
+ [WabaConfigEnum.WebhookEndpoint]: string;
43
+
44
+ /**
45
+ * The verification token that needs to match what is sent by the Cloud API webhook in order to subscribe.
46
+ */
47
+ [WabaConfigEnum.WebhookVerificationToken]: string;
48
+
49
+ /**
50
+ * The listener port for the webhook web server.
51
+ */
52
+ [WabaConfigEnum.ListenerPort]: number;
53
+
54
+ /**
55
+ * To turn on global debugging of the logger to print verbose output across the APIs.
56
+ */
57
+ [WabaConfigEnum.Debug]: boolean;
58
+
59
+ /**
60
+ * The total number of times a request should be retried after the wait period if it fails.
61
+ */
62
+ [WabaConfigEnum.MaxRetriesAfterWait]: number;
63
+
64
+ /**
65
+ * The timeout period for a request to quit and destroy the attempt in ms.
66
+ */
67
+ [WabaConfigEnum.RequestTimeout]: number;
68
+ };
@@ -0,0 +1,11 @@
1
+ import { ResponseData, ResponseSuccess, RequesterResponseInterface } from './request';
2
+
3
+ export type EncryptionPublicKeyResponse = ResponseData<{
4
+ business_public_key: string;
5
+ business_public_key_signature_status: 'VALID' | 'MISMATCH';
6
+ }>;
7
+
8
+ export interface EncryptionClass {
9
+ getEncryptionPublicKey(): Promise<RequesterResponseInterface<EncryptionPublicKeyResponse>>;
10
+ setEncryptionPublicKey(businessPublicKey: string): Promise<RequesterResponseInterface<ResponseSuccess>>;
11
+ }