-
Notifications
You must be signed in to change notification settings - Fork 208
/
BentleyCloudRpcManager.ts
101 lines (85 loc) · 4.52 KB
/
BentleyCloudRpcManager.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module RpcInterface
*/
import { RpcInterfaceDefinition } from "../../RpcInterface";
import { RpcManager } from "../../RpcManager";
import { RpcConfiguration } from "../core/RpcConfiguration";
import { RpcRequestEvent } from "../core/RpcConstants";
import { RpcRequest, RpcRequestEventHandler } from "../core/RpcRequest";
import { BentleyCloudRpcProtocol } from "./BentleyCloudRpcProtocol";
import { OpenAPIInfo } from "./OpenAPI";
import { RpcRoutingToken } from "../core/RpcRoutingToken";
/* eslint-disable deprecation/deprecation */
/** Initialization parameters for BentleyCloudRpcConfiguration.
* @beta
*/
export interface BentleyCloudRpcParams {
/** Identifies the remote server that implements a set of RpcInterfaces. Note that the ID of the remote server is not a URI or hostname. It is a string that matches a key in the orchestrator's app registry. */
info: OpenAPIInfo;
/** @internal The protocol for Bentley cloud RPC interface deployments */
protocol?: typeof BentleyCloudRpcProtocol;
/** The URI of the orchestrator that will route requests to the remote RpcInterface server. If not supplied, this default to the origin of the Web page. This is required only when calling initializeClient and only if the server is not the origin of the Web page. */
uriPrefix?: string;
/** @internal Handler for RPC request events. */
pendingRequestListener?: RpcRequestEventHandler;
/** An optional prefix for RPC operation URI paths. */
pathPrefix?: string;
}
/** Operating parameters for Bentley cloud RPC interface deployments.
* @beta
*/
export abstract class BentleyCloudRpcConfiguration extends RpcConfiguration {
/** Access-Control header values for backend servers that serve frontends using BentleyCloudRpcProtocol. */
public static readonly accessControl = {
allowOrigin: "*",
allowMethods: "POST, GET, OPTIONS",
allowHeaders: "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, X-Correlation-Id, X-Session-Id, X-Application-Id, X-Application-Version, X-User-Id, X-Protocol-Version",
};
/** @internal The protocol of the configuration. */
public abstract override readonly protocol: BentleyCloudRpcProtocol;
}
/** Coordinates usage of RPC interfaces for Bentley cloud deployments.
* @beta
*/
export class BentleyCloudRpcManager extends RpcManager {
/** @beta Initializes BentleyCloudRpcManager for the frontend of an application. */
public static initializeClient(params: BentleyCloudRpcParams, interfaces: RpcInterfaceDefinition[], routing: RpcRoutingToken = RpcRoutingToken.default): BentleyCloudRpcConfiguration {
return BentleyCloudRpcManager.performInitialization(params, interfaces, routing);
}
/** @beta Initializes BentleyCloudRpcManager for the backend of an application. */
public static initializeImpl(params: BentleyCloudRpcParams, interfaces: RpcInterfaceDefinition[]): BentleyCloudRpcConfiguration {
return BentleyCloudRpcManager.performInitialization(params, interfaces);
}
private static performInitialization(params: BentleyCloudRpcParams, interfaces: RpcInterfaceDefinition[], routing: RpcRoutingToken = RpcRoutingToken.default): BentleyCloudRpcConfiguration {
const protocol = class extends (params.protocol || BentleyCloudRpcProtocol) {
public override pathPrefix = params.uriPrefix || "";
public info = params.info;
};
const config = class extends BentleyCloudRpcConfiguration {
public interfaces = () => interfaces;
public protocol: BentleyCloudRpcProtocol = new protocol(this);
public override routing = routing;
};
for (const def of interfaces) {
RpcConfiguration.assignWithRouting(def, routing, config);
}
const instance = RpcConfiguration.obtain(config);
if (params.pathPrefix) {
instance.protocol.pathPrefix = params.pathPrefix;
}
RpcConfiguration.initializeInterfaces(instance);
if (params.pendingRequestListener) {
const listener = params.pendingRequestListener;
RpcRequest.events.addListener((type, request) => {
if (type === RpcRequestEvent.PendingUpdateReceived && request.protocol === instance.protocol) {
listener(type, request);
}
});
}
return instance;
}
}