mirror of
https://github.com/acid-info/logos-press-engine.git
synced 2025-02-23 06:38:27 +00:00
feat: implement getRecentPosts and getLatestEpisodes endpoints
This commit is contained in:
parent
734c548022
commit
3363b15587
20
src/pages/api/podcasts/[showSlug]/episodes/index.ts
Normal file
20
src/pages/api/podcasts/[showSlug]/episodes/index.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import unbodyApi from '../../../../../services/unbody/unbody.service'
|
||||
import { parseInt } from '../../../../../utils/data.utils'
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<any>,
|
||||
) {
|
||||
const {
|
||||
query: { page = 1, limit = 10, showSlug },
|
||||
} = req
|
||||
|
||||
const response = await unbodyApi.getLatestEpisodes({
|
||||
page: parseInt(page, 1),
|
||||
limit: parseInt(limit, 10),
|
||||
showSlug: Array.isArray(showSlug) ? showSlug[0] : showSlug,
|
||||
})
|
||||
|
||||
res.status(200).json(response)
|
||||
}
|
19
src/pages/api/posts/index.ts
Normal file
19
src/pages/api/posts/index.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import unbodyApi from '../../../services/unbody/unbody.service'
|
||||
import { parseInt } from '../../../utils/data.utils'
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<any>,
|
||||
) {
|
||||
const {
|
||||
query: { page = 1, limit = 10 },
|
||||
} = req
|
||||
|
||||
const response = await unbodyApi.getRecentPosts({
|
||||
page: parseInt(page, 1),
|
||||
limit: parseInt(limit, 10),
|
||||
})
|
||||
|
||||
res.status(200).json(response)
|
||||
}
|
@ -5,6 +5,7 @@ import { useSearchBarContext } from '@/context/searchbar.context'
|
||||
import { PostListLayout } from '@/types/ui.types'
|
||||
import { useEffect } from 'react'
|
||||
import SEO from '../components/SEO/SEO'
|
||||
import { api } from '../services/api.service'
|
||||
import unbodyApi from '../services/unbody/unbody.service'
|
||||
import { LPE } from '../types/lpe.types'
|
||||
|
||||
@ -22,6 +23,12 @@ export default function Home({ posts, featured, tags }: Props) {
|
||||
setTags(tags)
|
||||
}, [setTags, tags])
|
||||
|
||||
useEffect(() => {
|
||||
api
|
||||
.getLatestEpisodes({ showSlug: 'hashing-it-out', page: 1, limit: 1 })
|
||||
.then((res) => console.log(res))
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<>
|
||||
<SEO
|
||||
|
36
src/services/api.service.ts
Normal file
36
src/services/api.service.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { ApiResponse } from '../types/data.types'
|
||||
import { LPE } from '../types/lpe.types'
|
||||
|
||||
export class ApiService {
|
||||
getRecentPosts = async ({
|
||||
page = 1,
|
||||
limit = 10,
|
||||
}: {
|
||||
page?: number
|
||||
limit?: number
|
||||
}): Promise<ApiResponse<LPE.Post.Document[]>> =>
|
||||
fetch(`/api/posts?page=${page}&limit=${limit}`)
|
||||
.then((res) => res.json())
|
||||
.catch((e) => {
|
||||
console.error(e)
|
||||
return { data: [], errors: JSON.stringify(e) }
|
||||
})
|
||||
|
||||
getLatestEpisodes = async ({
|
||||
page = 1,
|
||||
limit = 10,
|
||||
showSlug,
|
||||
}: {
|
||||
page?: number
|
||||
limit?: number
|
||||
showSlug: string
|
||||
}): Promise<ApiResponse<LPE.Podcast.Document[]>> =>
|
||||
fetch(`/api/podcasts/${showSlug}/episodes?page=${page}&limit=${limit}`)
|
||||
.then((res) => res.json())
|
||||
.catch((e) => {
|
||||
console.error(e)
|
||||
return { data: [], errors: JSON.stringify(e) }
|
||||
})
|
||||
}
|
||||
|
||||
export const api = new ApiService()
|
@ -360,9 +360,11 @@ export class UnbodyService {
|
||||
|
||||
getPodcastShows = async ({
|
||||
showSlug,
|
||||
episodesLimit = 12,
|
||||
populateEpisodes = false,
|
||||
}: {
|
||||
showSlug?: string
|
||||
episodesLimit?: number
|
||||
populateEpisodes?: boolean
|
||||
}) =>
|
||||
this.handleRequest<LPE.Podcast.Show[]>(async () => {
|
||||
@ -398,7 +400,7 @@ export class UnbodyService {
|
||||
? await this.getPodcastEpisodes({
|
||||
showSlug: show.slug,
|
||||
page: 1,
|
||||
limit: 4,
|
||||
limit: episodesLimit,
|
||||
highlighted: 'include',
|
||||
}).then((res) => res.data)
|
||||
: [],
|
||||
@ -413,15 +415,18 @@ export class UnbodyService {
|
||||
|
||||
getPodcastShow = async ({
|
||||
showSlug,
|
||||
episodesLimit = 10,
|
||||
populateEpisodes = false,
|
||||
}: {
|
||||
showSlug: string
|
||||
episodesLimit?: number
|
||||
populateEpisodes?: boolean
|
||||
}) =>
|
||||
this.handleRequest<LPE.Podcast.Show | null>(
|
||||
async () =>
|
||||
this.getPodcastShows({
|
||||
showSlug,
|
||||
episodesLimit,
|
||||
populateEpisodes,
|
||||
}).then((res) => res.data[0]),
|
||||
null,
|
||||
|
@ -111,7 +111,6 @@ export namespace LPE {
|
||||
subtitle: string
|
||||
authors: Author.Document[]
|
||||
tags: string[]
|
||||
featured?: boolean
|
||||
highlighted?: boolean
|
||||
|
||||
createdAt: string | null
|
||||
@ -170,7 +169,6 @@ export namespace LPE {
|
||||
publishedAt: string
|
||||
episodeNumber: number
|
||||
showId?: string
|
||||
featured?: boolean
|
||||
highlighted?: boolean
|
||||
coverImage?: Post.ImageBlock
|
||||
show?: Show
|
||||
|
@ -45,3 +45,24 @@ export const getAudioSourceFromEpisode = async (episodId: string) => {
|
||||
const data = await result.json()
|
||||
return data
|
||||
}
|
||||
|
||||
export const parseInt = (
|
||||
inp: any,
|
||||
defaultValue?: number,
|
||||
): number | undefined => {
|
||||
if (Array.isArray(inp))
|
||||
return (
|
||||
inp
|
||||
.map((value) => parseInt(value))
|
||||
.find((value) => typeof value === 'number') ?? defaultValue
|
||||
)
|
||||
|
||||
let val = inp
|
||||
if (typeof val === 'number') return val
|
||||
if (typeof val === 'string') {
|
||||
val = Number.parseInt(val, 10)
|
||||
return Number.isNaN(val) ? defaultValue : val
|
||||
}
|
||||
|
||||
return defaultValue
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user