-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathsupport.ts
83 lines (76 loc) · 2.26 KB
/
support.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { AgentResponseMessage, AppRequestMessage } from '@finos/fdc3-schema/dist/generated/api/BrowserTypes';
import { AppRegistration, ServerContext } from '../ServerContext';
import { AppIdentifier } from '@finos/fdc3-standard';
/** Interface representing a full specified app identifier (instanceId is optional in the API type). */
export interface FullAppIdentifier {
readonly appId: string;
readonly instanceId: string;
}
export function isFullAppIdentifier(identifier: AppIdentifier | FullAppIdentifier): identifier is FullAppIdentifier {
const typedIdentifier = identifier as FullAppIdentifier;
return typedIdentifier.instanceId !== undefined && typedIdentifier.appId !== undefined;
}
export function successResponse(
sc: ServerContext<AppRegistration>,
request: AppRequestMessage,
to: FullAppIdentifier,
payload: AgentResponseMessage['payload'],
type: AgentResponseMessage['type']
) {
return successResponseId(sc, request.meta.requestUuid, to, payload, type);
}
export function errorResponse(
sc: ServerContext<AppRegistration>,
request: AppRequestMessage,
to: FullAppIdentifier,
error: string,
type: AgentResponseMessage['type']
) {
return errorResponseId(sc, request.meta.requestUuid, to, error, type);
}
export function successResponseId(
sc: ServerContext<AppRegistration>,
requestId: string,
to: FullAppIdentifier,
payload: AgentResponseMessage['payload'],
type: AgentResponseMessage['type']
) {
const msg = {
meta: {
responseUuid: sc.createUUID(),
requestUuid: requestId,
timestamp: new Date(),
},
type,
payload,
};
sc.post(msg, to.instanceId!);
}
export function errorResponseId(
sc: ServerContext<AppRegistration>,
requestId: string,
to: FullAppIdentifier,
error: string,
type: AgentResponseMessage['type']
) {
sc.post(
{
meta: {
responseUuid: sc.createUUID(),
requestUuid: requestId,
timestamp: new Date(),
},
type,
payload: {
error,
},
} as AgentResponseMessage,
to.instanceId!
);
}
/*
* from: https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates#14438954
*/
export function onlyUnique<X>(value: X, index: number, self: X[]) {
return self.indexOf(value) === index;
}