-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathOperation.tsx
516 lines (485 loc) · 16.4 KB
/
Operation.tsx
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import React, { useState } from 'react';
import { ChannelInterface, OperationInterface } from '@asyncapi/parser';
import { Message } from '../Messages/Message';
import { Security } from '../Servers/Security';
import {
Markdown,
Schema,
Bindings,
Tags,
Extensions,
CollapseButton,
} from '../../components';
import { Href } from '../../components/Href';
import { useConfig, useSpec } from '../../contexts';
import { CommonHelpers, SchemaHelpers } from '../../helpers';
import { EXTERAL_DOCUMENTATION_TEXT } from '../../constants';
import { PayloadType } from '../../types';
interface Props {
type: PayloadType;
operation: OperationInterface;
channelName: string;
channel: ChannelInterface;
}
// Construct the full relative URL, including path, query parameters to avoid path overwrite when
// location.hash is included
const relativePathname = `${window.location.pathname}${window.location.search}`;
export const Operation: React.FunctionComponent<Props> = (props) => {
const { type = PayloadType.SEND, operation, channelName, channel } = props;
const config = useConfig();
if (!operation || !channel) {
return null;
}
// check typeof as fallback for older version than `2.2.0`
const servers =
typeof channel.servers === 'function' && channel.servers().all();
// check typeof as fallback for older version than `2.4.0`
const security =
typeof operation.security === 'function' && operation.security();
const parameters =
channel.parameters() !== undefined
? SchemaHelpers.parametersToSchema(channel.parameters())
: undefined;
return (
<div>
<div className="panel-item--center px-8">
<OperationInfo {...props} />
{servers && servers.length > 0 ? (
<div className="mt-2 text-sm">
<p>Available only on servers:</p>
<ul className="flex flex-wrap leading-normal">
{servers.map((server) => (
<li className="inline-block mt-2 mr-2" key={server.id()}>
<a
href={`${relativePathname}#${CommonHelpers.getIdentifier(
'server-' + server.id(),
config,
)}`}
className="border border-solid border-blue-300 hover:bg-blue-300 hover:text-blue-600 text-blue-500 font-bold no-underline text-xs rounded px-3 py-1 cursor-pointer"
>
<span className="underline">{server.id()}</span>
</a>
</li>
))}
</ul>
</div>
) : null}
{parameters && (
<div
className="mt-2"
id={CommonHelpers.getIdentifier(
`operation-${type}-${channelName}-parameters`,
config,
)}
>
<Schema schemaName="Parameters" schema={parameters} expanded />
</div>
)}
{security && (
<div
className="mt-2"
id={CommonHelpers.getIdentifier(
`operation-${type}-${channelName}-security`,
config,
)}
>
<Security
security={security}
header="Additional security requirements"
/>
</div>
)}
{channel.bindings() && (
<div className="mt-2">
<Bindings
name="Channel specific information"
bindings={channel.bindings()}
/>
</div>
)}
<Extensions name="Channel Extensions" item={channel} />
{operation.bindings() && (
<div className="mt-2">
<Bindings
name="Operation specific information"
bindings={operation.bindings()}
/>
</div>
)}
<Extensions name="Operation Extensions" item={operation} />
{operation.tags() && (
<div className="mt-2">
<Tags tags={operation.tags()} />
</div>
)}
</div>
<div
className="w-full mt-4"
id={CommonHelpers.getIdentifier(
`operation-${type}-${channelName}-message`,
config,
)}
>
{operation.messages().length > 1 ? (
<div className="mt-2">
<p className="px-8">
Accepts <strong>one of</strong> the following messages:
</p>
<ul>
{operation
.messages()
.all()
.map((msg, idx) => (
<li className="mt-4" key={msg.id()}>
<Message message={msg} index={idx} showExamples />
</li>
))}
</ul>
</div>
) : (
<div className="mt-2">
<p className="px-8">Accepts the following message:</p>
<div className="mt-2">
<Message message={operation.messages().all()[0]} showExamples />
</div>
</div>
)}
</div>
<OperationReplyInfo {...props} />
</div>
);
};
export const OperationInfo: React.FunctionComponent<Props> = (props) => {
const { type = PayloadType.SEND, operation, channelName, channel } = props;
const config = useConfig();
const operationSummary = operation.summary();
const externalDocs = operation.externalDocs();
const operationId = operation.id();
const specV = useSpec().version();
const version = specV.localeCompare('2.6.0', undefined, { numeric: true });
const isAsyncAPIv2 = version === 0;
const { backgroundColor, typeLabel } =
CommonHelpers.getOperationDesignInformation({
type,
config,
isAsyncAPIv2,
});
return (
<>
<div className="mb-4">
<h3>
<span
className={`font-mono text-white uppercase p-1 rounded mr-2 ${backgroundColor}`}
title={type}
>
{typeLabel}
</span>{' '}
<span className="font-mono text-base">{channelName}</span>
</h3>
</div>
{channel.hasDescription() && (
<div className="mt-2">
<Markdown>{channel.description()}</Markdown>
</div>
)}
{operationSummary && (
<p className="text-gray-600 text-sm mt-2">{operationSummary}</p>
)}
{operation.hasDescription() && (
<div className="mt-2">
<Markdown>{operation.description()}</Markdown>
</div>
)}
{externalDocs && (
<ul className="leading-normal mt-2 mb-4 space-x-2 space-y-2">
{externalDocs && (
<li className="inline-block">
<Href
className="border border-solid border-orange-300 hover:bg-orange-300 hover:text-orange-600 text-orange-500 font-bold no-underline text-xs uppercase rounded px-3 py-1"
href={externalDocs.url()}
>
<span>{EXTERAL_DOCUMENTATION_TEXT}</span>
</Href>
</li>
)}
</ul>
)}
{operationId && (
<div className="border bg-gray-100 rounded px-4 py-2 mt-2">
<div className="text-sm text-gray-700">
Operation ID
<span className="bg-orange-600 text-white rounded text-xs ml-2 py-0 px-2">
{operationId}
</span>
</div>
</div>
)}
</>
);
};
export const OperationReplyInfo: React.FunctionComponent<Props> = (props) => {
const { type = PayloadType.SEND, operation } = props;
const [showMessages, setShowMessages] = useState(false);
const [showChannel, setShowChannel] = useState(false);
if (type !== PayloadType.REPLY && type !== PayloadType.REQUEST) {
return <></>;
}
const reply = operation.reply();
if (reply === undefined) {
return <></>;
}
const replyMessages = reply.messages();
const explicitChannel = reply.channel();
const replyAddress = reply.address()?.location();
return (
<div className="panel-item">
<div className="panel-item--center">
<div className="font-mono px-8 py-4">
<div className="border rounded">
<div
className={`w-full ${
type === PayloadType.REPLY
? 'bg-green-600 border-green-600'
: 'bg-blue-600 border-blue-600'
} text-sm rounded-t h-8 px-4 border text-white flex items-center`}
>
<strong>REPLY INFORMATION</strong>
</div>
<div className="flex">
<div
className={`w-1 h-11 ${
type === PayloadType.REPLY ? 'bg-green-600' : 'bg-blue-600'
} mt-4`}
/>
<div className="p-4">
<h3 className="text-xs">
<span className="mr-2" title={type}>
REPLY CHANNEL INFORMATION
</span>
</h3>
{explicitChannel?.address() ? (
<div className="text-xs text-gray-700">
Reply will be provided via this designated address:{' '}
<span className="border text-orange-600 rounded text-xs ml-2 py-0 px-2">
{explicitChannel.address()}{' '}
</span>
</div>
) : (
replyAddress != null && (
<div className="text-xs text-gray-700">
Reply will be directed to the address specified at this
location:{' '}
<span className="border text-orange-600 rounded text-xs ml-2 py-0 px-2">
{replyAddress}
</span>
</div>
)
)}
<div className="mt-2">
{explicitChannel && (
<CollapseButton
onClick={() => setShowChannel((prev) => !prev)}
expanded={showChannel}
>
<span className="inline-block py-0.5 mr-1 text-gray-500 text-xs text-center rounded focus:outline-none">
View channel details
</span>
</CollapseButton>
)}
{explicitChannel && (
<div
className={`w-full mt-4 ${
showChannel ? 'block' : 'hidden'
}`}
>
<OperationReplyChannelInfo {...props} />{' '}
</div>
)}
</div>
</div>
</div>
<OperationReplyAddressInfo {...props} />
{replyMessages.isEmpty() === false && (
<div className="p-4">
<CollapseButton
onClick={() => setShowMessages((prev) => !prev)}
expanded={showMessages}
>
<span className="inline-block py-0.5 mr-1 text-gray-500 text-xs text-center rounded focus:outline-none">
Expected Reply{' '}
{replyMessages.length > 1 ? 'Messages' : 'Message'}
</span>
</CollapseButton>
<div
className={`w-full mt-4 ${showMessages ? 'block' : 'hidden'}`}
>
{replyMessages.length > 1 ? (
<div className="mt-2">
<ul>
{replyMessages.all().map((msg, idx) => (
<li className="mt-4" key={msg.id()}>
<Message message={msg} index={idx} showExamples />
</li>
))}
</ul>
</div>
) : (
<div className="mt-2">
<div className="mt-2">
<Message
message={replyMessages.all()[0]}
showExamples
/>
</div>
</div>
)}
</div>
</div>
)}
</div>
</div>
<Extensions name="Operation Reply Extensions" item={reply} />
</div>
</div>
);
};
export const OperationReplyChannelInfo: React.FunctionComponent<Props> = ({
type = PayloadType.SEND,
operation,
}) => {
const reply = operation.reply();
const channel = reply?.channel();
const channelName = channel?.address() ?? '';
const config = useConfig();
const servers =
typeof channel?.servers === 'function' && channel.servers().all();
const parameters =
channel?.parameters() !== undefined
? SchemaHelpers.parametersToSchema(channel.parameters())
: undefined;
if (!channel) {
return <></>;
}
return (
<div>
{channel.address() && (
<div className="mt-2 text-xs text-gray-700">
Address:{' '}
<span className="border text-orange-600 rounded text-xs py-0 px-2">
{channel.address()}
</span>
</div>
)}
{channel.hasDescription() && (
<div className="mt-2">
<Markdown>{channel.description()}</Markdown>
</div>
)}
{servers && servers.length > 0 ? (
<div className="mt-2 text-sm">
<p>Available only on servers:</p>
<ul className="flex flex-wrap leading-normal">
{servers.map((server) => (
<li className="inline-block mt-2 mr-2" key={server.id()}>
<a
href={`${relativePathname}#${CommonHelpers.getIdentifier(
'server-' + server.id(),
config,
)}`}
className="border border-solid border-blue-300 hover:bg-blue-300 hover:text-blue-600 text-blue-500 font-bold no-underline text-xs rounded px-3 py-1 cursor-pointer"
>
<span className="underline">{server.id()}</span>
</a>
</li>
))}
</ul>
</div>
) : null}
{channel.messages().all().length > 1 ? (
<div className="mt-2">
<span className="text-xs text-gray-700">Messages:</span>
<ul>
{channel
.messages()
.all()
.map((msg, idx) => (
<li className="mt-4" key={msg.id()}>
<Message message={msg} index={idx} showExamples />
</li>
))}
</ul>
</div>
) : (
<div className="mt-2">
<span className="text-xs text-gray-700">Message:</span>
<div className="mt-2">
<Message message={channel.messages().all()[0]} showExamples />
</div>
</div>
)}
{parameters && (
<div
className="mt-2"
id={CommonHelpers.getIdentifier(
`operation-${type}-${channelName}-parameters`,
config,
)}
>
<Schema schemaName="Parameters" schema={parameters} expanded />
</div>
)}
{channel.bindings() && (
<div className="mt-2">
<Bindings name="Bindings" bindings={channel.bindings()} />
</div>
)}
</div>
);
};
export const OperationReplyAddressInfo: React.FunctionComponent<Props> = ({
type = PayloadType.SEND,
operation,
}) => {
if (type !== PayloadType.REPLY && type !== PayloadType.REQUEST) {
return <></>;
}
const reply = operation.reply();
if (!reply?.address) {
return <></>;
}
const replyAddress = reply.address();
if (!replyAddress) {
return <></>;
}
const replyAddressLocation = replyAddress.location();
return (
<div className="flex">
<div
className={`w-1 h-11 ${
type === PayloadType.REPLY ? 'bg-green-600' : 'bg-blue-600'
} mt-4`}
/>
<div className="p-4">
<h3 className="text-xs">
<span className="mr-2 uppercase" title={type}>
REPLY address information
</span>
</h3>
{replyAddressLocation && (
<div className="text-xs text-gray-700">
REPLY will be sent to the address provided in:
<span className="border text-orange-600 rounded text-xs ml-2 py-0 px-2">
{replyAddressLocation}
</span>
</div>
)}
{replyAddress.hasDescription() && (
<div className="mt-2">
<Markdown>{replyAddress.description()}</Markdown>
</div>
)}
<Extensions name="Operation Reply Address Extensions" item={reply} />
</div>
</div>
);
};