Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info
titleImportant

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

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

...

Code Block
languagejs
titlestart.js
app.get("/tsmodule2", profoundjs.express("pjstest/tsmodule2.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.

Image Added


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

Code Block
languagejs
titlestarttsmodule3.jsts
declare var pjs;
declare var productsp;
declare var pds;

export class tsmodule3 {
    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;
        }



        // Using the pjs.require API allows for this file to be eligible for hotReloading
        let tsclass2: any = pjs.require("tsclass2.ts");
        pds = tsclass2.getRecord(pid);


        res.status(200).json(pjs.toObject(pds));
    }
}

...

Code Block
languagejs
titlestarttsmodule4.jsts
import { ts1, ts2, ts3 } from "anotherPackage";


export class tsmodule4 {
    constructor(request: any, response: any) {

		let t1:any = ts1.myFunc(request);
        if (ts1.isValid) {
			response.status(200).json(t1.result);
			return;
		}


        if (!ts1.isValid) {
			let t2:any = ts2.myFunc(request);
	        response.status(200).json(t2.result);
			return;
        }


        response.status(500).send("Unknown error occurred");
    }
}

...