Skip to content

Commit 744d631

Browse files
authored
Add Events API beta (#741)
* Add Events API beta * Remove console log * Lint * Update pagination options * Use listEvents
1 parent bb1fbb1 commit 744d631

File tree

10 files changed

+193
-6
lines changed

10 files changed

+193
-6
lines changed

src/audit-trail/audit-trail.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CreateEventOptions } from './interfaces/create-event-options.interface';
2-
import { Event } from './interfaces/event.interface';
2+
import { AuditTrailEvent } from './interfaces/event.interface';
33
import { EventOptions } from './interfaces/event-options.interface';
44
import { List } from '../common/interfaces/list.interface';
55
import { ListEventsOptions } from './interfaces/list-events-options.interface';
@@ -15,7 +15,9 @@ export class AuditTrail {
1515
await this.workos.post('/events', event, { idempotencyKey });
1616
}
1717

18-
async listEvents(options?: ListEventsOptions): Promise<List<Event>> {
18+
async listEvents(
19+
options?: ListEventsOptions,
20+
): Promise<List<AuditTrailEvent>> {
1921
const { data } = await this.workos.get('/events', {
2022
query: options,
2123
});

src/audit-trail/interfaces/event.interface.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export interface EventAction {
1+
export interface AuditTrailEventAction {
22
object: 'event_action';
33
id: string;
44
name: string;
55
}
66

7-
export interface Event {
7+
export interface AuditTrailEvent {
88
object: 'event';
99
id: string;
1010
group: string;
@@ -17,7 +17,7 @@ export interface Event {
1717
target_name: string;
1818
target_id: string;
1919
occurred_at: Date;
20-
action: EventAction;
20+
action: AuditTrailEventAction;
2121
metadata?: {
2222
[key: string]: string;
2323
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { CreateEventOptions } from './create-event-options.interface';
22
export { EventOptions } from './event-options.interface';
3-
export { Event } from './event.interface';
3+
export { AuditTrailEvent } from './event.interface';
44
export { ListEventsOptions } from './list-events-options.interface';

src/events/events.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import axios from 'axios';
2+
3+
import MockAdapater from 'axios-mock-adapter';
4+
5+
import { List } from '../common/interfaces/list.interface';
6+
import { WorkOS } from '../workos';
7+
import { Event } from './interfaces/event.interface';
8+
9+
const mock = new MockAdapater(axios);
10+
11+
describe('Event', () => {
12+
afterEach(() => mock.resetHistory());
13+
14+
const workos = new WorkOS('sk_test_Sz3IQjepeSWaI4cMS4ms4sMuU');
15+
16+
const eventResponse: Event = {
17+
id: 'event_01234ABCD',
18+
created_at: '2020-05-06 04:21:48.649164',
19+
event: 'dsync.user.created',
20+
data: {
21+
id: 'event_01234ABCD',
22+
},
23+
};
24+
25+
describe('listEvents', () => {
26+
const eventsListResponse: List<Event> = {
27+
object: 'list',
28+
data: [eventResponse],
29+
list_metadata: {},
30+
};
31+
32+
it(`requests Events`, async () => {
33+
mock.onGet('/events', {}).replyOnce(200, eventsListResponse);
34+
35+
const list = await workos.events.listEvents({});
36+
37+
expect(list).toEqual(eventsListResponse);
38+
});
39+
40+
it(`requests Events with a valid event name`, async () => {
41+
mock.onGet('/events').replyOnce(200, eventsListResponse);
42+
43+
const list = await workos.events.listEvents({
44+
events: ['connection.activated'],
45+
});
46+
47+
expect(list).toEqual(eventsListResponse);
48+
});
49+
});
50+
});

src/events/events.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { WorkOS } from '../workos';
2+
import { Event } from './interfaces/event.interface';
3+
import { List } from '../common/interfaces/list.interface';
4+
import { ListEventOptions } from './interfaces';
5+
6+
export class Events {
7+
constructor(private readonly workos: WorkOS) {}
8+
9+
async listEvents(options: ListEventOptions): Promise<List<Event>> {
10+
const { data } = await this.workos.get(`/events`, {
11+
query: options,
12+
});
13+
return data;
14+
}
15+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
interface EventBase {
2+
id: string;
3+
created_at: string;
4+
}
5+
6+
export interface ConnectionActivatedEvent extends EventBase {
7+
event: 'connection.activated';
8+
data: Record<string, unknown>;
9+
}
10+
11+
export interface ConnectionDeactivatedEvent extends EventBase {
12+
event: 'connection.deactivated';
13+
data: Record<string, unknown>;
14+
}
15+
16+
export interface ConnectionDeletedEvent extends EventBase {
17+
event: 'connection.deleted';
18+
data: Record<string, unknown>;
19+
}
20+
21+
export interface DsyncActivatedEvent extends EventBase {
22+
event: 'dsync.activated';
23+
data: Record<string, unknown>;
24+
}
25+
26+
export interface DsyncDeactivatedEvent extends EventBase {
27+
event: 'dsync.deactivated';
28+
data: Record<string, unknown>;
29+
}
30+
31+
export interface DsyncDeletedEvent extends EventBase {
32+
event: 'dsync.deleted';
33+
data: Record<string, unknown>;
34+
}
35+
36+
export interface DsyncGroupCreatedEvent extends EventBase {
37+
event: 'dsync.group.created';
38+
data: Record<string, unknown>;
39+
}
40+
41+
export interface DsyncGroupDeletedEvent extends EventBase {
42+
event: 'dsync.group.deleted';
43+
data: Record<string, unknown>;
44+
}
45+
46+
export interface DsyncGroupUpdatedEvent extends EventBase {
47+
event: 'dsync.group.updated';
48+
data: Record<string, unknown>;
49+
}
50+
51+
export interface DsyncGroupUserAddedEvent extends EventBase {
52+
event: 'dsync.group.user_added';
53+
data: Record<string, unknown>;
54+
}
55+
56+
export interface DsyncGroupUserRemovedEvent extends EventBase {
57+
event: 'dsync.group.user_removed';
58+
data: Record<string, unknown>;
59+
}
60+
61+
export interface DsyncUserCreatedEvent extends EventBase {
62+
event: 'dsync.user.created';
63+
data: Record<string, unknown>;
64+
}
65+
66+
export interface DsyncUserDeletedEvent extends EventBase {
67+
event: 'dsync.user.deleted';
68+
data: Record<string, unknown>;
69+
}
70+
71+
export interface DsyncUserUpdatedEvent extends EventBase {
72+
event: 'dsync.user.updated';
73+
data: Record<string, unknown>;
74+
}
75+
76+
export type Event =
77+
| ConnectionActivatedEvent
78+
| ConnectionDeactivatedEvent
79+
| ConnectionDeletedEvent
80+
| DsyncActivatedEvent
81+
| DsyncDeactivatedEvent
82+
| DsyncDeletedEvent
83+
| DsyncGroupCreatedEvent
84+
| DsyncGroupUpdatedEvent
85+
| DsyncGroupDeletedEvent
86+
| DsyncGroupUserAddedEvent
87+
| DsyncGroupUserRemovedEvent
88+
| DsyncUserCreatedEvent
89+
| DsyncUserUpdatedEvent
90+
| DsyncUserDeletedEvent;
91+
92+
export type EventNames =
93+
| 'connection.activated'
94+
| 'connection.deactivated'
95+
| 'connection.deleted'
96+
| 'dsync.activated'
97+
| 'dsync.deactivated'
98+
| 'dsync.deleted'
99+
| 'dsync.group.created'
100+
| 'dsync.group.deleted'
101+
| 'dsync.group.updated'
102+
| 'dsync.group.user_added'
103+
| 'dsync.group.user_removed'
104+
| 'dsync.user.created'
105+
| 'dsync.user.deleted'
106+
| 'dsync.user.updated';

src/events/interfaces/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './list-events-options.interface';
2+
export * from './event.interface';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { EventNames } from './event.interface';
2+
3+
export interface ListEventOptions {
4+
events?: EventNames[];
5+
rangeStart?: string;
6+
rangeEnd?: string;
7+
limit?: number;
8+
after?: string;
9+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './common/exceptions';
66
export * from './common/interfaces';
77
export * from './directory-sync/interfaces';
88
export * from './directory-sync/utils/get-primary-email';
9+
export * from './events/interfaces';
910
export * from './organizations/interfaces';
1011
export * from './passwordless/interfaces';
1112
export * from './portal/interfaces';

src/workos.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
WorkOSResponseError,
1717
} from './common/interfaces';
1818
import { DirectorySync } from './directory-sync/directory-sync';
19+
import { Events } from './events/events';
1920
import { Organizations } from './organizations/organizations';
2021
import { Passwordless } from './passwordless/passwordless';
2122
import { Portal } from './portal/portal';
@@ -42,6 +43,7 @@ export class WorkOS {
4243
readonly sso = new SSO(this);
4344
readonly webhooks = new Webhooks();
4445
readonly mfa = new Mfa(this);
46+
readonly events = new Events(this);
4547

4648
constructor(readonly key?: string, readonly options: WorkOSOptions = {}) {
4749
if (!key) {

0 commit comments

Comments
 (0)