angrybeanie-front-end/data/external/cms.js

90 lines
No EOL
2.2 KiB
JavaScript
Executable file

import getConfig from 'next/config'
export const getAllPosts = async (filter, page, limit) => {
console.log(filter + " " + page + " " + limit)
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()
const qs = require('qs')
const qVal = []
if (page == null) {
page = 0
}
if (limit == null) {
limit = 50;
}
var filters = {}
if (filter) {
filters = {
tags: {
Slug: {
$contains: filter
}
}
}
}
console.log(filter)
const qArray = {
sort: ['publishedAt:desc'],
pagination: {
page: page,
pageSize: limit
},
filters
}
const query = qs.stringify( qArray, {
encodeValuesOnly: true,
})
const res = await fetch(serverRuntimeConfig.base_path + `articles?${query}`, {
headers: new Headers({
'Authorization': serverRuntimeConfig.strapi_token,
'Content-Type': 'application/x-www-form-urlencoded'
})
})
return await res.json()
}
export const getSinglePost = async (postId) => {
const { serverRuntimeConfig} = getConfig()
const url = serverRuntimeConfig.base_path + 'articles?filters[Slug][$eq]=' + postId + '&populate=*'
const res = await fetch(url, {
headers: new Headers({
'Authorization': serverRuntimeConfig.strapi_token,
'Content-Type': 'application/x-www-form-urlencoded'
})
})
const artdata = await res.json()
return artdata.data[0].attributes
}
export const getAllPodcastSeries = async () => {
const { serverRuntimeConfig } = getConfig()
const qs = require('qs')
const query = qs.stringify({
populate: {
podcast_episodes: {
populate:['Audio']
}
}
}, {
encodeValuesOnly: true,
})
const res = await fetch(serverRuntimeConfig.base_path + `podcast-series?${query}`, {
headers: new Headers({
'Authorization': serverRuntimeConfig.strapi_token,
'Content-Type': 'application/x-www-form-urlencoded'
})
})
return await res.json()
}