-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import EventEmitter from 'events'; | ||
|
||
export interface LevitateConfig { | ||
host?: string; | ||
orgSlug: string; | ||
refreshTokens: { | ||
write: string; | ||
}; | ||
} | ||
|
||
export interface DomainEventsBody { | ||
timestamp: string; | ||
event_name: string; | ||
event_state: 'start' | 'stop'; | ||
workspace?: string; | ||
namespace?: string; | ||
entity_type?: string; | ||
data_source_name: string; | ||
} | ||
|
||
const defaultHost = 'https://app.last9.io'; | ||
|
||
export class LevitateEvents extends EventEmitter { | ||
private eventsUrl: URL; | ||
public levitateConfig?: LevitateConfig; | ||
constructor() { | ||
super(); | ||
this.eventsUrl = new URL( | ||
`/api/v4/organizations/${this.levitateConfig?.orgSlug}/domain_events`, | ||
this.levitateConfig?.host ?? defaultHost | ||
); | ||
this.initiateEventListeners(); | ||
} | ||
|
||
// Making the emit and on methods type safe | ||
public emit( | ||
event: 'application_started', | ||
...args: (DomainEventsBody | any)[] | ||
): boolean; | ||
public emit( | ||
event: 'application_stopped', | ||
...args: (DomainEventsBody | any)[] | ||
): boolean; | ||
public emit(event: any, ...args: any[]): any { | ||
return super.emit(event, ...args); | ||
} | ||
|
||
public on( | ||
event: 'application_started', | ||
listener: (...args: (DomainEventsBody | any)[]) => void | ||
): this; | ||
public on( | ||
event: 'application_stopped', | ||
listener: (...args: (DomainEventsBody | any)[]) => void | ||
): this; | ||
public on(event: any, listener: (...args: any[]) => void): this { | ||
return super.on(event, listener); | ||
} | ||
|
||
public once( | ||
event: 'application_started', | ||
listener: (...args: (DomainEventsBody | any)[]) => void | ||
): this; | ||
public once( | ||
event: 'application_stopped', | ||
listener: (...args: (DomainEventsBody | any)[]) => void | ||
): this; | ||
public once(event: any, listener: (...args: any[]) => void): this { | ||
return super.on(event, listener); | ||
} | ||
|
||
private initiateEventListeners() { | ||
if (typeof this.levitateConfig?.refreshTokens?.write === 'string') { | ||
this.once('application_started', this.applicationStarted); | ||
} | ||
} | ||
|
||
private applicationStarted(body: DomainEventsBody) { | ||
// fetch(this.eventsUrl.toString(), { | ||
// method: 'PUT', | ||
// headers: { | ||
// 'Content-Type': 'application/json', | ||
// 'X-LAST9-API-TOKEN': `Bearer ` | ||
// }, | ||
// body: { | ||
|
||
// } | ||
// }); | ||
console.log(body); | ||
} | ||
} |
Empty file.