On this page

Hooks

Found an error? Have a suggestion?Edit this page on GitHub

Hooks are functions called by the generator on a specific moment in the generation process. Hooks can be anonymous functions but you can also add function names. These hooks can have arguments provided to them or being expected to return a value. The following types of hooks are currently supported:

Hook typeDescriptionReturn typeArguments
generate:beforeCalled after registration of all filters and before the generator starts processing of the template.void : Nothing is expected to be returned.The generator instance
generate:afterCalled at the very end of the generation.void : Nothing is expected to be returned.The generator instance
setFileTemplateName Called right before saving a new file generated by file template.string : a new filename for the generator to use for the file template.The generator instance and object in the form of { "originalFilename" : string }

The generator parses:

  • All the files in the .hooks directory inside the template.
  • All modules listed in the template configuration and triggers only hooks that names were added to the config. You can use the official AsyncAPI hooks library. To learn how to add hooks to configuration read more about the configuration file.

Examples

Some of the examples have names of hook functions provided and some not. Keep in mind that hook functions kept in template in default location do not require a name. Name is required only if you keep hooks in non default location or in a separate library, because such hooks need to be explicitly configured in the configuration file. For more details on hooks configuration read more about the configuration file.

Most basic modules with hooks look like this:

1
2
3
module.exports = {
  'generate:after': generator => console.log('This runs after generation is complete')
}

Below you have an example Hook that after generation creates an AsyncAPI file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const fs = require('fs');
const path = require('path');

module.exports = {
  'generate:after': generator => {
    const asyncapi = generator.originalAsyncAPI;
    let extension;

    try {
      JSON.parse(asyncapi);
      extension = 'json';
    } catch (e) {
      extension = 'yaml';
    }

    fs.writeFileSync(path.resolve(generator.targetDir, `asyncapi.${extension}`), asyncapi);
  }
};

And here an example Hook that before generation switches publish and subscribe operations for each channel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module.exports = {
  'generate:before': function switchOperations(generator) {
    const asyncapi = generator.asyncapi;
    for (let [key, value] of Object.entries(asyncapi.channels())) {
      let publish = value._json.publish;
      value._json.publish = value._json.subscribe;
      value._json.subscribe = publish;
      if (!value._json.subscribe) {
        delete value._json.subscribe;
      }
      if (!value._json.publish) {
        delete value._json.publish;
      }
    }
  };
};

Example hook for changing the filename of a template file. Replaces all '-' characters with '_'.

1
2
3
4
5
6
module.exports = {
    'setFileTemplateName': (generator, hookArguments) => {
        const currentFilename = hookArguments.originalFilename ;
        return currentFilename.replace('-', '_')
    };
};
Was this helpful?
Help us improve the docs by adding your contribution.
OR
Create Issue on GitHub