zcatalyst-integ-cliq
Version:
Node.js SDK for integrating Zoho Catalyst with Zoho Cliq
132 lines (131 loc) • 5.26 kB
JavaScript
import CliqApiService from './cliq-api/cliq-api-service.js';
import { CliqConnector } from './cliq-api/cliq-connector.js';
import BotHandler from './cliq-objects/handler-objects/bot-handler.js';
import CommandHandler from './cliq-objects/handler-objects/command-handler.js';
import FunctionHandler from './cliq-objects/handler-objects/function-handler.js';
import ExtensionHandler from './cliq-objects/handler-objects/extension-handler.js';
import LinkPreviewHandler from './cliq-objects/handler-objects/link-preview-handler.js';
import MessageActionHandler from './cliq-objects/handler-objects/message-action-handler.js';
import WidgetHandler from './cliq-objects/handler-objects/widget-handler.js';
import IntegResponse from './cliq-objects/response-objects/integ-response.js';
import { CatalystError } from './error.js';
import { Handler } from './handler.js';
import { getConfigJson, preProcess } from './handler-util.js';
import { isEmpty, sendRequest } from './util.js';
export const IntegrationResponse = new IntegResponse();
// handlers
export function bot() {
return new BotHandler();
}
export function command() {
return new CommandHandler();
}
export function CliqFunction() {
return new FunctionHandler();
}
export function extensionHandler() {
return new ExtensionHandler();
}
export function linkPreviewHandler() {
return new LinkPreviewHandler();
}
export function messageAction() {
return new MessageActionHandler();
}
export function widget() {
return new WidgetHandler();
}
// API-service
export function connector(path, req) {
return new CliqConnector(path, req);
}
export function cliqApiService(connector) {
return new CliqApiService(connector);
}
//handler execution
/**
* Executes the appropriate handler for the request
* @param integRequest Integration request. The request object obtained inside the default export function in the entry point file
* @param args Passthrough arguments that will be passed to the handlers during execution
* @returns A promise that resolves to an Integration response
*
* @example
* ```js
* // executeHandler function.
* import Cliq from 'zcatalyst-integ-cliq'; // import Cliq SDK
*
* // default export function
* module.exports = async (request, response) => {
* try {
* // call the executeHandler function with Integration request object
* const handlerResponse = await Cliq.execute(request);
*
* // end response with handlerResponse
* response.end(handlerResponse);
* } catch (err) {
* console.log('Error while executing handler. ', err);
* throw err;
* }
* };
*
* ```
*
* @example
* ```js
* // executeHandler function with Catalyst App (passthrough argument).
* import Cliq from 'zcatalyst-integ-cliq'; // import Cliq SDK
* import Catalyst from 'zcatalyst-sdk-node'; // import Catalyst SDK
*
* // default export function
* module.exports = async (request, response) => {
* const app = Catalyst.initialize(request); // initialize catalyst app with Integration Request
* try {
* // call the executeHandler function with Integration request object and Catalyst App object(passthrough)
* const handlerResponse = await Cliq.execute(request, app);
*
* // end response with handlerResponse
* response.end(handlerResponse);
* } catch (err) {
* console.log('Error while executing handler. ', err);
* throw err;
* }
* };
*
* ```
*/
export async function execute(integRequest, ...args) {
const config = await getConfigJson();
if (integRequest.handler.type === undefined) {
throw new CatalystError('Unknown request body', 2);
}
const handlerType = integRequest.handler.type; // message_handler
const componentHandlerName = integRequest.type !== undefined ? integRequest.type + '_handler' : handlerType;
const componentType = integRequest.type !== undefined ? integRequest.type : handlerType; // bot
const handlerFile = config.handlers[componentHandlerName];
if (handlerFile === undefined) {
throw new CatalystError('Handler file missing', 2);
}
if (isEmpty(Handler.handler[componentType])) {
await import(handlerFile);
}
const cliqHandler = Handler.handler[componentType][handlerType];
const handlerFunction = cliqHandler.fun;
const response = cliqHandler.res;
if (handlerFunction === undefined) {
throw new CatalystError('No Handler function found for: ' + handlerType);
}
if (typeof handlerFunction !== 'function') {
throw new CatalystError('Not a function', 2);
}
const params = preProcess(integRequest);
const handlerResponse = await handlerFunction(params, new response(), ...args); // invoking the handler function
IntegrationResponse.responseBody = handlerResponse;
IntegrationResponse.contentType = 'application/json';
if (Date.now() - integRequest.timestamp >= 15 * 1000 &&
integRequest.response_url !== undefined) {
await sendRequest(integRequest.response_url, 'POST', handlerResponse).catch((err) => {
throw new CatalystError('Unable to send timeout request', 1, err);
});
}
return IntegrationResponse;
}