@@ -14,7 +14,6 @@ import { AmfHelperMixin, expandKey, findAmfType, getArrayItems } from "./AmfHelp
14
14
/** @typedef {import('./types').ApiEndPoint } ApiEndPoint */
15
15
/** @typedef {import('./types').ApiServer } ApiServer */
16
16
/** @typedef {import('./types').ApiDocumentation } ApiDocumentation */
17
- /** @typedef {import('./types').SerializedApi } SerializedApi */
18
17
/** @typedef {import('./types').ApiShape } ApiShape */
19
18
/** @typedef {import('./types').ApiPropertyShape } ApiPropertyShape */
20
19
/** @typedef {import('./types').ApiAnyShape } ApiAnyShape */
@@ -54,6 +53,15 @@ import { AmfHelperMixin, expandKey, findAmfType, getArrayItems } from "./AmfHelp
54
53
/** @typedef {import('./types').ApiVariableValue } ApiVariableValue */
55
54
/** @typedef {import('./types').ApiAbstractDeclaration } ApiAbstractDeclaration */
56
55
/** @typedef {import('./types').ShapeProcessingOptions } ShapeProcessingOptions */
56
+ /** @typedef {import('./types').ApiSummary } ApiSummary */
57
+ /** @typedef {import('./types').ApiOrganization } ApiOrganization */
58
+ /** @typedef {import('./types').ApiLicense } ApiLicense */
59
+ /** @typedef {import('./types').ApiBase } ApiBase */
60
+ /** @typedef {import('./types').ApiWeb } ApiWeb */
61
+ /** @typedef {import('./types').ApiAsync } ApiAsync */
62
+ /** @typedef {import('./amf').Api } Api */
63
+ /** @typedef {import('./amf').WebApi } WebApi */
64
+ /** @typedef {import('./amf').AsyncApi } AsyncApi */
57
65
/** @typedef {import('./amf').Server } Server */
58
66
/** @typedef {import('./amf').Parameter } Parameter */
59
67
/** @typedef {import('./amf').Shape } Shape */
@@ -101,6 +109,8 @@ import { AmfHelperMixin, expandKey, findAmfType, getArrayItems } from "./AmfHelp
101
109
/** @typedef {import('./amf').ParametrizedTrait } ParametrizedTrait */
102
110
/** @typedef {import('./amf').VariableValue } VariableValue */
103
111
/** @typedef {import('./amf').AbstractDeclaration } AbstractDeclaration */
112
+ /** @typedef {import('./amf').Organization } Organization */
113
+ /** @typedef {import('./amf').License } License */
104
114
105
115
/**
106
116
* A class that takes AMF's ld+json model and outputs JavaScript interface of it.
@@ -116,6 +126,167 @@ export class AmfSerializer extends AmfHelperMixin(Object) {
116
126
}
117
127
}
118
128
129
+ /**
130
+ * @param {Api } object The API to serialize.
131
+ * @returns {ApiSummary } API summary, without complex objects.
132
+ */
133
+ apiSummary ( object ) {
134
+ const result = /** @type ApiSummary */ ( {
135
+ id : object [ '@id' ] ,
136
+ types : object [ '@type' ] . map ( this [ expandKey ] . bind ( this ) ) ,
137
+ customDomainProperties : this . customDomainProperties ( object ) ,
138
+ sourceMaps : this . sourceMap ( object ) ,
139
+ schemes : [ ] ,
140
+ accepts : [ ] ,
141
+ contentType : [ ] ,
142
+ documentations : [ ] ,
143
+ tags : [ ] ,
144
+ } ) ;
145
+ const { ns } = this ;
146
+ const name = this . _getValue ( object , ns . aml . vocabularies . core . name ) ;
147
+ if ( name && typeof name === 'string' ) {
148
+ result . name = name ;
149
+ }
150
+ const description = this . _getValue ( object , ns . aml . vocabularies . core . description ) ;
151
+ if ( description && typeof description === 'string' ) {
152
+ result . description = description ;
153
+ }
154
+ const version = this . _getValue ( object , ns . aml . vocabularies . core . version ) ;
155
+ if ( version && typeof version === 'string' ) {
156
+ result . version = version ;
157
+ }
158
+ const termsOfService = this . _getValue ( object , ns . aml . vocabularies . core . termsOfService ) ;
159
+ if ( termsOfService && typeof termsOfService === 'string' ) {
160
+ result . termsOfService = termsOfService ;
161
+ }
162
+ const accepts = object [ this . _getAmfKey ( ns . aml . vocabularies . apiContract . accepts ) ] ;
163
+ if ( Array . isArray ( accepts ) && accepts . length ) {
164
+ result . accepts = /** @type string[] */ ( this . _getValueArray ( object , ns . aml . vocabularies . apiContract . accepts ) ) ;
165
+ }
166
+ const contentType = object [ this . _getAmfKey ( ns . aml . vocabularies . apiContract . contentType ) ] ;
167
+ if ( Array . isArray ( contentType ) && contentType . length ) {
168
+ result . contentType = /** @type string[] */ ( this . _getValueArray ( object , ns . aml . vocabularies . apiContract . contentType ) ) ;
169
+ }
170
+ const schemes = object [ this . _getAmfKey ( ns . aml . vocabularies . apiContract . scheme ) ] ;
171
+ if ( Array . isArray ( schemes ) && schemes . length ) {
172
+ result . schemes = /** @type string[] */ ( this . _getValueArray ( object , ns . aml . vocabularies . apiContract . scheme ) ) ;
173
+ }
174
+ let provider = object [ this . _getAmfKey ( ns . aml . vocabularies . core . provider ) ] ;
175
+ if ( Array . isArray ( provider ) ) {
176
+ [ provider ] = provider ;
177
+ }
178
+ if ( provider ) {
179
+ result . provider = this . provider ( provider ) ;
180
+ }
181
+ let license = object [ this . _getAmfKey ( ns . aml . vocabularies . core . license ) ] ;
182
+ if ( Array . isArray ( license ) ) {
183
+ [ license ] = license ;
184
+ }
185
+ if ( license ) {
186
+ result . license = this . license ( license ) ;
187
+ }
188
+ const tags = object [ this . _getAmfKey ( ns . aml . vocabularies . apiContract . tag ) ] ;
189
+ if ( Array . isArray ( tags ) && tags . length ) {
190
+ result . tags = tags . map ( t => this . tag ( t ) ) ;
191
+ }
192
+ const docs = object [ this . _getAmfKey ( ns . aml . vocabularies . core . documentation ) ] ;
193
+ if ( Array . isArray ( docs ) && docs . length ) {
194
+ result . documentations = docs . map ( d => this . documentation ( d ) ) ;
195
+ }
196
+ return result ;
197
+ }
198
+
199
+ /**
200
+ * @param {Api } object
201
+ * @returns {ApiBase }
202
+ */
203
+ api ( object ) {
204
+ const result = /** @type ApiBase */ ( this . apiSummary ( object ) ) ;
205
+ result . endPoints = [ ] ;
206
+ result . servers = [ ] ;
207
+ result . security = [ ] ;
208
+ const { ns } = this ;
209
+ const endPoints = object [ this . _getAmfKey ( ns . aml . vocabularies . apiContract . endpoint ) ] ;
210
+ if ( Array . isArray ( endPoints ) && endPoints . length ) {
211
+ result . endPoints = endPoints . map ( e => this . endPoint ( e ) ) ;
212
+ }
213
+ const servers = object [ this . _getAmfKey ( ns . aml . vocabularies . apiContract . server ) ] ;
214
+ if ( Array . isArray ( servers ) && servers . length ) {
215
+ result . servers = servers . map ( s => this . server ( s ) ) ;
216
+ }
217
+ const security = object [ this . _getAmfKey ( ns . aml . vocabularies . security . security ) ] ;
218
+ if ( Array . isArray ( security ) && security . length ) {
219
+ result . security = security . map ( s => this . securityRequirement ( s ) ) ;
220
+ }
221
+ return result ;
222
+ }
223
+
224
+ /**
225
+ * @param {WebApi } object
226
+ * @returns {ApiWeb }
227
+ */
228
+ webApi ( object ) {
229
+ return this . api ( object ) ;
230
+ }
231
+
232
+ /**
233
+ * @param {AsyncApi } object
234
+ * @returns {ApiAsync }
235
+ */
236
+ asyncApi ( object ) {
237
+ return this . api ( object ) ;
238
+ }
239
+
240
+ /**
241
+ * @param {Organization } object
242
+ * @returns {ApiOrganization }
243
+ */
244
+ provider ( object ) {
245
+ const result = /** @type ApiOrganization */ ( {
246
+ id : object [ '@id' ] ,
247
+ types : object [ '@type' ] . map ( this [ expandKey ] . bind ( this ) ) ,
248
+ customDomainProperties : this . customDomainProperties ( object ) ,
249
+ sourceMaps : this . sourceMap ( object ) ,
250
+ } ) ;
251
+ const { ns } = this ;
252
+ const name = this . _getValue ( object , ns . aml . vocabularies . core . name ) ;
253
+ if ( name && typeof name === 'string' ) {
254
+ result . name = name ;
255
+ }
256
+ const url = this . _getLinkValue ( object , ns . aml . vocabularies . core . url ) ;
257
+ if ( url && typeof url === 'string' ) {
258
+ result . url = url ;
259
+ }
260
+ const email = this . _getValue ( object , ns . aml . vocabularies . core . email ) ;
261
+ if ( email && typeof email === 'string' ) {
262
+ result . email = email ;
263
+ }
264
+ return result ;
265
+ }
266
+
267
+ /**
268
+ * @param {License } object
269
+ * @returns {ApiLicense }
270
+ */
271
+ license ( object ) {
272
+ const result = /** @type ApiLicense */ ( {
273
+ id : object [ '@id' ] ,
274
+ types : object [ '@type' ] . map ( this [ expandKey ] . bind ( this ) ) ,
275
+ customDomainProperties : this . customDomainProperties ( object ) ,
276
+ sourceMaps : this . sourceMap ( object ) ,
277
+ } ) ;
278
+ const { ns } = this ;
279
+ const name = this . _getValue ( object , ns . aml . vocabularies . core . name ) ;
280
+ if ( name && typeof name === 'string' ) {
281
+ result . name = name ;
282
+ }
283
+ const url = this . _getLinkValue ( object , ns . aml . vocabularies . core . url ) ;
284
+ if ( url && typeof url === 'string' ) {
285
+ result . url = url ;
286
+ }
287
+ return result ;
288
+ }
289
+
119
290
/**
120
291
* @param {Server } object The AMF Server to serialize.
121
292
* @returns {ApiServer } Serialized Server
0 commit comments