laconic.com/src/lib/datocms.ts
2023-04-25 00:53:02 -04:00

78 lines
1.8 KiB
TypeScript

//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ====================================================
import { datocmsQueryIntercept } from 'lib/datocms-bypass'
//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ====================================================
import tiny from 'tiny-json-http'
interface props {
query: string
variables?: string
preview?: string
}
export async function request({ query, variables, preview }: props) {
//===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ====================================================
/*
let endpoint = 'https://graphql.datocms.com'
if (preview) {
endpoint += `/preview`
}
const { body } = await tiny.post({
url: endpoint,
headers: {
authorization: `Bearer ${process.env.NEXT_PUBLIC_CMS_ACCESS_TOKEN}`
},
data: {
query,
variables
}
})
if (body.errors) {
console.error('The query has errors!')
throw body.errors
}
return body.data
*/
if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') {
let interceptData: any = await datocmsQueryIntercept(query)
if (interceptData.error) {
console.error(interceptData.error)
interceptData = {}
return interceptData
} else {
return interceptData
}
} else {
let endpoint = 'https://graphql.datocms.com'
if (preview) {
endpoint += `/preview`
}
const { body } = await tiny.post({
url: endpoint,
headers: {
authorization: `Bearer ${process.env.NEXT_PUBLIC_CMS_ACCESS_TOKEN}`
},
data: {
query,
variables
}
})
if (body.errors) {
console.error('The query has errors!')
throw body.errors
}
return body.data
}
//===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ====================================================
}