mirror of
https://github.com/LaconicNetwork/laconic.com.git
synced 2026-04-26 12:04:10 +00:00
78 lines
1.8 KiB
TypeScript
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 /\ ====================================================
|
|
}
|