mirror of
https://github.com/LaconicNetwork/laconic.com.git
synced 2026-01-19 07:34:07 +00:00
34 lines
596 B
TypeScript
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
|
|
}
|