UNPKG

@kth/cortina-block

Version:

Node.js module for fetching Cortina blocks and optionally cache using Redis.

107 lines (106 loc) 4.66 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cortina = cortina; exports.cortinaMiddleware = cortinaMiddleware; const log_1 = __importDefault(require("@kth/log")); const kth_node_redis_1 = __importDefault(require("kth-node-redis")); const redis_utils_1 = require("./redis-utils"); const fetch_blocks_1 = require("./fetch-blocks"); const config_1 = require("./config"); __exportStar(require("./types"), exports); // Gets HTML blocks from Cortina using promises. function cortina(options) { const { blockApiUrl, language, shouldSkipCookieScripts, blocksConfig, redisClient, redisKey } = options; const fullBlocksConfig = { ...config_1.defaultBlocksConfig, ...blocksConfig }; if (shouldSkipCookieScripts) { fullBlocksConfig.klaroConfig = config_1.devBlocks.klaroConfig; fullBlocksConfig.matomoAnalytics = config_1.devBlocks.matomoAnalytics; } if (!blockApiUrl) { throw new Error('Block api url must be specified.'); } if (!redisClient) { return (0, fetch_blocks_1.fetchAllBlocks)(fullBlocksConfig, blockApiUrl, language); } const { defaultKey, redisExpire } = config_1.redisItemSettings; const finalRedisKey = redisKey || defaultKey; // Try to get from Redis otherwise get from web service then cache result // in Redis using redisKey. If Redis connection fails, call API // directly and don't cache results. return (0, redis_utils_1.getRedisItem)(redisClient, finalRedisKey, language) .then(storedBlocks => { if (storedBlocks) { return storedBlocks; } return (0, fetch_blocks_1.fetchAllBlocks)(fullBlocksConfig, blockApiUrl, language).then(cortinaBlocks => (0, redis_utils_1.setRedisItem)(redisClient, finalRedisKey, redisExpire, language, cortinaBlocks)); }) .catch(err => { log_1.default.error('Redis failed:', err.message, err.code); return (0, fetch_blocks_1.fetchAllBlocks)(fullBlocksConfig, blockApiUrl, language); }); } const getLanguage = (res, supportedLanguages) => { let detectedLanguage = res.locals?.locale?.language ?? 'sv'; const finalSupportedLanguages = supportedLanguages || config_1.defaultSupportedLanguages; if (!finalSupportedLanguages.includes(detectedLanguage)) { return finalSupportedLanguages[0]; } return detectedLanguage; }; function cortinaMiddleware(config) { return async (req, res, next) => { // don't load cortina blocks for static content, or if query parameter 'nocortinablocks' is present if (/^\/static\/.*/.test(req.url) || req.query.nocortinablocks !== undefined) { next(); return; } const { redisConfig, redisKey, skipCookieScriptsInDev = true, supportedLanguages } = config; let redisClient; if (redisConfig) { redisClient = await (0, kth_node_redis_1.default)('cortina', redisConfig); } const language = getLanguage(res, supportedLanguages); let shouldSkipCookieScripts = false; if (req.hostname.includes('localhost') && skipCookieScriptsInDev) { shouldSkipCookieScripts = true; } const { blockApiUrl, blocksConfig } = config; return cortina({ blockApiUrl, language, shouldSkipCookieScripts, blocksConfig, redisClient, redisKey, }) .then(blocks => { // @ts-ignore res.locals.blocks = blocks; log_1.default.debug('Cortina blocks loaded.'); next(); }) .catch(err => { log_1.default.error('Cortina failed to load blocks: ' + err.message); // @ts-ignore res.locals.blocks = {}; next(); }); }; }