supa-seed
Version:
A constraint-aware, framework-agnostic database seeding framework with deep PostgreSQL business logic discovery and MakerKit integration support
195 lines • 7.04 kB
JavaScript
;
/**
* Development Webhook Manager
* Manages webhook endpoints for development and testing
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevelopmentWebhookManager = void 0;
const logger_1 = require("../core/utils/logger");
class DevelopmentWebhookManager {
constructor(client, config) {
this.endpoints = [];
this.client = client;
this.config = config || {
enabled: false,
baseUrl: 'http://localhost:3000',
endpoints: [],
authentication: { type: 'none' },
events: ['user.created', 'user.updated', 'profile.created']
};
}
/**
* Configure webhook settings
*/
async configure(config) {
this.config = { ...this.config, ...config };
logger_1.Logger.info('🔗 Webhook configuration updated');
}
/**
* Setup development webhook endpoints
*/
async setupDevelopmentEndpoints() {
const errors = [];
if (!this.config.enabled) {
return {
success: false,
endpoints: [],
errors: ['Webhooks are disabled']
};
}
try {
// Create default endpoints if none specified
if (!this.config.endpoints || this.config.endpoints.length === 0) {
this.endpoints = [
{
id: 'dev-user-events',
url: `${this.config.baseUrl}/webhooks/users`,
events: ['user.created', 'user.updated'],
enabled: true,
headers: {
'Content-Type': 'application/json',
...(this.config.authentication?.token && {
'Authorization': `Bearer ${this.config.authentication.token}`
})
}
},
{
id: 'dev-profile-events',
url: `${this.config.baseUrl}/webhooks/profiles`,
events: ['profile.created', 'profile.updated'],
enabled: true,
headers: {
'Content-Type': 'application/json'
}
}
];
}
else {
this.endpoints = this.config.endpoints.map((url, index) => ({
id: `endpoint-${index}`,
url,
events: this.config.events || ['user.created'],
enabled: true,
headers: { 'Content-Type': 'application/json' }
}));
}
logger_1.Logger.success(`✅ Setup ${this.endpoints.length} webhook endpoints`);
return {
success: true,
endpoints: this.endpoints,
errors: []
};
}
catch (error) {
const errorMsg = `Failed to setup webhook endpoints: ${error.message}`;
errors.push(errorMsg);
logger_1.Logger.error(errorMsg);
return {
success: false,
endpoints: [],
errors
};
}
}
/**
* Generate platform-specific webhook configuration
*/
generatePlatformWebhookConfig(architecture = 'individual', domain = 'generic') {
const baseUrl = this.config.baseUrl || 'http://localhost:3000';
const endpoints = [
{
id: `${architecture}-${domain}-users`,
url: `${baseUrl}/webhooks/${domain}/users`,
events: ['user.created', 'user.updated', 'user.deleted'],
enabled: true
},
{
id: `${architecture}-${domain}-profiles`,
url: `${baseUrl}/webhooks/${domain}/profiles`,
events: ['profile.created', 'profile.updated'],
enabled: true
}
];
// Add architecture-specific endpoints
if (architecture === 'team' || architecture === 'hybrid') {
endpoints.push({
id: `${architecture}-${domain}-teams`,
url: `${baseUrl}/webhooks/${domain}/teams`,
events: ['team.created', 'team.updated', 'member.added', 'member.removed'],
enabled: true
});
}
// Add domain-specific endpoints
if (domain === 'ecommerce') {
endpoints.push({
id: `${architecture}-${domain}-orders`,
url: `${baseUrl}/webhooks/${domain}/orders`,
events: ['order.created', 'order.updated', 'order.completed'],
enabled: true
});
}
return {
architecture,
domain,
endpoints,
defaultEvents: ['user.created', 'profile.created']
};
}
/**
* Get current webhook configuration
*/
getConfiguration() {
return {
enabled: this.config.enabled,
endpoints: this.endpoints,
defaultHeaders: {
'Content-Type': 'application/json',
'User-Agent': 'SupaSeed-Webhooks/2.4.2'
}
};
}
/**
* List all configured endpoints
*/
async listEndpoints() {
return this.endpoints;
}
/**
* Trigger a webhook for a specific event
*/
async triggerWebhook(event, payload) {
if (!this.config.enabled) {
logger_1.Logger.debug('Webhooks disabled - skipping trigger');
return [];
}
const relevantEndpoints = this.endpoints.filter(ep => ep.enabled && ep.events.includes(event));
if (relevantEndpoints.length === 0) {
logger_1.Logger.debug(`No endpoints configured for event: ${event}`);
return [];
}
const results = [];
for (const endpoint of relevantEndpoints) {
try {
logger_1.Logger.debug(`Triggering webhook: ${endpoint.url} for event: ${event}`);
// In a real implementation, this would make HTTP requests
// For now, we'll simulate the webhook trigger
results.push({
success: true,
status: 200,
response: { message: 'Webhook triggered successfully (simulated)' }
});
}
catch (error) {
logger_1.Logger.error(`Webhook trigger failed for ${endpoint.url}: ${error.message}`);
results.push({
success: false,
status: 500,
error: error.message
});
}
}
return results;
}
}
exports.DevelopmentWebhookManager = DevelopmentWebhookManager;
//# sourceMappingURL=development-webhook-manager.js.map