156 lines
3.7 KiB
TypeScript
156 lines
3.7 KiB
TypeScript
import { de } from 'date-fns/locale'
|
|
import { apiVersion, dataset, projectId, useCdn } from 'lib/sanity.api'
|
|
import {
|
|
type Gallery,
|
|
galleryBySlugQuery,
|
|
gallerySlugsQuery,
|
|
indexPageQuery,
|
|
indexQuery,
|
|
type Page,
|
|
pageAndMorePagesQuery,
|
|
pageBySlugQuery,
|
|
pageSlugsQuery,
|
|
type Post,
|
|
postAndMoreGallleriesQuery,
|
|
postAndMoreStoriesQuery,
|
|
postBySlugQuery,
|
|
postSlugsQuery,
|
|
type Settings,
|
|
settingsQuery,
|
|
} from 'lib/sanity.queries'
|
|
import { createClient } from 'next-sanity'
|
|
|
|
/**
|
|
* Checks if it's safe to create a client instance, as `@sanity/client` will throw an error if `projectId` is false
|
|
*/
|
|
const client = projectId
|
|
? createClient({ projectId, dataset, apiVersion, useCdn })
|
|
: null
|
|
|
|
export default client
|
|
|
|
export async function getSettings(): Promise<Settings> {
|
|
if (client) {
|
|
return (await client.fetch(settingsQuery)) || {}
|
|
}
|
|
return {}
|
|
}
|
|
|
|
export async function getAllPosts(): Promise<Post[]> {
|
|
if (client) {
|
|
return (await client.fetch(indexQuery)) || []
|
|
}
|
|
return []
|
|
}
|
|
export async function getAllGalleries(): Promise<Gallery[]> {
|
|
if (client) {
|
|
return (await client.fetch(indexQuery)) || []
|
|
}
|
|
return []
|
|
}
|
|
|
|
export async function getAllPages(): Promise<Page[]> {
|
|
if (client) {
|
|
return (await client.fetch(indexPageQuery)) || []
|
|
}
|
|
return []
|
|
}
|
|
|
|
export async function getAllPostsSlugs(): Promise<Pick<Post, 'slug'>[]> {
|
|
if (client) {
|
|
const slugs = (await client.fetch<string[]>(postSlugsQuery)) || []
|
|
return slugs.map((slug) => ({ slug }))
|
|
}
|
|
return []
|
|
}
|
|
|
|
// Gallery
|
|
export async function getAllGalleriesSlugs(): Promise<Pick<Gallery, 'slug'>[]> {
|
|
if (client) {
|
|
const slugs = (await client.fetch<string[]>(gallerySlugsQuery)) || []
|
|
return slugs.map((slug) => ({ slug }))
|
|
}
|
|
return []
|
|
}
|
|
|
|
export async function getAllPagesSlugs(): Promise<Pick<Page, 'slug'>[]> {
|
|
if (client) {
|
|
const slugs = (await client.fetch<string[]>(pageSlugsQuery)) || []
|
|
return slugs.map((slug) => ({ slug }))
|
|
console.log('slug')
|
|
}
|
|
return []
|
|
}
|
|
|
|
export async function getPostBySlug(slug: string): Promise<Post> {
|
|
if (client) {
|
|
return (await client.fetch(postBySlugQuery, { slug })) || ({} as any)
|
|
}
|
|
return {} as any
|
|
}
|
|
|
|
export async function getGalleryBySlug(slug: string): Promise<Gallery> {
|
|
if (client) {
|
|
return (await client.fetch(galleryBySlugQuery, { slug })) || ({} as any)
|
|
}
|
|
return {} as any
|
|
}
|
|
|
|
export async function getPageBySlug(slug: string): Promise<Page> {
|
|
if (client) {
|
|
return (await client.fetch(pageBySlugQuery, { slug })) || ({} as any)
|
|
}
|
|
return {} as any
|
|
}
|
|
|
|
export async function getPostAndMoreStories(
|
|
slug: string,
|
|
token?: string | null
|
|
): Promise<{ post: Post; morePosts: Post[] }> {
|
|
if (projectId) {
|
|
const client = createClient({
|
|
projectId,
|
|
dataset,
|
|
apiVersion,
|
|
useCdn,
|
|
token: token || undefined,
|
|
})
|
|
return await client.fetch(postAndMoreStoriesQuery, { slug })
|
|
}
|
|
return { post: null, morePosts: [] }
|
|
}
|
|
|
|
export async function getGalleryAndMoreStories(
|
|
slug: string,
|
|
token?: string | null
|
|
): Promise<{ post: Gallery; moreGalleries: Gallery[] }> {
|
|
if (projectId) {
|
|
const client = createClient({
|
|
projectId,
|
|
dataset,
|
|
apiVersion,
|
|
useCdn,
|
|
token: token || undefined,
|
|
})
|
|
return await client.fetch(postAndMoreGallleriesQuery, { slug })
|
|
}
|
|
return { post: null, moreGalleries: [] }
|
|
}
|
|
|
|
export async function getPageAndMorePagesQuery(
|
|
slug: string,
|
|
token?: string | null
|
|
): Promise<{ page: Page; morePages: Page[] }> {
|
|
if (projectId) {
|
|
const client = createClient({
|
|
projectId,
|
|
dataset,
|
|
apiVersion,
|
|
useCdn,
|
|
token: token || undefined,
|
|
})
|
|
return await client.fetch(pageAndMorePagesQuery, { slug })
|
|
}
|
|
return { page: null, morePages: [] }
|
|
}
|