feat(SW-508): preview for content pages
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import "server-only"
|
||||
|
||||
import { ContentstackLivePreview } from "@contentstack/live-preview-utils"
|
||||
import { ClientError, GraphQLClient } from "graphql-request"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
@@ -16,40 +17,54 @@ export async function request<T>(
|
||||
params?: RequestInit
|
||||
): Promise<Data<T>> {
|
||||
try {
|
||||
if (params?.cache) {
|
||||
client.requestConfig.cache = params.cache
|
||||
}
|
||||
if (params?.headers) {
|
||||
client.requestConfig.headers = params.headers
|
||||
}
|
||||
if (params?.next) {
|
||||
client.requestConfig.next = params.next
|
||||
}
|
||||
client.setHeaders({
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
})
|
||||
|
||||
if (env.PRINT_QUERY) {
|
||||
const print = (await import("graphql/language/printer")).print
|
||||
const rawResponse = await client.rawRequest<T>(
|
||||
print(query as DocumentNode),
|
||||
variables,
|
||||
{
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
const previewHash = ContentstackLivePreview.hash
|
||||
if (previewHash) {
|
||||
client.setEndpoint(env.CMS_PREVIEW_URL)
|
||||
client.setHeader("preview_token", env.CMS_PREVIEW_TOKEN)
|
||||
client.setHeader("live_preview", previewHash)
|
||||
} else {
|
||||
if (params?.cache) {
|
||||
client.requestConfig.cache = params.cache
|
||||
}
|
||||
if (params?.headers) {
|
||||
client.requestConfig.headers = params.headers
|
||||
}
|
||||
if (params?.next) {
|
||||
client.requestConfig.next = params.next
|
||||
}
|
||||
|
||||
if (env.PRINT_QUERY) {
|
||||
const print = (await import("graphql/language/printer")).print
|
||||
const rawResponse = await client.rawRequest<T>(
|
||||
print(query as DocumentNode),
|
||||
variables,
|
||||
{
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
)
|
||||
|
||||
/**
|
||||
* TODO: Send to Monitoring (Logging and Metrics)
|
||||
*/
|
||||
console.log({
|
||||
complexityLimit: rawResponse.headers.get("x-query-complexity"),
|
||||
})
|
||||
console.log({
|
||||
referenceDepth: rawResponse.headers.get("x-reference-depth"),
|
||||
})
|
||||
console.log({
|
||||
resolverCost: rawResponse.headers.get("x-resolver-cost"),
|
||||
})
|
||||
|
||||
return {
|
||||
data: rawResponse.data,
|
||||
}
|
||||
)
|
||||
|
||||
/**
|
||||
* TODO: Send to Monitoring (Logging and Metrics)
|
||||
*/
|
||||
console.log({
|
||||
complexityLimit: rawResponse.headers.get("x-query-complexity"),
|
||||
})
|
||||
console.log({
|
||||
referenceDepth: rawResponse.headers.get("x-reference-depth"),
|
||||
})
|
||||
console.log({ resolverCost: rawResponse.headers.get("x-resolver-cost") })
|
||||
|
||||
return {
|
||||
data: rawResponse.data,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,10 +93,6 @@ export async function request<T>(
|
||||
|
||||
const response = await client.request<T>({
|
||||
document: query,
|
||||
requestHeaders: {
|
||||
access_token: env.CMS_ACCESS_TOKEN,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
variables,
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import "server-only"
|
||||
|
||||
import ContentstackLivePreview from "@contentstack/live-preview-utils"
|
||||
import { ContentstackLivePreview } from "@contentstack/live-preview-utils"
|
||||
import { request as graphqlRequest } from "graphql-request"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
|
||||
Reference in New Issue
Block a user