Gwk-Cultural-Park/pages/api/event.ts
2024-09-07 08:40:25 +07:00

68 lines
1.5 KiB
TypeScript

/* eslint-disable import/no-anonymous-default-export */
// API: Events
import { projectId } from 'lib/sanity.api'
import { createClient } from 'next-sanity'
const client = projectId
? createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
apiVersion: process.env.NEXT_PUBLIC_SANITY_API_VERSION,
useCdn: true,
token: process.env.SANITY_API_READ_TOKEN,
})
: null
export const getEvents = async () => {
const query = `*[_type == "eventSection"][0]{
title,
subtitle,
"coverImage":
coverImage.asset->path,
Event[]->{
title,
"slug":slug.current,
content[],
"coverImage":
coverImage.asset->path,
date,
author->{name}
}
}` //HomeIntro section
try {
const data = await client.fetch(query)
return data;
}catch (error) {
//console.error('Error fetching Sanity data:', error)
return null;
}
}
export default async (req, res) => {
const query = `*[_type == "eventSection"][0]{
title,
subtitle,
"coverImage":
coverImage.asset->path,
Event[]->{
title,
"slug":slug.current,
content[],
"coverImage":
coverImage.asset->path,
date,
author->{name}
}
}` //HomeIntro section
try {
const data = await client.fetch(query)
res.status(200).json(data)
} catch (error) {
//console.error('Error fetching Sanity data:', error)
res.status(500).json({ error: 'Error fetching Sanity data' })
}
}