@webiny/webhooks 0.0.0-unstable.5387e5f7c8 → 0.0.0-unstable.676006d08e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/Webhooks.d.ts +6 -0
  2. package/Webhooks.js +11 -0
  3. package/Webhooks.js.map +1 -0
  4. package/admin/Extension.d.ts +1 -0
  5. package/admin/Extension.js +1 -0
  6. package/admin/WebhookRoutes.js +1 -1
  7. package/admin/WebhookRoutes.js.map +1 -1
  8. package/admin/Webhooks.js +1 -1
  9. package/admin/Webhooks.js.map +1 -1
  10. package/admin/presentation/WebhookDeliveriesPage/WebhookDeliveriesPagePresenter.d.ts +8 -0
  11. package/admin/presentation/WebhookDeliveriesPage/WebhookDeliveriesPagePresenter.js +64 -25
  12. package/admin/presentation/WebhookDeliveriesPage/WebhookDeliveriesPagePresenter.js.map +1 -1
  13. package/admin/presentation/WebhookDeliveriesPage/WebhookDeliveriesPagePresenter.test.d.ts +1 -0
  14. package/admin/presentation/WebhookDeliveriesPage/WebhookDeliveriesPagePresenter.test.js +119 -0
  15. package/admin/presentation/WebhookDeliveriesPage/WebhookDeliveriesPagePresenter.test.js.map +1 -0
  16. package/admin/presentation/WebhookDeliveriesPage/abstractions.d.ts +2 -0
  17. package/admin/presentation/WebhookDeliveriesPage/abstractions.js.map +1 -1
  18. package/admin/presentation/WebhookDeliveriesPage/components/DeliveryBottomInfoBar.js.map +1 -1
  19. package/admin/presentation/WebhookDeliveriesPage/components/WebhookDeliveriesPage.js +1 -0
  20. package/admin/presentation/WebhookDeliveriesPage/components/WebhookDeliveriesPage.js.map +1 -1
  21. package/admin/presentation/WebhookList/WebhookListPresenter.d.ts +1 -0
  22. package/admin/presentation/WebhookList/WebhookListPresenter.js +12 -1
  23. package/admin/presentation/WebhookList/WebhookListPresenter.js.map +1 -1
  24. package/admin/presentation/WebhookList/abstractions.d.ts +2 -0
  25. package/admin/presentation/WebhookList/abstractions.js.map +1 -1
  26. package/admin/presentation/WebhookList/components/WebhookListView.js +1 -0
  27. package/admin/presentation/WebhookList/components/WebhookListView.js.map +1 -1
  28. package/api/domain/errors.d.ts +12 -0
  29. package/api/domain/errors.js +12 -1
  30. package/api/domain/errors.js.map +1 -1
  31. package/api/features/CreateWebhook/schema.js +1 -1
  32. package/api/features/CreateWebhook/schema.js.map +1 -1
  33. package/api/features/DeleteWebhook/DeleteWebhookRepository.js +2 -1
  34. package/api/features/DeleteWebhook/DeleteWebhookRepository.js.map +1 -1
  35. package/api/features/GetWebhookSettings/GetWebhookSettingsUseCase.d.ts +14 -0
  36. package/api/features/GetWebhookSettings/GetWebhookSettingsUseCase.js +24 -0
  37. package/api/features/GetWebhookSettings/GetWebhookSettingsUseCase.js.map +1 -0
  38. package/api/features/GetWebhookSettings/abstractions.d.ts +14 -1
  39. package/api/features/GetWebhookSettings/abstractions.js +2 -1
  40. package/api/features/GetWebhookSettings/abstractions.js.map +1 -1
  41. package/api/features/GetWebhookSettings/feature.js +2 -0
  42. package/api/features/GetWebhookSettings/feature.js.map +1 -1
  43. package/api/features/ListWebhookDeliveries/ListWebhookDeliveriesRepository.js +7 -4
  44. package/api/features/ListWebhookDeliveries/ListWebhookDeliveriesRepository.js.map +1 -1
  45. package/api/features/ListWebhooks/ListWebhooksRepository.js +7 -4
  46. package/api/features/ListWebhooks/ListWebhooksRepository.js.map +1 -1
  47. package/api/features/ResendWebhookDelivery/ResendWebhookDeliveryUseCase.d.ts +3 -1
  48. package/api/features/ResendWebhookDelivery/ResendWebhookDeliveryUseCase.js +18 -5
  49. package/api/features/ResendWebhookDelivery/ResendWebhookDeliveryUseCase.js.map +1 -1
  50. package/api/features/ResendWebhookDelivery/abstractions.d.ts +2 -1
  51. package/api/features/ResendWebhookDelivery/abstractions.js.map +1 -1
  52. package/api/features/SendWebhookTask/SendWebhookTask.js +15 -1
  53. package/api/features/SendWebhookTask/SendWebhookTask.js.map +1 -1
  54. package/api/features/TriggerWebhook/TriggerWebhookUseCase.d.ts +3 -1
  55. package/api/features/TriggerWebhook/TriggerWebhookUseCase.js +18 -5
  56. package/api/features/TriggerWebhook/TriggerWebhookUseCase.js.map +1 -1
  57. package/api/features/TriggerWebhook/abstractions.d.ts +2 -1
  58. package/api/features/TriggerWebhook/abstractions.js.map +1 -1
  59. package/api/features/UpdateWebhook/schema.js +1 -1
  60. package/api/features/UpdateWebhook/schema.js.map +1 -1
  61. package/api/features/WebhookDeliver/WebhookDeliver.js +1 -0
  62. package/api/features/WebhookDeliver/WebhookDeliver.js.map +1 -1
  63. package/api/features/WebhookDispatcher/WebhookDispatcher.d.ts +5 -1
  64. package/api/features/WebhookDispatcher/WebhookDispatcher.js +25 -4
  65. package/api/features/WebhookDispatcher/WebhookDispatcher.js.map +1 -1
  66. package/api/graphql/WebhookCrudSchema.js +21 -6
  67. package/api/graphql/WebhookCrudSchema.js.map +1 -1
  68. package/api/graphql/WebhookSettingsSchema.js +2 -2
  69. package/api/graphql/WebhookSettingsSchema.js.map +1 -1
  70. package/api/utils/isValidEndpointUrl.js +29 -3
  71. package/api/utils/isValidEndpointUrl.js.map +1 -1
  72. package/index.d.ts +1 -0
  73. package/index.js +1 -0
  74. package/package.json +28 -26
@@ -1 +1 @@
1
- {"version":3,"file":"api/features/ResendWebhookDelivery/abstractions.js","sources":["../../../../src/api/features/ResendWebhookDelivery/abstractions.ts"],"sourcesContent":["import { createAbstraction, type Result } from \"@webiny/feature/api\";\nimport type {\n WebhookDeliveryNotFoundError,\n WebhookNotFoundError,\n WebhookPersistenceError,\n WebhookModelNotFoundError,\n WebhookNotAuthorizedError,\n WebhookValidationError\n} from \"~/api/domain/errors.js\";\n\ntype IError =\n | WebhookDeliveryNotFoundError\n | WebhookNotFoundError\n | WebhookPersistenceError\n | WebhookModelNotFoundError\n | WebhookNotAuthorizedError\n | WebhookValidationError;\n\nexport interface IResendWebhookDeliveryUseCase {\n execute(deliveryId: string): Promise<Result<boolean, IError>>;\n}\n\nexport const ResendWebhookDeliveryUseCase = createAbstraction<IResendWebhookDeliveryUseCase>(\n \"Webhooks/ResendWebhookDeliveryUseCase\"\n);\n\nexport namespace ResendWebhookDeliveryUseCase {\n export type Interface = IResendWebhookDeliveryUseCase;\n export type Error = IError;\n}\n"],"names":["ResendWebhookDeliveryUseCase","createAbstraction"],"mappings":";AAsBO,MAAMA,+BAA+BC,kBACxC"}
1
+ {"version":3,"file":"api/features/ResendWebhookDelivery/abstractions.js","sources":["../../../../src/api/features/ResendWebhookDelivery/abstractions.ts"],"sourcesContent":["import { createAbstraction, type Result } from \"@webiny/feature/api\";\nimport type {\n WebhookDeliveryNotFoundError,\n WebhookNotFoundError,\n WebhookPersistenceError,\n WebhookModelNotFoundError,\n WebhookNotAuthorizedError,\n WebhookValidationError\n} from \"~/api/domain/errors.js\";\nimport type { WebhookDeliveryNotQueuedError } from \"~/api/domain/errors.js\";\n\ntype IError =\n | WebhookDeliveryNotFoundError\n | WebhookNotFoundError\n | WebhookPersistenceError\n | WebhookModelNotFoundError\n | WebhookNotAuthorizedError\n | WebhookValidationError\n | WebhookDeliveryNotQueuedError;\n\nexport interface IResendWebhookDeliveryUseCase {\n execute(deliveryId: string): Promise<Result<boolean, IError>>;\n}\n\nexport const ResendWebhookDeliveryUseCase = createAbstraction<IResendWebhookDeliveryUseCase>(\n \"Webhooks/ResendWebhookDeliveryUseCase\"\n);\n\nexport namespace ResendWebhookDeliveryUseCase {\n export type Interface = IResendWebhookDeliveryUseCase;\n export type Error = IError;\n}\n"],"names":["ResendWebhookDeliveryUseCase","createAbstraction"],"mappings":";AAwBO,MAAMA,+BAA+BC,kBACxC"}
@@ -1,5 +1,6 @@
1
1
  import "@webiny/background-tasks/api/types.js";
2
2
  import { TaskDefinition, TaskHandler } from "@webiny/api-core/exports/api/tasks.js";
3
+ import { isValidEndpointUrl } from "../../utils/isValidEndpointUrl.js";
3
4
  import { TenantContext } from "@webiny/api-core/exports/api/tenancy.js";
4
5
  import { WebhookSignPayload } from "@webiny/api-core/features/webhooks/index.js";
5
6
  import { GetWebhookRepository } from "../GetWebhook/abstractions.js";
@@ -8,6 +9,11 @@ import { UpdateWebhookDeliveryRepository } from "../UpdateWebhookDelivery/abstra
8
9
  import { GetWebhookSettingsRepository } from "../GetWebhookSettings/abstractions.js";
9
10
  import { WebhookDeliver } from "../WebhookDeliver/abstractions.js";
10
11
  import { SEND_WEBHOOK_TASK } from "../../domain/constants.js";
