feat(WEB-127): add trpc to handle requests both serverside and clientside

This commit is contained in:
Simon Emanuelsson
2024-03-20 16:39:11 +01:00
parent 2087ac6c91
commit ec4da5798b
31 changed files with 422 additions and 40 deletions

View File

@@ -0,0 +1,39 @@
import "server-only"
import { request as graphqlRequest } from "graphql-request"
import { env } from "@/env/server"
import ContentstackLivePreview from "@contentstack/live-preview-utils"
import type { Data } from "@/types/request"
import type { DocumentNode } from "graphql"
export async function previewRequest<T>(
query: string | DocumentNode,
variables?: {}
): Promise<Data<T>> {
try {
const hash = ContentstackLivePreview.hash
if (!hash) {
throw new Error("No hash received")
}
const headers = new Headers({
access_token: env.CMS_ACCESS_TOKEN,
preview_token: env.CMS_PREVIEW_TOKEN,
live_preview: hash,
})
const response = await graphqlRequest<T>({
document: query,
requestHeaders: headers,
url: env.CMS_PREVIEW_URL,
variables,
})
return { data: response }
} catch (error) {
console.error(error)
throw new Error("Something went wrong")
}
}