| 1 | import { schema } from '@ghost/db'; |
| 2 | import { ApiProperty, ApiPropertyOptional |
| 3 | import { |
| 4 | ArrayMinSize, |
| 5 | IsArray, |
| 6 | IsBoolean, |
| 7 | IsIn, |
| 8 | IsOptional, |
| 9 | IsString, |
| 10 | MaxLength, |
| 11 | } from 'class-validator'; |
| 12 | |
| 13 | import { |
| 14 | type WebhookEvent, |
| 15 | webhookEvents, |
| 16 | } from '../../../lib/webhooks/webhooks.js'; |
| 17 | |
| 18 | const eventsProperty = { |
| 19 | enumName: 'WebhookEvent', |
| 20 | enum: webhookEvents, |
| 21 | isArray: true, |
| 22 | }; |
| 23 | |
| 24 | export class CreateWebhookRequestDTO |
| 25 | @ApiProperty({ example |
| 26 | @IsString() |
| 27 | @MaxLength(2048) |
| 28 | url: string; |
| 29 | |
| 30 | @ApiProperty({ |
| 31 | ...eventsProperty, |
| 32 | description: 'Events that trigger a delivery. At least one.', |
| 33 | }) |
| 34 | @IsArray() |
| 35 | @ArrayMinSize(1) |
| 36 | @IsIn(webhookEvents, |
| 37 | events: WebhookEvent |
| 38 | } |
| 39 | |
| 40 | export class UpdateWebhookRequestDTO |
| 41 | @ApiPropertyOptional() |
| 42 | @IsOptional() |
| 43 | @IsString() |
| 44 | @MaxLength(2048) |
| 45 | url?: string |
| 46 | |
| 47 | @ApiPropertyOptional(eventsProperty) |
| 48 | @IsOptional() |
| 49 | @IsArray() |
| 50 | @ArrayMinSize(1) |
| 51 | @IsIn(webhookEvents, |
| 52 | events?: WebhookEvent |
| 53 | |
| 54 | @ApiPropertyOptional({ |
| 55 | description: |
| 56 | 'Turning an endpoint back on clears the reason it was turned off.', |
| 57 | }) |
| 58 | @IsOptional() |
| 59 | @IsBoolean() |
| 60 | active?: boolean |
| 61 | } |
| 62 | |
| 63 | export class WebhookDTO |
| 64 | @ApiProperty() |
| 65 | id: string; |
| 66 | |
| 67 | @ApiProperty() |
| 68 | url: string; |
| 69 | |
| 70 | @ApiProperty(eventsProperty) |
| 71 | events: WebhookEvent |
| 72 | |
| 73 | @ApiProperty() |
| 74 | active: boolean; |
| 75 | |
| 76 | @ApiProperty({ |
| 77 | type: String, |
| 78 | nullable: true, |
| 79 | description: 'Why Ghost turned the endpoint off, such as a 410 Gone.', |
| 80 | }) |
| 81 | disabledReason: string |
| 82 | |
| 83 | @ApiProperty() |
| 84 | createdAt: string; |
| 85 | } |
| 86 | |
| 87 | export class CreatedWebhookDTO |
| 88 | @ApiProperty({ |
| 89 | description: |
| 90 | 'Signs every delivery. Shown only now: Ghost stores it encrypted and never returns it again.', |
| 91 | }) |
| 92 | secret: string; |
| 93 | } |
| 94 | |
| 95 | export class WebhookSecretDTO |
| 96 | @ApiProperty({ |
| 97 | description: |
| 98 | 'The new signing secret. The old one stops working at once, and this is the only time the new one is shown.', |
| 99 | }) |
| 100 | secret: string; |
| 101 | } |
| 102 | |
| 103 | export class ListWebhooksResponseDTO |
| 104 | @ApiProperty({ type |
| 105 | webhooks: WebhookDTO |
| 106 | } |
| 107 | |
| 108 | export class DeliveryAttemptDTO |
| 109 | @ApiProperty() |
| 110 | startedAt: string; |
| 111 | |
| 112 | @ApiProperty() |
| 113 | durationMs: number; |
| 114 | |
| 115 | @ApiProperty({ |
| 116 | type: Number, |
| 117 | nullable: true, |
| 118 | description: 'Null when no response came back.', |
| 119 | }) |
| 120 | statusCode: number |
| 121 | |
| 122 | @ApiProperty({ type |
| 123 | error: string |
| 124 | |
| 125 | @ApiProperty({ type |
| 126 | requestHeaders: Record |
| 127 | |
| 128 | @ApiProperty({ |
| 129 | type: String, |
| 130 | nullable: true, |
| 131 | description: 'The first 4 KB of the response.', |
| 132 | }) |
| 133 | responseBody: string |
| 134 | } |
| 135 | |
| 136 | export class DeliveryDTO |
| 137 | @ApiProperty({ description |
| 138 | id: string; |
| 139 | |
| 140 | @ApiProperty() |
| 141 | event: string; |
| 142 | |
| 143 | @ApiProperty({ |
| 144 | enumName: 'DeliveryStatus', |
| 145 | enum: schema.deliveryJobStatus |
| 146 | description: |
| 147 | '`dead` means Ghost gave up. A redelivery sends it again as a new delivery.', |
| 148 | }) |
| 149 | status: (typeof |
| 150 | |
| 151 | @ApiProperty({ description |
| 152 | body: string; |
| 153 | |
| 154 | @ApiProperty() |
| 155 | createdAt: string; |
| 156 | |
| 157 | @ApiProperty({ |
| 158 | type: String, |
| 159 | nullable: true, |
| 160 | description: 'When the next try is due, while the delivery is pending.', |
| 161 | }) |
| 162 | nextAttemptAt: string |
| 163 | |
| 164 | @ApiProperty({ type |
| 165 | attempts: DeliveryAttemptDTO |
| 166 | } |
| 167 | |
| 168 | export class ListDeliveriesResponseDTO |
| 169 | @ApiProperty({ type |
| 170 | deliveries: DeliveryDTO |
| 171 | } |