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

93 lines
2.3 KiB
TypeScript

// API: Story
import { apiVersion, dataset, projectId, useCdn } 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 getWedding = async () => {
const query = `*[_type == "WeddingSection"][]{
title,
subtitle,
"coverImageUrl": coverImage.asset->url,
descriptionTitle,
content[]{
children[]{
text,
marks[],
_type
}
},
Wedding[]->{
"slug": slug.current,
"cover": coverImage.asset->url,
},
"package" : Weddingpackage[]->{
title
,content[]{
children[]{
text,
marks[],
_type
}
}
,"fileWedding": fileWedding.asset->url
},
}` //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 == "WeddingSection"][]{
title,
subtitle,
"coverImageUrl": coverImage.asset->url,
descriptionTitle,
content[]{
children[]{
text,
marks[],
_type
}
},
Wedding[]->{
"slug": slug.current,
"cover": coverImage.asset->url,
},
"package" : Weddingpackage[]->{
title
,content[]{
children[]{
text,
marks[],
_type
}
}
,"fileWedding": fileWedding.asset->url
},
}` //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' })
}
}