UNPKG

burdy

Version:

Open Source Headless Platform

209 lines (208 loc) 11.4 kB
"use strict"; 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 }); const express_1 = __importDefault(require("express")); const content_middleware_1 = __importDefault(require("../middleware/content.middleware")); const async_middleware_1 = __importDefault(require("../middleware/async.middleware")); const orm_helpers_1 = require("../common/orm-helpers"); const post_model_1 = __importDefault(require("../models/post.model")); const utility_1 = require("../../admin/helpers/utility"); const typeorm_1 = require("typeorm"); const post_utility_1 = require("../common/post.utility"); const mappers_1 = require("../common/mappers"); const tag_model_1 = __importDefault(require("../models/tag.model")); const app = (0, express_1.default)(); app.get('/search/posts', (0, content_middleware_1.default)({ alwaysAuthorize: true }), (0, async_middleware_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w; const postRepository = (0, orm_helpers_1.getEnhancedRepository)(post_model_1.default); const qb = postRepository.createQueryBuilder('post'); const contentTypeName = (_a = req === null || req === void 0 ? void 0 : req.query) === null || _a === void 0 ? void 0 : _a.contentTypeName; const type = (_b = req === null || req === void 0 ? void 0 : req.query) === null || _b === void 0 ? void 0 : _b.type; const search = (_c = req === null || req === void 0 ? void 0 : req.query) === null || _c === void 0 ? void 0 : _c.search; const parent = (_d = req === null || req === void 0 ? void 0 : req.query) === null || _d === void 0 ? void 0 : _d.parent; const onlyOrphans = (_e = req === null || req === void 0 ? void 0 : req.query) === null || _e === void 0 ? void 0 : _e.onlyOrphans; const slugPath = (_f = req === null || req === void 0 ? void 0 : req.query) === null || _f === void 0 ? void 0 : _f.slugPath; const tags = (_g = req === null || req === void 0 ? void 0 : req.query) === null || _g === void 0 ? void 0 : _g.tags; const compile = (_h = req === null || req === void 0 ? void 0 : req.query) === null || _h === void 0 ? void 0 : _h.compile; let relationsDepth; if (((_j = req === null || req === void 0 ? void 0 : req.query) === null || _j === void 0 ? void 0 : _j.relationsDepth) && !Number.isNaN(Number((_k = req === null || req === void 0 ? void 0 : req.query) === null || _k === void 0 ? void 0 : _k.relationsDepth))) { relationsDepth = Number((_l = req === null || req === void 0 ? void 0 : req.query) === null || _l === void 0 ? void 0 : _l.relationsDepth); } let limit = 100; if (((_m = req === null || req === void 0 ? void 0 : req.query) === null || _m === void 0 ? void 0 : _m.limit) && !Number.isNaN(Number((_o = req === null || req === void 0 ? void 0 : req.query) === null || _o === void 0 ? void 0 : _o.limit))) { limit = Number((_p = req === null || req === void 0 ? void 0 : req.query) === null || _p === void 0 ? void 0 : _p.limit); } let page = 1; if (((_q = req === null || req === void 0 ? void 0 : req.query) === null || _q === void 0 ? void 0 : _q.page) && !Number.isNaN(Number((_r = req === null || req === void 0 ? void 0 : req.query) === null || _r === void 0 ? void 0 : _r.page))) { page = Number((_s = req === null || req === void 0 ? void 0 : req.query) === null || _s === void 0 ? void 0 : _s.page); } const orderBy = (_t = req === null || req === void 0 ? void 0 : req.query) === null || _t === void 0 ? void 0 : _t.orderBy; const order = (_u = req === null || req === void 0 ? void 0 : req.query) === null || _u === void 0 ? void 0 : _u.order; const expand = (((_v = req === null || req === void 0 ? void 0 : req.query) === null || _v === void 0 ? void 0 : _v.expand) || '').split(','); if (contentTypeName || expand.find((val) => val === 'contentType')) { qb.leftJoinAndSelect('post.contentType', 'contentType'); } if ((0, utility_1.isTrue)(compile) || expand.find((val) => val === 'meta')) { qb.leftJoinAndSelect('post.meta', 'meta'); } if (expand.find((val) => val === 'author')) { qb.leftJoinAndSelect('post.author', 'author'); } if (tags || expand.find((val) => val === 'tags')) { qb.leftJoinAndSelect('post.tags', 'tags'); } if (parent || expand.find((val) => val === 'parent')) { qb.leftJoinAndSelect('post.parent', 'parent'); } qb.where('post.type IN (:...types)', { types: type ? type.split(',') : ['hierarchical_post', 'post', 'page'], }); if (slugPath) { qb.andWhere('post.slugPath IN (:...slugPaths)', { slugPaths: slugPath.split(','), }); } if (contentTypeName) { qb.andWhere('contentType.name IN (:...contentTypeNames)', { contentTypeNames: contentTypeName.split(','), }); } if (tags) { qb.andWhere('tags.slugPath IN (:...tagsSlugPaths)', { tagsSlugPaths: tags.split(','), }); } if ((0, utility_1.isTrue)(onlyOrphans)) { if (parent) { qb.andWhere('parent.slugPath = :parentSlugPath', { parentSlugPath: parent, }); } else { qb.andWhere('post.parentId IS NULL'); } } else if (parent) { qb.andWhere('post.slugPath LIKE :parentSlugPath', { parentSlugPath: `${parent.toLowerCase()}/%`, }); } if ((search === null || search === void 0 ? void 0 : search.length) > 0) { qb.andWhere(new typeorm_1.Brackets((subQb) => { subQb .where('LOWER(post.name) LIKE :search', { search: `%${search.toLowerCase()}%`, }) .orWhere('LOWER(post.slugPath) LIKE :search', { search: `%${search.toLowerCase()}%`, }); })); } const draft = (0, utility_1.isTrue)((_w = req === null || req === void 0 ? void 0 : req.query) === null || _w === void 0 ? void 0 : _w.draft); if (!draft) { (0, post_utility_1.publishedQuery)(qb); } const getMany = () => { qb.take(limit).skip((page - 1) * limit); return qb.getMany(); }; qb.addOrderBy(orderBy || 'post.slugPath', order === 'DESC' ? 'DESC' : 'ASC'); const [count, results] = yield Promise.all([qb.getCount(), getMany()]); let posts; if ((0, utility_1.isTrue)(compile)) { posts = yield Promise.all((results || []).map(post => (0, post_utility_1.compilePost)(post, { draft, relationsDepth: relationsDepth || 1 }))); } else { posts = (results || []).map((post) => (0, mappers_1.mapPublicPostWithMeta)(post)); } res.send({ count, results: posts, page, limit, }); }))); app.get('/search/tags', (0, content_middleware_1.default)({ alwaysAuthorize: true }), (0, async_middleware_1.default)((req, res) => __awaiter(void 0, void 0, void 0, function* () { var _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9; const tagRepository = (0, orm_helpers_1.getEnhancedRepository)(tag_model_1.default); const qb = tagRepository.createQueryBuilder('tag'); const search = (_x = req === null || req === void 0 ? void 0 : req.query) === null || _x === void 0 ? void 0 : _x.search; const parent = (_y = req === null || req === void 0 ? void 0 : req.query) === null || _y === void 0 ? void 0 : _y.parent; const onlyOrphans = (_z = req === null || req === void 0 ? void 0 : req.query) === null || _z === void 0 ? void 0 : _z.onlyOrphans; const slugPath = (_0 = req === null || req === void 0 ? void 0 : req.query) === null || _0 === void 0 ? void 0 : _0.slugPath; let limit = 100; if (((_1 = req === null || req === void 0 ? void 0 : req.query) === null || _1 === void 0 ? void 0 : _1.limit) && !Number.isNaN(Number((_2 = req === null || req === void 0 ? void 0 : req.query) === null || _2 === void 0 ? void 0 : _2.limit))) { limit = Number((_3 = req === null || req === void 0 ? void 0 : req.query) === null || _3 === void 0 ? void 0 : _3.limit); } let page = 1; if (((_4 = req === null || req === void 0 ? void 0 : req.query) === null || _4 === void 0 ? void 0 : _4.page) && !Number.isNaN(Number((_5 = req === null || req === void 0 ? void 0 : req.query) === null || _5 === void 0 ? void 0 : _5.page))) { page = Number((_6 = req === null || req === void 0 ? void 0 : req.query) === null || _6 === void 0 ? void 0 : _6.page); } const orderBy = (_7 = req === null || req === void 0 ? void 0 : req.query) === null || _7 === void 0 ? void 0 : _7.orderBy; const order = (_8 = req === null || req === void 0 ? void 0 : req.query) === null || _8 === void 0 ? void 0 : _8.order; const expand = (((_9 = req === null || req === void 0 ? void 0 : req.query) === null || _9 === void 0 ? void 0 : _9.expand) || '').split(','); if (parent || expand.find((val) => val === 'parent')) { qb.leftJoinAndSelect('tag.parent', 'parent'); } if (slugPath) { qb.andWhere('tag.slugPath IN (:...slugPaths)', { slugPaths: slugPath.split(','), }); } if ((0, utility_1.isTrue)(onlyOrphans)) { if (parent) { qb.andWhere('parent.slugPath = :parentSlugPath', { parentSlugPath: parent, }); } else { qb.andWhere('tag.parentId IS NULL'); } } else if (parent) { qb.andWhere('tag.slugPath LIKE :parentSlugPath', { parentSlugPath: `${parent.toLowerCase()}/%`, }); } if ((search === null || search === void 0 ? void 0 : search.length) > 0) { qb.andWhere(new typeorm_1.Brackets((subQb) => { subQb .where('LOWER(tag.name) LIKE :search', { search: `%${search.toLowerCase()}%`, }) .orWhere('LOWER(tag.slugPath) LIKE :search', { search: `%${search.toLowerCase()}%`, }); })); } const getMany = () => { qb.take(limit).skip((page - 1) * limit); return qb.getMany(); }; qb.addOrderBy(orderBy || 'tag.slugPath', order === 'DESC' ? 'DESC' : 'ASC'); const [count, results] = yield Promise.all([ qb.getCount(), getMany() ]); res.send({ count, results: (results || []).map((tag) => (0, mappers_1.mapPublicTag)(tag)), page, limit }); }))); exports.default = app;