api page coverSrc support; filter tags response by field
This commit is contained in:
1 parent
11ff066e6c
commit
1318328fe7
4 files changed
+42
-10
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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<string, ImageArtifact>
|
||||
|
||||
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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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]))
|
||||
|
||||
return [...map.values()]
|
||||
@@ -16,3 +16,16 @@ export function ensureArray<T>(value: T | T[]): T[] {
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user