-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrokerize-main.js
More file actions
177 lines (154 loc) · 5.07 KB
/
brokerize-main.js
File metadata and controls
177 lines (154 loc) · 5.07 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* eslint-disable no-unused-vars */
/**
* The following typedef imports enable code completion in VS Code:
*/
/** @typedef {import("@brokerize/elements").Client.AuthorizedApiContext} AuthorizedApiContext} */
/** @typedef {import("@brokerize/elements").Client} BrokerizeClient} */
/** @typedef {import("@brokerize/elements").Elements} BrokerizeElements} */
/** @typedef {import("@brokerize/elements").BrokerizeMainElement} BrokerizeMainElement} */
/** @typedef {import("@brokerize/elements").SecurityQuotesProvider} SecurityQuotesProvider} */
/** @typedef {import("@brokerize/elements")} BrokerizeBundle} */
const Brokerize = /** @type {BrokerizeBundle} */ (window.Brokerize);
const config = window.BROKERIZE_CONFIG;
if (!config || !config.CLIENT_ID) {
alert(
"no config could be found. Please include config.js in your HTML file."
);
throw new Error("No config provided");
}
const client = new Brokerize.Client.Brokerize({
// API configuration
basePath: config.API_URL,
basePathCryptoService: config.API_URL_CRYPTO_SERVICE,
clientId: config.CLIENT_ID,
// provide global dependencies
fetch: window.fetch.bind(window),
createAbortController: () => new AbortController(),
createWebSocket: (url, protocol) => new WebSocket(url, protocol),
});
/* this changes when the user logs in/out of brokerize or starts/ends guest sessions */
/**
* @type {AuthorizedApiContext}
*/
let globalApiCtx = null;
/* let's render everything in the #content element */
const $el = document.getElementById("content");
/**
* @type {BrokerizeMainElement}
*/
let brokerizeMainElement;
function showError(err) {
alert("ERROR: " + JSON.stringify(err));
}
function storeTokens(authCtxCfg) {
sessionStorage.setItem("brokerize", JSON.stringify(authCtxCfg));
}
function setLogin(authCtxCfg) {
storeTokens(authCtxCfg);
initSessionIf();
}
function cleanUpUrl() {
const url = new URL(window.location.href);
for (const key of ["verifysession", "code", "ticketId"]) {
url.searchParams.delete(key);
}
window.location.replace(url);
}
/* restore session from sessionStorage, if possible */
function initSessionIf() {
const scfg = sessionStorage.getItem("brokerize");
const cfg = scfg ? JSON.parse(scfg) : null;
if (!cfg) {
acquireBrokerizeGuestUser();
} else {
globalApiCtx = client.createAuthorizedContext(cfg, (updatedTokens) => {
console.log("tokens where updated");
storeTokens(updatedTokens);
});
globalApiCtx.subscribeLogout((err) => {
console.log(
err,
"guest user has been logged out from brokerize API."
);
acquireBrokerizeGuestUser();
});
setUpModalPortal(globalApiCtx);
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get("verifysession")) {
const code = urlParams.get("code");
const ticketId = urlParams.get("ticketId");
globalApiCtx.confirmOAuth({ code, ticketId }).then(
() => {
cleanUpUrl();
showMain();
},
(err) => {
showError(
"An error occured when trying to confirm the OAuth-based broker login: " +
JSON.stringify(err)
);
cleanUpUrl();
}
);
} else {
showMain();
}
}
}
let modalHost = null;
function setUpModalPortal(authorizedApiContext) {
modalHost?.destroy();
modalHost = Brokerize.Elements.createModalHost({
authorizedApiContext,
renderTo: document.getElementById("brokerize-modal-portal"),
theme,
});
}
/* theme configuration. example themes are available under https://app.brokerize.com/theming/ */
const theme = {
layout: "block",
logoStyle: "light",
tokens: {
// 'zl-color-primary-base': 'red', /* just a flashy example */
},
};
function resetRenderTo() {
brokerizeMainElement && brokerizeMainElement.destroy();
$el.innerHTML = "";
return $el;
}
function showMain() {
if (!globalApiCtx) {
return alert("you must authorize first.");
}
brokerizeMainElement = Brokerize.Elements.createBrokerizeMain({
theme,
renderTo: resetRenderTo(),
authorizedApiContext: globalApiCtx,
returnToUrl: "http://localhost:8080/brokerize-main.html",
preferredExchangeId: 22
});
}
function acquireBrokerizeGuestUser() {
console.log("logging in as guest");
client.createGuestUser().then(
(authCtxCfg) => {
setLogin(authCtxCfg);
},
(err) => showError(err)
);
}
async function startExampleOrderFlow() {
brokerizeMainElement.navigation.startOrderFlow({
security: {
name: "DE0005557508",
selector: { isin: "DE0005557508" },
},
initialOrderCreateValues: {
direction: "buy",
orderModel: "limit",
limit: 15,
},
});
}
initSessionIf();