mirror of
https://github.com/acid-info/logos-press-engine.git
synced 2025-02-23 22:58:08 +00:00
refactor: LPE types
This commit is contained in:
parent
1035bccc9a
commit
9abacc4f92
@ -49,7 +49,6 @@ export const getStaticProps = async () => {
|
||||
} = await unbodyApi.getHomepagePosts()
|
||||
|
||||
const { data: topics, errors: topicErrors } = await unbodyApi.getTopics()
|
||||
await unbodyApi.getPodcastsInfo()
|
||||
|
||||
return {
|
||||
props: {
|
||||
|
@ -25,14 +25,12 @@ export const ArticleDataType: UnbodyDataTypeConfig<
|
||||
classes: 'article',
|
||||
})
|
||||
|
||||
const blocks = await helpers.dataTypes.transformMany<
|
||||
any,
|
||||
LPE.Article.ContentBlock
|
||||
>(
|
||||
[...textBlock, ...imageBlock],
|
||||
[...(data.blocks || [])].sort((a, b) => a.order - b.order),
|
||||
data,
|
||||
)
|
||||
const blocks =
|
||||
await helpers.dataTypes.transformMany<LPE.Article.ContentBlock>(
|
||||
[...textBlock, ...imageBlock],
|
||||
[...(data.blocks || [])].sort((a, b) => a.order - b.order),
|
||||
data,
|
||||
)
|
||||
|
||||
const readingTime = calcReadingTime(
|
||||
(data.blocks || [])
|
||||
|
@ -43,7 +43,7 @@ export const ArticleSearchResultItemDataType: UnbodyDataTypeConfig<
|
||||
),
|
||||
}
|
||||
|
||||
const document = helpers.dataTypes.transform(
|
||||
const document = await helpers.dataTypes.transform(
|
||||
[helpers.dataTypes.getOne({ key: 'ArticleDocument' })!],
|
||||
'document' in data && data.document?.[0],
|
||||
)
|
||||
|
@ -2,14 +2,14 @@ import { LPE } from '../../../types/lpe.types'
|
||||
import { UnbodyResGoogleDocData } from '../unbody.types'
|
||||
import { UnbodyDataTypeConfig } from './types'
|
||||
|
||||
export const PodcastInfoDataType: UnbodyDataTypeConfig<
|
||||
export const PodcastShowDataType: UnbodyDataTypeConfig<
|
||||
LPE.Article.Data,
|
||||
LPE.Podcast.Info,
|
||||
LPE.Podcast.Show,
|
||||
UnbodyResGoogleDocData
|
||||
> = {
|
||||
key: 'PodcastInfoDocument',
|
||||
key: 'PodcastShowDocument',
|
||||
objectType: 'GoogleDoc',
|
||||
classes: ['podcast', 'document'],
|
||||
classes: ['podcast', 'show', 'document'],
|
||||
|
||||
isMatch: (helpers, data, original) =>
|
||||
original
|
@ -62,7 +62,7 @@ export class UnbodyDataTypes {
|
||||
return dataTypes
|
||||
}
|
||||
|
||||
transform = async <T = any, O = any>(
|
||||
transform = async <O = any, T = any>(
|
||||
pipeline: UnbodyDataTypeConfig[],
|
||||
data: T,
|
||||
root?: any,
|
||||
@ -78,11 +78,11 @@ export class UnbodyDataTypes {
|
||||
return obj as O | Promise<O>
|
||||
}
|
||||
|
||||
transformMany = async <T = any, O = any>(
|
||||
transformMany = async <O = any, T = any>(
|
||||
pipeline: UnbodyDataTypeConfig[],
|
||||
data: T[],
|
||||
root?: any,
|
||||
): Promise<O[]> => {
|
||||
return Promise.all(data.map((d) => this.transform<T, O>(pipeline, d, root)))
|
||||
return Promise.all(data.map((d) => this.transform<O, T>(pipeline, d, root)))
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import { ArticleImageBlockDataType } from './ArticleImageBlock.dataType'
|
||||
import { ArticleSearchResultItemDataType } from './ArticleSearchResultItem.dataType'
|
||||
import { ArticleTextBlockDataType } from './ArticleTextBlock.dataType'
|
||||
import { ImageBlockDataType } from './ImageBlock.dataType'
|
||||
import { PodcastInfoDataType } from './PodcastInfoDocument.dataType'
|
||||
import { PodcastShowDataType } from './PodcastShowDocument.dataType'
|
||||
import { TextBlockDataType } from './TextBlock.dataType'
|
||||
import { UnbodyDataTypes } from './UnbodyDataTypes'
|
||||
|
||||
@ -14,5 +14,5 @@ export const unbodyDataTypes = new UnbodyDataTypes([
|
||||
ArticleTextBlockDataType,
|
||||
ArticleImageBlockDataType,
|
||||
ArticleSearchResultItemDataType,
|
||||
PodcastInfoDataType,
|
||||
PodcastShowDataType,
|
||||
])
|
||||
|
@ -39,7 +39,8 @@ export const UnbodyDataTypeKeys = {
|
||||
ArticleTextBlock: 'ArticleTextBlock',
|
||||
ArticleImageBlock: 'ArticleImageBlock',
|
||||
ArticleSearchResultItem: 'ArticleSearchResultItem',
|
||||
PodcastInfoDocument: 'PodcastInfoDocument',
|
||||
PodcastShowDocument: 'PodcastShowDocument',
|
||||
PodcastEpisodeDocument: 'PodcastEpisodeDocument',
|
||||
} as const
|
||||
|
||||
export type UnbodyDataTypeKey =
|
||||
@ -48,6 +49,8 @@ export type UnbodyDataTypeKey =
|
||||
export const UnbodyDataTypeClasses = {
|
||||
Article: 'article',
|
||||
Podcast: 'podcast',
|
||||
Show: 'show',
|
||||
Episode: 'episode',
|
||||
Document: 'document',
|
||||
Search: 'search',
|
||||
} as const
|
||||
|
@ -25,7 +25,7 @@ const articleDocument = unbodyDataTypes.get({
|
||||
const articleSearchResultItem = unbodyDataTypes.get({
|
||||
classes: ['article', 'search'],
|
||||
})!
|
||||
const podcastInfoDocument = unbodyDataTypes.get({
|
||||
const podcastShowDocument = unbodyDataTypes.get({
|
||||
classes: ['podcast', 'document'],
|
||||
objectType: 'GoogleDoc',
|
||||
})!
|
||||
@ -99,10 +99,14 @@ export class UnbodyService {
|
||||
const docs = data.Get?.GoogleDoc || []
|
||||
const [doc] = docs
|
||||
|
||||
return this.handleResponse(
|
||||
(doc && (await unbodyDataTypes.transform(articleDocument, doc))) ||
|
||||
null,
|
||||
)
|
||||
const result = doc
|
||||
? await unbodyDataTypes.transform<LPE.Article.Data>(
|
||||
articleDocument,
|
||||
doc,
|
||||
)
|
||||
: null
|
||||
|
||||
return this.handleResponse(result)
|
||||
} catch (error) {
|
||||
return this.handleResponse(null, error)
|
||||
}
|
||||
@ -130,8 +134,9 @@ export class UnbodyService {
|
||||
|
||||
return this.handleResponse({
|
||||
featured: (await this.getFeaturedPost())?.data,
|
||||
posts: await Promise.all(
|
||||
docs.map((doc) => unbodyDataTypes.transform(articleDocument, doc)),
|
||||
posts: await unbodyDataTypes.transformMany<LPE.Article.Data>(
|
||||
articleDocument,
|
||||
docs,
|
||||
),
|
||||
})
|
||||
} catch (error) {
|
||||
@ -194,7 +199,10 @@ export class UnbodyService {
|
||||
|
||||
return doc
|
||||
? this.handleResponse(
|
||||
await unbodyDataTypes.transform(articleDocument, doc),
|
||||
await unbodyDataTypes.transform<LPE.Article.Data>(
|
||||
articleDocument,
|
||||
doc,
|
||||
),
|
||||
)
|
||||
: this.handleResponse(null, 'No data')
|
||||
} catch (error) {
|
||||
@ -258,8 +266,9 @@ export class UnbodyService {
|
||||
return this.handleResponse([], 'No data for same authors')
|
||||
|
||||
return this.handleResponse(
|
||||
await Promise.all(
|
||||
docs.map((doc) => unbodyDataTypes.transform(articleDocument, doc)),
|
||||
await unbodyDataTypes.transformMany<LPE.Article.Metadata>(
|
||||
articleDocument,
|
||||
docs,
|
||||
),
|
||||
)
|
||||
} catch (error) {
|
||||
@ -325,18 +334,13 @@ export class UnbodyService {
|
||||
},
|
||||
})
|
||||
|
||||
const blocks = (
|
||||
await Promise.all(
|
||||
[...(ImageBlock || []), ...(TextBlock || [])].map((block) =>
|
||||
unbodyDataTypes.transform<any, SearchResultItem<LPE.Article.Data>>(
|
||||
articleSearchResultItem,
|
||||
block,
|
||||
),
|
||||
),
|
||||
)
|
||||
).sort((a, b) => b.score - a.score)
|
||||
const blocks = await unbodyDataTypes.transformMany<
|
||||
SearchResultItem<LPE.Article.ContentBlock>
|
||||
>(articleSearchResultItem, [...(ImageBlock || []), ...(TextBlock || [])])
|
||||
|
||||
return this.handleResponse(blocks)
|
||||
const result = blocks.sort((a, b) => b.score - a.score)
|
||||
|
||||
return this.handleResponse(result)
|
||||
} catch (error) {
|
||||
return this.handleResponse([], error)
|
||||
}
|
||||
@ -378,11 +382,12 @@ export class UnbodyService {
|
||||
|
||||
if (!data) return this.handleResponse([], 'No data')
|
||||
|
||||
const docs = data.Get.GoogleDoc.map((doc) =>
|
||||
unbodyDataTypes.transform(articleSearchResultItem, doc),
|
||||
const result = await unbodyDataTypes.transformMany<LPE.Article.Data>(
|
||||
articleSearchResultItem,
|
||||
data.Get.GoogleDoc || [],
|
||||
)
|
||||
|
||||
return this.handleResponse(docs)
|
||||
return this.handleResponse(result)
|
||||
} catch (error) {
|
||||
return this.handleResponse(null, error)
|
||||
}
|
||||
@ -405,7 +410,7 @@ export class UnbodyService {
|
||||
}
|
||||
}
|
||||
|
||||
getPodcastsInfo = async (): Promise<ApiResponse<LPE.Podcast.Info[]>> => {
|
||||
getPodcastShows = async (): Promise<ApiResponse<LPE.Podcast.Show[]>> => {
|
||||
try {
|
||||
const { data } = await this.client.query({
|
||||
query: GetArticlePostQueryDocument,
|
||||
@ -419,10 +424,11 @@ export class UnbodyService {
|
||||
})
|
||||
|
||||
const docs = data.Get.GoogleDoc
|
||||
const result = await unbodyDataTypes.transformMany(
|
||||
podcastInfoDocument,
|
||||
const result = await unbodyDataTypes.transformMany<LPE.Podcast.Show>(
|
||||
podcastShowDocument,
|
||||
docs,
|
||||
)
|
||||
|
||||
console.log(JSON.stringify(result))
|
||||
|
||||
return this.handleResponse([])
|
||||
|
@ -17,7 +17,7 @@ export namespace LPE {
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Article {
|
||||
export namespace Post {
|
||||
export type Footnote = {
|
||||
index: number
|
||||
id: string
|
||||
@ -55,28 +55,40 @@ export namespace LPE {
|
||||
export type ContentBlockLabel =
|
||||
(typeof ContentBlockLabels)[keyof typeof ContentBlockLabels]
|
||||
|
||||
export type ContentBlockCommon = {
|
||||
export type ContentBlockCommon<D = any> = {
|
||||
id: string
|
||||
order: number
|
||||
labels: ContentBlockLabel[]
|
||||
document?: Partial<Article.Metadata>
|
||||
document?: D
|
||||
}
|
||||
|
||||
export type TextBlock = ContentBlockCommon & {
|
||||
export type TextBlock<D = any> = ContentBlockCommon<D> & {
|
||||
text: string
|
||||
html: string
|
||||
tagName: string
|
||||
classNames: string[]
|
||||
type: Extract<ContentBlockType, 'text'>
|
||||
footnotes: Article.Footnotes
|
||||
footnotes: Post.Footnotes
|
||||
}
|
||||
|
||||
export type ImageBlock = ContentBlockCommon &
|
||||
export type ImageBlock<D = any> = ContentBlockCommon<D> &
|
||||
Image.Document & {
|
||||
type: Extract<ContentBlockType, 'image'>
|
||||
}
|
||||
|
||||
export type ContentBlock = TextBlock | ImageBlock
|
||||
export type ContentBlock<D = any> = TextBlock<D> | ImageBlock<D>
|
||||
}
|
||||
|
||||
export namespace Article {
|
||||
export type Toc = Post.Toc
|
||||
export type TocItem = Post.TocItem
|
||||
export type TextBlock = Post.TextBlock<Metadata>
|
||||
export type ImageBlock = Post.ImageBlock<Metadata>
|
||||
export type ContentBlock = Post.ContentBlock<Metadata>
|
||||
export type Footnote = Post.Footnote
|
||||
export type Footnotes = Post.Footnotes
|
||||
export const ContentBlockLabels = Post.ContentBlockLabels
|
||||
export type ContentBlockLabel = Post.ContentBlockLabel
|
||||
|
||||
export type Metadata = {
|
||||
id: string
|
||||
@ -92,7 +104,7 @@ export namespace LPE {
|
||||
}
|
||||
|
||||
export type Data = Article.Metadata & {
|
||||
toc: Article.Toc
|
||||
toc: Post.Toc
|
||||
readingTime: number
|
||||
coverImage: Image.Document | null
|
||||
content: Array<Article.ContentBlock>
|
||||
@ -106,28 +118,43 @@ export namespace LPE {
|
||||
}
|
||||
|
||||
export namespace Podcast {
|
||||
export type Info = {
|
||||
export type Show = {
|
||||
id: string
|
||||
slug: string
|
||||
title: string
|
||||
description: string
|
||||
coverImage: Image.Document | null
|
||||
logo: Image.Document
|
||||
hosts: Author.Document[]
|
||||
episodes?: Omit<Podcast.Document, 'show'>[]
|
||||
}
|
||||
|
||||
export type Metadata = {
|
||||
id: string
|
||||
slug: string
|
||||
title: string
|
||||
tags: string[]
|
||||
summary: string
|
||||
publishedAt: string
|
||||
episodeNumber: number
|
||||
}
|
||||
|
||||
export type TranscriptionItem = {
|
||||
text: string
|
||||
start: number
|
||||
end: number
|
||||
speaker: string
|
||||
start?: number
|
||||
end?: number
|
||||
speaker?: string
|
||||
}
|
||||
|
||||
export type EpisodeExtra = {
|
||||
info: Partial<Podcast.Info>
|
||||
export type Content = {
|
||||
coverImage?: Post.ImageBlock
|
||||
transcription: TranscriptionItem[]
|
||||
references?: Post.TextBlock[]
|
||||
credits?: Post.TextBlock[]
|
||||
}
|
||||
|
||||
export type Episode = Article.Data & EpisodeExtra
|
||||
|
||||
export type Document = Info & { episodes: Podcast.Episode[] }
|
||||
export type Document = Metadata &
|
||||
Content & {
|
||||
show: Show
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user