Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add baseDir option #148

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ console.log(document.string()); // get JSON string
| [options] | <code>Object</code> | |
| [options.base] | <code>string</code> \| <code>object</code> | <p>Base object whose properties will be retained.</p> |
| [options.referenceIntoComponents] | <code>boolean</code> | <p>Pass <code>true</code> to resolve external references to components.</p> |
| [options.baseDir] | <code>string</code> | <p>Pass folder path to</p> |

**Example**
**TypeScript**
Expand Down
10 changes: 7 additions & 3 deletions example/v3/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ const {readFileSync, writeFileSync} = require('fs')
async function main() {
await singleFile()
await multiFile()
await singleFileWithBaseDir()
}


async function singleFile(){
const document = await bundle([readFileSync('./main.yaml', 'utf-8')] )
async function singleFileWithBaseDir(){
const document = await bundle([readFileSync('./spec/main.yaml', 'utf-8')], {baseDir: './spec'})
writeFileSync('asyncapi.yaml', document.yml())
}

async function singleFile(){
const document = await bundle([readFileSync('./spec/main.yaml', 'utf-8')])
writeFileSync('asyncapi.yaml', document.yml())

}

async function multiFile(){
Expand Down
3 changes: 2 additions & 1 deletion example/v3/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions example/v3/spec/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
asyncapi: 3.0.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signupsA
channels:
userSignedup:
address: 'user/signedup'
messages:
userSignedUpMessage:
$ref: './messages.yaml#/messages/UserSignedUp'
test:
address: '/test'
messages:
testMessage:
$ref: '#/components/messages/TestMessage'
operations:
UserSignedUp:
action: send
channel:
$ref: '#/channels/userSignedup'
messages:
- $ref: './messages.yaml#/messages/UserSignedUp'
TestOpp:
action: send
channel:
$ref: '#/channels/test'
messages:
- $ref: '#/components/messages/TestMessage'
components:
messages:
TestMessage:
payload:
type: string
17 changes: 17 additions & 0 deletions example/v3/spec/messages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
UserLoggedIn:
payload:
type: object
properties:
id: string
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toJS, resolve, versionCheck } from './util';
import { toJS, resolve, versionCheck, resolveBaseFileDir } from './util';
import { Document } from './document';
import { parse } from './parser';

Expand All @@ -15,6 +15,7 @@ import { resolveV3Document } from './v3/parser';
* retained.
* @param {boolean} [options.referenceIntoComponents] Pass `true` to resolve
* external references to components.
* @param {string} [options.baseDir] Pass folder path to
*
* @return {Document}
*
Expand Down Expand Up @@ -80,6 +81,10 @@ export default async function bundle(files: string[], options: any = {}) {

const parsedJsons = files.map(file => toJS(file)) as AsyncAPIObject[];

if (typeof options.baseDir !== 'undefined') {
parsedJsons.forEach(parsedJson => resolveBaseFileDir(parsedJson, options.baseDir));
}

const majorVersion = versionCheck(parsedJsons);
let resolvedJsons;

Expand Down
46 changes: 46 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { cloneDeep } from 'lodash';
import yaml from 'js-yaml';
import { parse } from './parser';
import { ParserError } from './errors';
import {JSONPath} from 'jsonpath-plus';

import type { AsyncAPIObject } from './spec-types';
import path from 'path';

/**
* @private
Expand Down Expand Up @@ -99,4 +101,48 @@ export function versionCheck(asyncapiDocuments: AsyncAPIObject[]): number {
currentVersion = majorVersion;
}
return currentVersion;
}

export function isExternalReference(ref: string): boolean {
return typeof ref === 'string' && !ref.startsWith('#');
}

export function notAUrl(ref: string): boolean {
try {
new URL(ref);
return false;
} catch (error) {
return true;
}
}

export function resolveBaseFileDir(file: object, baseFileDir: string) {
/**
* Update the local refences in a given file with the
* absolute file path using the baseDir passed by the
* user as an option.
*/
JSONPath({
json: file,
resultType: 'all',
path: '$.channels.*.messages.*'
}).forEach(({parent, parentProperty}: {parent: any, parentProperty: string}) => {
const ref = parent[String(parentProperty)]['$ref'];
if (isExternalReference(ref) && notAUrl(ref)) {
parent[String(parentProperty)]['$ref'] = path.resolve(baseFileDir, ref);
}
});

JSONPath({
json: file,
resultType: 'all',
path: '$.operations.*.messages.*'
}).forEach(
({parent, parentProperty}: {parent: any, parentProperty: string}) => {
const ref = parent[String(parentProperty)]['$ref'];
if (isExternalReference(ref) && notAUrl(ref)) {
parent[String(parentProperty)]['$ref'] = path.resolve(baseFileDir, ref);
}
}
);
}
10 changes: 10 additions & 0 deletions tests/lib/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ describe('[integration testing] bundler should ', () => {
).resolves;

})

test('should be able to change the baseDir folder', async () => {
const files = ['./tests/specfiles/main.yaml']
expect(
await bundle(
files.map(file => fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8')),
{baseDir: './tests/specfiles'}
)
).resolves
})
});

describe('[unit testing]', () => {
Expand Down
34 changes: 34 additions & 0 deletions tests/specfiles/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
asyncapi: 3.0.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signupsA
channels:
userSignedup:
address: 'user/signedup'
messages:
userSignedUpMessage:
$ref: './messages.yaml#/messages/UserSignedUp'
test:
address: '/test'
messages:
testMessage:
$ref: '#/components/messages/TestMessage'
operations:
UserSignedUp:
action: send
channel:
$ref: '#/channels/userSignedup'
messages:
- $ref: './messages.yaml#/messages/UserSignedUp'
TestOpp:
action: send
channel:
$ref: '#/channels/test'
messages:
- $ref: '#/components/messages/TestMessage'
components:
messages:
TestMessage:
payload:
type: string
17 changes: 17 additions & 0 deletions tests/specfiles/messages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
messages:
UserSignedUp:
payload:
type: object
properties:
displayName:
type: string
description: Name of the user
email:
type: string
format: email
description: Email of the user
UserLoggedIn:
payload:
type: object
properties:
id: string