@talqui-oss/template-builder 0.0.0-stage → 0.1.0-beta.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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Talqui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/adapters/meta.ts
21
+ var meta_exports = {};
22
+ __export(meta_exports, {
23
+ meta: () => meta
24
+ });
25
+ module.exports = __toCommonJS(meta_exports);
26
+
27
+ // src/validate/text.ts
28
+ var PLACEHOLDER = /\{\{(.*?)\}\}/g;
29
+ var VARIABLE_NAME = /^[a-z][a-z0-9_]*$/;
30
+ function placeholders(text) {
31
+ return [...text.matchAll(PLACEHOLDER)].map((match) => {
32
+ const name = match[1] ?? "";
33
+ return { raw: match[0], name, valid: VARIABLE_NAME.test(name) };
34
+ });
35
+ }
36
+
37
+ // src/adapters/types.ts
38
+ function defineAdapter(definition) {
39
+ return (...[options]) => ({
40
+ name: definition.name,
41
+ toRequests: (template) => definition.toRequests(template, options ?? {})
42
+ });
43
+ }
44
+ var UnsupportedByProviderError = class extends Error {
45
+ constructor(provider, path, message) {
46
+ super(`${provider}: ${message} (${path})`);
47
+ this.provider = provider;
48
+ this.path = path;
49
+ this.name = "UnsupportedByProviderError";
50
+ }
51
+ provider;
52
+ path;
53
+ code = "UNSUPPORTED_BY_PROVIDER";
54
+ };
55
+ function unsupported(provider, path, what) {
56
+ throw new UnsupportedByProviderError(
57
+ provider,
58
+ path,
59
+ `${what} is not supported by this provider.`
60
+ );
61
+ }
62
+
63
+ // src/adapters/meta.ts
64
+ var NAME = "meta";
65
+ var MEDIA_HANDLE = "$MEDIA_HANDLE";
66
+ var meta = defineAdapter({
67
+ name: NAME,
68
+ toRequests(template, {
69
+ wabaId = "$WABA_ID",
70
+ accessToken = "$META_ACCESS_TOKEN",
71
+ apiVersion = "v26.0"
72
+ }) {
73
+ const body2 = template.category === "AUTHENTICATION" ? authentication(template) : standard(template);
74
+ const media = mediaUrls(template);
75
+ return [
76
+ {
77
+ method: "POST",
78
+ url: `https://graph.facebook.com/${apiVersion}/${wabaId}/message_templates`,
79
+ headers: {
80
+ Authorization: `Bearer ${accessToken}`,
81
+ "Content-Type": "application/json"
82
+ },
83
+ body: body2,
84
+ comments: media.length > 0 ? [
85
+ `Upload each sample file with the Resumable Upload API and set ${MEDIA_HANDLE} to the handle it returns:`,
86
+ "https://developers.facebook.com/docs/graph-api/guides/upload",
87
+ ...media.map((url) => `- ${url}`)
88
+ ] : void 0
89
+ }
90
+ ];
91
+ }
92
+ });
93
+ function standard(t) {
94
+ if (t.displayFormat)
95
+ unsupported(NAME, "displayFormat", "The ORDER_DETAILS display format");
96
+ return {
97
+ name: t.name,
98
+ language: t.language,
99
+ category: t.category,
100
+ parameter_format: "NAMED",
101
+ message_send_ttl_seconds: t.messageSendTtlSeconds,
102
+ components: [
103
+ t.header && header(t.header, t.variables),
104
+ t.limitedTimeOffer && {
105
+ type: "LIMITED_TIME_OFFER",
106
+ limited_time_offer: {
107
+ text: t.limitedTimeOffer.text,
108
+ has_expiration: t.limitedTimeOffer.hasExpiration ?? false
109
+ }
110
+ },
111
+ body(t.body.text, t.variables),
112
+ t.footer && { type: "FOOTER", text: t.footer.text },
113
+ t.buttons?.length && buttons(t.buttons, t.variables, "buttons"),
114
+ t.carousel && {
115
+ type: "CAROUSEL",
116
+ cards: t.carousel.cards.map(
117
+ (card, index) => carouselCard(card, t.variables, `carousel.cards.${index}`)
118
+ )
119
+ },
120
+ t.callPermissionRequest && { type: "CALL_PERMISSION_REQUEST" }
121
+ ].filter(Boolean)
122
+ };
123
+ }
124
+ function authentication(t) {
125
+ const button = t.button;
126
+ return {
127
+ name: t.name,
128
+ language: t.language,
129
+ category: t.category,
130
+ message_send_ttl_seconds: t.messageSendTtlSeconds,
131
+ components: [
132
+ {
133
+ type: "BODY",
134
+ add_security_recommendation: t.body?.addSecurityRecommendation
135
+ },
136
+ t.footer?.codeExpirationMinutes !== void 0 && {
137
+ type: "FOOTER",
138
+ code_expiration_minutes: t.footer.codeExpirationMinutes
139
+ },
140
+ {
141
+ type: "BUTTONS",
142
+ buttons: [
143
+ {
144
+ type: "OTP",
145
+ otp_type: button.otpType,
146
+ text: button.text,
147
+ autofill_text: button.autofillText,
148
+ supported_apps: button.supportedApps?.map((app) => ({
149
+ package_name: app.packageName,
150
+ signature_hash: app.signatureHash
151
+ })),
152
+ zero_tap_terms_accepted: button.zeroTapTermsAccepted
153
+ }
154
+ ]
155
+ }
156
+ ].filter(Boolean)
157
+ };
158
+ }
159
+ var namedParams = (text, variables = {}) => [...new Set(placeholders(text).map((p) => p.name))].map((name) => ({
160
+ param_name: name,
161
+ example: variables[name]?.example ?? ""
162
+ }));
163
+ function header(h, variables) {
164
+ if (h.type === "PRODUCT")
165
+ return unsupported(NAME, "header", "A PRODUCT header");
166
+ if (h.type === "LOCATION") return { type: "HEADER", format: "LOCATION" };
167
+ if (h.type !== "TEXT")
168
+ return {
169
+ type: "HEADER",
170
+ format: h.type,
171
+ example: { header_handle: [MEDIA_HANDLE] }
172
+ };
173
+ const params = namedParams(h.text, variables);
174
+ return {
175
+ type: "HEADER",
176
+ format: "TEXT",
177
+ text: h.text,
178
+ example: params.length ? { header_text_named_params: params } : void 0
179
+ };
180
+ }
181
+ function body(text, variables) {
182
+ const params = namedParams(text, variables);
183
+ return {
184
+ type: "BODY",
185
+ text,
186
+ example: params.length ? { body_text_named_params: params } : void 0
187
+ };
188
+ }
189
+ function buttons(list, variables = {}, path) {
190
+ return {
191
+ type: "BUTTONS",
192
+ buttons: list.map((b, index) => {
193
+ switch (b.type) {
194
+ case "QUICK_REPLY":
195
+ return { type: b.type, text: b.text };
196
+ case "URL": {
197
+ const [param] = placeholders(b.url);
198
+ return {
199
+ type: b.type,
200
+ text: b.text,
201
+ url: param ? b.url.replace(param.raw, "{{1}}") : b.url,
202
+ example: param ? [b.url.replace(param.raw, variables[param.name]?.example ?? "")] : void 0
203
+ };
204
+ }
205
+ case "PHONE_NUMBER":
206
+ return { type: b.type, text: b.text, phone_number: b.phoneNumber };
207
+ case "VOICE_CALL":
208
+ return { type: b.type, text: b.text, ttl_minutes: b.ttlMinutes };
209
+ case "COPY_CODE":
210
+ return { type: b.type, example: b.example };
211
+ default:
212
+ return unsupported(NAME, `${path}.${index}`, `The ${b.type} button`);
213
+ }
214
+ })
215
+ };
216
+ }
217
+ function carouselCard(card, variables = {}, path) {
218
+ if (card.header.type === "PRODUCT")
219
+ unsupported(NAME, `${path}.header`, "A PRODUCT card header");
220
+ return {
221
+ components: [
222
+ {
223
+ type: "HEADER",
224
+ format: card.header.type,
225
+ example: { header_handle: [MEDIA_HANDLE] }
226
+ },
227
+ card.body && body(card.body.text, variables),
228
+ card.buttons?.length && buttons(card.buttons, variables, `${path}.buttons`)
229
+ ].filter(Boolean)
230
+ };
231
+ }
232
+ function mediaUrls(t) {
233
+ if (t.category === "AUTHENTICATION") return [];
234
+ const urls = t.header && "url" in t.header ? [t.header.url] : [];
235
+ for (const card of t.carousel?.cards ?? [])
236
+ if ("url" in card.header) urls.push(card.header.url);
237
+ return urls;
238
+ }
239
+ // Annotate the CommonJS export names for ESM import in node:
240
+ 0 && (module.exports = {
241
+ meta
242
+ });
@@ -0,0 +1,15 @@
1
+ import { Adapter } from '../index.cjs';
2
+
3
+ type MetaOptions = {
4
+ /** WhatsApp Business Account ID. Default: `$WABA_ID`. */
5
+ wabaId?: string;
6
+ /** Default: `$META_ACCESS_TOKEN`. */
7
+ accessToken?: string;
8
+ /** Graph API version. Default: `v26.0`. */
9
+ apiVersion?: string;
10
+ };
11
+ /** Meta Cloud API: `POST /{WABA_ID}/message_templates`. */
12
+ declare const meta: (options?: MetaOptions | undefined) => Adapter;
13
+
14
+ export { meta };
15
+ export type { MetaOptions };
@@ -0,0 +1,15 @@
1
+ import { Adapter } from '../index.js';
2
+
3
+ type MetaOptions = {
4
+ /** WhatsApp Business Account ID. Default: `$WABA_ID`. */
5
+ wabaId?: string;
6
+ /** Default: `$META_ACCESS_TOKEN`. */
7
+ accessToken?: string;
8
+ /** Graph API version. Default: `v26.0`. */
9
+ apiVersion?: string;
10
+ };
11
+ /** Meta Cloud API: `POST /{WABA_ID}/message_templates`. */
12
+ declare const meta: (options?: MetaOptions | undefined) => Adapter;
13
+
14
+ export { meta };
15
+ export type { MetaOptions };
@@ -0,0 +1,185 @@
1
+ import {
2
+ defineAdapter,
3
+ placeholders,
4
+ unsupported
5
+ } from "../chunk-ULTGMISN.js";
6
+
7
+ // src/adapters/meta.ts
8
+ var NAME = "meta";
9
+ var MEDIA_HANDLE = "$MEDIA_HANDLE";
10
+ var meta = defineAdapter({
11
+ name: NAME,
12
+ toRequests(template, {
13
+ wabaId = "$WABA_ID",
14
+ accessToken = "$META_ACCESS_TOKEN",
15
+ apiVersion = "v26.0"
16
+ }) {
17
+ const body2 = template.category === "AUTHENTICATION" ? authentication(template) : standard(template);
18
+ const media = mediaUrls(template);
19
+ return [
20
+ {
21
+ method: "POST",
22
+ url: `https://graph.facebook.com/${apiVersion}/${wabaId}/message_templates`,
23
+ headers: {
24
+ Authorization: `Bearer ${accessToken}`,
25
+ "Content-Type": "application/json"
26
+ },
27
+ body: body2,
28
+ comments: media.length > 0 ? [
29
+ `Upload each sample file with the Resumable Upload API and set ${MEDIA_HANDLE} to the handle it returns:`,
30
+ "https://developers.facebook.com/docs/graph-api/guides/upload",
31
+ ...media.map((url) => `- ${url}`)
32
+ ] : void 0
33
+ }
34
+ ];
35
+ }
36
+ });
37
+ function standard(t) {
38
+ if (t.displayFormat)
39
+ unsupported(NAME, "displayFormat", "The ORDER_DETAILS display format");
40
+ return {
41
+ name: t.name,
42
+ language: t.language,
43
+ category: t.category,
44
+ parameter_format: "NAMED",
45
+ message_send_ttl_seconds: t.messageSendTtlSeconds,
46
+ components: [
47
+ t.header && header(t.header, t.variables),
48
+ t.limitedTimeOffer && {
49
+ type: "LIMITED_TIME_OFFER",
50
+ limited_time_offer: {
51
+ text: t.limitedTimeOffer.text,
52
+ has_expiration: t.limitedTimeOffer.hasExpiration ?? false
53
+ }
54
+ },
55
+ body(t.body.text, t.variables),
56
+ t.footer && { type: "FOOTER", text: t.footer.text },
57
+ t.buttons?.length && buttons(t.buttons, t.variables, "buttons"),
58
+ t.carousel && {
59
+ type: "CAROUSEL",
60
+ cards: t.carousel.cards.map(
61
+ (card, index) => carouselCard(card, t.variables, `carousel.cards.${index}`)
62
+ )
63
+ },
64
+ t.callPermissionRequest && { type: "CALL_PERMISSION_REQUEST" }
65
+ ].filter(Boolean)
66
+ };
67
+ }
68
+ function authentication(t) {
69
+ const button = t.button;
70
+ return {
71
+ name: t.name,
72
+ language: t.language,
73
+ category: t.category,
74
+ message_send_ttl_seconds: t.messageSendTtlSeconds,
75
+ components: [
76
+ {
77
+ type: "BODY",
78
+ add_security_recommendation: t.body?.addSecurityRecommendation
79
+ },
80
+ t.footer?.codeExpirationMinutes !== void 0 && {
81
+ type: "FOOTER",
82
+ code_expiration_minutes: t.footer.codeExpirationMinutes
83
+ },
84
+ {
85
+ type: "BUTTONS",
86
+ buttons: [
87
+ {
88
+ type: "OTP",
89
+ otp_type: button.otpType,
90
+ text: button.text,
91
+ autofill_text: button.autofillText,
92
+ supported_apps: button.supportedApps?.map((app) => ({
93
+ package_name: app.packageName,
94
+ signature_hash: app.signatureHash
95
+ })),
96
+ zero_tap_terms_accepted: button.zeroTapTermsAccepted
97
+ }
98
+ ]
99
+ }
100
+ ].filter(Boolean)
101
+ };
102
+ }
103
+ var namedParams = (text, variables = {}) => [...new Set(placeholders(text).map((p) => p.name))].map((name) => ({
104
+ param_name: name,
105
+ example: variables[name]?.example ?? ""
106
+ }));
107
+ function header(h, variables) {
108
+ if (h.type === "PRODUCT")
109
+ return unsupported(NAME, "header", "A PRODUCT header");
110
+ if (h.type === "LOCATION") return { type: "HEADER", format: "LOCATION" };
111
+ if (h.type !== "TEXT")
112
+ return {
113
+ type: "HEADER",
114
+ format: h.type,
115
+ example: { header_handle: [MEDIA_HANDLE] }
116
+ };
117
+ const params = namedParams(h.text, variables);
118
+ return {
119
+ type: "HEADER",
120
+ format: "TEXT",
121
+ text: h.text,
122
+ example: params.length ? { header_text_named_params: params } : void 0
123
+ };
124
+ }
125
+ function body(text, variables) {
126
+ const params = namedParams(text, variables);
127
+ return {
128
+ type: "BODY",
129
+ text,
130
+ example: params.length ? { body_text_named_params: params } : void 0
131
+ };
132
+ }
133
+ function buttons(list, variables = {}, path) {
134
+ return {
135
+ type: "BUTTONS",
136
+ buttons: list.map((b, index) => {
137
+ switch (b.type) {
138
+ case "QUICK_REPLY":
139
+ return { type: b.type, text: b.text };
140
+ case "URL": {
141
+ const [param] = placeholders(b.url);
142
+ return {
143
+ type: b.type,
144
+ text: b.text,
145
+ url: param ? b.url.replace(param.raw, "{{1}}") : b.url,
146
+ example: param ? [b.url.replace(param.raw, variables[param.name]?.example ?? "")] : void 0
147
+ };
148
+ }
149
+ case "PHONE_NUMBER":
150
+ return { type: b.type, text: b.text, phone_number: b.phoneNumber };
151
+ case "VOICE_CALL":
152
+ return { type: b.type, text: b.text, ttl_minutes: b.ttlMinutes };
153
+ case "COPY_CODE":
154
+ return { type: b.type, example: b.example };
155
+ default:
156
+ return unsupported(NAME, `${path}.${index}`, `The ${b.type} button`);
157
+ }
158
+ })
159
+ };
160
+ }
161
+ function carouselCard(card, variables = {}, path) {
162
+ if (card.header.type === "PRODUCT")
163
+ unsupported(NAME, `${path}.header`, "A PRODUCT card header");
164
+ return {
165
+ components: [
166
+ {
167
+ type: "HEADER",
168
+ format: card.header.type,
169
+ example: { header_handle: [MEDIA_HANDLE] }
170
+ },
171
+ card.body && body(card.body.text, variables),
172
+ card.buttons?.length && buttons(card.buttons, variables, `${path}.buttons`)
173
+ ].filter(Boolean)
174
+ };
175
+ }
176
+ function mediaUrls(t) {
177
+ if (t.category === "AUTHENTICATION") return [];
178
+ const urls = t.header && "url" in t.header ? [t.header.url] : [];
179
+ for (const card of t.carousel?.cards ?? [])
180
+ if ("url" in card.header) urls.push(card.header.url);
181
+ return urls;
182
+ }
183
+ export {
184
+ meta
185
+ };