Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

refactor(webhooks): add a dedicated webhook response api formatter (#1026)

Corentin Thomasset5f8e2f63751a
diff --git a/apps/papra-server/src/modules/webhooks/webhooks.models.test.ts b/apps/papra-server/src/modules/webhooks/webhooks.models.test.ts
new file mode 100644
index 00000000..e3077c21
--- /dev/null
+++ b/apps/papra-server/src/modules/webhooks/webhooks.models.test.ts
@@ -0,0 +1,58 @@
+import { describe, expect, test } from 'vitest';
+import { formatWebhookForApi } from './webhooks.models';
+
+describe('webhooks formatter', () => {
+ describe('formatWebhookForApi', () => {
+ test('removes the secret field from a webhook', () => {
+ expect(
+ formatWebhookForApi({
+ id: 'wbh_1',
+ name: 'My Webhook',
+ url: 'https://example.com/hook',
+ secret: 's3cr3t',
+ enabled: true,
+ organizationId: 'org_1',
+ createdBy: 'user_1',
+ createdAt: new Date('2024-01-01'),
+ updatedAt: new Date('2024-01-01'),
+ }),
+ ).to.eql({
+ id: 'wbh_1',
+ name: 'My Webhook',
+ url: 'https://example.com/hook',
+ enabled: true,
+ organizationId: 'org_1',
+ createdBy: 'user_1',
+ createdAt: new Date('2024-01-01'),
+ updatedAt: new Date('2024-01-01'),
+ });
+ });
+
+ test('preserve extra fields if present', () => {
+ expect(
+ formatWebhookForApi({
+ id: 'wbh_1',
+ name: 'My Webhook',
+ url: 'https://example.com/hook',
+ secret: 's3cr3t',
+ enabled: true,
+ organizationId: 'org_1',
+ createdBy: 'user_1',
+ events: ['document:created', 'document:updated'], // Extra field
+ createdAt: new Date('2024-01-01'),
+ updatedAt: new Date('2024-01-01'),
+ }),
+ ).to.eql({
+ id: 'wbh_1',
+ name: 'My Webhook',
+ url: 'https://example.com/hook',
+ enabled: true,
+ organizationId: 'org_1',
+ events: ['document:created', 'document:updated'],
+ createdBy: 'user_1',
+ createdAt: new Date('2024-01-01'),
+ updatedAt: new Date('2024-01-01'),
+ });
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/webhooks/webhooks.models.ts b/apps/papra-server/src/modules/webhooks/webhooks.models.ts
new file mode 100644
index 00000000..ce4290a4
--- /dev/null
+++ b/apps/papra-server/src/modules/webhooks/webhooks.models.ts
@@ -0,0 +1,6 @@
+import type { Webhook } from './webhooks.types';
+import { omit } from '../shared/objects';
+
+export function formatWebhookForApi<T extends Webhook>(webhook: T) {
+ return omit(webhook, ['secret']);
+}
diff --git a/apps/papra-server/src/modules/webhooks/webhooks.repository.ts b/apps/papra-server/src/modules/webhooks/webhooks.repository.ts
index 44292d6f..7d1a2577 100644
--- a/apps/papra-server/src/modules/webhooks/webhooks.repository.ts
+++ b/apps/papra-server/src/modules/webhooks/webhooks.repository.ts
@@ -105,7 +105,13 @@ async function getOrganizationWebhookById({ db, webhookId, organizationId }: { d
}
const [{ webhook } = {}] = records;
- const events = records.map(record => record.webhookEvents?.eventName);
+
+ if (!webhook) {
+ // For type safety
+ return { webhook: undefined };
+ }
+
+ const events = records.map(record => record.webhookEvents?.eventName).filter(Boolean);
return { webhook: { ...webhook, events } };
}
diff --git a/apps/papra-server/src/modules/webhooks/webhooks.routes.ts b/apps/papra-server/src/modules/webhooks/webhooks.routes.ts
index d5439dcf..e6a4cca0 100644
--- a/apps/papra-server/src/modules/webhooks/webhooks.routes.ts
+++ b/apps/papra-server/src/modules/webhooks/webhooks.routes.ts
@@ -5,9 +5,9 @@ import { getUser } from '../app/auth/auth.models';
import { organizationIdSchema } from '../organizations/organization.schemas';
import { createOrganizationsRepository } from '../organizations/organizations.repository';
import { ensureUserIsInOrganization } from '../organizations/organizations.usecases';
-import { omit } from '../shared/objects';
import { validateJsonBody, validateParams } from '../shared/validation/validation';
import { createWebhookNotFoundError } from './webhooks.errors';
+import { formatWebhookForApi } from './webhooks.models';
import { createWebhookRepository } from './webhooks.repository';
import { webhookEventListSchema, webhookIdSchema, webhookNameSchema, webhookSecretSchema, webhookUrlSchema } from './webhooks.schemas';
import { createWebhook, updateWebhook } from './webhooks.usecases';
@@ -57,7 +57,7 @@ function setupCreateWebhookRoute({ app, db, config }: RouteDefinitionContext) {
});
return context.json({
- webhook,
+ webhook: formatWebhookForApi(webhook),
});
},
);
@@ -82,7 +82,7 @@ function setupGetWebhooksRoute({ app, db }: RouteDefinitionContext) {
const { webhooks } = await webhookRepository.getOrganizationWebhooks({ organizationId });
return context.json({
- webhooks: webhooks.map(webhook => omit(webhook, ['secret'])),
+ webhooks: webhooks.map(formatWebhookForApi),
});
},
);
@@ -112,7 +112,7 @@ function setupGetWebhookRoute({ app, db }: RouteDefinitionContext) {
}
return context.json({
- webhook: omit(webhook, ['secret']),
+ webhook: formatWebhookForApi(webhook),
});
},
);
@@ -155,7 +155,7 @@ function setupUpdateWebhookRoute({ app, db, config }: RouteDefinitionContext) {
});
return context.json({
- webhook: omit(webhook, ['secret']),
+ webhook: formatWebhookForApi(webhook),
});
},
);