This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import axios from 'axios'
|
||||
import toCamel from 'camelcase-keys'
|
||||
|
||||
import { API_BASE_URL } from '@/config'
|
||||
|
||||
import type { Post } from '@/types'
|
||||
|
||||
|
||||
export const fetchPosts = async ({ tags, match, limit, cursor }: {
|
||||
tags: string
|
||||
match: 'any' | 'all'
|
||||
limit: number
|
||||
cursor?: string }): Promise<{ posts: Post[]; nextCursor: string }> => {
|
||||
const res = await axios.get (`${ API_BASE_URL }/posts`, {
|
||||
params: { tags, match, limit, ...(cursor && { cursor }) } })
|
||||
|
||||
return toCamel (res.data as any, { deep: true }) as { posts: Post[]
|
||||
nextCursor: string }
|
||||
}
|
||||
|
||||
|
||||
export const fetchPost = async (id: string): Promise<Post> => {
|
||||
const res = await axios.get (`${ API_BASE_URL }/posts/${ id }`, {
|
||||
headers: { 'X-Transfer-Code': localStorage.getItem ('user_code') ?? '' } })
|
||||
|
||||
return toCamel (res.data as any, { deep: true }) as Post
|
||||
}
|
||||
|
||||
|
||||
export const toggleViewedFlg = async (id: string, viewed: boolean): Promise<void> => {
|
||||
const url = `${ API_BASE_URL }/posts/${ id }/viewed`
|
||||
const opt = { headers: { 'X-Transfer-Code': localStorage.getItem ('user_code') ?? '' } }
|
||||
if (viewed)
|
||||
await axios.post (url, { }, opt)
|
||||
else
|
||||
await axios.delete (url, opt)
|
||||
}
|
||||
Reference in New Issue
Block a user