ab_frontend_3/src/app/lib/api.tsx

61 lines
No EOL
1.3 KiB
TypeScript

export async function getAllPosts(filter: string, page: number, limit: number) {
const qs = require('qs')
const qVal = []
if (page == null) {
page = 0
}
if (limit == null) {
limit = 50;
}
var filters = {}
if (filter) {
filters = {
project: {
Slug: {
$in: filter
}
}
}
}
const qArray = {
sort: ['publishedAt:desc'],
pagination: {
page: page,
pageSize: limit
},
populate: '*',
filters
}
const query = qs.stringify( qArray, {
encodeValuesOnly: true,
})
const res = await fetch(process.env.API + `articles?${query}`, {
headers: new Headers({
'Authorization': process.env.STRAPI_TOKEN,
'Content-Type': 'application/x-www-form-urlencoded'
})
})
return await res.json()
}
export async function getSinglePost(postId: string) {
const url = process.env.API + 'articles?filters[Slug][$eq]=' + postId + '&populate=*'
const res = await fetch(url, {
headers: new Headers({
'Authorization': process.env.STRAPI_TOKEN,
'Content-Type': 'application/x-www-form-urlencoded'
})
})
const artdata = await res.json()
return artdata.data[0].attributes
}