api page coverSrc support; filter tags response by field

This commit is contained in:
tmshv committed 2020-03-03 07:43:30 +03:00
1 parent 11ff066e6c
commit 1318328fe7
4 files changed
+42 -10

No files matched your search

+8 -2
View File
@@ -1,8 +1,10 @@
import { pick } from 'lodash'
import { Request, Response } from 'express' import { Request, Response } from 'express'
import Page from '../core/Page' 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 { encodePage } from '../factory/page'
import * as breadcrumb from './breadcrumb' import * as breadcrumb from './breadcrumb'
import { isArrayOk } from '../lib/array'
function getSort(sortBy: string) { function getSort(sortBy: string) {
if (!sortBy) { if (!sortBy) {
@@ -64,6 +66,7 @@ export async function getByTags(req: Request, res: Response) {
const limit = readQueryInt(req, 'limit') const limit = readQueryInt(req, 'limit')
const sortBy = readQueryString(req, 'sortBy') const sortBy = readQueryString(req, 'sortBy')
const sort = getSort(sortBy) const sort = getSort(sortBy)
const fields = readQueryArray(req, 'field')
const tags = readQueryArray(req, 'tag') const tags = readQueryArray(req, 'tag')
if (tags.length === 0) { if (tags.length === 0) {
@@ -80,7 +83,10 @@ export async function getByTags(req: Request, res: Response) {
if (models) { if (models) {
const pages = await Promise.all(models.map(resolveItem)) 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({ res.json({
items, items,
+16 -7
View File
@@ -36,9 +36,9 @@ export default class Image {
const artifacts = data.data const artifacts = data.data
return new Image({ ...data, artifacts }) return new Image({ ...data, artifacts })
} }
public hash: any public hash: string
public file: any public file: string
public artifacts: any public artifacts: Map<string, ImageArtifact>
constructor({ hash, file, artifacts }) { constructor({ hash, file, artifacts }) {
this.hash = hash this.hash = hash
@@ -79,13 +79,22 @@ export default class Image {
set: srcset, set: srcset,
} }
} }
getSrc(): string | null {
const a = this.getArtifact(ImageArtifactType.ORIGINAL)
if (!a) {
return null
}
return a.url
}
} }
class ImageArtifact { class ImageArtifact {
public url: any public url: string
public size: any public size: string
public width: any public width: number
public height: any public height: number
constructor({ url, size, width, height }) { constructor({ url, size, width, height }) {
this.url = url this.url = url
+4
View File
@@ -20,6 +20,7 @@ type PageDto = {
tags: Tag[] tags: Tag[]
featured: boolean featured: boolean
breadcrumb: Breadcrumb breadcrumb: Breadcrumb
coverSrc?: string
} }
export function encodePage(page: Page): PageDto { 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 date = page.date ? page.date.toString() : null
const breadcrumb = page.getBreadcrumb() const breadcrumb = page.getBreadcrumb()
const coverSrc = page.preview ? page.preview.getSrc() : null
return { return {
id: page.id, id: page.id,
title: page.title, title: page.title,
@@ -39,5 +42,6 @@ export function encodePage(page: Page): PageDto {
preview, preview,
date, date,
breadcrumb, breadcrumb,
coverSrc,
} }
} }
+14 -1
View File
@@ -1,4 +1,4 @@
export function unique<T>(items: T[], key: (item: T) => string): T[]{ export function unique<T>(items: T[], key: (item: T) => string): T[] {
const map = new Map(items.map(x => [key(x), x])) const map = new Map(items.map(x => [key(x), x]))
return [...map.values()] return [...map.values()]
@@ -16,3 +16,16 @@ export function ensureArray<T>(value: T | T[]): T[] {
return [value] return [value]
} }
/**
*
* Check if array has one item at least
*
* @param items
*/
export function isArrayOk<T>(items: T[]): boolean {
if (!Array.isArray(items)) {
return false
}
return items.length > 0
}