1
- import { AsyncApi } from './models/asyncapi.model' ;
2
- import { Server } from './models/server.model' ;
3
- import { Channel , CHANNEL_ANCHOR_PREFIX , Message , Operation , OperationType } from './models/channel.model' ;
4
- import { Schema } from './models/schema.model' ;
5
- import { Injectable } from '@angular/core' ;
6
- import { Example } from " ./models/example.model" ;
7
- import { Info } from " ./models/info.model" ;
1
+ import { AsyncApi } from './models/asyncapi.model' ;
2
+ import { Server } from './models/server.model' ;
3
+ import { Channel , CHANNEL_ANCHOR_PREFIX , Message , MessageBinding , Operation , OperationType } from './models/channel.model' ;
4
+ import { Schema } from './models/schema.model' ;
5
+ import { Injectable } from '@angular/core' ;
6
+ import { Example } from ' ./models/example.model' ;
7
+ import { Info } from ' ./models/info.model' ;
8
8
9
9
interface ServerAsyncApiSchema {
10
10
description ?: string ;
@@ -26,6 +26,10 @@ interface ServerAsyncApiMessage {
26
26
description ?: string ;
27
27
payload : { $ref : string } ;
28
28
headers : { $ref : string } ;
29
+ bindings : { [ key : string ] : ServerAsyncApiMessageBinding } ;
30
+ }
31
+ interface ServerAsyncApiMessageBinding {
32
+ [ key : string ] : ServerAsyncApiSchema | string ;
29
33
}
30
34
31
35
interface ServerAsyncApiInfo {
@@ -64,7 +68,7 @@ export interface ServerAsyncApi {
64
68
65
69
@Injectable ( )
66
70
export class AsyncApiMapperService {
67
- static BASE_URL = window . location . pathname + window . location . search + "#" ;
71
+ static BASE_URL = window . location . pathname + window . location . search + '#' ;
68
72
69
73
constructor ( ) {
70
74
}
@@ -89,49 +93,52 @@ export class AsyncApiMapperService {
89
93
} ;
90
94
}
91
95
92
- private mapServers ( servers : ServerAsyncApi [ " servers" ] ) : Map < string , Server > {
96
+ private mapServers ( servers : ServerAsyncApi [ ' servers' ] ) : Map < string , Server > {
93
97
const s = new Map < string , Server > ( ) ;
94
98
Object . entries ( servers ) . forEach ( ( [ k , v ] ) => s . set ( k , v ) ) ;
95
99
return s ;
96
100
}
97
101
98
- private mapChannels ( channels : ServerAsyncApi [ " channels" ] ) : Channel [ ] {
102
+ private mapChannels ( channels : ServerAsyncApi [ ' channels' ] ) : Channel [ ] {
99
103
const s = new Array < Channel > ( ) ;
100
104
Object . entries ( channels ) . forEach ( ( [ k , v ] ) => {
101
- const subscriberChannels = this . mapChannel ( k , v . description , v . subscribe , " subscribe" )
102
- subscriberChannels . forEach ( channel => s . push ( channel ) )
105
+ const subscriberChannels = this . mapChannel ( k , v . description , v . subscribe , ' subscribe' ) ;
106
+ subscriberChannels . forEach ( channel => s . push ( channel ) ) ;
103
107
104
- const publisherChannels = this . mapChannel ( k , v . description , v . publish , " publish" )
105
- publisherChannels . forEach ( channel => s . push ( channel ) )
108
+ const publisherChannels = this . mapChannel ( k , v . description , v . publish , ' publish' ) ;
109
+ publisherChannels . forEach ( channel => s . push ( channel ) ) ;
106
110
} ) ;
107
111
return s ;
108
112
}
109
113
110
114
private mapChannel (
111
115
topicName : string ,
112
- description : ServerAsyncApi [ "channels" ] [ "" ] [ "description" ] ,
113
- serverOperation : ServerAsyncApi [ "channels" ] [ "" ] [ "subscribe" ] | ServerAsyncApi [ "channels" ] [ "" ] [ "publish" ] ,
114
- operationType : OperationType ) : Channel [ ]
116
+ description : ServerAsyncApi [ 'channels' ] [ '' ] [ 'description' ] ,
117
+ serverOperation : ServerAsyncApi [ 'channels' ] [ '' ] [ 'subscribe' ] | ServerAsyncApi [ 'channels' ] [ '' ] [ 'publish' ] ,
118
+ operationType : OperationType
119
+ ) : Channel [ ]
115
120
{
116
- if ( serverOperation !== undefined ) {
117
- let messages : Message [ ] = this . mapMessages ( serverOperation . message )
121
+ if ( serverOperation !== undefined ) {
122
+ const messages : Message [ ] = this . mapMessages ( serverOperation . message ) ;
118
123
119
124
return messages . map ( message => {
120
- const operation = this . mapOperation ( operationType , message , serverOperation . bindings )
125
+ const operation = this . mapOperation ( operationType , message , serverOperation . bindings ) ;
121
126
return {
122
127
name : topicName ,
123
- anchorIdentifier : CHANNEL_ANCHOR_PREFIX + [ operation . protocol , topicName , operation . operation , operation . message . title ] . join ( "-" ) ,
124
- description : description ,
125
- operation : operation ,
126
- }
127
- } )
128
+ anchorIdentifier : CHANNEL_ANCHOR_PREFIX + [
129
+ operation . protocol , topicName , operation . operation , operation . message . title
130
+ ] . join ( '-' ) ,
131
+ description,
132
+ operation,
133
+ } ;
134
+ } ) ;
128
135
}
129
136
return [ ] ;
130
137
}
131
138
132
139
private mapMessages ( message : ServerAsyncApiChannelMessage ) : Message [ ] {
133
- if ( 'oneOf' in message ) {
134
- return this . mapServerAsyncApiMessages ( message . oneOf )
140
+ if ( 'oneOf' in message ) {
141
+ return this . mapServerAsyncApiMessages ( message . oneOf ) ;
135
142
}
136
143
return this . mapServerAsyncApiMessages ( [ message ] ) ;
137
144
}
@@ -144,23 +151,50 @@ export class AsyncApiMapperService {
144
151
description : v . description ,
145
152
payload : {
146
153
name : v . payload . $ref ,
147
- anchorUrl : AsyncApiMapperService . BASE_URL + v . payload . $ref ?. split ( '/' ) ?. pop ( )
154
+ anchorUrl : AsyncApiMapperService . BASE_URL + v . payload . $ref ?. split ( '/' ) ?. pop ( )
148
155
} ,
149
156
headers : {
150
157
name : v . headers . $ref ,
151
158
anchorUrl : AsyncApiMapperService . BASE_URL + v . headers . $ref ?. split ( '/' ) ?. pop ( )
152
- }
153
- }
154
- } )
159
+ } ,
160
+ bindings : this . mapServerAsyncApiMessageBindings ( v . bindings )
161
+ } ;
162
+ } ) ;
155
163
}
156
164
157
- private mapOperation ( operationType : OperationType , message : Message , bindings ?: any ) : Operation {
165
+ private mapServerAsyncApiMessageBindings (
166
+ serverMessageBindings : { [ type : string ] : ServerAsyncApiMessageBinding }
167
+ ) : Map < string , MessageBinding > {
168
+ const messageBindings = new Map < string , MessageBinding > ( ) ;
169
+ Object . keys ( serverMessageBindings ) . forEach ( ( protocol ) => {
170
+ messageBindings . set ( protocol , this . mapServerAsyncApiMessageBinding ( serverMessageBindings [ protocol ] ) ) ;
171
+ } ) ;
172
+ return messageBindings ;
173
+ }
174
+
175
+ private mapServerAsyncApiMessageBinding ( serverMessageBinding : ServerAsyncApiMessageBinding ) : MessageBinding {
176
+ const messageBinding : MessageBinding = { } ;
177
+
178
+ Object . keys ( serverMessageBinding ) . forEach ( ( key ) => {
179
+ const value = serverMessageBinding [ key ] ;
180
+ if ( typeof value === 'object' ) {
181
+ messageBinding [ key ] = this . mapSchema ( 'MessageBinding' , value ) ;
182
+ } else {
183
+ messageBinding [ key ] = value ;
184
+ }
185
+ } ) ;
186
+
187
+ return messageBinding ;
188
+ }
189
+
190
+
191
+ private mapOperation ( operationType : OperationType , message : Message , bindings ?: any ) : Operation {
158
192
return {
159
193
protocol : this . getProtocol ( bindings ) ,
160
194
operation : operationType ,
161
- message : message ,
162
- bindings : bindings
163
- }
195
+ message,
196
+ bindings
197
+ } ;
164
198
}
165
199
166
200
private getProtocol ( bindings ?: any ) : string {
@@ -184,12 +218,12 @@ export class AsyncApiMapperService {
184
218
anchorIdentifier : '#' + schemaName ,
185
219
anchorUrl : anchorUrl ,
186
220
type : schema . type ,
187
- items : items ,
221
+ items,
188
222
format : schema . format ,
189
223
enum : schema . enum ,
190
- properties : properties ,
224
+ properties,
191
225
required : schema . required ,
192
- example : example ,
193
- }
226
+ example,
227
+ } ;
194
228
}
195
229
}
0 commit comments