forked from asyncapi/parser-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmixins.ts
More file actions
131 lines (108 loc) · 4.01 KB
/
Copy pathmixins.ts
File metadata and controls
131 lines (108 loc) · 4.01 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
import { BaseModel } from '../base';
import { Bindings } from './bindings';
import { Binding } from './binding';
import { Extensions } from './extensions';
import { Extension } from './extension';
import { ExternalDocumentation } from './external-docs';
import { Tags } from './tags';
import { Tag } from './tag';
import { createModel } from '../utils';
import { EXTENSION_REGEX } from '../../constants';
import type { BindingsInterface } from '../bindings';
import type { ExtensionsInterface } from '../extensions';
import type { ExtensionInterface } from '../extension';
import type { ExternalDocumentationInterface } from '../external-docs';
import type { TagsInterface } from '../tags';
import type { v3 } from '../../spec-types';
type BindingsObject =
| v3.ServerBindingsObject
| v3.ChannelBindingsObject
| v3.OperationBindingsObject
| v3.MessageBindingsObject
| v3.ReferenceObject;
export interface CoreObject extends v3.SpecificationExtensions {
title?: string;
summary?: string;
description?: string;
externalDocs?: v3.ExternalDocumentationObject | v3.ReferenceObject;
tags?: v3.TagsObject;
bindings?: BindingsObject;
}
export abstract class CoreModel<J extends CoreObject = CoreObject, M extends Record<string, any> = {}> extends BaseModel<J, M> {
hasTitle(): boolean {
return !!this._json.title;
}
title(): string | undefined {
return this._json.title;
}
hasSummary(): boolean {
return !!this._json.summary;
}
summary(): string | undefined {
return this._json.summary;
}
hasDescription(): boolean {
return hasDescription(this);
}
description(): string | undefined {
return description(this);
}
hasExternalDocs(): boolean {
return hasExternalDocs(this);
}
externalDocs(): ExternalDocumentationInterface | undefined {
return externalDocs(this);
}
tags(): TagsInterface {
return tags(this);
}
bindings(): BindingsInterface {
return bindings(this);
}
extensions(): ExtensionsInterface {
return extensions(this);
}
}
export function bindings(model: BaseModel<{ bindings?: BindingsObject }>): BindingsInterface {
const bindings = model.json('bindings') || {};
return new Bindings(
Object.entries(bindings || {})
.filter(([key]) => !key.startsWith('$'))
.map(([protocol, binding]) =>
createModel(Binding, binding, { protocol, pointer: model.jsonPath(`bindings/${protocol}`) }, model)
),
{ originalData: bindings as Record<string, Binding>, asyncapi: model.meta('asyncapi'), pointer: model.jsonPath('bindings') }
);
}
export function hasDescription(model: BaseModel<{ description?: string }>) {
return Boolean(description(model));
}
export function description(model: BaseModel<{ description?: string }>): string | undefined {
return model.json('description');
}
export function extensions(model: BaseModel<v3.SpecificationExtensions>): ExtensionsInterface {
const extensions: ExtensionInterface[] = [];
Object.entries(model.json()).forEach(([id, value]: [string, any]) => {
if (EXTENSION_REGEX.test(id)) {
extensions.push(
createModel(Extension, value, { id, pointer: model.jsonPath(id) } as any, model) as Extension
);
}
});
return new Extensions(extensions);
}
export function hasExternalDocs(model: BaseModel<{ externalDocs?: v3.ExternalDocumentationObject | v3.ReferenceObject }>): boolean {
return Object.keys(model.json('externalDocs') || {}).length > 0;
}
export function externalDocs(model: BaseModel<{ externalDocs?: v3.ExternalDocumentationObject | v3.ReferenceObject }>): ExternalDocumentationInterface | undefined {
if (hasExternalDocs(model as BaseModel<{ externalDocs?: v3.ExternalDocumentationObject }>)) {
return new ExternalDocumentation(model.json('externalDocs') as v3.ExternalDocumentationObject);
}
}
export function tags(model: BaseModel<{ tags?: v3.TagsObject }>): TagsInterface {
return new Tags(
(model.json('tags') || []).map((tag, idx) =>
createModel(Tag, tag as v3.TagObject, { pointer: model.jsonPath(`tags/${idx}`) }, model)
)
);
}