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

docs: enhance API docs with information about results of code generation with generateFromString #967

Closed
wants to merge 12 commits into from
46 changes: 43 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ weight: 75
---

Reference API documentation for AsyncAPI Generator library.
## Classes

<dl>
<dt><a href="#Generator">Generator</a></dt>
<dd></dd>
</dl>

## Typedefs

<dl>
<dt><a href="#TemplateRenderMetadata">TemplateRenderMetadata</a> : <code>Object</code></dt>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derberg @chinma-yyy what do the #TemplateRenderMetadata and #TemplateRenderResults link to. They don't navigate to any section of this page.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what it will look like in the website:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it should render the object properties 🤔. Will look into it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chinma-yyy any updates regarding this?

<dd></dd>
<dt><a href="#TemplateRenderResult">TemplateRenderResult</a> : <code>Object</code></dt>
<dd></dd>
</dl>


<a name="Generator"></a>

Expand All @@ -29,9 +45,9 @@ Reference API documentation for AsyncAPI Generator library.
* [.originalAsyncAPI](#Generator+originalAsyncAPI) : `String`
* [.generate(asyncapiDocument)](#Generator+generate) ⇒ `Promise`
* [.configureTemplate()](#Generator+configureTemplate)
* [.generateFromString(asyncapiString, [parseOptions])](#Generator+generateFromString) ⇒ `Promise`
* [.generateFromURL(asyncapiURL)](#Generator+generateFromURL) ⇒ `Promise`
* [.generateFromFile(asyncapiFile)](#Generator+generateFromFile) ⇒ `Promise`
* [.generateFromString(asyncapiString, [parseOptions])](#Generator+generateFromString) ⇒ `Promise.<(TemplateRenderResult|undefined)>`
* [.generateFromURL(asyncapiURL)](#Generator+generateFromURL) ⇒ `Promise.<(TemplateRenderResult|undefined)>`
* [.generateFromFile(asyncapiFile)](#Generator+generateFromFile) ⇒ `Promise.<(TemplateRenderResult|undefined)>`
* [.installTemplate([force])](#Generator+installTemplate)
* _static_
* [.getTemplateFile(templateName, filePath, [templatesDir])](#Generator.getTemplateFile) ⇒ `Promise`
Expand Down Expand Up @@ -342,3 +358,27 @@ const content = await Generator.getTemplateFile('@asyncapi/html-template', 'part
const Generator = require('@asyncapi/generator');
const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html', '~/my-templates');
```

<a name="TemplateRenderMetadata"></a>

* TemplateRenderMetadata : `Object`** :
**Kind**: global typedef
**Properties**

| Name | Type |
| --- | --- |
| fileName | `string` |
| permissions | `string` |


<a name="TemplateRenderResult"></a>

* TemplateRenderResult : `Object`** :
**Kind**: global typedef
**Properties**

| Name | Type |
| --- | --- |
| content | `string` |
| metadata | [`TemplateRenderMetadata`](#TemplateRenderMetadata) |

31 changes: 21 additions & 10 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ class Generator {
this.nunjucks = configureNunjucks(this.debug, this.templateDir);
}
}
/**
* @typedef {Object} TemplateRenderMetadata
* @property {string} fileName
* @property {string} permissions
*/

/**
* @typedef {Object} TemplateRenderResult
* @property {string} content
* @property {TemplateRenderMetadata} metadata
*/

/**
* Generates files from a given template and AsyncAPI string.
Expand Down Expand Up @@ -250,7 +261,7 @@ class Generator {
*
* @param {String} asyncapiString AsyncAPI string to use as source.
* @param {Object} [parseOptions={}] AsyncAPI Parser parse options. Check out {@link https://www.github.com/asyncapi/parser-js|@asyncapi/parser} for more information.
* @return {Promise}
* @return {Promise<TemplateRenderResult|undefined>}
*/
async generateFromString(asyncapiString, parseOptions = {}) {
if (!asyncapiString || typeof asyncapiString !== 'string') throw new Error('Parameter "asyncapiString" must be a non-empty string.');
Expand Down Expand Up @@ -288,7 +299,7 @@ class Generator {
* }
*
* @param {String} asyncapiURL Link to AsyncAPI file
* @return {Promise}
* @return {Promise<TemplateRenderResult|undefined>}
*/
async generateFromURL(asyncapiURL) {
const doc = await fetchSpec(asyncapiURL);
Expand All @@ -315,7 +326,7 @@ class Generator {
* }
*
* @param {String} asyncapiFile AsyncAPI file to use as source.
* @return {Promise}
* @return {Promise<TemplateRenderResult|undefined>}
*/
async generateFromFile(asyncapiFile) {
const doc = await readFile(asyncapiFile, { encoding: 'utf8' });
Expand Down Expand Up @@ -436,7 +447,7 @@ class Generator {
}
});
}

if (asyncapiDocument.hasComponents()) {
for (const [key, value] of Object.entries(asyncapiDocument.components().parameters())) {
parameters.set(key, value);
Expand Down Expand Up @@ -692,12 +703,12 @@ class Generator {
*/
maybeRenameSourceFile(sourceFile) {
switch (path.basename(sourceFile)) {
case GIT_IGNORE_FILENAME:
return path.join(path.dirname(sourceFile), '.gitignore');
case NPM_IGNORE_FILENAME:
return path.join(path.dirname(sourceFile), '.npmignore');
default:
return sourceFile;
case GIT_IGNORE_FILENAME:
return path.join(path.dirname(sourceFile), '.gitignore');
case NPM_IGNORE_FILENAME:
return path.join(path.dirname(sourceFile), '.npmignore');
default:
return sourceFile;
}
}

Expand Down