Skip to content

Post hook

  • Lines of code which will be run after the complete execution of the actual API request.
  • API Maker users can add multiple post hooks.
  • It will run from top to bottom order sequentially.
  • API Maker users can activate/inactivate, update/delete any post hooks. It will affect in next request. No need to restart the project.
  • Post hooks can be added in instance APIs, custom APIs, system APIs, and Third party APIs.
  • Pay attention, when using Stream api do not add postHook because, at that time " data you are getting is incomplete, you might only have the last chank of data ".
  • Duplicate API calls or cycle detected, while calling api / event / listener / prehook & posthook in same transaction.

Keep In mind:

  1. Return without any value
    • This means that you only exit the specific hook not from the entire execution.
  2. Return with any value
    • This means that the hook can end the next execution and return a value.
  3. Override output
    • This means that the [g.res.output] command will replace the main API output without affecting the next execution.
  4. Throw error
    • This means that the "throw Error" statement will send an error message in the response and stop the next execution.

Note: If API has caching enabled and data is coming from the cache system will not execute pre-hook and post-hook.

Global object 'g'

  • Post hook has available global object 'g'. API Maker users can use all methods of 'g' in post hooks.
import * as T from 'types';
async function main(g: T.IAMGlobal) {
    return await g.sys.db.getAll({
        instance: "INSTANCE_NAME",
        collection: "COLLECTION_NAME",
        database: "DATABASE_NAME",
    });
};
module.exports = main;

Use Utility classes

  • Utility classes can be imported and used it's active versions methods.
  • All utility classes are available in the 'utils/' path.
  • Click CTRL+SPACE to get a list of all utility classes.
import * as T from 'types';
import * as addition from 'utils/Addition';
async function main(g: T.IAMGlobal) {
    return addition.add(g.req.body.x, g.req.body.y);
};
module.exports = main;