API Connect

 View Only
  • 1.  GatewayScript common/shared utility libraries on IBM API Connect v10

    Posted Tue June 21, 2022 10:13 AM
    Trying to see if this is possible.  

    When using the GatewayScript policy can the script reference external GatewayScript libraries to call standard utility functions?  Since I need to write GatewayScript policies it would be useful to reference standard functions that are reused across API definitions and not embedded into each API specification file. 

    Example:

    GatewayScript Policy:

    var apim = require('apim');
    var utils = require('local://common_utility.js');

    apim.setvariable('message.headers.SOME-HEADER', utils.getSomeValue());


    I have tried a couple of times to get this to work, but have been getting 500 errors.



    ------------------------------
    Steve Ruscik
    ------------------------------


  • 2.  RE: GatewayScript common/shared utility libraries on IBM API Connect v10

    Posted Wed June 22, 2022 06:58 AM
    Answering my own question: I was able to get the code sample to work by including an exports directive/keyword to the utility.js file.
    utility.js
    -------------------------------------------------
    exports.getSomeValue = function ()
    {
    return 'some value';
    }

    But also have a follow-up question:  In the DataPower documents for GatewayScript programming Gateway programming model and GatewayScript - IBM Documentation it states that ECMAScript and CommonJS are supported specifications.   Is there a preferred method for creating reusable GatewayScript modules?  ECMAScript or CommonJS?

    My code example was CommonJS.


    ------------------------------
    Steve Ruscik
    ------------------------------



  • 3.  RE: GatewayScript common/shared utility libraries on IBM API Connect v10
    Best Answer

    Posted Wed June 22, 2022 04:15 PM

    Hi Steve,
    CommonJS.  Effectively GatewayScript is ECMAScript that has built-in CommonJS modules for accessing DataPower specific features like context and service variables, protocol headers, urlopen, etc.  You can add your own modules with CommonJS as you discovered.

    Best Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------