Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
422 lines (392 loc) · 15 KB
/
Copy pathapp.js
File metadata and controls
422 lines (392 loc) · 15 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
Copyright (c) 2022-2026 Super Durable, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
const STORAGE_EXAMPLE_SERVER = "dex-playground-example-server";
const STORAGE_DEX_WEB = "dex-playground-dex-web";
const bootConfig = window.PLAYGROUND_CONFIG || {};
const catalog = window.PLAYGROUND_CATALOG || [];
const state = {
exampleServer: readStored(STORAGE_EXAMPLE_SERVER, bootConfig.exampleServer || "http://127.0.0.1:8080"),
dexWeb: readStored(STORAGE_DEX_WEB, bootConfig.dexWeb || "http://127.0.0.1:8802"),
flowIds: {},
runIds: {},
};
function readStored(key, fallback) {
try {
return window.localStorage.getItem(key) || fallback;
} catch {
return fallback;
}
}
function writeStored(key, value) {
try {
window.localStorage.setItem(key, value);
} catch {
/* ignore quota / private mode */
}
}
function newFlowId(prefix) {
return prefix + "-" + Date.now() + "-" + Math.random().toString(16).slice(2, 8);
}
function exampleFlowId(example) {
if (!state.flowIds[example.id]) {
state.flowIds[example.id] = newFlowId(example.flowIdPrefix);
}
return state.flowIds[example.id];
}
function setExampleFlowId(exampleId, flowId) {
if (!flowId) {
return;
}
state.flowIds[exampleId] = flowId;
document.querySelectorAll(`[data-flow-id-for="${exampleId}"]`).forEach((input) => {
if (input.value !== flowId) {
input.value = flowId;
}
});
renderExampleLinks(exampleId);
}
function setExampleRunId(exampleId, runId) {
if (!runId) {
return;
}
state.runIds[exampleId] = runId;
renderExampleLinks(exampleId);
}
function trimSlash(url) {
return String(url || "").replace(/\/+$/, "");
}
function dexWebFlowURL(flowId, runId) {
const base = trimSlash(state.dexWeb);
if (!flowId || !base) {
return "";
}
if (runId) {
return `${base}/v1/flows/${encodeURIComponent(flowId)}/${encodeURIComponent(runId)}`;
}
return `${base}/v1/flows/${encodeURIComponent(flowId)}`;
}
function dexWebSearchURL(flowId) {
const base = trimSlash(state.dexWeb);
if (!flowId || !base) {
return "";
}
return `${base}/v1/flows?q=${encodeURIComponent(`WorkflowId="${flowId}"`)}`;
}
function fieldValue(form, field, example) {
if (field.role === "flowId") {
const input = form.querySelector(`[name="${field.name}"]`);
return (input && input.value.trim()) || exampleFlowId(example);
}
const input = form.querySelector(`[name="${field.name}"]`);
if (!input) {
return field.default || "";
}
return input.value;
}
function buildRequest(example, endpoint, form) {
const query = new URLSearchParams();
const bodyObject = {};
let rawJson = null;
let injectFlowId = null;
(endpoint.fields || []).forEach((field) => {
const value = fieldValue(form, field, example);
if (field.in === "raw-json") {
rawJson = value;
injectFlowId = field.injectFlowId;
return;
}
if (field.in === "body") {
if (field.type === "number") {
bodyObject[field.name] = Number(value);
} else if (field.type === "boolean") {
bodyObject[field.name] = value === "true";
} else {
bodyObject[field.name] = value;
}
return;
}
if (value !== "") {
query.set(field.name, value);
}
});
const path = endpoint.path;
const queryText = query.toString();
const url = trimSlash(state.exampleServer) + path + (queryText ? `?${queryText}` : "");
const init = { method: endpoint.method, headers: {} };
if (rawJson !== null) {
let parsed = JSON.parse(rawJson);
if (injectFlowId) {
parsed[injectFlowId] = exampleFlowId(example);
}
init.headers["Content-Type"] = "application/json";
init.body = JSON.stringify(parsed);
} else if (endpoint.method !== "GET" && Object.keys(bodyObject).length > 0) {
init.headers["Content-Type"] = "application/json";
init.body = JSON.stringify(bodyObject);
}
return { url, init };
}
function extractIds(payload, text, endpoint) {
let flowId = null;
let runId = null;
if (payload && typeof payload === "object" && !Array.isArray(payload)) {
flowId = payload.flowID || payload.flowId || payload.workflowId || payload.workflowID || payload.userId || null;
runId = payload.runID || payload.runId || null;
}
const source = typeof payload === "string" ? payload : text;
if (!flowId && source) {
const match = source.match(
/(?:started workflowId|Started workflowId|workflowId|flowID|flowId)\s*[:=]\s*([^\s,}"']+)/i,
);
if (match) {
flowId = match[1];
}
}
if (!runId && source) {
const match = source.match(/(?:runId|runID)\s*[:=]?\s*([^\s,}"']+)/i);
if (match) {
runId = match[1];
}
}
const isStartPath = endpoint && /\/start/i.test(endpoint.path);
if (!runId && isStartPath && typeof payload === "string") {
const bare = payload.replace(/^"|"$/g, "").trim();
if (bare && !/\s/.test(bare) && bare.length < 80 && bare[0] !== "{" && bare[0] !== "[") {
runId = bare;
}
}
return { flowId, runId };
}
function renderField(example, field) {
const defaultValue = field.role === "flowId" ? exampleFlowId(example) : (field.default ?? "");
const flowAttr = field.role === "flowId" ? ` data-flow-id-for="${example.id}"` : "";
if (field.in === "raw-json") {
return `<label class="field field-wide"><span>${field.name}</span><textarea name="${field.name}" rows="10">${escapeHtml(defaultValue)}</textarea></label>`;
}
if (field.type === "select") {
const options = (field.options || [])
.map((option) => `<option value="${escapeAttr(option)}"${option === defaultValue ? " selected" : ""}>${escapeHtml(option)}</option>`)
.join("");
return `<label class="field"><span>${field.name}</span><select name="${field.name}">${options}</select></label>`;
}
if (field.type === "boolean") {
return `<label class="field"><span>${field.name}</span><select name="${field.name}">
<option value="true"${defaultValue === "true" ? " selected" : ""}>true</option>
<option value="false"${defaultValue === "false" ? " selected" : ""}>false</option>
</select></label>`;
}
const inputType = field.type === "number" ? "number" : "text";
return `<label class="field"><span>${field.name}</span><input${flowAttr} type="${inputType}" name="${field.name}" value="${escapeAttr(defaultValue)}" autocomplete="off" /></label>`;
}
function renderEndpoint(example, endpoint, index) {
const hasFlowIdField = (endpoint.fields || []).some((field) => field.role === "flowId");
const flowField = hasFlowIdField
? ""
: `<label class="field"><span>workflowId</span><input data-flow-id-for="${example.id}" type="text" name="workflowId" value="${escapeAttr(exampleFlowId(example))}" autocomplete="off" /></label>`;
const fields = (endpoint.fields || []).map((field) => renderField(example, field)).join("");
return `<article class="endpoint" data-example="${example.id}" data-endpoint="${index}">
<header>
<span class="method method-${endpoint.method.toLowerCase()}">${endpoint.method}</span>
<code>${escapeHtml(endpoint.path)}</code>
<strong>${escapeHtml(endpoint.title)}</strong>
</header>
<form>
${flowField}
${fields}
<button type="submit">Call API</button>
</form>
<div class="result" hidden>
<p class="meta"></p>
<pre class="response"></pre>
<p class="ids"></p>
<p class="links"></p>
</div>
</article>`;
}
function renderExample(example) {
const flowId = exampleFlowId(example);
return `<section class="example" id="${example.group}-${example.id}">
<div class="example-head">
<h3>${escapeHtml(example.title)}</h3>
<label class="field">
<span>flowID / ${escapeHtml(example.idParam || "workflowId")}</span>
<input data-flow-id-for="${example.id}" type="text" value="${escapeAttr(flowId)}" autocomplete="off" />
<button type="button" class="secondary" data-new-id="${example.id}">New ID</button>
</label>
<p class="web-links" data-links-for="${example.id}"></p>
</div>
${example.note ? `<p class="note">${escapeHtml(example.note)}</p>` : ""}
${example.endpoints.map((endpoint, index) => renderEndpoint(example, endpoint, index)).join("")}
</section>`;
}
function render() {
const groups = [
{ id: "products", title: "Products" },
{ id: "patterns", title: "Patterns" },
{ id: "primitives", title: "Primitives" },
];
const nav = document.getElementById("nav");
const main = document.getElementById("main");
nav.innerHTML = groups
.map((group) => {
const items = catalog
.filter((example) => example.group === group.id)
.map((example) => `<a href="#${group.id}-${example.id}">${escapeHtml(example.title)}</a>`)
.join("");
return `<div class="nav-group"><h2>${group.title}</h2>${items}</div>`;
})
.join("");
main.innerHTML = groups
.map((group) => {
const sections = catalog
.filter((example) => example.group === group.id)
.map(renderExample)
.join("");
return `<section class="group"><h2 id="${group.id}">${group.title}</h2>${sections}</section>`;
})
.join("");
document.getElementById("example-server-url").value = state.exampleServer;
document.getElementById("dex-web-url").value = state.dexWeb;
catalog.forEach((example) => renderExampleLinks(example.id));
}
function renderExampleLinks(exampleId) {
const container = document.querySelector(`[data-links-for="${exampleId}"]`);
if (!container) {
return;
}
const flowId = state.flowIds[exampleId];
const runId = state.runIds[exampleId];
if (!flowId) {
container.innerHTML = "";
return;
}
const flowURL = dexWebFlowURL(flowId, runId);
const currentURL = dexWebFlowURL(flowId);
const searchURL = dexWebSearchURL(flowId);
container.innerHTML = `Dex Web:
<a href="${escapeAttr(currentURL)}" target="_blank" rel="noreferrer">open ${escapeHtml(flowId)}</a>
${runId ? ` · <a href="${escapeAttr(flowURL)}" target="_blank" rel="noreferrer">run ${escapeHtml(runId)}</a>` : ""}
· <a href="${escapeAttr(searchURL)}" target="_blank" rel="noreferrer">search</a>`;
}
async function onSubmit(event) {
const form = event.target;
if (!(form instanceof HTMLFormElement)) {
return;
}
event.preventDefault();
const article = form.closest(".endpoint");
const example = catalog.find((item) => item.id === article.dataset.example);
const endpoint = example.endpoints[Number(article.dataset.endpoint)];
const flowInput = form.querySelector("[data-flow-id-for]");
if (flowInput && flowInput.value.trim()) {
setExampleFlowId(example.id, flowInput.value.trim());
}
const result = article.querySelector(".result");
const meta = article.querySelector(".meta");
const responsePre = article.querySelector(".response");
const ids = article.querySelector(".ids");
const links = article.querySelector(".links");
result.hidden = false;
meta.textContent = "Calling…";
responsePre.textContent = "";
ids.textContent = "";
links.innerHTML = "";
let request;
try {
request = buildRequest(example, endpoint, form);
} catch (error) {
meta.textContent = "Invalid request";
responsePre.textContent = String(error);
return;
}
meta.textContent = `${request.init.method} ${request.url}`;
try {
const response = await fetch(request.url, request.init);
const text = await response.text();
let payload = text;
try {
payload = JSON.parse(text);
} catch {
payload = text;
}
responsePre.textContent = typeof payload === "string" ? payload : JSON.stringify(payload, null, 2);
meta.textContent = `${request.init.method} ${request.url} → ${response.status} ${response.statusText}`;
const extracted = extractIds(payload, text, endpoint);
if (extracted.flowId) {
setExampleFlowId(example.id, extracted.flowId);
}
if (extracted.runId) {
setExampleRunId(example.id, extracted.runId);
}
const flowId = extracted.flowId || exampleFlowId(example);
const runId = extracted.runId || state.runIds[example.id] || "";
ids.textContent = `flowID: ${flowId}${runId ? ` · runID: ${runId}` : ""}`;
const flowURL = dexWebFlowURL(flowId, runId);
const currentURL = dexWebFlowURL(flowId);
const searchURL = dexWebSearchURL(flowId);
links.innerHTML = `Dex Web:
<a href="${escapeAttr(currentURL)}" target="_blank" rel="noreferrer">open flow</a>
${runId ? ` · <a href="${escapeAttr(flowURL)}" target="_blank" rel="noreferrer">open run</a>` : ""}
· <a href="${escapeAttr(searchURL)}" target="_blank" rel="noreferrer">search</a>`;
} catch (error) {
meta.textContent = `${request.init.method} ${request.url} failed`;
responsePre.textContent = String(error);
}
}
function onClick(event) {
const newIdButton = event.target.closest("[data-new-id]");
if (newIdButton) {
const example = catalog.find((item) => item.id === newIdButton.dataset.newId);
setExampleFlowId(example.id, newFlowId(example.flowIdPrefix));
delete state.runIds[example.id];
renderExampleLinks(example.id);
}
}
function onInput(event) {
const input = event.target;
if (!(input instanceof HTMLInputElement) || !input.dataset.flowIdFor) {
return;
}
setExampleFlowId(input.dataset.flowIdFor, input.value.trim());
}
function saveSettings(event) {
event.preventDefault();
state.exampleServer = document.getElementById("example-server-url").value.trim();
state.dexWeb = document.getElementById("dex-web-url").value.trim();
writeStored(STORAGE_EXAMPLE_SERVER, state.exampleServer);
writeStored(STORAGE_DEX_WEB, state.dexWeb);
catalog.forEach((example) => renderExampleLinks(example.id));
}
function escapeHtml(value) {
return String(value)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
function escapeAttr(value) {
return escapeHtml(value);
}
document.addEventListener("DOMContentLoaded", () => {
render();
document.getElementById("main").addEventListener("submit", onSubmit);
document.getElementById("main").addEventListener("click", onClick);
document.getElementById("main").addEventListener("input", onInput);
document.getElementById("settings").addEventListener("submit", saveSettings);
});