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;

    • Announcements

    • Important Announcements in Support for Catalyst Features

      Hello Catalyst Users, This announcement is to bring to your notice some of the recent updates in our support for various Catalyst components, to ensure that your existing Catalyst applications function properly. You can continue to build robust applications
    • Introducing Catalyst 2.0, the simplest cloud-based pro-code development platform.

      Dear Catalyst Community, We are beyond thrilled to present you Catalyst 2.0—the new-age, pro-code development platform that redefines the way you build, deploy, and scale apps. Catalyst 2.0 is not a mere upgrade, but a significant leap into the future
    • Set budgets to optionally disable your Catalyst production environment

      Hello all,    We are pleased to inform you that you can now configure the automatic disabling of a project's production environment upon reaching a specified budget's threshold. Budget alerts in Catalyst facilitate setting usage limits in amounts or Catalyst
    • #CatalystServerless Hackathon 2022

      We are pleased to announce #CatalystServerless Hackathon with prizes worth $5000 up for grabs. Create a working prototype aimed to solve a critical business issue using Catalyst and stand a chance to win exciting prizes. The Hackathon will happen in 3
    • Share your interest to participate in the #CatalystServerless Blogathon

      Hi, we are working on announcing the next edition of the #CatalystServerless Blogathon. Please fill up this form to stay posted on all updates related to the Blogathon. https://zfrmz.com/JN9ChxPU9JQpPgB1vRp8

      Catalyst Community