Skip to content

Commit

Permalink
Merge pull request #144 from hopinc/webhook-data-types
Browse files Browse the repository at this point in the history
webhook types
  • Loading branch information
Looskie authored Oct 4, 2023
2 parents 899b665 + 89a52dd commit 13fde01
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-dragons-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@onehop/js': minor
---

Add types for event data and removes unimplemented events.
12 changes: 7 additions & 5 deletions src/rest/types/ignite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
104 changes: 74 additions & 30 deletions src/rest/types/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -249,38 +258,73 @@ export interface Webhook {
/**
* An event is sent from a webhook to an endpoint
*/
export interface Event<T extends PossibleWebhookIDs = PossibleWebhookIDs> {
/**
* 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> = 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',
Expand Down
5 changes: 3 additions & 2 deletions src/util/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -23,7 +24,7 @@ export const POSSIBLE_EVENTS = {
id: 'channel.client.disconnected',
name: 'Client Disconnected',
},
],
], */
Ignite: [
{
id: 'ignite.deployment.created',
Expand Down
2 changes: 1 addition & 1 deletion tests/projects/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 13fde01

Please sign in to comment.