diff --git a/.changeset/dirty-dragons-heal.md b/.changeset/dirty-dragons-heal.md new file mode 100644 index 00000000..daec8aec --- /dev/null +++ b/.changeset/dirty-dragons-heal.md @@ -0,0 +1,5 @@ +--- +'@onehop/js': minor +--- + +Add types for event data and removes unimplemented events. diff --git a/src/rest/types/ignite.ts b/src/rest/types/ignite.ts index e1d24076..7a5afc88 100644 --- a/src/rest/types/ignite.ts +++ b/src/rest/types/ignite.ts @@ -132,6 +132,12 @@ export interface VolumeDefinition { mount_path: string; } +export interface ContainerMetrics { + cpu_usage_percent: number; + memory_usage_percent: number; + memory_usage_bytes: number; +} + /** * The definition of a container * @public @@ -165,11 +171,7 @@ export interface Container { /** * Metrics for this container */ - metrics: { - cpu_usage_percent: number; - memory_usage_percent: number; - memory_usage_bytes: number; - } | null; + metrics: ContainerMetrics | null; /** * Information about the container diff --git a/src/rest/types/projects.ts b/src/rest/types/projects.ts index a82d5161..c84a555d 100644 --- a/src/rest/types/projects.ts +++ b/src/rest/types/projects.ts @@ -5,6 +5,15 @@ import type { Timestamp, } from '../../util/types.ts'; import type {Endpoint} from '../endpoints.ts'; +import type { + Build, + Container, + ContainerMetrics, + Deployment, + DeploymentRollout, + Gateway, + HealthCheck, +} from './ignite.ts'; import type {User} from './users.ts'; /** @@ -249,38 +258,73 @@ export interface Webhook { /** * An event is sent from a webhook to an endpoint */ -export interface Event { - /** - * The ID of the webhook that sent this event - */ - webhook_id: Id<'webhook'>; - /** - * The ID of the project that this event is for - */ - project_id: Id<'project'>; - /** - * The time this event occurred at - */ - occurred_at: string; - /** - * The ID of the event - */ - id: Id<'event'>; - /** - * The event that occurred - * @example ignite.deployment.container.updated - */ - event: T; - /** - * The data for this event - */ - data: unknown; + +export interface HealthCheckEventUpdate { + state: 'failed' | 'succeeded' | 'pending'; + container_id: string; + deployment_id: string; } -/** - * The endpoints for projects - * @public - */ +export type EventDataMap = { + 'ignite.deployment.container.updated': Container; + 'ignite.deployment.container.created': Container; + 'ignite.deployment.container.deleted': Container; + 'ignite.deployment.created': Deployment; + 'ignite.deployment.updated': Deployment; + 'ignite.deployment.deleted': Deployment; + 'ignite.deployment.build.created': Build; + 'ignite.deployment.build.updated': Build; + 'ignite.deployment.rollout.created': DeploymentRollout; + 'ignite.deployment.rollout.updated': DeploymentRollout; + 'ignite.deployment.container.metrics_update': ContainerMetrics; + 'ignite.deployment.healthcheck.created': HealthCheck; + 'ignite.deployment.healthcheck.updated': HealthCheck; + 'ignite.deployment.healthcheck.deleted': HealthCheck; + 'ignite.deployment.healthcheck.events.failed': HealthCheckEventUpdate; + 'ignite.deployment.healthcheck.events.succeeded': HealthCheckEventUpdate; + 'ignite.deployment.gateway.created': Gateway; + 'ignite.deployment.gateway.updated': Gateway; + 'ignite.deployment.gateway.deleted': Gateway; + 'project.updated': Project; + 'project.member.created': Member; + 'project.member.updated': Member; + 'project.member.deleted': Member; + 'project.tokens.created': ProjectToken; + 'project.tokens.deleted': ProjectToken; + 'project.secrets.created': Secret; + 'project.secrets.updated': Secret; + 'project.secrets.deleted': Secret; + [key: string]: unknown; +}; + +type Events = E[keyof E]; + +export type Event = Events<{ + [Key in PossibleWebhookIDs]: { + webhook_id: Id<'webhook'>; + /** + * The ID of the project that this event is for + */ + project_id: Id<'project'>; + /** + * The time this event occurred at + */ + occurred_at: string; + /** + * The ID of the event + */ + id: Id<'event'>; + /** + * The event that occurred + */ + event: Key; + /** + * The data the belongs to the event + */ + data: EventDataMap[Key]; + }; +}>; + export type ProjectsEndpoints = | Endpoint< 'DELETE', diff --git a/src/util/webhooks.ts b/src/util/webhooks.ts index c2e0df0e..b8c3320a 100644 --- a/src/util/webhooks.ts +++ b/src/util/webhooks.ts @@ -2,7 +2,8 @@ import type {Event} from '..'; import {crypto} from './crypto'; export const POSSIBLE_EVENTS = { - Channels: [ + // These are not yet implemented + /* Channels: [ { id: 'channel.created', name: 'Created', @@ -23,7 +24,7 @@ export const POSSIBLE_EVENTS = { id: 'channel.client.disconnected', name: 'Client Disconnected', }, - ], + ], */ Ignite: [ { id: 'ignite.deployment.created', diff --git a/tests/projects/webhooks.ts b/tests/projects/webhooks.ts index 40e7bd68..98cfc53e 100644 --- a/tests/projects/webhooks.ts +++ b/tests/projects/webhooks.ts @@ -8,7 +8,7 @@ export function webhookTests(hop: Hop) { test('It creates a webhook', async () => { const webhook = await hop.projects.webhooks.create( 'https://example.com/webhook', - ['ignite.deployment.build.cancelled', 'channel.client.disconnected'], + ['ignite.deployment.build.cancelled', 'ignite.deployment.build.created'], ); assert.ok(