Skip to content

Commit 0c8e04e

Browse files
authored
Merge pull request #17 from api-components/feat/update-amf-client-5.4.4
@W-21680934 Feat/update amf client 5.4.4
2 parents 71feb3f + c1f4461 commit 0c8e04e

9 files changed

Lines changed: 1600 additions & 3308 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ node_modules
5858
bower_components
5959
build
6060
dist
61+
62+
coverage
63+
.nyc_output

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
## 0.3.0 (2026-04-07)
6+
7+
### Features
8+
9+
* **amf**: upgrade amf-client-js from 4.7.8 to 5.10.2
10+
* **grpc**: add support for gRPC/Protobuf API parsing
11+
* **api**: migrate to AMF v5 API (RAMLConfiguration, OASConfiguration, GRPCConfiguration)
12+
13+
### Breaking Changes
14+
15+
* Requires amf-client-js ^5.10.2
16+
* API method signatures changed to use AMF v5 configuration objects
17+
518
### 0.2.5 (2020-01-22)
619

720

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ If the value is an array then first element must be API format and second is API
2121
`ApisList.dest``String` - path where output generated models. Default to `demo/`.
2222
`ApisList.<path>``String`, `Array`, `ApiDefinition` - a definition of an API to process. Key is a path to the API main file. The value depending on a type has different meaning.
2323

24-
`String` value represents API type. Can be `RAML 0.8`, `RAML 1.0`, `OAS 2.0`, or `OAS 3.0`. It generates api model for `application/yaml` media type and for the `editing` resolution pipeline.
24+
`String` value represents API type. Can be `RAML 0.8`, `RAML 1.0`, `OAS 2.0`, `OAS 3.0`, `ASYNC 2.0`, or `GRPC`. It generates api model for `application/yaml` media type and for the `editing` resolution pipeline.
2525

2626
`Array` value is deprecated. Please don't use it.
2727

2828
`ApiDefinition``Object`
29-
`ApiDefinition.type``String`. API type to process. Can be `RAML 0.8`, `RAML 1.0`, `OAS 2.0`, or `OAS 3.0`.
30-
`ApiDefinition.mime``String`. API media type. Default to `application/yaml`.
29+
`ApiDefinition.type``String`. API type to process. Can be `RAML 0.8`, `RAML 1.0`, `OAS 2.0`, `OAS 3.0`, `ASYNC 2.0`, or `GRPC`.
30+
`ApiDefinition.mime``String`. API media type. Default to `application/yaml`. For GRPC use `application/x-protobuf`.
3131
`ApiDefinition.resolution``String`. AMF resolution pipeline. Default to `editing` which is the original resolution pipeline for API Console. Future releases of AMF can support different options.
3232

3333

index.js

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,93 @@ const amf = require('amf-client-js');
33
const fs = require('fs-extra');
44
const path = require('path');
55

6-
amf.plugins.document.WebApi.register();
7-
amf.plugins.document.Vocabularies.register();
8-
amf.plugins.features.AMFValidation.register();
6+
const {
7+
RAMLConfiguration,
8+
OASConfiguration,
9+
AsyncAPIConfiguration,
10+
GRPCConfiguration,
11+
RenderOptions,
12+
PipelineId,
13+
} = amf;
914

1015
/** @typedef {import('./types').ApiConfiguration} ApiConfiguration */
1116
/** @typedef {import('./types').FilePrepareResult} FilePrepareResult */
1217
/** @typedef {import('./types').ApiGenerationOptions} ApiGenerationOptions */
1318
/** @typedef {import('./types').ApiType} ApiType */
1419

