forked from asyncapi/asyncapi-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
447 lines (385 loc) · 9.57 KB
/
types.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
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
import { ConfigInterface } from './config';
// Helpers
export type PrimitiveType = number | boolean | string | null;
export type PropsWithDefaults<T, D> = T & D;
export type ExcludeNullable<T> = Exclude<T, null | undefined>;
export interface TypeWithKey<TKey, TContent> {
key: TKey;
content: TContent;
}
export type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]> };
// Types
export type AsyncAPIVersion = string;
export type UniqueID = string;
export type DefaultContentType = string;
export type BaseTopic = string;
export type DescriptionHTML = string | React.ReactNode;
export type ExternalSpecification = Record<string, any>;
export type ReferenceString = string;
export type OneOf = 'oneOf';
export type AnyOf = 'anyOf';
export type SchemaType =
| 'array'
| 'boolean'
| 'integer'
| 'null'
| 'number'
| 'object'
| 'string';
// AsyncAPI types
export enum BindingsType {
http = 'http',
ws = 'ws',
kafka = 'kafka',
amqp = 'amqp',
amqp1 = 'amqp1',
mqtt = 'mqtt',
mqtt5 = 'mqtt5',
nats = 'nats',
jms = 'jms',
sns = 'sns',
sqs = 'sqs',
stomp = 'stomp',
redis = 'redis',
}
export type Bindings = keyof typeof BindingsType;
export interface BaseBindings {
[key: string]: any;
}
export interface AsyncAPI {
asyncapi: AsyncAPIVersion;
id?: UniqueID;
info: Info;
servers?: Servers;
channels: Channels;
defaultContentType?: DefaultContentType;
components?: Components;
tags?: Tag[];
externalDocs?: ExternalDocs;
}
export interface Info {
title: string;
version: string;
description?: DescriptionHTML;
termsOfService?: string;
contact?: Contact;
license?: License;
}
export interface Contact {
name?: string;
url?: string;
email?: string;
}
export interface License {
name: string;
url?: string;
}
export interface Servers {
[k: string]: Server;
}
export interface Server {
url: string;
protocol: string;
protocolVersion?: string;
description?: DescriptionHTML;
variables?: ServerVariables;
security?: SecurityRequirement[];
bindings?: BaseBindings[];
}
export interface ServerVariables {
[k: string]: ServerVariable;
}
export interface ServerVariable {
enum?: string[];
default?: string;
description?: DescriptionHTML;
examples?: string[];
}
export interface SecurityRequirement {
[key: string]: string[];
}
export interface Channels {
[key: string]: Channel;
}
export interface Channel {
parameters?: Parameters;
description?: DescriptionHTML;
publish?: Operation;
subscribe?: Operation;
deprecated?: boolean;
protocolInfo?: ProtocolInfo;
bindings?: BaseBindings;
}
export interface OperationTrait {
summary?: string;
description?: DescriptionHTML;
tags?: Tag[];
externalDocs?: ExternalDocs;
operationId?: string;
protocolInfo?: any;
}
export type TraitType = OperationTrait | [OperationTrait, any];
export interface Operation {
traits?: TraitType[];
summary?: string;
description?: DescriptionHTML;
tags?: Tag[];
externalDocs?: ExternalDocs;
operationId?: string;
protocolInfo?: ProtocolInfo;
message?: Message;
bindings?: BaseBindings[];
}
export interface ProtocolInfo {
[key: string]: any;
}
export interface Parameters {
[key: string]: Parameter;
}
export interface Topic {
$ref?: ReferenceString;
deprecated?: boolean;
subscribe?: Message | Record<OneOf, Message[]>;
publish?: Message | Record<OneOf, Message[]>;
parameters?: Parameter[];
}
export interface Parameter {
description?: DescriptionHTML;
schema?: Schema;
location?: string;
}
export interface Reference {
$ref: ReferenceString;
}
export type Message = RawMessage | Record<OneOf, RawMessage[]>;
export function isRawMessage(message: Message): message is RawMessage {
return !(message as any).oneOf;
}
export enum PayloadType {
PUBLISH = 'publish',
SUBSCRIBE = 'subscribe',
}
export function isOneOfPayload(
payload: RawMessage['payload'],
): payload is Record<OneOf, Schema[]> {
return !!payload && (payload as Record<OneOf, Schema[]>).oneOf !== undefined;
}
export function isAnyOfPayload(
payload: RawMessage['payload'],
): payload is Record<AnyOf, Schema[]> {
return !!payload && (payload as Record<AnyOf, Schema[]>).anyOf !== undefined;
}
export interface RawMessage {
schemaFormat?: string;
contentType?: string;
headers?: Schema;
payload?: Schema | Record<OneOf, Schema[]> | Record<AnyOf, Schema[]>; // payload is Schema, not any https://github.com/asyncapi/parser-js/blob/master/lib/models/message.js#L35
correlationId?: CorrelationId;
tags?: Tag[];
summary?: DescriptionHTML;
name?: string;
title?: string;
description?: DescriptionHTML;
externalDocs?: ExternalDocs;
deprecated?: boolean;
examples?: Example[];
protocolInfo?: any;
traits?: MessageTrait | [MessageTrait, any];
bindings?: BaseBindings;
}
export interface Tag {
name: string;
description?: string;
externalDocs?: ExternalDocs;
}
export interface ExternalDocs {
url: string;
description?: DescriptionHTML;
}
export interface CorrelationId {
description?: string;
location: string;
}
export interface MessageTrait {
schemaFormat?: string;
contentType?: string;
headers?: Schema;
correlationId?: CorrelationId;
tags?: Tag[];
summary?: string;
name?: string;
title?: string;
description?: DescriptionHTML;
externalDocs?: ExternalDocs;
deprecated?: boolean;
examples?: Example[];
protocolInfo?: Record<string, any>;
}
export interface Example {
headers?: object;
payload?: object;
}
export interface Components {
schemas?: Record<string, Schema>;
messages?: Record<string, Message>;
securitySchemes?: Record<Bindings, SecurityScheme>;
parameters?: Record<string, Parameter>;
correlationIds?: CorrelationId;
operationTraits?: Record<string, OperationTrait>;
messageTraits?: Record<string, MessageTrait>;
}
export enum SecuritySchemeType {
userPassword = 'User / Password',
apiKey = 'API key',
X509 = 'X509',
symmetricEncryption = 'Symmetric Encryption',
asymmetricEncryption = 'Asymmetric Encryption',
httpApiKey = 'HTTP API key',
http = 'HTTP',
oauth2 = 'OAuth2',
openIdConnect = 'Open ID',
}
export type SecuritySchemeTypes = keyof typeof SecuritySchemeType;
export interface SecurityScheme {
type: SecuritySchemeTypes;
description?: DescriptionHTML;
in: string;
name: string;
scheme: string;
bearerFormat?: string;
flows?: OAuthFlows;
openIdConnectUrl?: string;
}
export enum OAuthFlowsType {
implicit = 'Implicit',
password = 'Password',
clientCredentials = 'Client Credentials',
authorizationCode = 'Authorization Code',
}
export type OAuthFlowsTypes = keyof typeof OAuthFlowsType;
export interface OAuthFlows {
implicit?: OAuthFlow;
password?: OAuthFlow;
clientCredentials?: OAuthFlow;
authorizationCode?: OAuthFlow;
}
export interface OAuthFlow {
authorizationUrl: string;
tokenUrl: string;
refreshUrl?: string;
scopes: Record<string, string>;
}
export interface XML {
name?: string;
namespace?: string;
prefix?: string;
attribute?: boolean;
wrapped?: boolean;
}
export type AdditionalProperties = boolean | Record<string, Schema>;
export interface Schema {
nullable?: boolean;
format?: string;
title?: string;
description?: DescriptionHTML;
default?: PrimitiveType | {};
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
exclusiveMinimum?: boolean;
maxLength?: number;
minLength?: number;
pattern?: RegExp | string;
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
maxProperties?: number;
minProperties?: number;
required?: string[];
enum?: any[];
deprecated?: boolean;
const?: any;
type?: SchemaType;
items?: Schema /*| Schema[];*/; // todo: end this
discriminator?: string;
readOnly?: boolean;
xml?: XML;
externalDocs?: ExternalDocs;
example?: any;
examples?: any[];
allOf?: Schema[];
oneOf?: Schema[];
anyOf?: Schema[];
not?: Schema;
properties?: Record<string, Schema>;
additionalProperties?: AdditionalProperties;
// old field
// $schema?: string;
// $id?: string;
// definitions?: Record<string, any>;
// additionalItems?: { anyOf: Schema[] } | Schema;
// propertyOrder?: string[];
// defaultProperties?: string[];
}
export type PropsSchema = string | FetchingSchemaInterface | any; // any for JSON input
export interface AsyncApiProps {
schema: PropsSchema;
config?: Partial<ConfigInterface>;
}
export type NullableAsyncApi = AsyncAPI | null;
export interface AsyncApiState {
validatedSchema: NullableAsyncApi;
error?: ErrorObject;
}
export function isFetchingSchemaInterface(
schema: PropsSchema,
): schema is FetchingSchemaInterface {
return (schema as FetchingSchemaInterface).url !== undefined;
}
export interface FetchingSchemaInterface {
url: string;
requestOptions?: RequestInit;
}
export interface ParserReturn {
data: NullableAsyncApi;
error?: ErrorObject;
}
export type TableColumnName = string;
export type PushStateBehavior = (hash: string) => void;
export interface Identifier {
id: string;
toKebabCase?: boolean;
}
export interface ValidationError {
title: string;
jsonPointer: string;
startLine: number;
startColumn: number;
startOffset: number;
endLine: number;
endColumn: number;
endOffset: number;
}
export interface ErrorObject {
type: string;
title: string;
detail?: string;
parsedJSON?: any;
validationErrors?: ValidationError[];
location?: {
startLine: number;
startColumn: number;
startOffset: number;
};
refs?: Array<{
title: string;
jsonPointer: string;
startLine: number;
startColumn: number;
startOffset: number;
endLine: number;
endColumn: number;
endOffset: number;
}>;
}