Liviu Damian, July 12, 2020
import client, { previewClient } from './sanity'
import imageUrlBuilder from '@sanity/image-url'
const blogFields = `
title,
subtitle,
'slug': slug.current,
date,
'author': author->{name, 'avatar': avatar.asset->url},
coverImage,
`
const builder = imageUrlBuilder(client)
const getClient = preview => preview ? previewClient : client
export function urlFor(source) {
return builder.image(source)
}
// offset: how many data you want to skip, limit: how manhy data you want to fetch
export async function getAllBlogs() {
const results = await client
.fetch(`*[_type == "blog"] | order(date desc) {${blogFields}}`)
return results
}
export async function getPaginatedBlogs({offset = 0, date = 'desc'} = {offset: 0, date: 'desc'}) {
const results = await client
.fetch(`*[_type == "blog"] | order(date ${date}) {${blogFields}}[${offset}...${offset + 6}]`)
return results
}
export async function getBlogBySlug(slug, preview) {
const currentClient = getClient(preview)
const result = await currentClient
.fetch(`*[_type == "blog" && slug.current == $slug] {
${blogFields}
content[]{..., "asset": asset->}
}`, {slug})
.then(res => preview ? (res?.[1] ? res[1] : res[0]) : res?.[0])
return result
}
lib/api.js