tyntec-sdk
Version:
TypeScript SDK for Tyntec Conversations API V3
213 lines (212 loc) • 9.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.whatsAppTemplatesSchema = exports.whatsAppTemplateResponseSchema = exports.localizationResponseSchema = exports.componentResponseSchema = exports.templateCarouselComponentResponseSchema = exports.carouselCardComponentResponseSchema = exports.templateButtonsComponentResponseSchema = exports.buttonResponseSchema = exports.templateFooterComponentResponseSchema = exports.templateBodyComponentResponseSchema = exports.templateHeaderComponentResponseSchema = exports.templatePatchRequestSchema = exports.whatsAppTemplateRequestSchema = exports.localizationRequestSchema = exports.componentRequestSchema = exports.templateCarouselComponentRequestSchema = exports.templateButtonsComponentRequestSchema = exports.templateManagementButtonSchema = exports.templateFooterComponentRequestSchema = exports.templateBodyComponentRequestSchema = exports.templateHeaderComponentRequestSchema = exports.languageCodeSchema = exports.componentTypeSchema = exports.templateStatusSchema = exports.templateCategorySchema = void 0;
const zod_1 = require("zod");
// Enums and constants
exports.templateCategorySchema = zod_1.z.enum(['AUTHENTICATION', 'MARKETING', 'UTILITY']);
exports.templateStatusSchema = zod_1.z.enum(['APPROVED', 'PENDING', 'REJECTED', 'DISABLED', 'DELETED']);
exports.componentTypeSchema = zod_1.z.enum(['HEADER', 'BODY', 'FOOTER', 'BUTTONS', 'CAROUSEL']);
exports.languageCodeSchema = zod_1.z.string(); // ISO 639-1 codes like "en", "de", etc.
// Header component request schemas
const textHeaderComponentRequestSchema = zod_1.z.object({
type: zod_1.z.literal('TEXT'),
text: zod_1.z.string().optional(),
example: zod_1.z
.object({
texts: zod_1.z.array(zod_1.z.string()),
})
.optional(),
});
const mediaHeaderComponentRequestSchema = zod_1.z.object({
type: zod_1.z.enum(['IMAGE', 'VIDEO', 'DOCUMENT']),
example: zod_1.z.object({
url: zod_1.z.string().url(),
}),
});
const locationHeaderComponentRequestSchema = zod_1.z.object({
type: zod_1.z.literal('LOCATION'),
});
exports.templateHeaderComponentRequestSchema = zod_1.z.discriminatedUnion('type', [
textHeaderComponentRequestSchema,
mediaHeaderComponentRequestSchema,
locationHeaderComponentRequestSchema,
]);
// Body component request schema
exports.templateBodyComponentRequestSchema = zod_1.z.object({
type: zod_1.z.literal('BODY'),
text: zod_1.z.string().optional(),
addSecurityRecommendation: zod_1.z.boolean().optional(),
example: zod_1.z
.object({
texts: zod_1.z.array(zod_1.z.string()),
})
.optional(),
});
// Footer component request schema
exports.templateFooterComponentRequestSchema = zod_1.z.object({
type: zod_1.z.literal('FOOTER'),
text: zod_1.z.string().optional(),
codeExpirationMinutes: zod_1.z.number().optional(),
});
// Button component request schemas
const quickReplyButtonSchema = zod_1.z.object({
type: zod_1.z.literal('QUICK_REPLY'),
text: zod_1.z.string(),
});
const urlButtonSchema = zod_1.z.object({
type: zod_1.z.literal('URL'),
text: zod_1.z.string(),
url: zod_1.z.string().url(),
example: zod_1.z.array(zod_1.z.string()).optional(),
});
const phoneNumberButtonSchema = zod_1.z.object({
type: zod_1.z.literal('PHONE_NUMBER'),
text: zod_1.z.string(),
phoneNumber: zod_1.z.string(),
});
const otpButtonSchema = zod_1.z.object({
type: zod_1.z.literal('OTP'),
otpType: zod_1.z.enum(['COPY_CODE', 'ONE_TAP', 'ZERO_TAP']),
text: zod_1.z.string().optional(),
autofillButtonText: zod_1.z.string().optional(),
packageName: zod_1.z.string().optional(),
signatureHash: zod_1.z.string().optional(),
});
const flowButtonSchema = zod_1.z.object({
type: zod_1.z.literal('FLOW'),
text: zod_1.z.string(),
flowId: zod_1.z.string().optional(),
flowAction: zod_1.z.enum(['navigate', 'data_exchange']).optional(),
navigateScreen: zod_1.z.string().optional(),
});
exports.templateManagementButtonSchema = zod_1.z.discriminatedUnion('type', [
quickReplyButtonSchema,
urlButtonSchema,
phoneNumberButtonSchema,
otpButtonSchema,
flowButtonSchema,
]);
// Buttons component request schema
exports.templateButtonsComponentRequestSchema = zod_1.z.object({
type: zod_1.z.literal('BUTTONS'),
buttons: zod_1.z.array(exports.templateManagementButtonSchema),
});
// Carousel component request schemas
const carouselCardComponentSchema = zod_1.z.object({
components: zod_1.z.array(zod_1.z.union([
exports.templateHeaderComponentRequestSchema,
exports.templateBodyComponentRequestSchema,
exports.templateButtonsComponentRequestSchema,
])),
});
exports.templateCarouselComponentRequestSchema = zod_1.z.object({
type: zod_1.z.literal('CAROUSEL'),
cards: zod_1.z.array(carouselCardComponentSchema),
});
// Combined component request schema
exports.componentRequestSchema = zod_1.z.union([
exports.templateHeaderComponentRequestSchema,
exports.templateBodyComponentRequestSchema,
exports.templateFooterComponentRequestSchema,
exports.templateButtonsComponentRequestSchema,
exports.templateCarouselComponentRequestSchema,
]);
// Localization request schema
exports.localizationRequestSchema = zod_1.z.object({
language: exports.languageCodeSchema,
components: zod_1.z.array(exports.componentRequestSchema),
});
// Template request schema (for creating templates)
exports.whatsAppTemplateRequestSchema = zod_1.z.object({
name: zod_1.z.string(),
category: exports.templateCategorySchema,
allowCategoryChange: zod_1.z.boolean().optional(),
localizations: zod_1.z.array(exports.localizationRequestSchema),
});
// Template patch request schema (for editing templates)
exports.templatePatchRequestSchema = zod_1.z.object({
category: exports.templateCategorySchema.optional(),
});
// Response schemas
exports.templateHeaderComponentResponseSchema = zod_1.z.object({
type: zod_1.z.enum(['TEXT', 'IMAGE', 'VIDEO', 'DOCUMENT', 'LOCATION']),
text: zod_1.z.string().optional(),
format: zod_1.z.string().optional(),
example: zod_1.z
.object({
headerText: zod_1.z.array(zod_1.z.string()).optional(),
headerHandle: zod_1.z.array(zod_1.z.string()).optional(),
})
.optional(),
});
exports.templateBodyComponentResponseSchema = zod_1.z.object({
type: zod_1.z.literal('BODY'),
text: zod_1.z.string(),
example: zod_1.z
.object({
bodyText: zod_1.z.array(zod_1.z.array(zod_1.z.string())),
})
.optional(),
});
exports.templateFooterComponentResponseSchema = zod_1.z.object({
type: zod_1.z.literal('FOOTER'),
text: zod_1.z.string(),
codeExpirationMinutes: zod_1.z.number().optional(),
});
exports.buttonResponseSchema = zod_1.z.object({
type: zod_1.z.enum(['QUICK_REPLY', 'URL', 'PHONE_NUMBER', 'OTP', 'FLOW']),
text: zod_1.z.string(),
url: zod_1.z.string().optional(),
phoneNumber: zod_1.z.string().optional(),
example: zod_1.z.array(zod_1.z.string()).optional(),
otpType: zod_1.z.enum(['COPY_CODE', 'ONE_TAP', 'ZERO_TAP']).optional(),
autofillButtonText: zod_1.z.string().optional(),
packageName: zod_1.z.string().optional(),
signatureHash: zod_1.z.string().optional(),
flowId: zod_1.z.string().optional(),
flowAction: zod_1.z.string().optional(),
navigateScreen: zod_1.z.string().optional(),
});
exports.templateButtonsComponentResponseSchema = zod_1.z.object({
type: zod_1.z.literal('BUTTONS'),
buttons: zod_1.z.array(exports.buttonResponseSchema),
});
exports.carouselCardComponentResponseSchema = zod_1.z.object({
components: zod_1.z.array(zod_1.z.union([
exports.templateHeaderComponentResponseSchema,
exports.templateBodyComponentResponseSchema,
exports.templateButtonsComponentResponseSchema,
])),
});
exports.templateCarouselComponentResponseSchema = zod_1.z.object({
type: zod_1.z.literal('CAROUSEL'),
cards: zod_1.z.array(exports.carouselCardComponentResponseSchema),
});
exports.componentResponseSchema = zod_1.z.union([
exports.templateHeaderComponentResponseSchema,
exports.templateBodyComponentResponseSchema,
exports.templateFooterComponentResponseSchema,
exports.templateButtonsComponentResponseSchema,
exports.templateCarouselComponentResponseSchema,
]);
// Localization response schema
exports.localizationResponseSchema = zod_1.z.object({
language: exports.languageCodeSchema,
status: exports.templateStatusSchema,
components: zod_1.z.array(exports.componentResponseSchema),
rejectionReason: zod_1.z.string().optional(),
});
// Template response schema
exports.whatsAppTemplateResponseSchema = zod_1.z.object({
id: zod_1.z.string().optional(),
name: zod_1.z.string(),
category: exports.templateCategorySchema,
status: exports.templateStatusSchema.optional(),
language: exports.languageCodeSchema.optional(),
localizations: zod_1.z.array(exports.localizationResponseSchema).optional(),
rejectionReason: zod_1.z.string().optional(),
});
// List templates response schema
exports.whatsAppTemplatesSchema = zod_1.z.object({
templates: zod_1.z.array(exports.whatsAppTemplateResponseSchema),
});