Skip to content

Global object 'g.'

Global object 'g'

g.req

  • The 'g.req' will get the requested API data.

headers

  • Get/Set requested API headers.
g.req.headers['x-am-content-type-response'] = 'text/xml';

params

  • Get requested API params.
let requestedInstanceName = g.req.params['instanceName'];

query

  • Get/Set requested API query params.
g.req.query['getTotalCount'] = true;

body

  • Get/Set requested API body object.
g.req.body['first_name'] = "John";

eventData

  • Existing event data will be get in 'eventData'.
g.req.eventData['first_name'] = "John";

auth

  • Requested API's authorization related data get in 'auth'.
  • We have multiple authorizations in 'g.req.auth'.
    • authAMUser - API user authorization
    • authAMDB - Database user authorization
    • authGoogle - Google user authorization
    • authAWS - AWS Cognito authorization
    • authAzure - Azure Active Directory authorization

g.logger.log(g.req.auth.authAMUser);
- The end user provides a token in 'x-am-authorization', and it will print in logs. API user details are provided in the logs.

g.res

  • Requested API's whole response gets in the 'g.res'.
  • 'g.res' used in Post hook. If AM user sets the response statusCode in the pre hook, it will replace with the actual API response statusCode after executing the API code.

statusCode

  • Get the HTTP response status code of the requested API.

All supported status code.

OK = 200
NO_CONTENT = 204
CREATED = 201
BAD_REQUEST = 400
UNAUTHORIZED = 401
FORBIDDEN = 403
RESOURCE_NOT_FOUND = 404
INTERNAL_SERVER_ERROR = 500

g.res.statusCode = T.EStatusCode.BAD_REQUEST;

contentType

  • Get a response as per the given content type in the custom API code.
  • The user does not need to set the response content type to send JSON as a response.
  • If the response content type is not set that means it is application/JSON.

If ContentType is set to below system will return data accordingly.

JSON = 'application/json'
XML = 'text/xml'
YAML = 'text/yaml'
TEXT = 'text/plain'
HTML = 'text/html'

Note: If the content type is set and not from the above list, then API Maker will consider it as base64 encoded.

g.res.contentType = T.EContentType.HTML;

output

  • Get actual response data.
g.res.output;

shared

  • It is the API Maker's variable. Share values between Pre-Post hooks.
g.res.shared.preHookArr = [3.14, 108];

errors

  • If any errors in the response it will get in this.
g.loggers.error(g.res.errors);

warnings

  • If any warnings in response it will get in this.
g.loggers.error(g.res.warnings);

g.sys

g.sys.db

  • Call instance schema APIs.
await g.sys.db.getAll({
    instance: "INSTANCE_NAME",
    database: "DATABASE_NAME",
    collection: "COLLECTION_NAME",
    headers: {
        'x-am-authorization': "ednjlkf4i3jf89kjsnf9s9dfi0h"
    },
    queryParams: {
        'limit': 10,
        'skip': 1
    }
});

All 'g.sys.db' methods list.

g.sys.db.gen

  • Call instance generated APIs.
  • Instance generated APIs is schemaless. It is used only for MongoDB.
await g.sys.db.gen.getAllGen({
    instance: "INSTANCE_NAME",
    database: "DATABASE_NAME",
    collection: "COLLECTION_NAME",
    headers: {
        'x-am-authorization': "ednjlkf4i3jf89kjsnf9s9dfi0h"
    },
    queryParams: {
        'limit': 10,
        'skip': 1
    }
});

All 'g.sys.db' methods list.

g.sys.cache

  • To do operations on cache use 'g.sys.cache' methods.

Set redis keys

Set redis key using global object 'g'.

await g.sys.cache.setKey([
    {
        key: "redis_key_name",
        value: "redis_key_value",
        ttl: 1500  // This key will be available till next 1500 seconds.
    },
    {
        key: "redis_key_name",
        value: "redis_key_value"
    }
]);

All 'g.sys.cache' methods list.

g.sys.system

  • API Maker provides intellectual system APIs to do your work smartly.

await g.sys.system.encrypt("Hello World");
All 'g.sys.system' methods list.

g.logger

  • To print logs while coding API Maker user can use 'g.logger' methods.

g.logger.log(g.req.statusCode);
All 'g.logger' methods list.

g.shared

  • Shared space can be used to hold some variables, functions, and database connections.
  • To shared variables between pre hook, post hook, custom API code, and utility code.
  • You can use it globally. like,
g.shared.preHookArr = [3.14, 108];

g.shared.hello = "World";

g.shared.name = {
    Fname: "Bob",
    Lname: "Alice"
};

In Pre hook, set 'count' variable 234.

g.shared.count = 234;

Use 'count' in post hook.

let count = g.shared.count;

Define an object directly in a shared variable, its scope will be limited to that specific code panel. like,

g.shared = {
    Hello: "World"
};

For clear shared space,

1
2
3
delete g.shared.key; //Remove single property    

for (const key in g.shared) delete g.shared[key]; //Remove all property