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
2. Navigate inside your function directory (functions/{your function name}) and then run the command
- npm install typescript --save-dev
- 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 :
- "scripts": {
- "init":"./node_modules/.bin/tsc --init",
- "build": "./node_modules/.bin/tsc",
- "watch": "./node_modules/.bin/tsc --watch"
- }
For Windows :
- "scripts": {
- "init":"node_modules\\.bin\\tsc --init",
- "build": "node_modules\\.bin\\tsc",
- "watch": "node_modules\\.bin\\tsc --watch"
- }
6. Run the below command to initialise the typescript project
7. Modify the below fields in tsconfig.json
- "rootDir": "./src",
- "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
- "execution": {
- "main": "build/index.js"
- }
9. Include the below script inside functions key in catalyst.json file present in your root directory
For MacOS & Linux :
- "scripts": {
- "{your function name}": "cd {your function name} && rm -rf build && npm run build",
- "preserve": "catalyst run functions:{your function name}",
- "predeploy": "catalyst run functions:{your function name}"
- }
For Windows :
- "scripts": {
- "{your function name}": "cd {your function name} && del build\\* && npm run build",
- "preserve": "catalyst run functions:{your function name}",
- "predeploy": "catalyst run functions:{your function name}"
- }
10. Navigate to your (functions/{your function name}) directory in the terminal and run the command
11.Open another terminal window and navigate to your project directory in the terminal and run the command in order to serve your function
- catalyst serve
You can run catalyst deploy in order to deploy your application to the development environment.
// sample index.ts code snippet for reference
- "use strict";
- import catalyst from "zcatalyst-sdk-node";
- import express, { Express, Request, Response } from "express";
- const app: Express = express();
- app.use((req, res, next: any) => {
- console.log("Req", req.method);
- res.header("Access-Control-Allow-Origin", "*");
- next();
- });
- app.post("/cache", async (req: Request, res: Response) => {
- try {
- const catalystApp = catalyst.initialize(req as Record<string, any>);
- //Insert Cache by passing the key-value pair
- let cache = catalystApp.cache();
- let segment = cache.segment();
- let cachePromise = segment.put("Name", "Linda McCartney");
- cachePromise.then((entity) => {
- console.log(entity);
- res.status(200).send(entity);
- });
- } catch (err) {
- res.status(500).send(err);
- }
- });
- module.exports = app;