Skip to content

Commit f76367b

Browse files
feat: coded action app event service
1 parent 67b93ed commit f76367b

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

src/models/action-center/tasks.types.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,47 @@ export type TaskGetByIdOptions = BaseOptions
244244
* Options for getting users with task permissions
245245
*/
246246
export type TaskGetUsersOptions = RequestOptions & PaginationOptions;
247+
248+
export enum ActionCenterEventNames {
249+
INIT = 'init',
250+
COMPLETE = 'complete',
251+
DATACHANGED = 'dataChanged',
252+
LOADAPP = 'loadApp',
253+
ERROR = 'error',
254+
TOKENREFRESHED = 'tokenRefreshed',
255+
LANGUAGECHANGED = 'languageChanged',
256+
THEMECHANGED = 'themeChanged',
257+
}
258+
259+
enum Theme {
260+
autoTheme = 'autoTheme',
261+
light = 'light',
262+
dark = 'dark',
263+
lighthc = 'light-hc',
264+
darkhc = 'dark-hc',
265+
}
266+
267+
export type ActionCenterData = {
268+
taskId: number,
269+
title: string,
270+
status: TaskStatus,
271+
isReadOnly: boolean,
272+
data: any,
273+
action: string,
274+
organizationUnitId: number,
275+
organizationUnitFullyQualifiedName: string,
276+
baseUrl: string,
277+
orgName: string,
278+
tenantName: string,
279+
token: string,
280+
language: string,
281+
theme: Theme,
282+
newToken: string,
283+
newLanguage: string,
284+
newTheme: string,
285+
}
286+
287+
export type ActionCenterEventResponsePayload = {
288+
eventType: ActionCenterEventNames,
289+
content: ActionCenterData,
290+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { ActionCenterData, ActionCenterEventResponsePayload, ActionCenterEventNames } from '../../models/action-center/tasks.types';
2+
3+
export class TaskEventsService {
4+
private messageListener: ((event: MessageEvent<ActionCenterEventResponsePayload>) => void) | null = null;
5+
private parentOrigin = new URLSearchParams(window.location.search).get('basedomain');
6+
private subscribedEvents = [ActionCenterEventNames.LOADAPP, ActionCenterEventNames.TOKENREFRESHED, ActionCenterEventNames.LANGUAGECHANGED, ActionCenterEventNames.THEMECHANGED];
7+
8+
initializeInActionCenter(): void {
9+
this.sendMessageToParent(ActionCenterEventNames.INIT);
10+
}
11+
12+
dataChanged(data: any): void {
13+
this.sendMessageToParent(ActionCenterEventNames.DATACHANGED, data);
14+
}
15+
16+
completeTask(actionTaken: string, data: any): void {
17+
const content = {
18+
data: data,
19+
action: actionTaken,
20+
};
21+
this.sendMessageToParent(ActionCenterEventNames.COMPLETE, content);
22+
}
23+
24+
getTaskDetailsFromActionCenter(callback: (data: ActionCenterData) => void): void {
25+
if (this.isValidOrigin(this.parentOrigin) && this.messageListener === null) {
26+
this.messageListener = (event: MessageEvent<ActionCenterEventResponsePayload>) => this.actionCenterEventCallback(event, callback);
27+
window.addEventListener('message', this.messageListener);
28+
}
29+
}
30+
31+
cleanup(): void {
32+
if (this.messageListener) {
33+
window.removeEventListener('message', this.messageListener);
34+
this.messageListener = null;
35+
}
36+
}
37+
38+
private sendMessageToParent(eventType: string, content?: any): void {
39+
if (window.parent && this.isValidOrigin(this.parentOrigin)) {
40+
try {
41+
window.parent.postMessage(
42+
{ eventType, content },
43+
this.parentOrigin!,
44+
);
45+
} catch (error) {
46+
window.parent.postMessage(
47+
{
48+
eventType: ActionCenterEventNames.ERROR,
49+
content: {
50+
errorData: error,
51+
}
52+
},
53+
this.parentOrigin!
54+
);
55+
}
56+
}
57+
}
58+
59+
private isValidOrigin(origin: string | null): boolean {
60+
if (origin && (origin.endsWith('.uipath.com') || origin === 'http://localhost:4202')) {
61+
return true;
62+
}
63+
64+
return false;
65+
}
66+
67+
private actionCenterEventCallback(event: MessageEvent<ActionCenterEventResponsePayload>, callback: (data: ActionCenterData) => void): void {
68+
if (event.origin === this.parentOrigin && this.subscribedEvents.includes(event.data?.eventType)) {
69+
callback(event.data?.content);
70+
}
71+
}
72+
}

src/uipath.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { UiPathSDKConfig, hasOAuthConfig, hasSecretConfig } from './core/config/
1818
import { validateConfig, normalizeBaseUrl } from './core/config/config-utils';
1919
import { TokenManager } from './core/auth/token-manager';
2020
import { telemetryClient, trackEvent } from './core/telemetry';
21+
import { TaskEventsService } from './services/action-center/codedActionEventsService';
2122

2223
type ServiceConstructor<T> = new (config: UiPathConfig, context: ExecutionContext, tokenManager: TokenManager) => T;
2324

@@ -151,6 +152,10 @@ export class UiPath {
151152
return this.authService.getToken();
152153
}
153154

155+
public updateToken(token: string) {
156+
return this.authService.authenticateWithSecret(token);
157+
}
158+
154159
private getService<T>(serviceConstructor: ServiceConstructor<T>): T {
155160
const serviceName = serviceConstructor.name;
156161
if (!this._services.has(serviceName)) {
@@ -205,6 +210,13 @@ export class UiPath {
205210
return this.getService(TaskService);
206211
}
207212

213+
/**
214+
* Access to Task Events service
215+
*/
216+
get taskEvents(): TaskEventsService {
217+
return this.getService(TaskEventsService);
218+
}
219+
208220
/**
209221
* Access to Orchestrator Processes service
210222
*/

0 commit comments

Comments
 (0)