Instructions to configure Typescript in Catalyst NodeJS advancedio function

Instructions to configure Typescript in Catalyst NodeJS advancedio function

Prerequisites: Catalyst CLI should be installed and a Catalyst project should be initialized in a folder in your local machine.
Then you can follow the below steps:
1. Create a new advanced function by running the command from your project directory
  1. catalyst functions:add
2. Navigate inside your function directory (functions/{your function name}) and then run the command 
  1. npm install typescript --save-dev  
  2. npm i --save-dev @types/express  
3. Create a folder named src and create an index.ts file inside it.
4. You can code your logic in index.ts (you can also refer the code snippet at the end)
5. Include the below script in package.json before “dependencies” key.

For MacOS & Linux :
  1. "scripts": {
  2. "init":"./node_modules/.bin/tsc --init",
  3. "build": "./node_modules/.bin/tsc",
  4. "watch": "./node_modules/.bin/tsc --watch"
  5. }
For Windows :
  1. "scripts": {
  2. "init":"node_modules\\.bin\\tsc --init",
  3. "build": "node_modules\\.bin\\tsc",
  4. "watch": "node_modules\\.bin\\tsc --watch"
  5. }
6. Run the below command to initialise the typescript project
  1. npm run init
7. Modify the below fields in tsconfig.json
  1. "rootDir": "./src", 
  2. "outDir": "./build"  
Add your remaining configurations
8. Create a folder named build inside functions/{your_function_name} directory and modify the below code in your catalyst-config.json
  1. "execution": {
  2. "main": "build/index.js"
  3. }
9. Include the below script inside functions key in catalyst.json file present in your root directory

For MacOS & Linux :
  1. "scripts": {
  2. "{your function name}": "cd {your function name} && rm -rf build && npm run build",
  3. "preserve": "catalyst run functions:{your function name}",
  4. "predeploy": "catalyst run functions:{your function name}"
  5. }
For Windows :
  1. "scripts": {
  2. "{your function name}": "cd {your function name} && del build\\* && npm run build",
  3. "preserve": "catalyst run functions:{your function name}",
  4. "predeploy": "catalyst run functions:{your function name}"
  5. }
10. Navigate to your (functions/{your function name}) directory in the terminal and run the command
  1. npm run watch
11.Open another terminal window and navigate to your project directory in the terminal and run the command in order to serve your function
  1. catalyst serve 
You can run catalyst deploy in order to deploy your application to the development environment.

// sample index.ts code snippet for reference
  1. "use strict";
  2. import catalyst from "zcatalyst-sdk-node";
  3. import express, { Express, Request, Response } from "express";
  4. const app: Express = express();
  5. app.use((req, res, next: any) => {
  6.   console.log("Req", req.method);
  7.   res.header("Access-Control-Allow-Origin", "*");
  8.   next();
  9. });
  10. app.post("/cache", async (req: Request, res: Response) => {
  11.   try {
  12.     const catalystApp = catalyst.initialize(req as Record<string, any>);
  13.     //Insert Cache by passing the key-value pair
  14.     let cache = catalystApp.cache();
  15.     let segment = cache.segment();
  16.     let cachePromise = segment.put("Name", "Linda McCartney");
  17.     cachePromise.then((entity) => {
  18.       console.log(entity);
  19.       res.status(200).send(entity);
  20.     });
  21.   } catch (err) {
  22.     res.status(500).send(err);
  23.   }
  24. });
  25. module.exports = app;


      Catalyst Community