Skip to content

Commit 7e863aa

Browse files
Add flattened config option (#6)
* feat: add flattened option to config * test: add test for flattened model * chore(deps): upgrading deps * 0.2.12
1 parent 93395ef commit 7e863aa

7 files changed

Lines changed: 204 additions & 174 deletions

File tree

index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ amf.plugins.features.AMFValidation.register();
2020
* @param {string} type
2121
* @param {string} destPath
2222
* @param {string} resolution
23+
* @param {boolean} flattened
2324
* @return {Promise<void>}
2425
*/
25-
async function processFile(doc, file, type, destPath, resolution) {
26+
async function processFile(doc, file, type, destPath, resolution, flattened) {
2627
let validateProfile;
2728
switch (type) {
2829
case 'RAML 1.0': validateProfile = amf.ProfileNames.RAML; break;
@@ -63,13 +64,19 @@ async function processFile(doc, file, type, destPath, resolution) {
6364
const compactFile = path.join(destPath, compactDest);
6465

6566
// @ts-ignore
66-
const fullOpts = amf.render.RenderOptions().withSourceMaps;
67+
let fullOpts = amf.render.RenderOptions().withSourceMaps;
68+
if (flattened) {
69+
fullOpts = fullOpts.withFlattenedJsonLd;
70+
}
6771
const fullData = await generator.generateString(doc, fullOpts);
6872
await fs.ensureFile(fullFile);
6973
await fs.writeFile(fullFile, fullData, 'utf8');
7074

7175
// @ts-ignore
72-
const compactOpts = amf.render.RenderOptions().withSourceMaps.withCompactUris;
76+
let compactOpts = amf.render.RenderOptions().withSourceMaps.withCompactUris;
77+
if (flattened) {
78+
compactOpts = fullOpts.withFlattenedJsonLd;
79+
}
7380
// withRawSourceMaps.
7481
const compactData = await generator.generateString(doc, compactOpts);
7582
await fs.ensureFile(compactFile);
@@ -83,9 +90,9 @@ async function processFile(doc, file, type, destPath, resolution) {
8390
*/
8491
function normalizeOptions(input) {
8592
if (Array.isArray(input)) {
86-
const [type, mime, resolution] = input;
93+
const [type, mime, resolution, flattened] = input;
8794
// @ts-ignore
88-
return { type, mime, resolution };
95+
return { type, mime, resolution, flattened };
8996
}
9097
if (typeof input === 'object') {
9198
return input;
@@ -111,10 +118,10 @@ async function parseFile(file, cnf, opts) {
111118
if (!dest.endsWith('/')) {
112119
dest += '/';
113120
}
114-
const { type, mime='application/yaml', resolution='editing' } = normalizeOptions(cnf);
121+
const { type, mime='application/yaml', resolution='editing', flattened = false } = normalizeOptions(cnf);
115122
const parser = amf.Core.parser(type, mime);
116123
const doc = await parser.parseFileAsync(`file://${src}${file}`);
117-
return processFile(doc, file, type, dest, resolution);
124+
return processFile(doc, file, type, dest, resolution, flattened);
118125
}
119126

120127
/**

package-lock.json

Lines changed: 151 additions & 163 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api-components/api-model-generator",
3-
"version": "0.2.11",
3+
"version": "0.2.12",
44
"description": "AMF model generator for API components",
55
"main": "index.js",
66
"scripts": {
@@ -12,7 +12,7 @@
1212
"email": "arc@mulesoft.com"
1313
},
1414
"dependencies": {
15-
"amf-client-js": "^4.7.3-1",
15+
"amf-client-js": "^4.7.4",
1616
"fs-extra": "^10.0.0"
1717
},
1818
"devDependencies": {
@@ -21,11 +21,11 @@
2121
"@types/fs-extra": "^9.0.11",
2222
"@types/mocha": "^8.2.2",
2323
"chai": "^4.3.4",
24-
"eslint": "^7.27.0",
24+
"eslint": "^7.29.0",
2525
"eslint-config-google": "^0.14.0",
2626
"eslint-config-node": "^4.1.0",
2727
"eslint-plugin-babel": "^5.3.1",
28-
"eslint-plugin-import": "^2.23.3",
28+
"eslint-plugin-import": "^2.23.4",
2929
"eslint-plugin-no-only-tests": "^2.6.0",
3030
"husky": "^6.0.0",
3131
"lint-staged": "^11.0.0",

test/api-generation.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ describe('API generation', () => {
154154
describe('Api list config file with options', () => {
155155
const modelFile = path.join(dest, 'raml1.json');
156156
const compactModelFile = path.join(dest, 'raml1-compact.json');
157+
const flattenedModelFile = path.join(dest, 'flattenedApi-compact.json');
157158
const configFile = path.join('test', 'apis-options.json');
158159

159160
afterEach(() => fs.remove(dest));
@@ -167,6 +168,16 @@ describe('API generation', () => {
167168
}));
168169

169170
it('Generates data model for compact model', () => generator(configFile)
171+
.then(() => fs.pathExists(flattenedModelFile))
172+
.then((exists) => assert.isTrue(exists))
173+
.then(() => fs.readJson(flattenedModelFile))
174+
.then((data) => {
175+
const graph = data['@graph'];
176+
assert.isDefined(graph);
177+
}));
178+
179+
180+
it('Generates flattened data model', () => generator(configFile)
170181
.then(() => fs.pathExists(compactModelFile))
171182
.then((exists) => assert.isTrue(exists))
172183
.then(() => fs.readJson(compactModelFile))

test/apis-options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"apis/raml1.raml": "RAML 1.0",
3+
"apis/flattenedApi.raml": { "type": "RAML 1.0", "flattened": true },
34
"src": "test/",
45
"dest": "test/playground/"
56
}

test/apis/flattenedApi.raml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#%RAML 1.0
2+
title: API body demo
3+
version: v1
4+
baseUri: http://domain.com/
5+
6+
/endpoint:
7+
post:
8+
headers:
9+
x-header: string
10+
queryParameters:
11+
param1: number
12+
responses:
13+
200:
14+
headers:
15+
x-response: integer
16+
body:
17+
application/json:
18+
type: object

types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export declare interface ApiConfiguration {
1515
* @default editing
1616
*/
1717
resolution?: string;
18+
/**
19+
* Whether to apply flattened to render options
20+
* @default false
21+
*/
22+
flattened?: boolean;
1823
}
1924

2025
export declare interface ApiGenerationOptions {

0 commit comments

Comments
 (0)