This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { createClient } from 'next-sanity'
|
||||
import imageUrlBuilder from '@sanity/image-url'
|
||||
import type { SanityImageSource } from '@sanity/image-url/lib/types/types'
|
||||
|
||||
export const projectId = 'zal102rv'
|
||||
export const dataset = 'production'
|
||||
export const apiVersion = '2024-01-01'
|
||||
|
||||
export const client = createClient({
|
||||
projectId,
|
||||
dataset,
|
||||
apiVersion,
|
||||
useCdn: false, // false = direct API; avoids CDN DNS issues in dev/server
|
||||
})
|
||||
|
||||
const builder = imageUrlBuilder(client)
|
||||
|
||||
export function urlFor(source: SanityImageSource) {
|
||||
return builder.image(source)
|
||||
}
|
||||
|
||||
// ─── GROQ Queries ────────────────────────────────────────────────────────────
|
||||
|
||||
/** All published posts for the listing page, newest first */
|
||||
export const postsQuery = `
|
||||
*[_type == "post" && defined(slug.current) && publishedAt <= now()]
|
||||
| order(publishedAt desc) {
|
||||
_id,
|
||||
title,
|
||||
slug,
|
||||
publishedAt,
|
||||
excerpt,
|
||||
mainImage { ..., alt },
|
||||
"author": author->{ name, image }
|
||||
}
|
||||
`
|
||||
|
||||
/** Single post by slug for the detail page */
|
||||
export const postBySlugQuery = `
|
||||
*[_type == "post" && slug.current == $slug][0] {
|
||||
_id,
|
||||
title,
|
||||
slug,
|
||||
publishedAt,
|
||||
excerpt,
|
||||
mainImage { ..., alt },
|
||||
body,
|
||||
seoTitle,
|
||||
seoDescription,
|
||||
"author": author->{ name, image, bio }
|
||||
}
|
||||
`
|
||||
|
||||
/** All slugs — used for generateStaticParams */
|
||||
export const postSlugsQuery = `
|
||||
*[_type == "post" && defined(slug.current)] { "slug": slug.current }
|
||||
`
|
||||
@@ -0,0 +1,45 @@
|
||||
import https from 'https'
|
||||
import { projectId, dataset, apiVersion } from './sanity'
|
||||
|
||||
const SANITY_IP = '34.102.242.91'
|
||||
const SANITY_HOST = `${projectId}.api.sanity.io`
|
||||
|
||||
export function sanityFetch<T = unknown>(
|
||||
query: string,
|
||||
params?: Record<string, unknown>
|
||||
): Promise<T> {
|
||||
let path = `/v${apiVersion}/data/query/${dataset}?query=${encodeURIComponent(query)}`
|
||||
if (params) {
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
path += `&$${key}=${encodeURIComponent(JSON.stringify(value))}`
|
||||
}
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = https.request(
|
||||
{
|
||||
method: 'GET',
|
||||
hostname: SANITY_IP,
|
||||
port: 443,
|
||||
path,
|
||||
servername: SANITY_HOST,
|
||||
headers: { Host: SANITY_HOST, Accept: 'application/json', 'User-Agent': 'battl-builders/1.0' },
|
||||
},
|
||||
(res) => {
|
||||
let data = ''
|
||||
res.on('data', (c) => (data += c))
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const parsed = JSON.parse(data)
|
||||
res.statusCode && res.statusCode >= 400
|
||||
? reject(new Error(`Sanity ${res.statusCode}: ${JSON.stringify(parsed)}`))
|
||||
: resolve(parsed.result as T)
|
||||
} catch {
|
||||
reject(new Error(`Sanity parse error: ${data.slice(0, 200)}`))
|
||||
}
|
||||
})
|
||||
}
|
||||
)
|
||||
req.on('error', reject)
|
||||
req.end()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user