laconic.com/src/lib/datocms.ts
Fede Álvarez 91b47f439d
Work on CMS (#65)
* Work on CMS
2022-05-18 23:57:44 +02:00

34 lines
596 B
TypeScript

import tiny from 'tiny-json-http'
interface props {
query: string
variables?: string
preview?: string
}
export async function request({ query, variables, preview }: props) {
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
}