UNPKG

bottender

Version:

A framework for building conversational user interfaces.

41 lines 1.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const url_1 = __importDefault(require("url")); const object_fromentries_1 = __importDefault(require("object.fromentries")); const path_to_regexp_1 = require("path-to-regexp"); const getChannelBots_1 = __importDefault(require("./getChannelBots")); function getChannelBotAndRequestContext(req) { const hostname = req.hostname || req.headers.host; const protocol = req.protocol || 'https'; const requestUrl = `${protocol}://${hostname}${req.url}`; const { pathname, searchParams } = new url_1.default.URL(requestUrl); const query = object_fromentries_1.default(searchParams.entries()); const channelBots = getChannelBots_1.default(); for (let i = 0; i < channelBots.length; i++) { const channelBot = channelBots[i]; const matchPath = path_to_regexp_1.match(channelBot.webhookPath); const matchResult = matchPath(pathname); if (matchResult) { return { requestContext: { id: req.id, method: req.method, path: pathname, query, headers: req.headers, rawBody: req.rawBody, body: req.body, params: matchResult.params, url: requestUrl, }, channelBot, }; } } return undefined; } exports.default = getChannelBotAndRequestContext; //# sourceMappingURL=getChannelBotAndRequestContext.js.map