Server-side Typescript

 

You can write callable Profound.js modules using typescript with just a couple requirements.

  • Implement your logic within the class's constructor

    • Callable modules are just a single function call

  • Define global variables outside of the class

    • Within Profound.js, you strongly define variables with the pjs.define() API.

    • This is needed because typescript compile requires all variables to be defined

  • When calling a typescript module, you must include the ".ts" file extension on the pjs.call() API.

 

Here is an example of a javascript module (jsmodule.js) calling a typescript module (tsmodule1.ts):

jsmodule.js
function jsmodule() { pjs.define("field1", { type: 'char', length: 25 }); pjs.define("field2", { type: 'integer', length: 10 }); pjs.define("field3", { type: 'timestamp'}); // Call a Typescript module and get back values pjs.call("tsmodule1.ts", field1, field2, field3); // See the results that were returned console.log("field1:", field1); console.log("field2:", field2); console.log("field3:", field3.toISOString()); } exports.run = jsmodule;

 

Here is that typescript module

tsmodule1.ts
declare var pjs; export class tsmodule1 { constructor(p1:any, p2:any, p3:any) { pjs.define("p1", { type: 'char', length: 25, refParm: p1 }); pjs.define("p2", { type: 'integer', length: 10, refParm: p2 }); pjs.define("p3", { type: 'timestamp', refParm: p3 }); p1 = "Here is some text"; p2 = p1.length; p3 = new Date(); } }

 

Important

1) Notice that variable pjs is declared outside of the class.

2) Notice that the logic is implemented within the class constructor

 

Here is an example of a typescript module used as a web service:

tsmodule2.ts
declare var pjs; declare var productsp; declare var pds; export class tsmodule2 { constructor(request: any, res: any) { pjs.defineTable("productsp", "pjstest/productsp", { read: true, keyed: true }); pjs.define("pds", { type: 'data structure', likeRec: 'products' }); var pid = request.query.pid; if (pid) pid = Number(pid.trim()); if (!pid || isNaN(pid)) { res.send(`Query parameter pid is required to be a number. Value sent was [${request.query.pid}]`); return; } productsp.getRecord([pid], false, null, pjs.ds("pds")); res.status(200).json(pjs.toObject(pds)); } }

 

To register this as a webservice in Profound.js, open your start.js and add the standard line, just remember to add the .ts file extension.

start.js

To register this as a webservice in Profound.js Spaces, right click on that file and choose properties, and then setup the routing path.

 

Here is an example of a callable typescript module calling some functions of a typescript class (that does not use any pjs APIs)

tsmodule3.ts

 

Here is an example of a callable typescript module calling some functions, or accessing properties of a typescript class, such as 3rd party files imports,

tsmodule4.ts