plazbot
Version:
Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.
56 lines (55 loc) • 1.96 kB
JavaScript
;
// ============================================================================
// Plazbot Workers - defineSync()
// Crea un sincronizador de datos externos hacia contactos de Plazbot
// ============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.defineSync = defineSync;
/**
* Define un sync que trae datos de una API externa y los mapea a contactos de Plazbot.
*
* Estructura fija: `fetch` obtiene registros → `map` los transforma → Plazbot aplica.
* Maneja cursor, match de contactos, crear/update y estadisticas automaticamente.
*
* Diferencia con defineSchedule: el sync tiene logica built-in para sincronizacion
* (cursor, match, create/update, stats). El schedule es generico.
*
* @example
* ```typescript
* import { defineSync } from 'plazbot/workers';
*
* export default defineSync<{ email: string; plan: string }>({
* name: 'stripe-customers',
* reference: 'Sincroniza clientes de Stripe',
* source: 'Stripe',
* schedule: '0 * * * *', // cada hora
* async fetch(plz) {
* const res = await plz.fetch('https://api.stripe.com/v1/customers', {
* headers: { Authorization: `Bearer ${plz.env.STRIPE_KEY}` },
* });
* return (await res.json()).data;
* },
* map(customer) {
* return {
* match: { email: customer.email },
* variables: { stripe_plan: customer.plan },
* contact: { tags: ['stripe-synced'] },
* };
* },
* });
* ```
*/
function defineSync(config) {
if (!config.name)
throw new Error('defineSync: "name" es requerido');
if (!config.schedule)
throw new Error('defineSync: "schedule" es requerido');
if (!config.fetch)
throw new Error('defineSync: "fetch" es requerido');
if (!config.map)
throw new Error('defineSync: "map" es requerido');
return {
__type: 'sync',
config,
};
}