12
+ const MAX_STORED_RESPONSE_BODY_LENGTH = 4096;
13
+ const truncateResponseBody = (body)=>{
14
+ if (!body || body.length <= MAX_STORED_RESPONSE_BODY_LENGTH) return body;
15
+ return `${body.slice(0, MAX_STORED_RESPONSE_BODY_LENGTH)}… (truncated)`;
16
+ };
11
17
  class SendWebhookTaskHandlerImpl {
12
18
  constructor(getWebhookRepository, getWebhookDeliveryRepository, updateDeliveryRepository, signPayload, tenantContext, getWebhookSettingsRepository, deliver){
13
19
  this.getWebhookRepository = getWebhookRepository;
@@ -67,6 +73,14 @@ class SendWebhookTaskHandlerImpl {
67
73
  "Content-Type": "application/json",
68
74
  ...signHeaders
69
75
  };
76
+ if (!isValidEndpointUrl(webhook.endpointUrl)) {
77
+ await this.updateDeliveryRepository.execute(input.deliveryId, {
78
+ status: "failed"
79
+ });
80
+ return controller.response.error({
81
+ message: `Webhook endpoint URL is not allowed: ${webhook.endpointUrl}`
82
+ });
83
+ }
70
84
  const result = await this.deliver.execute({
71
85
  url: webhook.endpointUrl,
72
86
  headers: requestHeaders,
@@ -81,7 +95,7 @@ class SendWebhookTaskHandlerImpl {
81
95
  requestHeaders,
82
96
  responseTime: result.responseTime,
83
97
  responseStatus: result.status,
84
- responseBody: result.body,
98
+ responseBody: truncateResponseBody(result.body),
85
99
  status: result.status > 0 ? "delivered" : "failed"
86
100
  });
87
101
  return controller.response.done();
@@ -1 +1 @@
1
- {"version":3,"file":"api/features/SendWebhookTask/SendWebhookTask.js","sources":["../../../../src/api/features/SendWebhookTask/SendWebhookTask.ts"],"sourcesContent":["import \"@webiny/background-tasks/api/types.js\";\nimport { TaskDefinition, TaskHandler } from \"@webiny/api-core/exports/api/tasks.js\";\nimport { TenantContext } from \"@webiny/api-core/exports/api/tenancy.js\";\nimport { WebhookSignPayload } from \"@webiny/api-core/features/webhooks/index.js\";\nimport { GetWebhookRepository } from \"~/api/features/GetWebhook/abstractions.js\";\nimport { GetWebhookDeliveryRepository } from \"~/api/features/GetWebhookDelivery/abstractions.js\";\nimport { UpdateWebhookDeliveryRepository } from \"~/api/features/UpdateWebhookDelivery/abstractions.js\";\nimport { GetWebhookSettingsRepository } from \"~/api/features/GetWebhookSettings/abstractions.js\";\nimport { WebhookDeliver } from \"~/api/features/WebhookDeliver/abstractions.js\";\nimport { SEND_WEBHOOK_TASK } from \"~/api/domain/constants.js\";\nimport type { ISendWebhookTaskInput, ISendWebhookTaskOutput, IWebhookPayload } from \"./types.js\";\nimport type { IWebhookSignPayloadHeaders } from \"@webiny/api-core/features/webhooks/WebhookSignPayload/abstractions.js\";\n\ntype IRunParams = TaskHandler.RunParams<ISendWebhookTaskInput, ISendWebhookTaskOutput>;\n\nclass SendWebhookTaskHandlerImpl implements TaskHandler.Interface<\n ISendWebhookTaskInput,\n ISendWebhookTaskOutput\n> {\n public constructor(\n private readonly getWebhookRepository: GetWebhookRepository.Interface,\n private readonly getWebhookDeliveryRepository: GetWebhookDeliveryRepository.Interface,\n private readonly updateDeliveryRepository: UpdateWebhookDeliveryRepository.Interface,\n private readonly signPayload: WebhookSignPayload.Interface,\n private readonly tenantContext: TenantContext.Interface,\n private readonly getWebhookSettingsRepository: GetWebhookSettingsRepository.Interface,\n private readonly deliver: WebhookDeliver.Interface\n ) {}\n\n public async run(params: IRunParams) {\n const { input, controller } = params;\n const taskId = controller.state.getTask().id;\n\n const delivery = await this.getWebhookDeliveryRepository.execute(input.deliveryId);\n if (delivery.isFail()) {\n return controller.response.error(delivery.error);\n }\n\n await this.updateDeliveryRepository.execute(input.deliveryId, {\n backgroundTaskId: taskId,\n status: \"delivering\"\n });\n\n const webhookResult = await this.getWebhookRepository.execute(input.webhookId);\n if (webhookResult.isFail()) {\n await this.updateDeliveryRepository.execute(input.deliveryId, { status: \"failed\" });\n return controller.response.error(webhookResult.error);\n }\n const webhook = webhookResult.value;\n\n const now = new Date();\n const payload: IWebhookPayload = {\n id: taskId,\n event: input.eventName,\n timestamp: now.toISOString(),\n webhookId: input.webhookId,\n deliveryId: input.deliveryId,\n tenant: this.tenantContext.getTenant().id,\n data: delivery.value.payload\n };\n const rawBody = JSON.stringify(payload);\n let signHeaders: IWebhookSignPayloadHeaders;\n\n const settingsResult = await this.getWebhookSettingsRepository.execute();\n if (settingsResult.isFail()) {\n await this.updateDeliveryRepository.execute(input.deliveryId, { status: \"failed\" });\n return controller.response.error(settingsResult.error);\n }\n const signingSecret = webhook.signingSecret || settingsResult.value.signingSecret || \"\";\n\n try {\n signHeaders = await this.signPayload.sign(taskId, now, rawBody, signingSecret);\n } catch (ex) {\n await this.updateDeliveryRepository.execute(input.deliveryId, { status: \"failed\" });\n return controller.response.error(ex);\n }\n\n const requestHeaders: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n ...signHeaders\n };\n\n const result = await this.deliver.execute({\n url: webhook.endpointUrl,\n headers: requestHeaders,\n body: rawBody,\n timeout: 600_000,\n maxRetries: 3,\n initialDelay: 1000,\n maxDelay: 30_000\n });\n\n await this.updateDeliveryRepository.execute(input.deliveryId, {\n payload,\n requestHeaders,\n responseTime: result.responseTime,\n responseStatus: result.status,\n responseBody: result.body,\n status: result.status > 0 ? \"delivered\" : \"failed\"\n });\n\n return controller.response.done();\n }\n}\n\nconst SendWebhookTaskHandler = TaskHandler.createImplementation({\n implementation: SendWebhookTaskHandlerImpl,\n dependencies: [\n GetWebhookRepository,\n GetWebhookDeliveryRepository,\n UpdateWebhookDeliveryRepository,\n WebhookSignPayload,\n TenantContext,\n GetWebhookSettingsRepository,\n WebhookDeliver\n ]\n});\n\nclass SendWebhookTaskDefinition implements TaskDefinition.Interface {\n public readonly id = SEND_WEBHOOK_TASK;\n public readonly title = \"Send Webhook\";\n public readonly maxIterations = 1;\n public readonly isPrivate = true;\n public readonly databaseLogs = false;\n public readonly description =\n \"POST a signed event payload to a webhook endpoint and log the delivery.\";\n public readonly selfCleanup = \"always\";\n\n handler = SendWebhookTaskHandler;\n}\n\nexport const SendWebhookTask = TaskDefinition.createImplementation({\n implementation: SendWebhookTaskDefinition,\n dependencies: []\n});\n"],"names":["SendWebhookTaskHandlerImpl","getWebhookRepository","getWebhookDeliveryRepository","updateDeliveryRepository","signPayload","tenantContext","getWebhookSettingsRepository","deliver","params","input","controller","taskId","delivery","webhookResult","webhook","now","Date","payload","rawBody","JSON","signHeaders","settingsResult","signingSecret","ex","requestHeaders","result","SendWebhookTaskHandler","TaskHandler","GetWebhookRepository","GetWebhookDeliveryRepository","UpdateWebhookDeliveryRepository","WebhookSignPayload","TenantContext","GetWebhookSettingsRepository","WebhookDeliver","SendWebhookTaskDefinition","SEND_WEBHOOK_TASK","SendWebhookTask","TaskDefinition"],"mappings":";;;;;;;;;;AAeA,MAAMA;IAIF,YACqBC,oBAAoD,EACpDC,4BAAoE,EACpEC,wBAAmE,EACnEC,WAAyC,EACzCC,aAAsC,EACtCC,4BAAoE,EACpEC,OAAiC,CACpD;aAPmBN,oBAAoB,GAApBA;aACAC,4BAA4B,GAA5BA;aACAC,wBAAwB,GAAxBA;aACAC,WAAW,GAAXA;aACAC,aAAa,GAAbA;aACAC,4BAA4B,GAA5BA;aACAC,OAAO,GAAPA;IAClB;IAEH,MAAa,IAAIC,MAAkB,EAAE;QACjC,MAAM,EAAEC,KAAK,EAAEC,UAAU,EAAE,GAAGF;QAC9B,MAAMG,SAASD,WAAW,KAAK,CAAC,OAAO,GAAG,EAAE;QAE5C,MAAME,WAAW,MAAM,IAAI,CAAC,4BAA4B,CAAC,OAAO,CAACH,MAAM,UAAU;QACjF,IAAIG,SAAS,MAAM,IACf,OAAOF,WAAW,QAAQ,CAAC,KAAK,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BAAE,QAAQ;YAAS;YACjF,OAAOC,WAAW,QAAQ,CAAC,KAAK,CAACa;QACrC;QAEA,MAAMC,iBAAyC;YAC3C,gBAAgB;YAChB,GAAGJ,WAAW;QAClB;QAEA,MAAMK,SAAS,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;YACtC,KAAKX,QAAQ,WAAW;YACxB,SAASU;YACT,MAAMN;YACN,SAAS;YACT,YAAY;YACZ,cAAc;YACd,UAAU;QACd;QAEA,MAAM,IAAI,CAAC,wBAAwB,CAAC,OAAO,CAACT,MAAM,UAAU,EAAE;YAC1DQ;YACAO;YACA,cAAcC,OAAO,YAAY;YACjC,gBAAgBA,OAAO,MAAM;YAC7B,cAAcA,OAAO,IAAI;YACzB,QAAQA,OAAO,MAAM,GAAG,IAAI,cAAc;QAC9C;QAEA,OAAOf,WAAW,QAAQ,CAAC,IAAI;IACnC;AACJ;AAEA,MAAMgB,yBAAyBC,YAAY,oBAAoB,CAAC;IAC5D,gBAAgB3B;IAChB,cAAc;QACV4B;QACAC;QACAC;QACAC;QACAC;QACAC;QACAC;KACH;AACL;AAEA,MAAMC;;aACc,EAAE,GAAGC;aACL,KAAK,GAAG;aACR,aAAa,GAAG;aAChB,SAAS,GAAG;aACZ,YAAY,GAAG;aACf,WAAW,GACvB;aACY,WAAW,GAAG;aAE9B,OAAO,GAAGV;;AACd;AAEO,MAAMW,kBAAkBC,eAAe,oBAAoB,CAAC;IAC/D,gBAAgBH;IAChB,cAAc,EAAE;AACpB"}
1
+ {"version":3,"file":"api/features/SendWebhookTask/SendWebhookTask.js","sources":["../../../../src/api/features/SendWebhookTask/SendWebhookTask.ts"],"sourcesContent":["import \"@webiny/background-tasks/api/types.js\";\nimport { TaskDefinition, TaskHandler } from \"@webiny/api-core/exports/api/tasks.js\";\nimport { isValidEndpointUrl } from \"~/api/utils/isValidEndpointUrl.js\";\nimport { TenantContext } from \"@webiny/api-core/exports/api/tenancy.js\";\nimport { WebhookSignPayload } from \"@webiny/api-core/features/webhooks/index.js\";\nimport { GetWebhookRepository } from \"~/api/features/GetWebhook/abstractions.js\";\nimport { GetWebhookDeliveryRepository } from \"~/api/features/GetWebhookDelivery/abstractions.js\";\nimport { UpdateWebhookDeliveryRepository } from \"~/api/features/UpdateWebhookDelivery/abstractions.js\";\nimport { GetWebhookSettingsRepository } from \"~/api/features/GetWebhookSettings/abstractions.js\";\nimport { WebhookDeliver } from \"~/api/features/WebhookDeliver/abstractions.js\";\nimport { SEND_WEBHOOK_TASK } from \"~/api/domain/constants.js\";\nimport type { ISendWebhookTaskInput, ISendWebhookTaskOutput, IWebhookPayload } from \"./types.js\";\nimport type { IWebhookSignPayloadHeaders } from \"@webiny/api-core/features/webhooks/WebhookSignPayload/abstractions.js\";\n\ntype IRunParams = TaskHandler.RunParams<ISendWebhookTaskInput, ISendWebhookTaskOutput>;\n\n/* The delivery log only needs enough of the response to debug the receiver. */\nconst MAX_STORED_RESPONSE_BODY_LENGTH = 4096;\nconst truncateResponseBody = (body: string | undefined) => {\n if (!body || body.length <= MAX_STORED_RESPONSE_BODY_LENGTH) {\n return body;\n }\n return `${body.slice(0, MAX_STORED_RESPONSE_BODY_LENGTH)}… (truncated)`;\n};\n\nclass SendWebhookTaskHandlerImpl implements TaskHandler.Interface<\n ISendWebhookTaskInput,\n ISendWebhookTaskOutput\n> {\n public constructor(\n private readonly getWebhookRepository: GetWebhookRepository.Interface,\n private readonly getWebhookDeliveryRepository: GetWebhookDeliveryRepository.Interface,\n private readonly updateDeliveryRepository: UpdateWebhookDeliveryRepository.Interface,\n private readonly signPayload: WebhookSignPayload.Interface,\n private readonly tenantContext: TenantContext.Interface,\n private readonly getWebhookSettingsRepository: GetWebhookSettingsRepository.Interface,\n private readonly deliver: WebhookDeliver.Interface\n ) {}\n\n public async run(params: IRunParams) {\n const { input, controller } = params;\n const taskId = controller.state.getTask().id;\n\n const delivery = await this.getWebhookDeliveryRepository.execute(input.deliveryId);\n if (delivery.isFail()) {\n return controller.response.error(delivery.error);\n }\n\n await this.updateDeliveryRepository.execute(input.deliveryId, {\n backgroundTaskId: taskId,\n status: \"delivering\"\n });\n\n const webhookResult = await this.getWebhookRepository.execute(input.webhookId);\n if (webhookResult.isFail()) {\n await this.updateDeliveryRepository.execute(input.deliveryId, { status: \"failed\" });\n return controller.response.error(webhookResult.error);\n }\n const webhook = webhookResult.value;\n\n const now = new Date();\n const payload: IWebhookPayload = {\n id: taskId,\n event: input.eventName,\n timestamp: now.toISOString(),\n webhookId: input.webhookId,\n deliveryId: input.deliveryId,\n tenant: this.tenantContext.getTenant().id,\n data: delivery.value.payload\n };\n const rawBody = JSON.stringify(payload);\n let signHeaders: IWebhookSignPayloadHeaders;\n\n const settingsResult = await this.getWebhookSettingsRepository.execute();\n if (settingsResult.isFail()) {\n await this.updateDeliveryRepository.execute(input.deliveryId, { status: \"failed\" });\n return controller.response.error(settingsResult.error);\n }\n const signingSecret = webhook.signingSecret || settingsResult.value.signingSecret || \"\";\n\n try {\n signHeaders = await this.signPayload.sign(taskId, now, rawBody, signingSecret);\n } catch (ex) {\n await this.updateDeliveryRepository.execute(input.deliveryId, { status: \"failed\" });\n return controller.response.error(ex);\n }\n\n const requestHeaders: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n ...signHeaders\n };\n\n // Webhooks stored before the endpoint rules were tightened may still point to a local address.\n if (!isValidEndpointUrl(webhook.endpointUrl)) {\n await this.updateDeliveryRepository.execute(input.deliveryId, { status: \"failed\" });\n return controller.response.error({\n message: `Webhook endpoint URL is not allowed: ${webhook.endpointUrl}`\n });\n }\n\n const result = await this.deliver.execute({\n url: webhook.endpointUrl,\n headers: requestHeaders,\n body: rawBody,\n timeout: 600_000,\n maxRetries: 3,\n initialDelay: 1000,\n maxDelay: 30_000\n });\n\n await this.updateDeliveryRepository.execute(input.deliveryId, {\n payload,\n requestHeaders,\n responseTime: result.responseTime,\n responseStatus: result.status,\n responseBody: truncateResponseBody(result.body),\n status: result.status > 0 ? \"delivered\" : \"failed\"\n });\n\n return controller.response.done();\n }\n}\n\nconst SendWebhookTaskHandler = TaskHandler.createImplementation({\n implementation: SendWebhookTaskHandlerImpl,\n dependencies: [\n GetWebhookRepository,\n GetWebhookDeliveryRepository,\n UpdateWebhookDeliveryRepository,\n WebhookSignPayload,\n TenantContext,\n GetWebhookSettingsRepository,\n WebhookDeliver\n ]\n});\n\nclass SendWebhookTaskDefinition implements TaskDefinition.Interface {\n public readonly id = SEND_WEBHOOK_TASK;\n public readonly title = \"Send Webhook\";\n public readonly maxIterations = 1;\n public readonly isPrivate = true;\n public readonly databaseLogs = false;\n public readonly description =\n \"POST a signed event payload to a webhook endpoint and log the delivery.\";\n public readonly selfCleanup = \"always\";\n\n handler = SendWebhookTaskHandler;\n}\n\nexport const SendWebhookTask = TaskDefinition.createImplementation({\n implementation: SendWebhookTaskDefinition,\n dependencies: []\n});\n"],"names":["MAX_STORED_RESPONSE_BODY_LENGTH","truncateResponseBody","body","SendWebhookTaskHandlerImpl","getWebhookRepository","getWebhookDeliveryRepository","updateDeliveryRepository","signPayload","tenantContext","getWebhookSettingsRepository","deliver","params","input","controller","taskId","delivery","webhookResult","webhook","now","Date","payload","rawBody","JSON","signHeaders","settingsResult","signingSecret","ex","requestHeaders","isValidEndpointUrl","result","SendWebhookTaskHandler","TaskHandler","GetWebhookRepository","GetWebhookDeliveryRepository","UpdateWebhookDeliveryRepository","WebhookSignPayload","TenantContext","GetWebhookSettingsRepository","WebhookDeliver","SendWebhookTaskDefinition","SEND_WEBHOOK_TASK","SendWebhookTask","TaskDefinition"],"mappings":";;;;;;;;;;;AAiBA,MAAMA,kCAAkC;AACxC,MAAMC,uBAAuB,CAACC;IAC1B,IAAI,CAACA,QAAQA,KAAK,MAAM,IAAIF,iCACxB,OAAOE;IAEX,OAAO,GAAGA,KAAK,KAAK,CAAC,GAAGF,iCAAiC,aAAa,CAAC;AAC3E;AAEA,MAAMG;IAIF,YACqBC,oBAAoD,EACpDC,4BAAoE,EACpEC,wBAAmE,EACn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OAAO,MAAM,GAAG,IAAI,cAAc;QAC9C;QAEA,OAAOhB,WAAW,QAAQ,CAAC,IAAI;IACnC;AACJ;AAEA,MAAMiB,yBAAyBC,YAAY,oBAAoB,CAAC;IAC5D,gBAAgB5B;IAChB,cAAc;QACV6B;QACAC;QACAC;QACAC;QACAC;QACAC;QACAC;KACH;AACL;AAEA,MAAMC;;aACc,EAAE,GAAGC;aACL,KAAK,GAAG;aACR,aAAa,GAAG;aAChB,SAAS,GAAG;aACZ,YAAY,GAAG;aACf,WAAW,GACvB;aACY,WAAW,GAAG;aAE9B,OAAO,GAAGV;;AACd;AAEO,MAAMW,kBAAkBC,eAAe,oBAAoB,CAAC;IAC/D,gBAAgBH;IAChB,cAAc,EAAE;AACpB"}
@@ -3,6 +3,7 @@ import { TaskService } from "@webiny/api-core/exports/api/tasks.js";
3
3
  import { TriggerWebhookUseCase as UseCaseAbstraction } from "./abstractions.js";
4
4
  import { GetWebhookRepository } from "../../../api/features/GetWebhook/abstractions.js";
5
5
  import { CreateWebhookDeliveryRepository } from "../../../api/features/CreateWebhookDelivery/abstractions.js";
6
+ import { UpdateWebhookDeliveryRepository } from "../../../api/features/UpdateWebhookDelivery/abstractions.js";
6
7
  import { GetWebhookSettingsRepository } from "../../../api/features/GetWebhookSettings/abstractions.js";
7
8
  import { WebhookPermissions } from "../../../api/features/WebhookPermissions/abstractions.js";
8
9
  import type { WebhookDelivery } from "../../../api/domain/WebhookDelivery.js";
@@ -12,7 +13,8 @@ declare class TriggerWebhookUseCaseImpl implements UseCaseAbstraction.Interface
12
13
  private readonly createDeliveryRepository;
13
14
  private readonly taskService;
14
15
  private readonly getSettingsRepository;
15
- constructor(permissions: WebhookPermissions.Interface, getWebhookRepository: GetWebhookRepository.Interface, createDeliveryRepository: CreateWebhookDeliveryRepository.Interface, taskService: TaskService.Interface, getSettingsRepository: GetWebhookSettingsRepository.Interface);
16
+ private readonly updateDeliveryRepository;
17
+ constructor(permissions: WebhookPermissions.Interface, getWebhookRepository: GetWebhookRepository.Interface, createDeliveryRepository: CreateWebhookDeliveryRepository.Interface, taskService: TaskService.Interface, getSettingsRepository: GetWebhookSettingsRepository.Interface, updateDeliveryRepository: UpdateWebhookDeliveryRepository.Interface);
16
18
  execute(webhookId: string, payload: Record<string, unknown>): Promise<Result<WebhookDelivery, UseCaseAbstraction.Error>>;
17
19
  }
18
20
  export declare const TriggerWebhookUseCase: typeof TriggerWebhookUseCaseImpl & {
@@ -4,17 +4,19 @@ import { TriggerWebhookUseCase } from "./abstractions.js";
4
4
  import { TriggerWebhookInputSchema } from "./schema.js";
5
5
  import { GetWebhookRepository } from "../GetWebhook/abstractions.js";
6
6
  import { CreateWebhookDeliveryRepository } from "../CreateWebhookDelivery/abstractions.js";
7
+ import { UpdateWebhookDeliveryRepository } from "../UpdateWebhookDelivery/abstractions.js";
7
8
  import { GetWebhookSettingsRepository } from "../GetWebhookSettings/abstractions.js";
8
9
  import { WebhookPermissions } from "../WebhookPermissions/abstractions.js";
9
- import { WebhookNotAuthorizedError, WebhookValidationError } from "../../domain/errors.js";
10
+ import { WebhookDeliveryNotQueuedError, WebhookNotAuthorizedError, WebhookValidationError } from "../../domain/errors.js";
10
11
  import { SEND_WEBHOOK_TASK, WEBHOOK_DELIVERY_MAX_RETENTION_DAYS } from "../../domain/constants.js";
11
12
  class TriggerWebhookUseCaseImpl {
12
- constructor(permissions, getWebhookRepository, createDeliveryRepository, taskService, getSettingsRepository){
13
+ constructor(permissions, getWebhookRepository, createDeliveryRepository, taskService, getSettingsRepository, updateDeliveryRepository){
13
14
  this.permissions = permissions;
14
15
  this.getWebhookRepository = getWebhookRepository;
15
16
  this.createDeliveryRepository = createDeliveryRepository;
16
17
  this.taskService = taskService;
17
18
  this.getSettingsRepository = getSettingsRepository;
19
+ this.updateDeliveryRepository = updateDeliveryRepository;
18
20
  }
19
21
  async execute(webhookId, payload) {
20
22
  if (!await this.permissions.canEdit("webhook")) return Result.fail(new WebhookNotAuthorizedError());
@@ -28,7 +30,7 @@ class TriggerWebhookUseCaseImpl {
28
30
  const webhook = webhookResult.value;
29
31
  const settingsResult = await this.getSettingsRepository.execute();
30
32
  const retentionDays = settingsResult.isOk() ? settingsResult.value.deliveryRetentionDays ?? WEBHOOK_DELIVERY_MAX_RETENTION_DAYS : WEBHOOK_DELIVERY_MAX_RETENTION_DAYS;
31
- const expiresAt = new Date(Date.now() + 24 * retentionDays * 3600000);
33
+ const expiresAt = new Date(Date.now() + 24 * retentionDays * 60 * 60 * 1000);
32
34
  const deliveryResult = await this.createDeliveryRepository.execute({
33
35
  webhookId: webhook.id,
34
36
  eventType: "webhook.test",
@@ -38,7 +40,7 @@ class TriggerWebhookUseCaseImpl {
38
40
  });
39
41
  if (deliveryResult.isFail()) return Result.fail(deliveryResult.error);
40
42
  const delivery = deliveryResult.value;
41
- await this.taskService.trigger({
43
+ const triggered = await this.taskService.trigger({
42
44
  definition: SEND_WEBHOOK_TASK,
43
45
  name: `Test webhook: ${webhook.slug}`,
44
46
  input: {
@@ -47,6 +49,16 @@ class TriggerWebhookUseCaseImpl {
47
49
  deliveryId: delivery.id
48
50
  }
49
51
  });
52
+ if (triggered.isFail()) {
53
+ const marked = await this.updateDeliveryRepository.execute(delivery.id, {
54
+ status: "failed"
55
+ });
56
+ return Result.fail(new WebhookDeliveryNotQueuedError({
57
+ message: triggered.error.message,
58
+ deliveryId: delivery.id,
59
+ markedFailed: marked.isOk()
60
+ }));
61
+ }
50
62
  return Result.ok(delivery);
51
63
  }
52
64
  }
@@ -57,7 +69,8 @@ const TriggerWebhookUseCase_TriggerWebhookUseCase = TriggerWebhookUseCase.create
57
69
  GetWebhookRepository,
58
70
  CreateWebhookDeliveryRepository,
59
71
  TaskService,
60
- GetWebhookSettingsRepository
72
+ GetWebhookSettingsRepository,
73
+ UpdateWebhookDeliveryRepository
61
74
  ]
62
75
  });
63
76
  export { TriggerWebhookUseCase_TriggerWebhookUseCase as TriggerWebhookUseCase };
@@ -1 +1 @@
1
- {"version":3,"file":"api/features/TriggerWebhook/TriggerWebhookUseCase.js","sources":["../../../../src/api/features/TriggerWebhook/TriggerWebhookUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { TaskService } from \"@webiny/api-core/exports/api/tasks.js\";\nimport { TriggerWebhookUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { TriggerWebhookInputSchema } from \"./schema.js\";\nimport { GetWebhookRepository } from \"~/api/features/GetWebhook/abstractions.js\";\nimport { CreateWebhookDeliveryRepository } from \"~/api/features/CreateWebhookDelivery/abstractions.js\";\nimport { GetWebhookSettingsRepository } from \"~/api/features/GetWebhookSettings/abstractions.js\";\nimport { WebhookPermissions } from \"~/api/features/WebhookPermissions/abstractions.js\";\nimport { WebhookNotAuthorizedError, WebhookValidationError } from \"~/api/domain/errors.js\";\nimport { SEND_WEBHOOK_TASK, WEBHOOK_DELIVERY_MAX_RETENTION_DAYS } from \"~/api/domain/constants.js\";\nimport type { ISendWebhookTaskInput } from \"~/api/features/SendWebhookTask/types.js\";\nimport type { WebhookDelivery } from \"~/api/domain/WebhookDelivery.js\";\n\nclass TriggerWebhookUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private readonly permissions: WebhookPermissions.Interface,\n private readonly getWebhookRepository: GetWebhookRepository.Interface,\n private readonly createDeliveryRepository: CreateWebhookDeliveryRepository.Interface,\n private readonly taskService: TaskService.Interface,\n private readonly getSettingsRepository: GetWebhookSettingsRepository.Interface\n ) {}\n\n async execute(\n webhookId: string,\n payload: Record<string, unknown>\n ): Promise<Result<WebhookDelivery, UseCaseAbstraction.Error>> {\n if (!(await this.permissions.canEdit(\"webhook\"))) {\n return Result.fail(new WebhookNotAuthorizedError());\n }\n\n const parsed = TriggerWebhookInputSchema.safeParse({ webhookId, payload });\n if (!parsed.success) {\n return Result.fail(new WebhookValidationError(parsed.error));\n }\n\n const webhookResult = await this.getWebhookRepository.execute(parsed.data.webhookId);\n if (webhookResult.isFail()) {\n return Result.fail(webhookResult.error);\n }\n\n const webhook = webhookResult.value;\n\n const settingsResult = await this.getSettingsRepository.execute();\n const retentionDays = settingsResult.isOk()\n ? (settingsResult.value.deliveryRetentionDays ?? WEBHOOK_DELIVERY_MAX_RETENTION_DAYS)\n : WEBHOOK_DELIVERY_MAX_RETENTION_DAYS;\n\n const expiresAt = new Date(Date.now() + retentionDays * 24 * 60 * 60 * 1000);\n\n const deliveryResult = await this.createDeliveryRepository.execute({\n webhookId: webhook.id,\n eventType: \"webhook.test\",\n status: \"pending\",\n payload,\n expiresAt\n });\n\n if (deliveryResult.isFail()) {\n return Result.fail(deliveryResult.error);\n }\n\n const delivery = deliveryResult.value;\n\n await this.taskService.trigger<ISendWebhookTaskInput>({\n definition: SEND_WEBHOOK_TASK,\n name: `Test webhook: ${webhook.slug}`,\n input: {\n webhookId: webhook.id,\n eventName: \"webhook.test\",\n deliveryId: delivery.id\n }\n });\n\n return Result.ok(delivery);\n }\n}\n\nexport const TriggerWebhookUseCase = UseCaseAbstraction.createImplementation({\n implementation: TriggerWebhookUseCaseImpl,\n dependencies: [\n WebhookPermissions,\n GetWebhookRepository,\n CreateWebhookDeliveryRepository,\n TaskService,\n GetWebhookSettingsRepository\n ]\n});\n"],"names":["TriggerWebhookUseCaseImpl","permissions","getWebhookRepository","createDeliveryRepository","taskService","getSettingsRepository","webhookId","payload","Result","WebhookNotAuthorizedError","parsed","TriggerWebhookInputSchema","WebhookValidationError","webhookResult","webhook","settingsResult","retentionDays","WEBHOOK_DELIVERY_MAX_RETENTION_DAYS","expiresAt","Date","deliveryResult","delivery","SEND_WEBHOOK_TASK","TriggerWebhookUseCase","UseCaseAbstraction","WebhookPermissions","GetWebhookRepository","CreateWebhookDeliveryRepository","TaskService","GetWebhookSettingsRepository"],"mappings":";;;;;;;;;;AAaA,MAAMA;IACF,YACqBC,WAAyC,EACzCC,oBAAoD,EACpDC,wBAAmE,EACnEC,WAAkC,EAClCC,qBAA6D,CAChF;aALmBJ,WAAW,GAAXA;aACAC,oBA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QACJ;QAEA,OAAOb,OAAO,EAAE,CAACa;IACrB;AACJ;AAEO,MAAME,8CAAwBC,sBAAAA,oBAAuC,CAAC;IACzE,gBAAgBxB;IAChB,cAAc;QACVyB;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
1
+ {"version":3,"file":"api/features/TriggerWebhook/TriggerWebhookUseCase.js","sources":["../../../../src/api/features/TriggerWebhook/TriggerWebhookUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { TaskService } from \"@webiny/api-core/exports/api/tasks.js\";\nimport { TriggerWebhookUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport { TriggerWebhookInputSchema } from \"./schema.js\";\nimport { GetWebhookRepository } from \"~/api/features/GetWebhook/abstractions.js\";\nimport { CreateWebhookDeliveryRepository } from \"~/api/features/CreateWebhookDelivery/abstractions.js\";\nimport { UpdateWebhookDeliveryRepository } from \"~/api/features/UpdateWebhookDelivery/abstractions.js\";\nimport { GetWebhookSettingsRepository } from \"~/api/features/GetWebhookSettings/abstractions.js\";\nimport { WebhookPermissions } from \"~/api/features/WebhookPermissions/abstractions.js\";\nimport { WebhookNotAuthorizedError, WebhookValidationError } from \"~/api/domain/errors.js\";\nimport { WebhookDeliveryNotQueuedError } from \"~/api/domain/errors.js\";\nimport { SEND_WEBHOOK_TASK, WEBHOOK_DELIVERY_MAX_RETENTION_DAYS } from \"~/api/domain/constants.js\";\nimport type { ISendWebhookTaskInput } from \"~/api/features/SendWebhookTask/types.js\";\nimport type { WebhookDelivery } from \"~/api/domain/WebhookDelivery.js\";\n\nclass TriggerWebhookUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private readonly permissions: WebhookPermissions.Interface,\n private readonly getWebhookRepository: GetWebhookRepository.Interface,\n private readonly createDeliveryRepository: CreateWebhookDeliveryRepository.Interface,\n private readonly taskService: TaskService.Interface,\n private readonly getSettingsRepository: GetWebhookSettingsRepository.Interface,\n private readonly updateDeliveryRepository: UpdateWebhookDeliveryRepository.Interface\n ) {}\n\n async execute(\n webhookId: string,\n payload: Record<string, unknown>\n ): Promise<Result<WebhookDelivery, UseCaseAbstraction.Error>> {\n if (!(await this.permissions.canEdit(\"webhook\"))) {\n return Result.fail(new WebhookNotAuthorizedError());\n }\n\n const parsed = TriggerWebhookInputSchema.safeParse({ webhookId, payload });\n if (!parsed.success) {\n return Result.fail(new WebhookValidationError(parsed.error));\n }\n\n const webhookResult = await this.getWebhookRepository.execute(parsed.data.webhookId);\n if (webhookResult.isFail()) {\n return Result.fail(webhookResult.error);\n }\n\n const webhook = webhookResult.value;\n\n const settingsResult = await this.getSettingsRepository.execute();\n const retentionDays = settingsResult.isOk()\n ? (settingsResult.value.deliveryRetentionDays ?? WEBHOOK_DELIVERY_MAX_RETENTION_DAYS)\n : WEBHOOK_DELIVERY_MAX_RETENTION_DAYS;\n\n const expiresAt = new Date(Date.now() + retentionDays * 24 * 60 * 60 * 1000);\n\n const deliveryResult = await this.createDeliveryRepository.execute({\n webhookId: webhook.id,\n eventType: \"webhook.test\",\n status: \"pending\",\n payload,\n expiresAt\n });\n\n if (deliveryResult.isFail()) {\n return Result.fail(deliveryResult.error);\n }\n\n const delivery = deliveryResult.value;\n\n const triggered = await this.taskService.trigger<ISendWebhookTaskInput>({\n definition: SEND_WEBHOOK_TASK,\n name: `Test webhook: ${webhook.slug}`,\n input: {\n webhookId: webhook.id,\n eventName: \"webhook.test\",\n deliveryId: delivery.id\n }\n });\n if (triggered.isFail()) {\n // Nothing will send this delivery, so it must not stay pending.\n const marked = await this.updateDeliveryRepository.execute(delivery.id, {\n status: \"failed\"\n });\n return Result.fail(\n new WebhookDeliveryNotQueuedError({\n message: triggered.error.message,\n deliveryId: delivery.id,\n markedFailed: marked.isOk()\n })\n );\n }\n\n return Result.ok(delivery);\n }\n}\n\nexport const TriggerWebhookUseCase = UseCaseAbstraction.createImplementation({\n implementation: TriggerWebhookUseCaseImpl,\n dependencies: [\n WebhookPermissions,\n GetWebhookRepository,\n CreateWebhookDeliveryRepository,\n TaskService,\n GetWebhookSettingsRepository,\n UpdateWebhookDeliveryRepository\n ]\n});\n"],"names":["TriggerWebhookUseCaseImpl","permissions","getWebhookRepository","createDeliveryRepository","taskService","getSettingsRepository","updateDeliveryRepository","webhookId","payload","Result","WebhookNotAuthorizedError","parsed","TriggerWebhookInputSchema","WebhookValidationError","webhookResult","webhook","settingsResult","retentionDays","WEBHOOK_DELIVERY_MAX_RETENTION_DAYS","expiresAt","Date","deliveryResult","delivery","triggered","SEND_WEBHOOK_TASK","marked","WebhookDeliveryNotQueuedError","TriggerWebhookUseCase","UseCaseAbstraction","WebhookPermissions","GetWebhookRepository","CreateWebhookDeliveryRepository","TaskService","GetWebhookSettingsRepository","UpdateWebhookDeliveryRepository"],"mappings":";;;;;;;;;;;AAeA,MAAMA;IACF,YACqBC,WAAyC,EACzCC,oBAAoD,EACpDC,wBAAmE,EACnEC,WAAkC,EAClCC,qBAA6D,EAC7DC,wBAAmE,CACtF;aANmBL,WAAW,GAAXA;aACAC,oBAAoB,GAApBA;aACAC,wBAAwB,GAAxBA;aACAC,WAAW,GAAXA;aACAC,qBAAqB,GAArBA;aACAC,wBAAwB,GAAxBA;IAClB;IAEH,MAAM,QACFC,SAAiB,EACjBC,OAAgC,EAC0B;QAC1D,IAAI,CAAE,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,YACjC,OAAOC,OAAO,IAAI,CAAC,IAAIC;QAG3B,MAAMC,SAASC,0BAA0B,SAAS,CAAC;YAAEL;YAAWC;QAAQ;QACxE,IAAI,CAACG,OAAO,OAAO,EACf,OAAOF,OAAO,IAAI,CAAC,IAAII,uBAAuBF,OAAO,KAAK;QAG9D,MAAMG,gBAAgB,MAAM,IAAI,CAAC,oBAAoB,CAAC,OAAO,CAACH,OAAO,IAAI,CAAC,SAAS;QACnF,IAAIG,cAAc,MAAM,IACpB,OAAOL,OAAO,IAAI,CAACK,cAAc,KAAK;QAG1C,MAAMC,UAAUD,cAAc,KAAK;QAEnC,MAAME,iBAAiB,MAAM,IAAI,CAAC,qBAAqB,CAAC,OAAO;QAC/D,MAAMC,gBAAgBD,eAAe,IAAI,KAClCA,eAAe,KAAK,CAAC,qBAAqB,IAAIE,sCAC/CA;QAEN,MAAMC,YAAY,IAAIC,KAAKA,KAAK,GAAG,KAAKH,AAAgB,KAAhBA,gBAAqB,KAAK,KAAK;QAEvE,MAAMI,iBAAiB,MAAM,IAAI,CAAC,wBAAwB,CAAC,OAAO,CAAC;YAC/D,WAAWN,QAAQ,EAAE;YACrB,WAAW;YACX,QAAQ;YACRP;YACAW;QACJ;QAEA,IAAIE,eAAe,MAAM,IACrB,OAAOZ,OAAO,IAAI,CAACY,eAAe,KAAK;QAG3C,MAAMC,WAAWD,eAAe,KAAK;QAErC,MAAME,YAAY,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAwB;YACpE,YAAYC;YACZ,MAAM,CAAC,cAAc,EAAET,QAAQ,IAAI,EAAE;YACrC,OAAO;gBACH,WAAWA,QAAQ,EAAE;gBACrB,WAAW;gBACX,YAAYO,SAAS,EAAE;YAC3B;QACJ;QACA,IAAIC,UAAU,MAAM,IAAI;YAEpB,MAAME,SAAS,MAAM,IAAI,CAAC,wBAAwB,CAAC,OAAO,CAACH,SAAS,EAAE,EAAE;gBACpE,QAAQ;YACZ;YACA,OAAOb,OAAO,IAAI,CACd,IAAIiB,8BAA8B;gBAC9B,SAASH,UAAU,KAAK,CAAC,OAAO;gBAChC,YAAYD,SAAS,EAAE;gBACvB,cAAcG,OAAO,IAAI;YAC7B;QAER;QAEA,OAAOhB,OAAO,EAAE,CAACa;IACrB;AACJ;AAEO,MAAMK,8CAAwBC,sBAAAA,oBAAuC,CAAC;IACzE,gBAAgB5B;IAChB,cAAc;QACV6B;QACAC;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
@@ -1,7 +1,8 @@
1
1
  import { type Result } from "@webiny/feature/api";
2
2
  import type { WebhookDelivery } from "../../../api/domain/WebhookDelivery.js";
3
3
  import type { WebhookNotFoundError, WebhookPersistenceError, WebhookModelNotFoundError, WebhookNotAuthorizedError, WebhookValidationError } from "../../../api/domain/errors.js";
4
- type IError = WebhookNotFoundError | WebhookPersistenceError | WebhookModelNotFoundError | WebhookNotAuthorizedError | WebhookValidationError;
4
+ import type { WebhookDeliveryNotQueuedError } from "../../../api/domain/errors.js";
5
+ type IError = WebhookNotFoundError | WebhookPersistenceError | WebhookModelNotFoundError | WebhookNotAuthorizedError | WebhookValidationError | WebhookDeliveryNotQueuedError;
5
6
  export interface ITriggerWebhookUseCase {
6
7
  execute(webhookId: string, payload: Record<string, unknown>): Promise<Result<WebhookDelivery, IError>>;
7
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"api/features/TriggerWebhook/abstractions.js","sources":["../../../../src/api/features/TriggerWebhook/abstractions.ts"],"sourcesContent":["import { createAbstraction, type Result } from \"@webiny/feature/api\";\nimport type { WebhookDelivery } from \"~/api/domain/WebhookDelivery.js\";\nimport type {\n WebhookNotFoundError,\n WebhookPersistenceError,\n WebhookModelNotFoundError,\n WebhookNotAuthorizedError,\n WebhookValidationError\n} from \"~/api/domain/errors.js\";\n\ntype IError =\n | WebhookNotFoundError\n | WebhookPersistenceError\n | WebhookModelNotFoundError\n | WebhookNotAuthorizedError\n | WebhookValidationError;\n\nexport interface ITriggerWebhookUseCase {\n execute(\n webhookId: string,\n payload: Record<string, unknown>\n ): Promise<Result<WebhookDelivery, IError>>;\n}\n\nexport const TriggerWebhookUseCase = createAbstraction<ITriggerWebhookUseCase>(\n \"Webhooks/TriggerWebhookUseCase\"\n);\n\nexport namespace TriggerWebhookUseCase {\n export type Interface = ITriggerWebhookUseCase;\n export type Error = IError;\n}\n"],"names":["TriggerWebhookUseCase","createAbstraction"],"mappings":";AAwBO,MAAMA,wBAAwBC,kBACjC"}
1
+ {"version":3,"file":"api/features/TriggerWebhook/abstractions.js","sources":["../../../../src/api/features/TriggerWebhook/abstractions.ts"],"sourcesContent":["import { createAbstraction, type Result } from \"@webiny/feature/api\";\nimport type { WebhookDelivery } from \"~/api/domain/WebhookDelivery.js\";\nimport type {\n WebhookNotFoundError,\n WebhookPersistenceError,\n WebhookModelNotFoundError,\n WebhookNotAuthorizedError,\n WebhookValidationError\n} from \"~/api/domain/errors.js\";\nimport type { WebhookDeliveryNotQueuedError } from \"~/api/domain/errors.js\";\n\ntype IError =\n | WebhookNotFoundError\n | WebhookPersistenceError\n | WebhookModelNotFoundError\n | WebhookNotAuthorizedError\n | WebhookValidationError\n | WebhookDeliveryNotQueuedError;\n\nexport interface ITriggerWebhookUseCase {\n execute(\n webhookId: string,\n payload: Record<string, unknown>\n ): Promise<Result<WebhookDelivery, IError>>;\n}\n\nexport const TriggerWebhookUseCase = createAbstraction<ITriggerWebhookUseCase>(\n \"Webhooks/TriggerWebhookUseCase\"\n);\n\nexport namespace TriggerWebhookUseCase {\n export type Interface = ITriggerWebhookUseCase;\n export type Error = IError;\n}\n"],"names":["TriggerWebhookUseCase","createAbstraction"],"mappings":";AA0BO,MAAMA,wBAAwBC,kBACjC"}
@@ -3,7 +3,7 @@ import { isValidEndpointUrl } from "../../utils/isValidEndpointUrl.js";
3
3
  const UpdateWebhookInputSchema = z.object({
4
4
  name: z.string().min(1, "Name must not be empty.").optional(),
5
5
  endpointUrl: z.string().refine(isValidEndpointUrl, {
6
- message: "Endpoint URL must use HTTPS. HTTP is only allowed for localhost."
6
+ message: "Endpoint URL must use HTTPS and must not point to a local or link-local address."
7
7
  }).optional(),
8
8
  description: z.string().optional(),
9
9
  enabled: z.boolean().optional(),
@@ -1 +1 @@
1
- {"version":3,"file":"api/features/UpdateWebhook/schema.js","sources":["../../../../src/api/features/UpdateWebhook/schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { isValidEndpointUrl } from \"~/api/utils/isValidEndpointUrl.js\";\n\nexport const UpdateWebhookInputSchema = z.object({\n name: z.string().min(1, \"Name must not be empty.\").optional(),\n endpointUrl: z\n .string()\n .refine(isValidEndpointUrl, {\n message: \"Endpoint URL must use HTTPS. HTTP is only allowed for localhost.\"\n })\n .optional(),\n description: z.string().optional(),\n enabled: z.boolean().optional(),\n events: z.array(z.string()).min(1, \"At least one event must be selected.\").optional(),\n signingSecret: z.string().optional()\n});\n"],"names":["UpdateWebhookInputSchema","z","isValidEndpointUrl"],"mappings":";;AAGO,MAAMA,2BAA2BC,EAAE,MAAM,CAAC;IAC7C,MAAMA,EAAE,MAAM,GAAG,GAAG,CAAC,GAAG,2BAA2B,QAAQ;IAC3D,aAAaA,EAAAA,MACF,GACN,MAAM,CAACC,oBAAoB;QACxB,SAAS;IACb,GACC,QAAQ;IACb,aAAaD,EAAE,MAAM,GAAG,QAAQ;IAChC,SAASA,EAAE,OAAO,GAAG,QAAQ;IAC7B,QAAQA,EAAE,KAAK,CAACA,EAAE,MAAM,IAAI,GAAG,CAAC,GAAG,wCAAwC,QAAQ;IACnF,eAAeA,EAAE,MAAM,GAAG,QAAQ;AACtC"}
1
+ {"version":3,"file":"api/features/UpdateWebhook/schema.js","sources":["../../../../src/api/features/UpdateWebhook/schema.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { isValidEndpointUrl } from \"~/api/utils/isValidEndpointUrl.js\";\n\nexport const UpdateWebhookInputSchema = z.object({\n name: z.string().min(1, \"Name must not be empty.\").optional(),\n endpointUrl: z\n .string()\n .refine(isValidEndpointUrl, {\n message:\n \"Endpoint URL must use HTTPS and must not point to a local or link-local address.\"\n })\n .optional(),\n description: z.string().optional(),\n enabled: z.boolean().optional(),\n events: z.array(z.string()).min(1, \"At least one event must be selected.\").optional(),\n signingSecret: z.string().optional()\n});\n"],"names":["UpdateWebhookInputSchema","z","isValidEndpointUrl"],"mappings":";;AAGO,MAAMA,2BAA2BC,EAAE,MAAM,CAAC;IAC7C,MAAMA,EAAE,MAAM,GAAG,GAAG,CAAC,GAAG,2BAA2B,QAAQ;IAC3D,aAAaA,EAAAA,MACF,GACN,MAAM,CAACC,oBAAoB;QACxB,SACI;IACR,GACC,QAAQ;IACb,aAAaD,EAAE,MAAM,GAAG,QAAQ;IAChC,SAASA,EAAE,OAAO,GAAG,QAAQ;IAC7B,QAAQA,EAAE,KAAK,CAACA,EAAE,MAAM,IAAI,GAAG,CAAC,GAAG,wCAAwC,QAAQ;IACnF,eAAeA,EAAE,MAAM,GAAG,QAAQ;AACtC"}
@@ -30,6 +30,7 @@ class WebhookDeliverImpl {
30
30
  method: "POST",
31
31
  headers: input.headers,
32
32
  body: input.body,
33
+ redirect: "manual",
33
34
  signal: AbortSignal.timeout(input.timeout)
34
35
  });
35
36
  const body = await response.text();
@@ -1 +1 @@
1
- {"version":3,"file":"api/features/WebhookDeliver/WebhookDeliver.js","sources":["../../../../src/api/features/WebhookDeliver/WebhookDeliver.ts"],"sourcesContent":["import {\n WebhookDeliver as Abstraction,\n type IWebhookDeliverInput,\n type IWebhookDeliverResult\n} from \"./abstractions.js\";\n\ninterface IAttemptResult {\n status: number;\n body: string;\n retryAfter: string | null;\n}\n\nclass WebhookDeliverImpl implements Abstraction.Interface {\n public async execute(input: IWebhookDeliverInput): Promise<IWebhookDeliverResult> {\n const startTime = Date.now();\n let attempts = 0;\n let lastResult: IAttemptResult = { status: 0, body: \"\", retryAfter: null };\n\n for (let i = 0; i <= input.maxRetries; i++) {\n if (i > 0) {\n const delay = this.computeDelay(i - 1, input, lastResult);\n await this.sleep(delay);\n }\n\n attempts++;\n lastResult = await this.attempt(input);\n\n if (!this.isRetryable(lastResult.status)) {\n break;\n }\n }\n\n return {\n status: lastResult.status,\n body: lastResult.body,\n responseTime: Date.now() - startTime,\n attempts\n };\n }\n\n private async attempt(input: IWebhookDeliverInput): Promise<IAttemptResult> {\n try {\n const response = await fetch(input.url, {\n method: \"POST\",\n headers: input.headers,\n body: input.body,\n signal: AbortSignal.timeout(input.timeout)\n });\n\n const body = await response.text();\n const retryAfter = response.headers.get(\"retry-after\");\n\n return { status: response.status, body, retryAfter };\n } catch (error) {\n return {\n status: 0,\n body: error instanceof Error ? error.message : \"Unknown fetch error\",\n retryAfter: null\n };\n }\n }\n\n private isRetryable(status: number): boolean {\n if (status === 0 || status === 429) {\n return true;\n }\n return status >= 500 && status <= 599;\n }\n\n private computeDelay(\n retryIndex: number,\n input: IWebhookDeliverInput,\n lastResult: IAttemptResult\n ): number {\n const exponential = Math.min(input.initialDelay * Math.pow(2, retryIndex), input.maxDelay);\n\n if (lastResult.status === 429 && lastResult.retryAfter) {\n const parsed = this.parseRetryAfter(lastResult.retryAfter);\n if (parsed !== null) {\n return Math.max(parsed, exponential);\n }\n }\n\n return exponential;\n }\n\n private parseRetryAfter(value: string): number | null {\n const seconds = Number(value);\n if (!Number.isNaN(seconds) && Number.isFinite(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const dateMs = Date.parse(value);\n if (!Number.isNaN(dateMs)) {\n return Math.max(0, dateMs - Date.now());\n }\n\n return null;\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n\nexport const WebhookDeliver = Abstraction.createImplementation({\n implementation: WebhookDeliverImpl,\n dependencies: []\n});\n"],"names":["WebhookDeliverImpl","input","startTime","Date","attempts","lastResult","i","delay","response","fetch","AbortSignal","body","retryAfter","error","Error","status","retryIndex","exponential","Math","parsed","value","seconds","Number","dateMs","ms","Promise","resolve","setTimeout","WebhookDeliver","Abstraction"],"mappings":";AAYA,MAAMA;IACF,MAAa,QAAQC,KAA2B,EAAkC;QAC9E,MAAMC,YAAYC,KAAK,GAAG;QAC1B,IAAIC,WAAW;QACf,IAAIC,aAA6B;YAAE,QAAQ;YAAG,MAAM;YAAI,YAAY;QAAK;QAEzE,IAAK,IAAIC,IAAI,GAAGA,KAAKL,MAAM,UAAU,EAAEK,IAAK;YACxC,IAAIA,IAAI,GAAG;gBACP,MAAMC,QAAQ,IAAI,CAAC,YAAY,CAACD,IAAI,GAAGL,OAAOI;gBAC9C,MAAM,IAAI,CAAC,KAAK,CAACE;YACrB;YAEAH;YACAC,aAAa,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B;QAClD,MAAMC,UAAUC,OAAOF;QACvB,IAAI,CAACE,OAAO,KAAK,CAACD,YAAYC,OAAO,QAAQ,CAACD,YAAYA,WAAW,GACjE,OAAOA,AAAU,OAAVA;QAGX,MAAME,SAASpB,KAAK,KAAK,CAACiB;QAC1B,IAAI,CAACE,OAAO,KAAK,CAACC,SACd,OAAOL,KAAK,GAAG,CAAC,GAAGK,SAASpB,KAAK,GAAG;QAGxC,OAAO;IACX;IAEQ,MAAMqB,EAAU,EAAiB;QACrC,OAAO,IAAIC,QAAQC,CAAAA,UAAWC,WAAWD,SAASF;IACtD;AACJ;AAEO,MAAMI,gCAAiBC,eAAAA,oBAAgC,CAAC;IAC3D,gBAAgB7B;IAChB,cAAc,EAAE;AACpB"}
1
+ {"version":3,"file":"api/features/WebhookDeliver/WebhookDeliver.js","sources":["../../../../src/api/features/WebhookDeliver/WebhookDeliver.ts"],"sourcesContent":["import {\n WebhookDeliver as Abstraction,\n type IWebhookDeliverInput,\n type IWebhookDeliverResult\n} from \"./abstractions.js\";\n\ninterface IAttemptResult {\n status: number;\n body: string;\n retryAfter: string | null;\n}\n\nclass WebhookDeliverImpl implements Abstraction.Interface {\n public async execute(input: IWebhookDeliverInput): Promise<IWebhookDeliverResult> {\n const startTime = Date.now();\n let attempts = 0;\n let lastResult: IAttemptResult = { status: 0, body: \"\", retryAfter: null };\n\n for (let i = 0; i <= input.maxRetries; i++) {\n if (i > 0) {\n const delay = this.computeDelay(i - 1, input, lastResult);\n await this.sleep(delay);\n }\n\n attempts++;\n lastResult = await this.attempt(input);\n\n if (!this.isRetryable(lastResult.status)) {\n break;\n }\n }\n\n return {\n status: lastResult.status,\n body: lastResult.body,\n responseTime: Date.now() - startTime,\n attempts\n };\n }\n\n private async attempt(input: IWebhookDeliverInput): Promise<IAttemptResult> {\n try {\n const response = await fetch(input.url, {\n method: \"POST\",\n headers: input.headers,\n body: input.body,\n /* Never follow redirects: the endpoint URL is validated when it's stored, a\n redirect could point anywhere (e.g. the Lambda Runtime API or other internal\n addresses), and the response body ends up in the delivery log. A 3xx is\n recorded as the delivery result. */\n redirect: \"manual\",\n signal: AbortSignal.timeout(input.timeout)\n });\n\n const body = await response.text();\n const retryAfter = response.headers.get(\"retry-after\");\n\n return { status: response.status, body, retryAfter };\n } catch (error) {\n return {\n status: 0,\n body: error instanceof Error ? error.message : \"Unknown fetch error\",\n retryAfter: null\n };\n }\n }\n\n private isRetryable(status: number): boolean {\n if (status === 0 || status === 429) {\n return true;\n }\n return status >= 500 && status <= 599;\n }\n\n private computeDelay(\n retryIndex: number,\n input: IWebhookDeliverInput,\n lastResult: IAttemptResult\n ): number {\n const exponential = Math.min(input.initialDelay * Math.pow(2, retryIndex), input.maxDelay);\n\n if (lastResult.status === 429 && lastResult.retryAfter) {\n const parsed = this.parseRetryAfter(lastResult.retryAfter);\n if (parsed !== null) {\n return Math.max(parsed, exponential);\n }\n }\n\n return exponential;\n }\n\n private parseRetryAfter(value: string): number | null {\n const seconds = Number(value);\n if (!Number.isNaN(seconds) && Number.isFinite(seconds) && seconds >= 0) {\n return seconds * 1000;\n }\n\n const dateMs = Date.parse(value);\n if (!Number.isNaN(dateMs)) {\n return Math.max(0, dateMs - Date.now());\n }\n\n return null;\n }\n\n private sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n\nexport const WebhookDeliver = Abstraction.createImplementation({\n implementation: WebhookDeliverImpl,\n dependencies: []\n});\n"],"names":["WebhookDeliverImpl","input","startTime","Date","attempts","lastResult","i","delay","response","fetch","AbortSignal","body","retryAfter","error","Error","status","retryIndex","exponential","Math","parsed","value","seconds","Number","dateMs","ms","Promise","resolve","setTimeout","WebhookDeliver","Abstraction"],"mappings":";AAYA,MAAMA;IACF,MAAa,QAAQC,KAA2B,EAAkC;QAC9E,MAAMC,YAAYC,KAAK,GAAG;QAC1B,IAAIC,WAAW;QACf,IAAIC,aAA6B;YAAE,QAAQ;YAAG,MAAM;YAAI,YAAY;QAAK;QAE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AAII,AAAsB,QAAtBA,WAAW,MAAM,IAAYA,WAAW,UAAU,EAAE;YACpD,MAAMc,SAAS,IAAI,CAAC,eAAe,CAACd,WAAW,UAAU;YACzD,IAAIc,AAAW,SAAXA,QACA,OAAOD,KAAK,GAAG,CAACC,QAAQF;QAEhC;QAEA,OAAOA;IACX;IAEQ,gBAAgBG,KAAa,EAAiB;QAClD,MAAMC,UAAUC,OAAOF;QACvB,IAAI,CAACE,OAAO,KAAK,CAACD,YAAYC,OAAO,QAAQ,CAACD,YAAYA,WAAW,GACjE,OAAOA,AAAU,OAAVA;QAGX,MAAME,SAASpB,KAAK,KAAK,CAACiB;QAC1B,IAAI,CAACE,OAAO,KAAK,CAACC,SACd,OAAOL,KAAK,GAAG,CAAC,GAAGK,SAASpB,KAAK,GAAG;QAGxC,OAAO;IACX;IAEQ,MAAMqB,EAAU,EAAiB;QACrC,OAAO,IAAIC,QAAQC,CAAAA,UAAWC,WAAWD,SAASF;IACtD;AACJ;AAEO,MAAMI,gCAAiBC,eAAAA,oBAAgC,CAAC;IAC3D,gBAAgB7B;IAChB,cAAc,EAAE;AACpB"}
@@ -1,7 +1,9 @@
1
1
  import { WebhookDispatcher as WebhookDispatcherAbstraction, WebhookProvider } from "@webiny/api-core/features/webhooks/index.js";
2
2
  import { TaskService } from "@webiny/api-core/exports/api/tasks.js";
3
+ import { Logger } from "@webiny/api-core/features/logger/index.js";
3
4
  import { ListWebhooksRepository } from "../../../api/features/ListWebhooks/abstractions.js";
4
5
  import { CreateWebhookDeliveryRepository } from "../../../api/features/CreateWebhookDelivery/abstractions.js";
6
+ import { UpdateWebhookDeliveryRepository } from "../../../api/features/UpdateWebhookDelivery/abstractions.js";
5
7
  import { GetWebhookSettingsRepository } from "../../../api/features/GetWebhookSettings/abstractions.js";
6
8
  import type { IWebhookDispatcherData } from "@webiny/api-core/features/webhooks/WebhookDispatcher/abstractions.js";
7
9
  declare class WebhookDispatcherImpl implements WebhookDispatcherAbstraction.Interface {
@@ -10,7 +12,9 @@ declare class WebhookDispatcherImpl implements WebhookDispatcherAbstraction.Inte
10
12
  private readonly taskService;
11
13
  private readonly provider;
12
14
  private readonly getSettingsRepository;
13
- constructor(listWebhooksRepository: ListWebhooksRepository.Interface, createDeliveryRepository: CreateWebhookDeliveryRepository.Interface, taskService: TaskService.Interface, provider: WebhookProvider.Interface, getSettingsRepository: GetWebhookSettingsRepository.Interface);
15
+ private readonly updateDeliveryRepository;
16
+ private readonly logger;
17
+ constructor(listWebhooksRepository: ListWebhooksRepository.Interface, createDeliveryRepository: CreateWebhookDeliveryRepository.Interface, taskService: TaskService.Interface, provider: WebhookProvider.Interface, getSettingsRepository: GetWebhookSettingsRepository.Interface, updateDeliveryRepository: UpdateWebhookDeliveryRepository.Interface, logger: Logger.Interface);
14
18
  dispatch<T extends IWebhookDispatcherData = IWebhookDispatcherData>(eventName: string, payload: T): Promise<void>;
15
19
  }
16
20
  export declare const WebhookDispatcher: typeof WebhookDispatcherImpl & {
@@ -1,16 +1,20 @@
1
1
  import { WebhookDispatcher, WebhookProvider } from "@webiny/api-core/features/webhooks/index.js";
2
2
  import { TaskService } from "@webiny/api-core/exports/api/tasks.js";
3
+ import { Logger } from "@webiny/api-core/features/logger/index.js";
3
4
  import { ListWebhooksRepository } from "../ListWebhooks/abstractions.js";
4
5
  import { CreateWebhookDeliveryRepository } from "../CreateWebhookDelivery/abstractions.js";
6
+ import { UpdateWebhookDeliveryRepository } from "../UpdateWebhookDelivery/abstractions.js";
5
7
  import { GetWebhookSettingsRepository } from "../GetWebhookSettings/abstractions.js";
6
8
  import { SEND_WEBHOOK_TASK, WEBHOOK_DELIVERY_MAX_RETENTION_DAYS } from "../../domain/constants.js";
7
9
  class WebhookDispatcherImpl {
8
- constructor(listWebhooksRepository, createDeliveryRepository, taskService, provider, getSettingsRepository){
10
+ constructor(listWebhooksRepository, createDeliveryRepository, taskService, provider, getSettingsRepository, updateDeliveryRepository, logger){
9
11
  this.listWebhooksRepository = listWebhooksRepository;
10
12
  this.createDeliveryRepository = createDeliveryRepository;
11
13
  this.taskService = taskService;
12
14
  this.provider = provider;
13
15
  this.getSettingsRepository = getSettingsRepository;
16
+ this.updateDeliveryRepository = updateDeliveryRepository;
17
+ this.logger = logger;
14
18
  }
15
19
  async dispatch(eventName, payload) {
16
20
  const events = await this.provider.execute();
@@ -25,7 +29,7 @@ class WebhookDispatcherImpl {
25
29
  if (result.isFail()) return;
26
30
  const settingsResult = await this.getSettingsRepository.execute();
27
31
  const retentionDays = settingsResult.isOk() ? settingsResult.value.deliveryRetentionDays ?? WEBHOOK_DELIVERY_MAX_RETENTION_DAYS : WEBHOOK_DELIVERY_MAX_RETENTION_DAYS;
28
- const expiresAt = new Date(Date.now() + 24 * retentionDays * 3600000);
32
+ const expiresAt = new Date(Date.now() + 24 * retentionDays * 60 * 60 * 1000);
29
33
  for (const webhook of result.value.items){
30
34
  const deliveryResult = await this.createDeliveryRepository.execute({
31
35
  webhookId: webhook.id,
@@ -34,7 +38,8 @@ class WebhookDispatcherImpl {
34
38
  expiresAt,
35
39
  payload
36
40
  });
37
- if (!deliveryResult.isFail()) await this.taskService.trigger({
41
+ if (deliveryResult.isFail()) continue;
42
+ const triggered = await this.taskService.trigger({
38
43
  definition: SEND_WEBHOOK_TASK,
39
44
  name: `Webhook: ${webhook.slug} — ${event.eventName}`,
40
45
  input: {
@@ -43,6 +48,20 @@ class WebhookDispatcherImpl {
43
48
  deliveryId: deliveryResult.value.id
44
49
  }
45
50
  });
51
+ if (triggered.isFail()) {
52
+ this.logger.error({
53
+ error: triggered.error,
54
+ webhookId: webhook.id,
55
+ deliveryId: deliveryResult.value.id
56
+ }, "Could not queue the webhook delivery.");
57
+ const marked = await this.updateDeliveryRepository.execute(deliveryResult.value.id, {
58
+ status: "failed"
59
+ });
60
+ if (marked.isFail()) this.logger.error({
61
+ error: marked.error,
62
+ deliveryId: deliveryResult.value.id
63
+ }, "Could not mark the unqueued webhook delivery as failed.");
64
+ }
46
65
  }
47
66
  }
48
67
  }
@@ -53,7 +72,9 @@ const WebhookDispatcher_WebhookDispatcher = WebhookDispatcher.createImplementati
53
72
  CreateWebhookDeliveryRepository,
54
73
  TaskService,
55
74
  WebhookProvider,
56
- GetWebhookSettingsRepository
75
+ GetWebhookSettingsRepository,
76
+ UpdateWebhookDeliveryRepository,
77
+ Logger
57
78
  ]
58
79
  });
59
80
  export { WebhookDispatcher_WebhookDispatcher as WebhookDispatcher };
@@ -1 +1 @@
1
- {"version":3,"file":"api/features/WebhookDispatcher/WebhookDispatcher.js","sources":["../../../../src/api/features/WebhookDispatcher/WebhookDispatcher.ts"],"sourcesContent":["import {\n WebhookDispatcher as WebhookDispatcherAbstraction,\n WebhookProvider\n} from \"@webiny/api-core/features/webhooks/index.js\";\nimport { TaskService } from \"@webiny/api-core/exports/api/tasks.js\";\nimport { ListWebhooksRepository } from \"~/api/features/ListWebhooks/abstractions.js\";\nimport { CreateWebhookDeliveryRepository } from \"~/api/features/CreateWebhookDelivery/abstractions.js\";\nimport { GetWebhookSettingsRepository } from \"~/api/features/GetWebhookSettings/abstractions.js\";\nimport { SEND_WEBHOOK_TASK, WEBHOOK_DELIVERY_MAX_RETENTION_DAYS } from \"~/api/domain/constants.js\";\nimport type { IWebhookDispatcherData } from \"@webiny/api-core/features/webhooks/WebhookDispatcher/abstractions.js\";\nimport type { ISendWebhookTaskInput } from \"~/api/features/SendWebhookTask/types.js\";\n\nclass WebhookDispatcherImpl implements WebhookDispatcherAbstraction.Interface {\n constructor(\n private readonly listWebhooksRepository: ListWebhooksRepository.Interface,\n private readonly createDeliveryRepository: CreateWebhookDeliveryRepository.Interface,\n private readonly taskService: TaskService.Interface,\n private readonly provider: WebhookProvider.Interface,\n private readonly getSettingsRepository: GetWebhookSettingsRepository.Interface\n ) {}\n\n async dispatch<T extends IWebhookDispatcherData = IWebhookDispatcherData>(\n eventName: string,\n payload: T\n ): Promise<void> {\n const events = await this.provider.execute();\n const event = events.find(event => {\n return event.eventName === eventName;\n });\n if (!event) {\n return;\n }\n\n const result = await this.listWebhooksRepository.execute({\n where: {\n enabled: true,\n events_contains: eventName\n }\n });\n\n if (result.isFail()) {\n return;\n }\n\n const settingsResult = await this.getSettingsRepository.execute();\n const retentionDays = settingsResult.isOk()\n ? (settingsResult.value.deliveryRetentionDays ?? WEBHOOK_DELIVERY_MAX_RETENTION_DAYS)\n : WEBHOOK_DELIVERY_MAX_RETENTION_DAYS;\n\n const expiresAt = new Date(Date.now() + retentionDays * 24 * 60 * 60 * 1000);\n\n for (const webhook of result.value.items) {\n const deliveryResult = await this.createDeliveryRepository.execute({\n webhookId: webhook.id,\n eventType: event.eventName,\n status: \"pending\",\n expiresAt,\n payload\n });\n\n if (deliveryResult.isFail()) {\n continue;\n }\n\n await this.taskService.trigger<ISendWebhookTaskInput>({\n definition: SEND_WEBHOOK_TASK,\n name: `Webhook: ${webhook.slug} — ${event.eventName}`,\n input: {\n webhookId: webhook.id,\n eventName,\n deliveryId: deliveryResult.value.id\n }\n });\n }\n }\n}\n\nexport const WebhookDispatcher = WebhookDispatcherAbstraction.createImplementation({\n implementation: WebhookDispatcherImpl,\n dependencies: [\n ListWebhooksRepository,\n CreateWebhookDeliveryRepository,\n TaskService,\n WebhookProvider,\n GetWebhookSettingsRepository\n ]\n});\n"],"names":["WebhookDispatcherImpl","listWebhooksRepository","createDeliveryRepository","taskService","provider","getSettingsRepository","eventName","payload","events","event","result","settingsResult","retentionDays","WEBHOOK_DELIVERY_MAX_RETENTION_DAYS","expiresAt","Date","webhook","deliveryResult","SEND_WEBHOOK_TASK","WebhookDispatcher","WebhookDispatcherAbstraction","ListWebhooksRepository","CreateWebhookDeliveryRepository","TaskService","WebhookProvider","GetWebhookSettingsRepository"],"mappings":";;;;;;AAYA,MAAMA;IACF,YACqBC,sBAAwD,EACxDC,wBAAmE,EACnEC,WAAkC,EAClCC,QAAmC,EACnCC,qBAA6D,CAChF;aALmBJ,sBAAsB,GAAtBA;aACAC,wBAAwB,GAAxBA;aACAC,WAA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;KACH;AACL"}
1
+ {"version":3,"file":"api/features/WebhookDispatcher/WebhookDispatcher.js","sources":["../../../../src/api/features/WebhookDispatcher/WebhookDispatcher.ts"],"sourcesContent":["import {\n WebhookDispatcher as WebhookDispatcherAbstraction,\n WebhookProvider\n} from \"@webiny/api-core/features/webhooks/index.js\";\nimport { TaskService } from \"@webiny/api-core/exports/api/tasks.js\";\nimport { Logger } from \"@webiny/api-core/features/logger/index.js\";\nimport { ListWebhooksRepository } from \"~/api/features/ListWebhooks/abstractions.js\";\nimport { CreateWebhookDeliveryRepository } from \"~/api/features/CreateWebhookDelivery/abstractions.js\";\nimport { UpdateWebhookDeliveryRepository } from \"~/api/features/UpdateWebhookDelivery/abstractions.js\";\nimport { GetWebhookSettingsRepository } from \"~/api/features/GetWebhookSettings/abstractions.js\";\nimport { SEND_WEBHOOK_TASK, WEBHOOK_DELIVERY_MAX_RETENTION_DAYS } from \"~/api/domain/constants.js\";\nimport type { IWebhookDispatcherData } from \"@webiny/api-core/features/webhooks/WebhookDispatcher/abstractions.js\";\nimport type { ISendWebhookTaskInput } from \"~/api/features/SendWebhookTask/types.js\";\n\nclass WebhookDispatcherImpl implements WebhookDispatcherAbstraction.Interface {\n constructor(\n private readonly listWebhooksRepository: ListWebhooksRepository.Interface,\n private readonly createDeliveryRepository: CreateWebhookDeliveryRepository.Interface,\n private readonly taskService: TaskService.Interface,\n private readonly provider: WebhookProvider.Interface,\n private readonly getSettingsRepository: GetWebhookSettingsRepository.Interface,\n private readonly updateDeliveryRepository: UpdateWebhookDeliveryRepository.Interface,\n private readonly logger: Logger.Interface\n ) {}\n\n async dispatch<T extends IWebhookDispatcherData = IWebhookDispatcherData>(\n eventName: string,\n payload: T\n ): Promise<void> {\n const events = await this.provider.execute();\n const event = events.find(event => {\n return event.eventName === eventName;\n });\n if (!event) {\n return;\n }\n\n const result = await this.listWebhooksRepository.execute({\n where: {\n enabled: true,\n events_contains: eventName\n }\n });\n\n if (result.isFail()) {\n return;\n }\n\n const settingsResult = await this.getSettingsRepository.execute();\n const retentionDays = settingsResult.isOk()\n ? (settingsResult.value.deliveryRetentionDays ?? WEBHOOK_DELIVERY_MAX_RETENTION_DAYS)\n : WEBHOOK_DELIVERY_MAX_RETENTION_DAYS;\n\n const expiresAt = new Date(Date.now() + retentionDays * 24 * 60 * 60 * 1000);\n\n for (const webhook of result.value.items) {\n const deliveryResult = await this.createDeliveryRepository.execute({\n webhookId: webhook.id,\n eventType: event.eventName,\n status: \"pending\",\n expiresAt,\n payload\n });\n\n if (deliveryResult.isFail()) {\n continue;\n }\n\n // The event's own write has already succeeded, so a delivery that can't be queued is\n // logged and marked failed, and doesn't fail the write.\n const triggered = await this.taskService.trigger<ISendWebhookTaskInput>({\n definition: SEND_WEBHOOK_TASK,\n name: `Webhook: ${webhook.slug} — ${event.eventName}`,\n input: {\n webhookId: webhook.id,\n eventName,\n deliveryId: deliveryResult.value.id\n }\n });\n if (triggered.isFail()) {\n this.logger.error(\n {\n error: triggered.error,\n webhookId: webhook.id,\n deliveryId: deliveryResult.value.id\n },\n \"Could not queue the webhook delivery.\"\n );\n const marked = await this.updateDeliveryRepository.execute(\n deliveryResult.value.id,\n { status: \"failed\" }\n );\n if (marked.isFail()) {\n this.logger.error(\n { error: marked.error, deliveryId: deliveryResult.value.id },\n \"Could not mark the unqueued webhook delivery as failed.\"\n );\n }\n }\n }\n }\n}\n\nexport const WebhookDispatcher = WebhookDispatcherAbstraction.createImplementation({\n implementation: WebhookDispatcherImpl,\n dependencies: [\n ListWebhooksRepository,\n CreateWebhookDeliveryRepository,\n TaskService,\n WebhookProvider,\n GetWebhookSettingsRepository,\n UpdateWebhookDeliveryRepository,\n Logger\n ]\n});\n"],"names":["WebhookDispatcherImpl","listWebhooksRepository","createDeliveryRepository","taskService","provider","getSettingsRepository","updateDeliveryRepository","logger","eventName","payload","events","event","result","settingsResult","retentionDays","WEBHOOK_DELIVERY_MAX_RETENTION_DAYS","expiresAt","Date","webhook","deliveryResult","triggered","SEND_WEBHOOK_TASK","marked","WebhookDispatcher","WebhookDispatcherAbstraction","ListWebhooksRepository","CreateWebhookDeliveryRepository","TaskService","WebhookProvider","GetWebhookSettingsRepository","UpdateWebhookDeliveryRepository","Logger"],"mappings":";;;;;;;;AAcA,MAAMA;IACF,YACqBC,sBAAwD,EACxDC,wBAAmE,EACnEC,WAAkC,EAClCC,QAAmC,EACnCC,qBAA6D,EAC7DC,wBAAmE,EACnEC,MAAwB,CAC3C;aAPmBN,sBAAsB,GAAtBA;aACAC,wBAAwB,GAAxBA;aACAC,WAAW,GAAXA;aACAC,QAAQ,GAARA;aACAC,qBAAqB,GAArBA;aACAC,wBAAwB,GAAxBA;aACAC,MAAM,GAANA;IAClB;IAEH,MAAM,SACFC,SAAiB,EACjBC,OAAU,EACG;QACb,MAAMC,SAAS,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO;QAC1C,MAAMC,QAAQD,OAAO,IAAI,CAACC,CAAAA,QACfA,MAAM,SAAS,KAAKH;QAE/B,IAAI,CAACG,OACD;QAGJ,MAAMC,SAAS,MAAM,IAAI,CAAC,sBAAsB,CAAC,OAAO,CAAC;YACrD,OAAO;gBACH,SAAS;gBACT,iBAAiBJ;YACrB;QACJ;QAEA,IAAII,OAAO,MAAM,IACb;QAGJ,MAAMC,iBAAiB,MAAM,IAAI,CAAC,qBAAqB,CAAC,OAAO;QAC/D,MAAMC,gBAAgBD,eAAe,IAAI,KAClCA,eAAe,KAAK,CAAC,qBAAqB,IAAIE,sCAC/CA;QAEN,MAAMC,YAAY,IAAIC,KAAKA,KAAK,GAAG,KAAKH,AAAgB,KAAhBA,gBAAqB,KAAK,KAAK;QAEvE,KAAK,MAAMI,WAAWN,OAAO,KAAK,CAAC,KAAK,CAAE;YACtC,MAAMO,iBAAiB,MAAM,IAAI,CAAC,wBAAwB,CAAC,OAAO,CAAC;gBAC/D,WAAWD,QAAQ,EAAE;gBACrB,WAAWP,MAAM,SAAS;gBAC1B,QAAQ;gBACRK;gBACAP;YACJ;YAEA,IAAIU,eAAe,MAAM,IACrB;YAKJ,MAAMC,YAAY,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,CAAwB;gBACpE,YAAYC;gBACZ,MAAM,CAAC,SAAS,EAAEH,QAAQ,IAAI,CAAC,GAAG,EAAEP,MAAM,SAAS,EAAE;gBACrD,OAAO;oBACH,WAAWO,QAAQ,EAAE;oBACrBV;oBACA,YAAYW,eAAe,KAAK,CAAC,EAAE;gBACvC;YACJ;YACA,IAAIC,UAAU,MAAM,IAAI;gBACpB,IAAI,CAAC,MAAM,CAAC,KAAK,CACb;oBACI,OAAOA,UAAU,KAAK;oBACtB,WAAWF,QAAQ,EAAE;oBACrB,YAAYC,eAAe,KAAK,CAAC,EAAE;gBACvC,GACA;gBAEJ,MAAMG,SAAS,MAAM,IAAI,CAAC,wBAAwB,CAAC,OAAO,CACtDH,eAAe,KAAK,CAAC,EAAE,EACvB;oBAAE,QAAQ;gBAAS;gBAEvB,IAAIG,OAAO,MAAM,IACb,IAAI,CAAC,MAAM,CAAC,KAAK,CACb;oBAAE,OAAOA,OAAO,KAAK;oBAAE,YAAYH,eAAe,KAAK,CAAC,EAAE;gBAAC,GAC3D;YAGZ;QACJ;IACJ;AACJ;AAEO,MAAMI,sCAAoBC,kBAAAA,oBAAiD,CAAC;IAC/E,gBAAgBxB;IAChB,cAAc;QACVyB;QACAC;QACAC;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
@@ -6,6 +6,17 @@ import { GetWebhookUseCase } from "../features/GetWebhook/abstractions.js";
6
6
  import { CreateWebhookUseCase } from "../features/CreateWebhook/abstractions.js";
7
7
  import { UpdateWebhookUseCase } from "../features/UpdateWebhook/abstractions.js";
8
8
  import { DeleteWebhookUseCase } from "../features/DeleteWebhook/abstractions.js";
9
+ import { WebhookPermissions } from "../features/WebhookPermissions/abstractions.js";
10
+ const createSecretFilter = async (permissions)=>{
11
+ const canSeeSecret = await permissions.canEdit("webhook");
12
+ return (webhook)=>{
13
+ if (canSeeSecret) return webhook;
14
+ return {
15
+ ...webhook,
16
+ signingSecret: null
17
+ };
18
+ };
19
+ };
9
20
  class WebhookCrudSchema_ {
10
21
  async execute(builder) {
11
22
  builder.addTypeDefs(`
@@ -114,23 +125,27 @@ class WebhookCrudSchema_ {
114
125
  builder.addResolver({
115
126
  path: "WebhookQuery.listWebhooks",
116
127
  dependencies: [
117
- ListWebhooksUseCase
128
+ ListWebhooksUseCase,
129
+ WebhookPermissions
118
130
  ],
119
- resolver: (listWebhooks)=>async ({ args })=>{
131
+ resolver: (listWebhooks, permissions)=>async ({ args })=>{
120
132
  const result = await listWebhooks.execute(args);
121
133
  if (result.isFail()) return new ListErrorResponse(result.error);
122
- return new ListResponse(result.value.items, result.value.meta);
134
+ const filterSecret = await createSecretFilter(permissions);
135
+ return new ListResponse(result.value.items.map(filterSecret), result.value.meta);
123
136
  }
124
137
  });
125
138
  builder.addResolver({
126
139
  path: "WebhookQuery.getWebhook",
127
140
  dependencies: [
128
- GetWebhookUseCase
141
+ GetWebhookUseCase,
142
+ WebhookPermissions
129
143
  ],
130
- resolver: (getWebhook)=>async ({ args })=>{
144
+ resolver: (getWebhook, permissions)=>async ({ args })=>{
131
145
  const result = await getWebhook.execute(args.id);
132
146
  if (result.isFail()) return new ErrorResponse(result.error);
133
- return new Response(result.value);
147
+ const filterSecret = await createSecretFilter(permissions);
148
+ return new Response(filterSecret(result.value));
134
149
  }
135
150
  });
136
151
  builder.addResolver({
@@ -1 +1 @@
1
- {"version":3,"file":"api/graphql/WebhookCrudSchema.js","sources":["../../../src/api/graphql/WebhookCrudSchema.ts"],"sourcesContent":["import { GraphQLSchemaFactory } from \"@webiny/api-graphql/graphql/abstractions.js\";\nimport { staticSchemaKey } from \"@webiny/api-graphql/graphql/staticSchemaKey.js\";\nimport { Response } from \"@webiny/api-graphql\";\nimport { ErrorResponse } from \"@webiny/api-graphql\";\nimport { ListResponse } from \"@webiny/api-graphql\";\nimport { ListErrorResponse } from \"@webiny/api-graphql\";\nimport { ListWebhooksUseCase } from \"~/api/features/ListWebhooks/abstractions.js\";\nimport type { IListWebhooksInput } from \"~/api/features/ListWebhooks/abstractions.js\";\nimport { GetWebhookUseCase } from \"~/api/features/GetWebhook/abstractions.js\";\nimport { CreateWebhookUseCase } from \"~/api/features/CreateWebhook/abstractions.js\";\nimport { UpdateWebhookUseCase } from \"~/api/features/UpdateWebhook/abstractions.js\";\nimport { DeleteWebhookUseCase } from \"~/api/features/DeleteWebhook/abstractions.js\";\n\ninterface IIdArgs {\n id: string;\n}\n\ninterface ICreateWebhookArgs {\n input: CreateWebhookUseCase.Input;\n}\n\ninterface IUpdateWebhookArgs {\n id: string;\n input: UpdateWebhookUseCase.Input;\n}\n\nclass WebhookCrudSchema_ implements GraphQLSchemaFactory.Interface {\n public getSchemaKey = staticSchemaKey(\"webhooks/WebhookCrudSchema_\");\n\n async execute(\n builder: GraphQLSchemaFactory.SchemaBuilder\n ): Promise<GraphQLSchemaFactory.SchemaBuilder> {\n builder.addTypeDefs(/* GraphQL */ `\n type WebhookError {\n code: String\n message: String\n data: JSON\n stack: String\n }\n\n type WebhookListMeta {\n cursor: String\n hasMoreItems: Boolean!\n totalCount: Int!\n }\n\n type Webhook {\n id: ID!\n name: String!\n slug: String!\n endpointUrl: String!\n description: String\n enabled: Boolean!\n events: [String!]!\n signingSecret: String\n createdOn: DateTime\n modifiedOn: DateTime\n }\n\n type WebhookResponse {\n data: Webhook\n error: WebhookError\n }\n\n type WebhookListResponse {\n data: [Webhook!]\n meta: WebhookListMeta\n error: WebhookError\n }\n\n input CreateWebhookInput {\n name: String!\n slug: String\n endpointUrl: String!\n description: String\n enabled: Boolean\n events: [String!]!\n signingSecret: String\n }\n\n input UpdateWebhookInput {\n name: String\n endpointUrl: String\n description: String\n enabled: Boolean\n events: [String!]\n }\n\n input ListWebhooksWhereInput {\n enabled: Boolean\n }\n\n enum ListWebhooksSort {\n createdOn_ASC\n createdOn_DESC\n modifiedOn_ASC\n modifiedOn_DESC\n name_ASC\n name_DESC\n }\n\n type WebhookQuery {\n listWebhooks(\n where: ListWebhooksWhereInput\n sort: [ListWebhooksSort!]\n limit: Int\n after: String\n ): WebhookListResponse!\n getWebhook(id: ID!): WebhookResponse!\n }\n\n type WebhookMutation {\n createWebhook(input: CreateWebhookInput!): WebhookResponse!\n updateWebhook(id: ID!, input: UpdateWebhookInput!): WebhookResponse!\n deleteWebhook(id: ID!): BooleanResponse!\n }\n\n extend type Query {\n webhooks: WebhookQuery!\n }\n\n extend type Mutation {\n webhooks: WebhookMutation!\n }\n `);\n\n builder.addResolver({\n path: \"Query.webhooks\",\n dependencies: [],\n resolver: () => () => ({})\n });\n\n builder.addResolver({\n path: \"Mutation.webhooks\",\n dependencies: [],\n resolver: () => () => ({})\n });\n\n builder.addResolver<IListWebhooksInput>({\n path: \"WebhookQuery.listWebhooks\",\n dependencies: [ListWebhooksUseCase],\n resolver: (listWebhooks: ListWebhooksUseCase.Interface) => {\n return async ({ args }) => {\n const result = await listWebhooks.execute(args);\n if (result.isFail()) {\n return new ListErrorResponse(result.error);\n }\n return new ListResponse(result.value.items, result.value.meta);\n };\n }\n });\n\n builder.addResolver<IIdArgs>({\n path: \"WebhookQuery.getWebhook\",\n dependencies: [GetWebhookUseCase],\n resolver: (getWebhook: GetWebhookUseCase.Interface) => {\n return async ({ args }) => {\n const result = await getWebhook.execute(args.id);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n builder.addResolver<ICreateWebhookArgs>({\n path: \"WebhookMutation.createWebhook\",\n dependencies: [CreateWebhookUseCase],\n resolver: (createWebhook: CreateWebhookUseCase.Interface) => {\n return async ({ args }) => {\n const result = await createWebhook.execute(args.input);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n builder.addResolver<IUpdateWebhookArgs>({\n path: \"WebhookMutation.updateWebhook\",\n dependencies: [UpdateWebhookUseCase],\n resolver: (updateWebhook: UpdateWebhookUseCase.Interface) => {\n return async ({ args }) => {\n const result = await updateWebhook.execute(args.id, args.input);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n builder.addResolver<IIdArgs>({\n path: \"WebhookMutation.deleteWebhook\",\n dependencies: [DeleteWebhookUseCase],\n resolver: (deleteWebhook: DeleteWebhookUseCase.Interface) => {\n return async ({ args }) => {\n const result = await deleteWebhook.execute(args.id);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(true);\n };\n }\n });\n\n return builder;\n }\n}\n\nexport const WebhookCrudSchema = GraphQLSchemaFactory.createImplementation({\n implementation: WebhookCrudSchema_,\n dependencies: []\n});\n"],"names":["WebhookCrudSchema_","builder","ListWebhooksUseCase","listWebhooks","args","result","ListErrorResponse","ListResponse","GetWebhookUseCase","getWebhook","ErrorResponse","Response","CreateWebhookUseCase","createWebhook","UpdateWebhookUseCase","updateWebhook","DeleteWebhookUseCase","deleteWebhook","staticSchemaKey","WebhookCrudSchema","GraphQLSchemaFactory"],"mappings":";;;;;;;;AA0BA,MAAMA;IAGF,MAAM,QACFC,OAA2C,EACA;QAC3CA,QAAQ,WAAW,CAAe,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QA4FnC,CAAC;QAEDA,QAAQ,WAAW,CAAC;YAChB,MAAM;YACN,cAAc,EAAE;YAChB,UAAU,IAAM,IAAO,EAAC;QAC5B;QAEAA,QAAQ,WAAW,CAAC;YAChB,MAAM;YACN,cAAc,EAAE;YAChB,UAAU,IAAM,IAAO,EAAC;QAC5B;QAEAA,QAAQ,WAAW,CAAqB;YACpC,MAAM;YACN,cAAc;gBAACC;aAAoB;YACnC,UAAU,CAACC,eACA,OAAO,EAAEC,IAAI,EAAE;oBAClB,MAAMC,SAAS,MAAMF,aAAa,OAAO,CAACC;oBAC1C,IAAIC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,IACb,OAAO,IAAIK,cAAcL,OAAO,KAAK;oBAEzC,OAAO,IAAIM,SAAS;gBACxB;QAER;QAEA,OAAOV;IACX;;aAtLO,YAAY,GAAGiB,gBAAgB;;AAuL1C;AAEO,MAAMC,oBAAoBC,qBAAqB,oBAAoB,CAAC;IACvE,gBAAgBpB;IAChB,cAAc,EAAE;AACpB"}
1
+ {"version":3,"file":"api/graphql/WebhookCrudSchema.js","sources":["../../../src/api/graphql/WebhookCrudSchema.ts"],"sourcesContent":["import { GraphQLSchemaFactory } from \"@webiny/api-graphql/graphql/abstractions.js\";\nimport { staticSchemaKey } from \"@webiny/api-graphql/graphql/staticSchemaKey.js\";\nimport { Response } from \"@webiny/api-graphql\";\nimport { ErrorResponse } from \"@webiny/api-graphql\";\nimport { ListResponse } from \"@webiny/api-graphql\";\nimport { ListErrorResponse } from \"@webiny/api-graphql\";\nimport { ListWebhooksUseCase } from \"~/api/features/ListWebhooks/abstractions.js\";\nimport type { IListWebhooksInput } from \"~/api/features/ListWebhooks/abstractions.js\";\nimport { GetWebhookUseCase } from \"~/api/features/GetWebhook/abstractions.js\";\nimport { CreateWebhookUseCase } from \"~/api/features/CreateWebhook/abstractions.js\";\nimport { UpdateWebhookUseCase } from \"~/api/features/UpdateWebhook/abstractions.js\";\nimport { DeleteWebhookUseCase } from \"~/api/features/DeleteWebhook/abstractions.js\";\nimport { WebhookPermissions } from \"~/api/features/WebhookPermissions/abstractions.js\";\nimport type { Webhook } from \"~/api/domain/Webhook.js\";\n\n/**\n * Security: the signing secret is returned only to identities that can edit webhooks.\n */\nconst createSecretFilter = async (permissions: WebhookPermissions.Interface) => {\n const canSeeSecret = await permissions.canEdit(\"webhook\");\n return (webhook: Webhook) => {\n if (canSeeSecret) {\n return webhook;\n }\n return { ...webhook, signingSecret: null };\n };\n};\n\ninterface IIdArgs {\n id: string;\n}\n\ninterface ICreateWebhookArgs {\n input: CreateWebhookUseCase.Input;\n}\n\ninterface IUpdateWebhookArgs {\n id: string;\n input: UpdateWebhookUseCase.Input;\n}\n\nclass WebhookCrudSchema_ implements GraphQLSchemaFactory.Interface {\n public getSchemaKey = staticSchemaKey(\"webhooks/WebhookCrudSchema_\");\n\n async execute(\n builder: GraphQLSchemaFactory.SchemaBuilder\n ): Promise<GraphQLSchemaFactory.SchemaBuilder> {\n builder.addTypeDefs(/* GraphQL */ `\n type WebhookError {\n code: String\n message: String\n data: JSON\n stack: String\n }\n\n type WebhookListMeta {\n cursor: String\n hasMoreItems: Boolean!\n totalCount: Int!\n }\n\n type Webhook {\n id: ID!\n name: String!\n slug: String!\n endpointUrl: String!\n description: String\n enabled: Boolean!\n events: [String!]!\n signingSecret: String\n createdOn: DateTime\n modifiedOn: DateTime\n }\n\n type WebhookResponse {\n data: Webhook\n error: WebhookError\n }\n\n type WebhookListResponse {\n data: [Webhook!]\n meta: WebhookListMeta\n error: WebhookError\n }\n\n input CreateWebhookInput {\n name: String!\n slug: String\n endpointUrl: String!\n description: String\n enabled: Boolean\n events: [String!]!\n signingSecret: String\n }\n\n input UpdateWebhookInput {\n name: String\n endpointUrl: String\n description: String\n enabled: Boolean\n events: [String!]\n }\n\n input ListWebhooksWhereInput {\n enabled: Boolean\n }\n\n enum ListWebhooksSort {\n createdOn_ASC\n createdOn_DESC\n modifiedOn_ASC\n modifiedOn_DESC\n name_ASC\n name_DESC\n }\n\n type WebhookQuery {\n listWebhooks(\n where: ListWebhooksWhereInput\n sort: [ListWebhooksSort!]\n limit: Int\n after: String\n ): WebhookListResponse!\n getWebhook(id: ID!): WebhookResponse!\n }\n\n type WebhookMutation {\n createWebhook(input: CreateWebhookInput!): WebhookResponse!\n updateWebhook(id: ID!, input: UpdateWebhookInput!): WebhookResponse!\n deleteWebhook(id: ID!): BooleanResponse!\n }\n\n extend type Query {\n webhooks: WebhookQuery!\n }\n\n extend type Mutation {\n webhooks: WebhookMutation!\n }\n `);\n\n builder.addResolver({\n path: \"Query.webhooks\",\n dependencies: [],\n resolver: () => () => ({})\n });\n\n builder.addResolver({\n path: \"Mutation.webhooks\",\n dependencies: [],\n resolver: () => () => ({})\n });\n\n builder.addResolver<IListWebhooksInput>({\n path: \"WebhookQuery.listWebhooks\",\n dependencies: [ListWebhooksUseCase, WebhookPermissions],\n resolver: (\n listWebhooks: ListWebhooksUseCase.Interface,\n permissions: WebhookPermissions.Interface\n ) => {\n return async ({ args }) => {\n const result = await listWebhooks.execute(args);\n if (result.isFail()) {\n return new ListErrorResponse(result.error);\n }\n const filterSecret = await createSecretFilter(permissions);\n return new ListResponse(\n result.value.items.map(filterSecret),\n result.value.meta\n );\n };\n }\n });\n\n builder.addResolver<IIdArgs>({\n path: \"WebhookQuery.getWebhook\",\n dependencies: [GetWebhookUseCase, WebhookPermissions],\n resolver: (\n getWebhook: GetWebhookUseCase.Interface,\n permissions: WebhookPermissions.Interface\n ) => {\n return async ({ args }) => {\n const result = await getWebhook.execute(args.id);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n const filterSecret = await createSecretFilter(permissions);\n return new Response(filterSecret(result.value));\n };\n }\n });\n\n builder.addResolver<ICreateWebhookArgs>({\n path: \"WebhookMutation.createWebhook\",\n dependencies: [CreateWebhookUseCase],\n resolver: (createWebhook: CreateWebhookUseCase.Interface) => {\n return async ({ args }) => {\n const result = await createWebhook.execute(args.input);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n builder.addResolver<IUpdateWebhookArgs>({\n path: \"WebhookMutation.updateWebhook\",\n dependencies: [UpdateWebhookUseCase],\n resolver: (updateWebhook: UpdateWebhookUseCase.Interface) => {\n return async ({ args }) => {\n const result = await updateWebhook.execute(args.id, args.input);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n builder.addResolver<IIdArgs>({\n path: \"WebhookMutation.deleteWebhook\",\n dependencies: [DeleteWebhookUseCase],\n resolver: (deleteWebhook: DeleteWebhookUseCase.Interface) => {\n return async ({ args }) => {\n const result = await deleteWebhook.execute(args.id);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(true);\n };\n }\n });\n\n return builder;\n }\n}\n\nexport const WebhookCrudSchema = GraphQLSchemaFactory.createImplementation({\n implementation: WebhookCrudSchema_,\n dependencies: []\n});\n"],"names":["createSecretFilter","permissions","canSeeSecret","webhook","WebhookCrudSchema_","builder","ListWebhooksUseCase","WebhookPermissions","listWebhooks","args","result","ListErrorResponse","filterSecret","ListResponse","GetWebhookUseCase","getWebhook","ErrorResponse","Response","CreateWebhookUseCase","createWebhook","UpdateWebhookUseCase","updateWebhook","DeleteWebhookUseCase","deleteWebhook","staticSchemaKey","WebhookCrudSchema","GraphQLSchemaFactory"],"mappings":";;;;;;;;;AAkBA,MAAMA,qBAAqB,OAAOC;IAC9B,MAAMC,eAAe,MAAMD,YAAY,OAAO,CAAC;IAC/C,OAAO,CAACE;QACJ,IAAID,cACA,OAAOC;QAEX,OAAO;YAAE,GAAGA,OAAO;YAAE,eAAe;QAAK;IAC7C;AACJ;AAeA,MAAMC;IAGF,MAAM,QACFC,OAA2C,EACA;QAC3CA,QAAQ,WAAW,CAAe,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QA4FnC,CAAC;QAEDA,QAAQ,WAAW,CAAC;YAChB,MAAM;YACN,cAAc,EAAE;YAChB,UAAU,IAAM,IAAO,EAAC;QAC5B;QAEAA,QAAQ,WAAW,CAAC;YAChB,MAAM;YACN,cAAc,EAAE;YAChB,UAAU,IAAM,IAAO,EAAC;QAC5B;QAEAA,QAAQ,WAAW,CAAqB;YACpC,MAAM;YACN,cAAc;gBAACC;gBAAqBC;aAAmB;YACvD,UAAU,CACNC,cACAP,cAEO,OAAO,EAAEQ,IAAI,EAAE;oBAClB,MAAMC,SAAS,MAAMF,aAAa,OAAO,CAACC;oBAC1C,IAAIC,OAAO,MAAM,I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AAO,EAAEd,IAAI,EAAE;oBAClB,MAAMC,SAAS,MAAMa,cAAc,OAAO,CAACd,KAAK,EAAE;oBAClD,IAAIC,OAAO,MAAM,IACb,OAAO,IAAIM,cAAcN,OAAO,KAAK;oBAEzC,OAAO,IAAIO,SAAS;gBACxB;QAER;QAEA,OAAOZ;IACX;;aAjMO,YAAY,GAAGmB,gBAAgB;;AAkM1C;AAEO,MAAMC,oBAAoBC,qBAAqB,oBAAoB,CAAC;IACvE,gBAAgBtB;IAChB,cAAc,EAAE;AACpB"}
@@ -1,7 +1,7 @@
1
1
  import { GraphQLSchemaFactory } from "@webiny/api-graphql/graphql/abstractions.js";
2
2
  import { staticSchemaKey } from "@webiny/api-graphql/graphql/staticSchemaKey.js";
3
3
  import { ErrorResponse, Response } from "@webiny/api-graphql";
4
- import { GetWebhookSettingsRepository } from "../features/GetWebhookSettings/abstractions.js";
4
+ import { GetWebhookSettingsUseCase } from "../features/GetWebhookSettings/abstractions.js";
5
5
  import { UpdateWebhookSettingsUseCase } from "../features/UpdateWebhookSettings/abstractions.js";
6
6
  class WebhookSettingsSchema_ {
7
7
  async execute(builder) {
@@ -32,7 +32,7 @@ class WebhookSettingsSchema_ {
32
32
  builder.addResolver({
33
33
  path: "WebhookQuery.getSettings",
34
34
  dependencies: [
35
- GetWebhookSettingsRepository
35
+ GetWebhookSettingsUseCase
36
36
  ],
37
37
  resolver: (getSettings)=>async ()=>{
38
38
  const result = await getSettings.execute();
@@ -1 +1 @@
1
- {"version":3,"file":"api/graphql/WebhookSettingsSchema.js","sources":["../../../src/api/graphql/WebhookSettingsSchema.ts"],"sourcesContent":["import { GraphQLSchemaFactory } from \"@webiny/api-graphql/graphql/abstractions.js\";\nimport { staticSchemaKey } from \"@webiny/api-graphql/graphql/staticSchemaKey.js\";\nimport { Response } from \"@webiny/api-graphql\";\nimport { ErrorResponse } from \"@webiny/api-graphql\";\nimport { GetWebhookSettingsRepository } from \"~/api/features/GetWebhookSettings/abstractions.js\";\nimport { UpdateWebhookSettingsUseCase } from \"~/api/features/UpdateWebhookSettings/abstractions.js\";\nimport type { IUpdateWebhookSettingsInput } from \"~/api/features/UpdateWebhookSettings/abstractions.js\";\n\ninterface IUpdateWebhookSettingsArgs {\n input: IUpdateWebhookSettingsInput;\n}\n\nclass WebhookSettingsSchema_ implements GraphQLSchemaFactory.Interface {\n public getSchemaKey = staticSchemaKey(\"webhooks/WebhookSettingsSchema_\");\n\n async execute(\n builder: GraphQLSchemaFactory.SchemaBuilder\n ): Promise<GraphQLSchemaFactory.SchemaBuilder> {\n builder.addTypeDefs(/* GraphQL */ `\n type WebhookSettings {\n signingSecret: String\n deliveryRetentionDays: Int\n }\n\n type WebhookSettingsResponse {\n data: WebhookSettings\n error: WebhookError\n }\n\n input UpdateWebhookSettingsInput {\n signingSecret: String\n deliveryRetentionDays: Int\n }\n\n extend type WebhookQuery {\n getSettings: WebhookSettingsResponse\n }\n\n extend type WebhookMutation {\n updateSettings(input: UpdateWebhookSettingsInput!): WebhookSettingsResponse\n }\n `);\n\n builder.addResolver({\n path: \"WebhookQuery.getSettings\",\n dependencies: [GetWebhookSettingsRepository],\n resolver: (getSettings: GetWebhookSettingsRepository.Interface) => {\n return async () => {\n const result = await getSettings.execute();\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n builder.addResolver<IUpdateWebhookSettingsArgs>({\n path: \"WebhookMutation.updateSettings\",\n dependencies: [UpdateWebhookSettingsUseCase],\n resolver: (updateSettings: UpdateWebhookSettingsUseCase.Interface) => {\n return async ({ args }) => {\n const result = await updateSettings.execute(args.input);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n return builder;\n }\n}\n\nexport const WebhookSettingsSchema = GraphQLSchemaFactory.createImplementation({\n implementation: WebhookSettingsSchema_,\n dependencies: []\n});\n"],"names":["WebhookSettingsSchema_","builder","GetWebhookSettingsRepository","getSettings","result","ErrorResponse","Response","UpdateWebhookSettingsUseCase","updateSettings","args","staticSchemaKey","WebhookSettingsSchema","GraphQLSchemaFactory"],"mappings":";;;;;AAYA,MAAMA;IAGF,MAAM,QACFC,OAA2C,EACA;QAC3CA,QAAQ,WAAW,CAAe,CAAC;;;;;;;;;;;;;;;;;;;;;;;QAuBnC,CAAC;QAEDA,QAAQ,WAAW,CAAC;YAChB,MAAM;YACN,cAAc;gBAACC;aAA6B;YAC5C,UAAU,CAACC,cACA;oBACH,MAAMC,SAAS,MAAMD,YAAY,OAAO;oBACxC,IAAIC,OAAO,MAAM,IACb,OAAO,IAAIC,cAAcD,OAAO,KAAK;oBAEzC,OAAO,IAAIE,SAASF,OAAO,KAAK;gBACpC;QAER;QAEAH,QAAQ,WAAW,CAA6B;YAC5C,MAAM;YACN,cAAc;gBAACM;aAA6B;YAC5C,UAAU,CAACC,iBACA,OAAO,EAAEC,IAAI,EAAE;oBAClB,MAAML,SAAS,MAAMI,eAAe,OAAO,CAACC,KAAK,KAAK;oBACtD,IAAIL,OAAO,MAAM,IACb,OAAO,IAAIC,cAAcD,OAAO,KAAK;oBAEzC,OAAO,IAAIE,SAASF,OAAO,KAAK;gBACpC;QAER;QAEA,OAAOH;IACX;;aA3DO,YAAY,GAAGS,gBAAgB;;AA4D1C;AAEO,MAAMC,wBAAwBC,qBAAqB,oBAAoB,CAAC;IAC3E,gBAAgBZ;IAChB,cAAc,EAAE;AACpB"}
1
+ {"version":3,"file":"api/graphql/WebhookSettingsSchema.js","sources":["../../../src/api/graphql/WebhookSettingsSchema.ts"],"sourcesContent":["import { GraphQLSchemaFactory } from \"@webiny/api-graphql/graphql/abstractions.js\";\nimport { staticSchemaKey } from \"@webiny/api-graphql/graphql/staticSchemaKey.js\";\nimport { Response } from \"@webiny/api-graphql\";\nimport { ErrorResponse } from \"@webiny/api-graphql\";\nimport { GetWebhookSettingsUseCase } from \"~/api/features/GetWebhookSettings/abstractions.js\";\nimport { UpdateWebhookSettingsUseCase } from \"~/api/features/UpdateWebhookSettings/abstractions.js\";\nimport type { IUpdateWebhookSettingsInput } from \"~/api/features/UpdateWebhookSettings/abstractions.js\";\n\ninterface IUpdateWebhookSettingsArgs {\n input: IUpdateWebhookSettingsInput;\n}\n\nclass WebhookSettingsSchema_ implements GraphQLSchemaFactory.Interface {\n public getSchemaKey = staticSchemaKey(\"webhooks/WebhookSettingsSchema_\");\n\n async execute(\n builder: GraphQLSchemaFactory.SchemaBuilder\n ): Promise<GraphQLSchemaFactory.SchemaBuilder> {\n builder.addTypeDefs(/* GraphQL */ `\n type WebhookSettings {\n signingSecret: String\n deliveryRetentionDays: Int\n }\n\n type WebhookSettingsResponse {\n data: WebhookSettings\n error: WebhookError\n }\n\n input UpdateWebhookSettingsInput {\n signingSecret: String\n deliveryRetentionDays: Int\n }\n\n extend type WebhookQuery {\n getSettings: WebhookSettingsResponse\n }\n\n extend type WebhookMutation {\n updateSettings(input: UpdateWebhookSettingsInput!): WebhookSettingsResponse\n }\n `);\n\n builder.addResolver({\n path: \"WebhookQuery.getSettings\",\n dependencies: [GetWebhookSettingsUseCase],\n resolver: (getSettings: GetWebhookSettingsUseCase.Interface) => {\n return async () => {\n const result = await getSettings.execute();\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n builder.addResolver<IUpdateWebhookSettingsArgs>({\n path: \"WebhookMutation.updateSettings\",\n dependencies: [UpdateWebhookSettingsUseCase],\n resolver: (updateSettings: UpdateWebhookSettingsUseCase.Interface) => {\n return async ({ args }) => {\n const result = await updateSettings.execute(args.input);\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n return new Response(result.value);\n };\n }\n });\n\n return builder;\n }\n}\n\nexport const WebhookSettingsSchema = GraphQLSchemaFactory.createImplementation({\n implementation: WebhookSettingsSchema_,\n dependencies: []\n});\n"],"names":["WebhookSettingsSchema_","builder","GetWebhookSettingsUseCase","getSettings","result","ErrorResponse","Response","UpdateWebhookSettingsUseCase","updateSettings","args","staticSchemaKey","WebhookSettingsSchema","GraphQLSchemaFactory"],"mappings":";;;;;AAYA,MAAMA;IAGF,MAAM,QACFC,OAA2C,EACA;QAC3CA,QAAQ,WAAW,CAAe,CAAC;;;;;;;;;;;;;;;;;;;;;;;QAuBnC,CAAC;QAEDA,QAAQ,WAAW,CAAC;YAChB,MAAM;YACN,cAAc;gBAACC;aAA0B;YACzC,UAAU,CAACC,cACA;oBACH,MAAMC,SAAS,MAAMD,YAAY,OAAO;oBACxC,IAAIC,OAAO,MAAM,IACb,OAAO,IAAIC,cAAcD,OAAO,KAAK;oBAEzC,OAAO,IAAIE,SAASF,OAAO,KAAK;gBACpC;QAER;QAEAH,QAAQ,WAAW,CAA6B;YAC5C,MAAM;YACN,cAAc;gBAACM;aAA6B;YAC5C,UAAU,CAACC,iBACA,OAAO,EAAEC,IAAI,EAAE;oBAClB,MAAML,SAAS,MAAMI,eAAe,OAAO,CAACC,KAAK,KAAK;oBACtD,IAAIL,OAAO,MAAM,IACb,OAAO,IAAIC,cAAcD,OAAO,KAAK;oBAEzC,OAAO,IAAIE,SAASF,OAAO,KAAK;gBACpC;QAER;QAEA,OAAOH;IACX;;aA3DO,YAAY,GAAGS,gBAAgB;;AA4D1C;AAEO,MAAMC,wBAAwBC,qBAAqB,oBAAoB,CAAC;IAC3E,gBAAgBZ;IAChB,cAAc,EAAE;AACpB"}