wa-chat-server
Version:
Watson Assistant powered chat server
78 lines (77 loc) • 3.71 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConversationServiceCloudFunctionCall = void 0;
const https_1 = __importDefault(require("https"));
const url_1 = __importDefault(require("url"));
const ConversationService_1 = require("./ConversationService");
class ConversationServiceCloudFunctionCall extends ConversationService_1.ConversationService {
constructor(config) {
super(Object.assign({ requestValidation: true, responseValidation: true }, config));
this.config = config;
this.config = Object.assign({ endpoint: '' }, config);
}
decodeUrl(urlToDecode) {
const urlObj = url_1.default.parse(urlToDecode);
const { protocol, hostname, port, pathname: path } = urlObj;
return { protocol, hostname, port, path };
}
/**
* This method enables to transform the request before it is passed to the conversation service
*/
transformRequest(request) {
return request;
}
/**
* This method enables to transform the response from the conversation service before
* it is mapped to the Watson Assistant context
*/
transformResponse(response) {
return response;
}
getResponse(request) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const data = JSON.stringify(this.transformRequest(request)).replace(/[\u007f-\uffff]/g, c => `\\u${c
.charCodeAt(0)
.toString(16)
.slice(-4)}`);
const reqOptions = Object.assign(Object.assign({}, this.decodeUrl(this.config.endpoint)), { headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
} });
this.logger.info(`${this.constructor.name} - Sending a POST request; options, data:`, reqOptions, data);
const req = https_1.default
.request(reqOptions, res => {
let result = '';
res.on('data', (chunk) => {
result += chunk;
});
res.on('end', () => {
const response = JSON.parse(result);
this.logger.info(`${this.constructor.name} - Response received:`, response);
resolve(this.transformResponse(response));
});
})
.on('error', err => {
this.logger.info(`${this.constructor.name} - Call failed:`, err);
reject(err);
});
req.write(data);
req.end();
});
});
}
}
exports.ConversationServiceCloudFunctionCall = ConversationServiceCloudFunctionCall;