Reference API documentation for AsyncAPI Generator library.
Generator
Kind: global class
- Generator
- new Generator(templateName, targetDir, options)
- instance
- .templateName :
String
- .targetDir :
String
- .entrypoint :
String
- .noOverwriteGlobs :
Array.<String>
- .disabledHooks :
Object.<String, (Boolean|String|Array.<String>)>
- .output :
String
- .forceWrite :
Boolean
- .debug :
Boolean
- .install :
Boolean
- .templateConfig :
Object
- .hooks :
Object
- .mapBaseUrlToFolder :
Object
- .templateParams :
Object
- .originalAsyncAPI :
String
- .generate(asyncapiDocument) ⇒
Promise
- .configureTemplate()
- .generateFromString(asyncapiString, [parserOptions]) ⇒
Promise
- .generateFromURL(asyncapiURL) ⇒
Promise
- .generateFromFile(asyncapiFile) ⇒
Promise
- .installTemplate([force])
- .templateName :
- static
new Generator
Instantiates a new Generator object.
Params
- templateName
String
- Name of the template to generate. - targetDir
String
- Path to the directory where the files will be generated. - options
Object
- [.templateParams]
String
- Optional parameters to pass to the template. Each template define their own params. - [.entrypoint]
String
- Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template. - [.noOverwriteGlobs]
Array.<String>
- List of globs to skip when regenerating the template. - [.disabledHooks]
Object.<String, (Boolean|String|Array.<String>)>
- Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array. - [.output]
String
= 'fs'
- Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set. - [.forceWrite]
Boolean
= false
- Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false. - [.install]
Boolean
= false
- Install the template and its dependencies, even when the template has already been installed. - [.debug]
Boolean
= false
- Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive. - [.mapBaseUrlToFolder]
Object.<String, String>
- Optional parameter to map schema references from a base url to a local base folder e.g. url=https://schema.example.com/crm/ folder=./test/docs/ .
- [.templateParams]
Example
1 2
const path = require('path'); const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'));
Example (Passing custom params to the template)
1 2 3 4 5 6
const path = require('path'); const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'), { templateParams: { sidebarOrganization: 'byTags' } });
- generator.templateName :
String
** : Name of the template to generate.
Kind: instance property of Generator
- generator.targetDir :
String
** : Path to the directory where the files will be generated.
Kind: instance property of Generator
- generator.entrypoint :
String
** : Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template.
Kind: instance property of Generator
- generator.noOverwriteGlobs :
Array.<String>
** : List of globs to skip when regenerating the template.
Kind: instance property of Generator
- generator.disabledHooks :
Object.<String, (Boolean|String|Array.<String>)>
** : Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array.
Kind: instance property of Generator
- generator.output :
String
** : Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set.
Kind: instance property of Generator
- generator.forceWrite :
Boolean
** : Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false.
Kind: instance property of Generator
- generator.debug :
Boolean
** : Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive.
Kind: instance property of Generator
- generator.install :
Boolean
** : Install the template and its dependencies, even when the template has already been installed.
Kind: instance property of Generator
- generator.templateConfig :
Object
** : The template configuration.
Kind: instance property of Generator
- generator.hooks :
Object
** : Hooks object with hooks functions grouped by the hook type.
Kind: instance property of Generator
- generator.mapBaseUrlToFolder :
Object
** : Maps schema URL to folder.
Kind: instance property of Generator
- generator.templateParams :
Object
** : The template parameters. The structure for this object is based on each individual template.
Kind: instance property of Generator
- generator.originalAsyncAPI :
String
** : AsyncAPI string to use as a source.
Kind: instance property of Generator
generator.generate
Generates files from a given template and an AsyncAPIDocument object.
Kind: instance method of Generator
Params
- asyncapiDocument
AsyncAPIDocument
- AsyncAPIDocument object to use as source.
Example
1 2 3 4 5 6
generator .generate(myAsyncAPIdocument) .then(() => { console.log('Done!'); }) .catch(console.error);
Example (Using async/await)
1 2 3 4 5 6
try { await generator.generate(myAsyncAPIdocument); console.log('Done!'); } catch (e) { console.error(e); }
- generator.configureTemplate()** : Configure the templates based the desired renderer.
Kind: instance method of Generator
generator.generateFromString
Generates files from a given template and AsyncAPI string.
Kind: instance method of Generator
Params
- asyncapiString
String
- AsyncAPI string to use as source. - [parserOptions]
Object
= {}
- AsyncAPI parser options. Check out @asyncapi/parser for more information.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13
const asyncapiString = ` asyncapi: '2.0.0' info: title: Example version: 1.0.0 ... `; generator .generateFromString(asyncapiString) .then(() => { console.log('Done!'); }) .catch(console.error);
Example (Using async/await)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const asyncapiString = ` asyncapi: '2.0.0' info: title: Example version: 1.0.0 ... `; try { await generator.generateFromString(asyncapiString); console.log('Done!'); } catch (e) { console.error(e); }
generator.generateFromURL
Generates files from a given template and AsyncAPI file stored on external server.
Kind: instance method of Generator
Params
- asyncapiURL
String
- Link to AsyncAPI file
Example
1 2 3 4 5 6
generator .generateFromURL('https://example.com/asyncapi.yaml') .then(() => { console.log('Done!'); }) .catch(console.error);
Example (Using async/await)
1 2 3 4 5 6
try { await generator.generateFromURL('https://example.com/asyncapi.yaml'); console.log('Done!'); } catch (e) { console.error(e); }
generator.generateFromFile
Generates files from a given template and AsyncAPI file.
Kind: instance method of Generator
Params
- asyncapiFile
String
- AsyncAPI file to use as source.
Example
1 2 3 4 5 6
generator .generateFromFile('asyncapi.yaml') .then(() => { console.log('Done!'); }) .catch(console.error);
Example (Using async/await)
1 2 3 4 5 6
try { await generator.generateFromFile('asyncapi.yaml'); console.log('Done!'); } catch (e) { console.error(e); }
generator.installTemplate
Downloads and installs a template and its dependencies
Kind: instance method of Generator
Params
- [force]
Boolean
= false
- Whether to force installation (and skip cache) or not.
Generator.getTemplateFile
Returns the content of a given template file.
Kind: static method of Generator
Params
- templateName
String
- Name of the template to generate. - filePath
String
- Path to the file to render. Relative to the template directory. - [templatesDir]
String
= DEFAULT_TEMPLATES_DIR
- Path to the directory where the templates are installed.
Example
1 2
const Generator = require('@asyncapi/generator'); const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html');
Example (Using a custom `templatesDir`)
1 2
const Generator = require('@asyncapi/generator'); const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html', '~/my-templates');