1
0
Fork 0
freesewing/sites/org/components/sanity/utils.mjs

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-06-20 19:27:29 +02:00
import { createClient } from 'next-sanity'
import { siteConfig } from 'site/site.config.mjs'
let sanityClient
2023-06-22 16:27:08 -05:00
const cache = {}
2023-06-22 16:43:31 -05:00
export const sanityLoader = async ({ query, language, type, slug, order, filters = '' }) => {
sanityClient =
sanityClient ||
createClient({
projectId: 'hl5bw8cj',
dataset: 'site-content',
apiVersion: '2023-06-17',
2023-06-22 16:27:08 -05:00
// token: process.env.SANITY_TOKEN,
useCdn: false,
})
if (!query) {
query = `*[_type == "${type}${language}"`
if (slug) query += ` && slug.current == "${slug}"`
query += ']'
}
if (order) {
query += ` | order(${order})`
}
2023-06-22 16:43:31 -05:00
query += filters
2023-06-22 16:27:08 -05:00
if (cache[query]) return cache[query]
console.warn('making a sanity request', query)
const result = await sanityClient.fetch(query)
cache[query] = result
return result
}
export const sanityImage = (image, dataset = 'site-content') => {
const [, assetName, origSize, format] = image.asset._ref.split('-')
return `https://cdn.sanity.io/images/${siteConfig.sanity.project}/${dataset}/${assetName}-${origSize}.${format}`
}
2023-06-22 16:27:08 -05:00
export const numPerPage = 12