1520
/**
16-
* Generates json/ld file from parsed document.
21+
* @param {string} type
22+
* @returns {any}
23+
*/
24+
function getConfiguration(type) {
25+
switch (type) {
26+
case 'RAML 0.8': return RAMLConfiguration.RAML08();
27+
case 'RAML 1.0': return RAMLConfiguration.RAML10();
28+
case 'OAS 2.0':
29+
case 'OAS 2':
30+
return OASConfiguration.OAS20();
31+
case 'OAS 3.0':
32+
case 'OAS 3':
33+
return OASConfiguration.OAS30();
34+
case 'ASYNC 2.0': return AsyncAPIConfiguration.Async20();
35+
case 'GRPC': return GRPCConfiguration.GRPC();
36+
default: throw new Error(`Unknown API type: ${type}`);
37+
}
38+
}
39+
40+
/**
41+
* Generates json/ld file from parsed document using AMF v5 API.
1742
*
18-
* @param {amf.model.document.BaseUnit} doc
43+
* @param {string} sourceFile
1944
* @param {string} file
2045
* @param {string} type
2146
* @param {string} destPath
2247
* @param {string} resolution
2348
* @param {boolean} flattened
2449
* @return {Promise<void>}
2550
*/
26-
async function processFile(doc, file, type, destPath, resolution, flattened) {
27-
let validateProfile;
28-
switch (type) {
29-
case 'RAML 1.0': validateProfile = amf.ProfileNames.RAML; break;
30-
case 'RAML 0.8': validateProfile = amf.ProfileNames.RAML08; break;
31-
case 'OAS 2.0':
32-
case 'OAS 3.0':
33-
validateProfile = amf.ProfileNames.OAS;
34-
break;
35-
case 'ASYNC 2.0':
36-
// @ts-ignore
37-
validateProfile = amf.ProfileNames.ASYNC20;
38-
break;
39-
}
51+
async function processFile(sourceFile, file, type, destPath, resolution, flattened) {
4052
let dest = `${file.substr(0, file.lastIndexOf('.')) }.json`;
4153
if (dest.indexOf('/') !== -1) {
4254
dest = dest.substr(dest.lastIndexOf('/'));
4355
}
4456

45-
const generator = amf.Core.generator('AMF Graph', 'application/ld+json');
46-
const vResult = await amf.AMF.validate(doc, validateProfile, undefined);
47-
if (!vResult.conforms) {
48-
/* eslint-disable-next-line no-console */
49-
console.log(vResult.toString());
50-
}
51-
let resolver;
52-
switch (type) {
53-
case 'RAML 1.0': resolver = amf.Core.resolver('RAML 1.0'); break;
54-
case 'RAML 0.8': resolver = amf.Core.resolver('RAML 0.8'); break;
55-
case 'OAS 2.0': resolver = amf.Core.resolver('OAS 2.0'); break;
56-
case 'OAS 3.0': resolver = amf.Core.resolver('OAS 3.0'); break;
57-
case 'ASYNC 2.0': resolver = amf.Core.resolver('ASYNC 2.0'); break;
57+
// Setup render options
58+
let renderOpts = new RenderOptions().withSourceMaps().withCompactUris();
59+
if (flattened) {
60+
renderOpts = renderOpts.withCompactedEmission();
5861
}
59-
if (resolver) {
60-
doc = resolver.resolve(doc, resolution);
62+
63+
// Get configuration for API type
64+
const apiConfiguration = getConfiguration(type).withRenderOptions(renderOpts);
65+
const client = apiConfiguration.baseUnitClient();
66+
67+
// Parse the file
68+
const parseResult = await client.parse(sourceFile);
69+
70+
if (!parseResult.conforms) {
71+
/* eslint-disable-next-line no-console */
72+
console.log('Validation warnings/errors:');
73+
console.log(parseResult.toString());
6174
}
75+
76+
// Transform using resolution pipeline
77+
const pipelineId = resolution === 'editing' ? PipelineId.Editing : PipelineId.Default;
78+
const transformed = client.transform(parseResult.baseUnit, pipelineId);
79+
80+
// Render to JSON-LD
6281
const fullFile = path.join(destPath, dest);
6382
const compactDest = dest.replace('.json', '-compact.json');
6483
const compactFile = path.join(destPath, compactDest);
6584

66-
// @ts-ignore
67-
let fullOpts = amf.render.RenderOptions().withSourceMaps;
68-
if (flattened) {
69-
fullOpts = fullOpts.withFlattenedJsonLd;
70-
}
71-
const fullData = await generator.generateString(doc, fullOpts);
85+
// Generate full model (same as compact in v5 with withCompactUris)
86+
const modelData = await client.render(transformed.baseUnit, 'application/ld+json');
87+
7288
await fs.ensureFile(fullFile);
73-
await fs.writeFile(fullFile, fullData, 'utf8');
89+
await fs.writeFile(fullFile, modelData, 'utf8');
7490

75-
// @ts-ignore
76-
let compactOpts = amf.render.RenderOptions().withSourceMaps.withCompactUris;
77-
if (flattened) {
78-
compactOpts = compactOpts.withFlattenedJsonLd;
79-
}
80-
// withRawSourceMaps.
81-
const compactData = await generator.generateString(doc, compactOpts);
8291
await fs.ensureFile(compactFile);
83-
await fs.writeFile(compactFile, compactData, 'utf8');
92+
await fs.writeFile(compactFile, modelData, 'utf8');
8493
}
8594

8695
/**
@@ -119,9 +128,8 @@ async function parseFile(file, cnf, opts) {
119128
dest += '/';
120129
}
121130
const { type, mime='application/yaml', resolution='editing', flattened = false } = normalizeOptions(cnf);
122-
const parser = amf.Core.parser(type, mime);
123-
const doc = await parser.parseFileAsync(`file://${src}${file}`);
124-
return processFile(doc, file, type, dest, resolution, flattened);
131+
const sourceFile = `file://${src}${file}`;
132+
return processFile(sourceFile, file, type, dest, resolution, flattened);
125133
}
126134

127135
/**
@@ -166,7 +174,7 @@ async function main(init, opts={}) {
166174
init = cnfFiles;
167175
opts = { ...cnfOpts, ...opts };
168176
}
169-
await amf.Core.init();
177+
// AMF v5 doesn't require explicit initialization
170178
for (const [file, type] of init) {
171179
await parseFile(file, type, opts);
172180
}

0 commit comments

Comments
 (0)