Skip to content

Commit 07b7da9

Browse files
committed
refactor: updating AMF library
Signed-off-by: Pawel Psztyc <[email protected]>
1 parent da2fe51 commit 07b7da9

File tree

97 files changed

+19712
-25199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+19712
-25199
lines changed

demo/api-annotation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class ComponentPage extends AmfDemoBase {
125125
['annotated-api', 'Annotated API'],
126126
].forEach(([file, label]) => {
127127
result[result.length] = html`
128-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>`;
128+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>`;
129129
});
130130
return result;
131131
}

demo/api-channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class ComponentPage extends AmfDemoBase {
113113
['Streetlights', 'Streetlights API'],
114114
].forEach(([file, label]) => {
115115
result[result.length] = html`
116-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>`;
116+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>`;
117117
});
118118
return result;
119119
}

demo/api-console.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export class ApiConsole extends ApplicationPage {
178178
this.route = name;
179179
if (name === 'api') {
180180
if (result.params.apiId && this.apiId !== result.params.apiId) {
181-
this.loadFile(`models/${result.params.apiId}-compact.json`);
181+
this.loadFile(`models/${result.params.apiId}.json`);
182182
}
183183
this.apiId = result.params.apiId;
184184
if (result.params.domainId) {
@@ -432,7 +432,7 @@ export class ApiConsole extends ApplicationPage {
432432
if (['selector'].includes(this.route)) {
433433
return '';
434434
}
435-
const { model, isMobile, navigationOpened } = this;
435+
const { isMobile, navigationOpened } = this;
436436
const classes = {
437437
navigation: true,
438438
toggle: isMobile,

demo/api-documentation-document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class ComponentPage extends AmfDemoBase {
9494
['Petstore-v2', 'Petstore OAS API'],
9595
].forEach(([file, label]) => {
9696
result[result.length] = html`
97-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>
97+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>
9898
`;
9999
});
100100
return result;

demo/api-documentation-partial.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class ComponentDemo extends AmfDemoBase {
296296
['async-api', 'async-api'],
297297
].forEach(([file, label]) => {
298298
result[result.length] = html`
299-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>`;
299+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>`;
300300
});
301301
return result;
302302
}

demo/api-documentation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class ComponentDemo extends AmfDemoBase {
221221
['APIC-711', 'APIC-711: switching to a library']
222222
].forEach(([file, label]) => {
223223
result[result.length] = html`
224-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>`;
224+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>`;
225225
});
226226
return result;
227227
}

demo/api-navigation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class ComponentPage extends AmfDemoBase {
179179
['SE-19215', 'SE-19215'],
180180
].forEach(([file, label]) => {
181181
result[result.length] = html`
182-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>`;
182+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>`;
183183
});
184184
return result;
185185
}

demo/api-operation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class ComponentPage extends AmfDemoBase {
242242
['SE-12959', 'SE-12959: OAS summary field'],
243243
].forEach(([file, label]) => {
244244
result[result.length] = html`
245-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>
245+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>
246246
`;
247247
});
248248
return result;

demo/api-payload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class ComponentPage extends AmfDemoBase {
182182
['stevetest', 'stevetest'],
183183
].forEach(([file, label]) => {
184184
result[result.length] = html`
185-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>
185+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>
186186
`;
187187
});
188188
return result;

demo/api-request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class ComponentDemo extends AmfDemoBase {
9393
['secured-unions', 'Secured unions']
9494
].map(
9595
([file, label]) => html`
96-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>
96+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>
9797
`
9898
);
9999
}

demo/api-resource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class ComponentPage extends AmfDemoBase {
230230
['APIC-650', 'APIC-650'],
231231
].forEach(([file, label]) => {
232232
result[result.length] = html`
233-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>`;
233+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>`;
234234
});
235235
return result;
236236
}

demo/api-schema-documentation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class ComponentPage extends AmfDemoBase {
198198
['aap-1698', 'aap-1698'],
199199
].forEach(([file, label]) => {
200200
result[result.length] = html`
201-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>
201+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>
202202
`;
203203
});
204204
return result;

demo/api-security-documentation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class ComponentPage extends AmfDemoBase {
106106
['security-api', 'security-api API'],
107107
].forEach(([file, label]) => {
108108
result[result.length] = html`
109-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>`;
109+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>`;
110110
});
111111
return result;
112112
}

demo/api-summary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class ComponentPage extends AmfDemoBase {
9696
['APIC-711', 'A library'],
9797
].forEach(([file, label]) => {
9898
result[result.length] = html`
99-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>`;
99+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>`;
100100
});
101101
return result;
102102
}

demo/lib/AmfDemoBase.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ export class AmfDemoBase extends AmfHelperMixin(DemoPage) {
260260
return [
261261
['demo-api', 'Demo API'],
262262
].map(([file, label]) => html`
263-
<anypoint-item data-src="models/${file}-compact.json">${label} - compact model</anypoint-item>
263+
<anypoint-item data-src="models/${file}.json">${label} - compact model</anypoint-item>
264264
`);
265265
}
266266

demo/model.mjs

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
import generator from '@api-components/api-model-generator';
1+
import pkg from 'amf-client-js';
2+
import path from 'path';
3+
import { writeFile, mkdir } from 'fs/promises';
4+
5+
/** @typedef {import('amf-client-js').AMFConfiguration} AMFConfiguration */
6+
/**
7+
* @typedef ApiConfiguration
8+
* @property {string} type
9+
* @property {string=} mime
10+
*/
11+
12+
const {
13+
RAMLConfiguration,
14+
OASConfiguration,
15+
AsyncAPIConfiguration,
16+
RenderOptions,
17+
PipelineId,
18+
} = pkg;
219

3-
/** @typedef {import('@api-components/api-model-generator/types').ApiConfiguration} ApiConfiguration */
420

521
/** @type {Map<string, ApiConfiguration>} */
622
const config = new Map();
@@ -120,7 +136,78 @@ config.set('example-generator-api/example-generator-api.raml', { type: "RAML 1.0
120136
config.set('tracked-examples/tracked-to-linked.raml', { type: 'RAML 1.0' });
121137
config.set('types-list/types-list.raml', { type: 'RAML 1.0' });
122138

123-
generator.generate(config, {
124-
dest: 'demo/models/',
125-
src: 'demo/apis/',
126-
});
139+
const srcFolder = path.join('demo', 'apis');
140+
const descFolder = path.join('demo', 'models');
141+
142+
class ApiParser {
143+
/**
144+
* @param {Map<string, ApiConfiguration>} list
145+
*/
146+
async batch(list) {
147+
await mkdir(descFolder, { recursive: true });
148+
for (const [file, info] of list) {
149+
console.log('Processing API file', file);
150+
try {
151+
// eslint-disable-next-line no-await-in-loop
152+
await this.run(file, info.type);
153+
} catch (e) {
154+
let message;
155+
if (e.message) {
156+
message = e.message;
157+
} else {
158+
message = e.toString();
159+
}
160+
console.error(`Unable to finish: `, message);
161+
process.exit(1);
162+
}
163+
}
164+
}
165+
166+
/**
167+
* @param {string} file
168+
* @param {string} vendor
169+
* @returns {Promise<void>}
170+
*/
171+
async run(file, vendor) {
172+
let destFile = `${file.substring(0, file.lastIndexOf('.')) }.json`;
173+
if (destFile.indexOf('/') !== -1) {
174+
destFile = destFile.substring(destFile.lastIndexOf('/'));
175+
}
176+
const location = path.join(srcFolder, file);
177+
const content = await this.parse(vendor, location);
178+
const destination = path.join(descFolder, destFile);
179+
await writeFile(destination, content);
180+
}
181+
182+
/**
183+
* @param {string} vendor
184+
* @param {string} location
185+
* @returns {Promise<string>}
186+
*/
187+
async parse(vendor, location) {
188+
const ro = new RenderOptions().withSourceMaps().withCompactUris();
189+
const apiConfiguration = this.getConfiguration(vendor).withRenderOptions(ro);
190+
const client = apiConfiguration.baseUnitClient();
191+
const result = await client.parse(`file://${location}`);
192+
const transformed = client.transform(result.baseUnit, PipelineId.Editing);
193+
return client.render(transformed.baseUnit, 'application/ld+json');
194+
}
195+
196+
/**
197+
* @param {string} vendor
198+
* @returns {AMFConfiguration}
199+
*/
200+
getConfiguration(vendor) {
201+
switch (vendor) {
202+
case 'RAML 0.8': return RAMLConfiguration.RAML08();
203+
case 'RAML 1.0': return RAMLConfiguration.RAML10();
204+
case 'OAS 2.0': return OASConfiguration.OAS20();
205+
case 'OAS 3.0': return OASConfiguration.OAS30();
206+
case 'ASYNC 2.0': return AsyncAPIConfiguration.Async20();
207+
default: throw new Error(`Unknown vendor: ${vendor}`);
208+
}
209+
}
210+
}
211+
212+
const parser = new ApiParser();
213+
parser.batch(config);

demo/request-editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class ComponentDemo extends AmfDemoBase {
9999
['21143', '21143'],
100100
['annotated-parameters', 'annotated-parameters'],
101101
].map(([file, label]) => html`
102-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>
102+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>
103103
`);
104104
}
105105

demo/server-selector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ComponentDemo extends AmfDemoBase {
7777
["no-servers-api", "No Servers API"],
7878
].map(
7979
([file, label]) => html`
80-
<anypoint-item data-src="models/${file}-compact.json">${label}</anypoint-item>
80+
<anypoint-item data-src="models/${file}.json">${label}</anypoint-item>
8181
`
8282
);
8383
}

dev/amf-server/ParserProcess.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
const amf = require('amf-client-js');
2-
// import amf from 'amf-client-js';
3-
amf.plugins.document.WebApi.register();
4-
amf.plugins.document.Vocabularies.register();
5-
amf.plugins.features.AMFValidation.register();
62

3+
/** @typedef {import('amf-client-js').AMFConfiguration} AMFConfiguration */
74
/** @typedef {import('../types').ParserProcessMessage} ParserProcessMessage */
85
/** @typedef {import('../types').ContentParseCommand} ContentParseCommand */
96
/** @typedef {import('../types').ParserProcessResult} ParserProcessResult */
107

8+
const {
9+
RAMLConfiguration,
10+
OASConfiguration,
11+
AsyncAPIConfiguration,
12+
RenderOptions,
13+
PipelineId,
14+
} = amf;
15+
1116
class AmfParserProcess {
1217
/**
1318
* Handles the message from the main process.
@@ -50,15 +55,34 @@ class AmfParserProcess {
5055
* @param {string} vendor
5156
*/
5257
async doParseContent(content, mime, vendor) {
53-
await amf.Core.init();
54-
55-
const parser = amf.Core.parser(vendor, mime);
56-
const doc = await parser.parseStringAsync(content);
57-
const resolver = amf.Core.resolver(vendor);
58-
const resolved = resolver.resolve(doc, 'editing');
59-
const generator = amf.Core.generator('AMF Graph', 'application/ld+json');
60-
const opts = new amf.render.RenderOptions().withSourceMaps.withCompactUris;
61-
return generator.generateString(resolved, opts);
58+
const ro = new RenderOptions().withSourceMaps().withCompactUris();
59+
const apiConfiguration = this.getConfiguration(vendor).withRenderOptions(ro);
60+
const client = apiConfiguration.baseUnitClient();
61+
const result = await client.parseContent(content, mime);
62+
const transformed = client.transform(result.baseUnit, PipelineId.Editing);
63+
return client.render(transformed.baseUnit, 'application/ld+json');
64+
// const parser = amf.Core.parser(vendor, mime);
65+
// const doc = await parser.parseStringAsync(content);
66+
// const resolver = amf.Core.resolver(vendor);
67+
// const resolved = resolver.resolve(doc, 'editing');
68+
// const generator = amf.Core.generator('AMF Graph', 'application/ld+json');
69+
// const opts = new amf.render.RenderOptions().withSourceMaps.withCompactUris;
70+
// return generator.generateString(resolved, opts);
71+
}
72+
73+
/**
74+
* @param {string} vendor
75+
* @returns {AMFConfiguration}
76+
*/
77+
getConfiguration(vendor) {
78+
switch (vendor) {
79+
case 'RAML 0.8': return RAMLConfiguration.RAML08();
80+
case 'RAML 1.0': return RAMLConfiguration.RAML10();
81+
case 'OAS 2.0': return OASConfiguration.OAS20();
82+
case 'OAS 3.0': return OASConfiguration.OAS30();
83+
case 'ASYNC 2.0': return AsyncAPIConfiguration.Async20();
84+
default: throw new Error(`Unknown vendor: ${vendor}`);
85+
}
6286
}
6387
}
6488

0 commit comments

Comments
 (0)