From b28c62946b1376e5ea8fa1d32c566bb3ff4dc7ce Mon Sep 17 00:00:00 2001 From: nael Date: Thu, 19 Sep 2024 03:21:12 +0200 Subject: [PATCH] :bug: Fix sdk docs --- .../panora-webhooks/dto/webhook.dto.ts | 8 +--- .../panora-webhooks/webhook.service.ts | 44 ++++++++++++++++++- packages/api/swagger/swagger-spec.yaml | 6 --- 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/packages/api/src/@core/@core-services/webhooks/panora-webhooks/dto/webhook.dto.ts b/packages/api/src/@core/@core-services/webhooks/panora-webhooks/dto/webhook.dto.ts index 42059c7d0..cf59516e6 100644 --- a/packages/api/src/@core/@core-services/webhooks/panora-webhooks/dto/webhook.dto.ts +++ b/packages/api/src/@core/@core-services/webhooks/panora-webhooks/dto/webhook.dto.ts @@ -43,13 +43,7 @@ export class EventPayload { }) type: string; - @ApiProperty({ - type: Object, - additionalProperties: true, - nullable: true, - description: 'The data payload event of the webhook.', - }) - data: { [key: string]: any }; + [key: string]: any; } export class SignatureVerificationDto { diff --git a/packages/api/src/@core/@core-services/webhooks/panora-webhooks/webhook.service.ts b/packages/api/src/@core/@core-services/webhooks/panora-webhooks/webhook.service.ts index 45ac0ed33..8cff872dd 100644 --- a/packages/api/src/@core/@core-services/webhooks/panora-webhooks/webhook.service.ts +++ b/packages/api/src/@core/@core-services/webhooks/panora-webhooks/webhook.service.ts @@ -18,6 +18,46 @@ export class WebhookService { this.logger.setContext(WebhookService.name); } + private safeSerialize(data: any): any { + return JSON.parse( + JSON.stringify(data, (key, value) => { + if (typeof value === 'bigint') { + return value.toString(); + } + if (value instanceof Date) { + return value.toISOString(); + } + if (typeof value === 'function') { + return value.toString(); + } + if (value === undefined) { + return null; + } + if (typeof value === 'symbol') { + return value.toString(); + } + if ( + value !== null && + typeof value === 'object' && + !Array.isArray(value) + ) { + if (value.toJSON && typeof value.toJSON === 'function') { + return value.toJSON(); + } + const proto = Object.getPrototypeOf(value); + if ( + proto && + proto.constructor && + proto.constructor.name !== 'Object' + ) { + return `[object ${proto.constructor.name}]`; + } + } + return value; + }), + ); + } + generateSignature(payload: any, secret: string): string { try { return createHmac('sha256', secret) @@ -121,11 +161,11 @@ export class WebhookService { if (!webhook) return; this.logger.log('handling webhook payload....'); - + const serializedData = this.safeSerialize(data); const w_payload = await this.prisma.webhooks_payloads.create({ data: { id_webhooks_payload: uuidv4(), - data: JSON.stringify(data), + data: JSON.stringify(serializedData), }, }); this.logger.log('handling webhook delivery....'); diff --git a/packages/api/swagger/swagger-spec.yaml b/packages/api/swagger/swagger-spec.yaml index 02bedaae9..c692f5d81 100644 --- a/packages/api/swagger/swagger-spec.yaml +++ b/packages/api/swagger/swagger-spec.yaml @@ -10007,15 +10007,9 @@ components: example: connection.created nullable: true description: The type of the event. - data: - type: object - additionalProperties: true - nullable: true - description: The data payload event of the webhook. required: - id_event - type - - data SignatureVerificationDto: type: object properties: