From 1318328fe76524a8d728e14832df17e7fcf47a7e Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 3 Mar 2020 07:43:30 +0300 Subject: [PATCH] api page coverSrc support; filter tags response by field --- modules/hudozka-api/src/controllers/page.ts | 10 +++++++-- modules/hudozka-api/src/core/Image.ts | 23 ++++++++++++++------- modules/hudozka-api/src/factory/page.ts | 4 ++++ modules/hudozka-api/src/lib/array.ts | 15 +++++++++++++- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/modules/hudozka-api/src/controllers/page.ts b/modules/hudozka-api/src/controllers/page.ts index 7f3296eb..2ad77df4 100644 --- a/modules/hudozka-api/src/controllers/page.ts +++ b/modules/hudozka-api/src/controllers/page.ts @@ -1,8 +1,10 @@ +import { pick } from 'lodash' import { Request, Response } from 'express' import Page from '../core/Page' -import { dropTrailingSlash, getPathWaterfall, readQueryArray, readQueryInt, readQueryString } from '../lib/url' +import { dropTrailingSlash, readQueryArray, readQueryInt, readQueryString } from '../lib/url' import { encodePage } from '../factory/page' import * as breadcrumb from './breadcrumb' +import { isArrayOk } from '../lib/array' function getSort(sortBy: string) { if (!sortBy) { @@ -64,6 +66,7 @@ export async function getByTags(req: Request, res: Response) { const limit = readQueryInt(req, 'limit') const sortBy = readQueryString(req, 'sortBy') const sort = getSort(sortBy) + const fields = readQueryArray(req, 'field') const tags = readQueryArray(req, 'tag') if (tags.length === 0) { @@ -80,7 +83,10 @@ export async function getByTags(req: Request, res: Response) { if (models) { const pages = await Promise.all(models.map(resolveItem)) - const items = pages.map(encodePage) + const encodedItems = pages.map(encodePage) + const items = isArrayOk(fields) + ? encodedItems.map(x => pick(x, fields)) + : encodedItems res.json({ items, diff --git a/modules/hudozka-api/src/core/Image.ts b/modules/hudozka-api/src/core/Image.ts index 568d7edc..49ee7ef1 100644 --- a/modules/hudozka-api/src/core/Image.ts +++ b/modules/hudozka-api/src/core/Image.ts @@ -36,9 +36,9 @@ export default class Image { const artifacts = data.data return new Image({ ...data, artifacts }) } - public hash: any - public file: any - public artifacts: any + public hash: string + public file: string + public artifacts: Map constructor({ hash, file, artifacts }) { this.hash = hash @@ -79,13 +79,22 @@ export default class Image { set: srcset, } } + + getSrc(): string | null { + const a = this.getArtifact(ImageArtifactType.ORIGINAL) + if (!a) { + return null + } + + return a.url + } } class ImageArtifact { - public url: any - public size: any - public width: any - public height: any + public url: string + public size: string + public width: number + public height: number constructor({ url, size, width, height }) { this.url = url diff --git a/modules/hudozka-api/src/factory/page.ts b/modules/hudozka-api/src/factory/page.ts index 231b5916..bc2cad20 100644 --- a/modules/hudozka-api/src/factory/page.ts +++ b/modules/hudozka-api/src/factory/page.ts @@ -20,6 +20,7 @@ type PageDto = { tags: Tag[] featured: boolean breadcrumb: Breadcrumb + coverSrc?: string } export function encodePage(page: Page): PageDto { @@ -27,6 +28,8 @@ export function encodePage(page: Page): PageDto { const date = page.date ? page.date.toString() : null const breadcrumb = page.getBreadcrumb() + const coverSrc = page.preview ? page.preview.getSrc() : null + return { id: page.id, title: page.title, @@ -39,5 +42,6 @@ export function encodePage(page: Page): PageDto { preview, date, breadcrumb, + coverSrc, } } diff --git a/modules/hudozka-api/src/lib/array.ts b/modules/hudozka-api/src/lib/array.ts index 294db4eb..cacae12b 100644 --- a/modules/hudozka-api/src/lib/array.ts +++ b/modules/hudozka-api/src/lib/array.ts @@ -1,4 +1,4 @@ -export function unique(items: T[], key: (item: T) => string): T[]{ +export function unique(items: T[], key: (item: T) => string): T[] { const map = new Map(items.map(x => [key(x), x])) return [...map.values()] @@ -16,3 +16,16 @@ export function ensureArray(value: T | T[]): T[] { return [value] } +/** + * + * Check if array has one item at least + * + * @param items + */ +export function isArrayOk(items: T[]): boolean { + if (!Array.isArray(items)) { + return false + } + + return items.length > 0 +}