From 906389f3dd284269e7e6864064b53c63de73d1cc Mon Sep 17 00:00:00 2001 From: robjmorrissey Date: Thu, 5 Oct 2023 00:02:51 +0100 Subject: [PATCH 1/6] webhook types --- src/rest/types/ignite.ts | 12 +++-- src/rest/types/projects.ts | 105 ++++++++++++++++++++++++++++--------- 2 files changed, 86 insertions(+), 31 deletions(-) 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..a214bd4c 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,34 +258,78 @@ 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; } +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; +}; + +type Events = keyof EventDataMap; + +export type Event = Events extends infer E + ? E extends Events + ? { + /** + * 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: E; + /** + * The data for this event + */ + data: EventDataMap[E]; + } + : never + : never; + /** * The endpoints for projects * @public From 174b2621e7c89fd16aaaba3c2d1865f289ffee46 Mon Sep 17 00:00:00 2001 From: robjmorrissey Date: Thu, 5 Oct 2023 00:11:22 +0100 Subject: [PATCH 2/6] improve --- src/rest/types/projects.ts | 58 ++++++++++++++------------------------ 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/src/rest/types/projects.ts b/src/rest/types/projects.ts index a214bd4c..4a9341b4 100644 --- a/src/rest/types/projects.ts +++ b/src/rest/types/projects.ts @@ -296,44 +296,28 @@ export type EventDataMap = { 'project.secrets.deleted': Secret; }; -type Events = keyof EventDataMap; - -export type Event = Events extends infer E - ? E extends Events - ? { - /** - * 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: E; - /** - * The data for this event - */ - data: EventDataMap[E]; - } - : never - : never; +type Events = E[keyof E]; + +export type Event = Events<{ + [Key in keyof EventDataMap]: { + 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'>; + event: Key; + data: EventDataMap[Key]; + }; +}>; -/** - * The endpoints for projects - * @public - */ export type ProjectsEndpoints = | Endpoint< 'DELETE', From 3ad2d1f7f57cdfe640a582f2681ec51a1326d27f Mon Sep 17 00:00:00 2001 From: robjmorrissey Date: Thu, 5 Oct 2023 00:11:47 +0100 Subject: [PATCH 3/6] docs(changeset): Add types for event data --- .changeset/dirty-dragons-heal.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/dirty-dragons-heal.md diff --git a/.changeset/dirty-dragons-heal.md b/.changeset/dirty-dragons-heal.md new file mode 100644 index 00000000..410917e5 --- /dev/null +++ b/.changeset/dirty-dragons-heal.md @@ -0,0 +1,5 @@ +--- +'@onehop/js': minor +--- + +Add types for event data From cba9caed75084d17896f2288b6cfa8be8f5516c6 Mon Sep 17 00:00:00 2001 From: Cody Miller Date: Wed, 4 Oct 2023 19:18:33 -0400 Subject: [PATCH 4/6] fix: small type adjustments --- src/rest/types/projects.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/rest/types/projects.ts b/src/rest/types/projects.ts index 4a9341b4..c84a555d 100644 --- a/src/rest/types/projects.ts +++ b/src/rest/types/projects.ts @@ -294,12 +294,13 @@ export type EventDataMap = { '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 keyof EventDataMap]: { + [Key in PossibleWebhookIDs]: { webhook_id: Id<'webhook'>; /** * The ID of the project that this event is for @@ -313,7 +314,13 @@ export type Event = Events<{ * The ID of the event */ id: Id<'event'>; + /** + * The event that occurred + */ event: Key; + /** + * The data the belongs to the event + */ data: EventDataMap[Key]; }; }>; From 31d45b36c7789906d98329a639de574299688e4c Mon Sep 17 00:00:00 2001 From: Cody Miller Date: Wed, 4 Oct 2023 19:21:44 -0400 Subject: [PATCH 5/6] fix: remove unimplemented events --- src/util/webhooks.ts | 5 +++-- tests/projects/webhooks.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) 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( From 89a52dd65734818362470d77a3e24a6125cf4c38 Mon Sep 17 00:00:00 2001 From: Cody Miller Date: Wed, 4 Oct 2023 19:22:40 -0400 Subject: [PATCH 6/6] refactor: update changeset description --- .changeset/dirty-dragons-heal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/dirty-dragons-heal.md b/.changeset/dirty-dragons-heal.md index 410917e5..daec8aec 100644 --- a/.changeset/dirty-dragons-heal.md +++ b/.changeset/dirty-dragons-heal.md @@ -2,4 +2,4 @@ '@onehop/js': minor --- -Add types for event data +Add types for event data and removes unimplemented events.