-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the beginnings of the push API handler, and some refactoring
- Loading branch information
Showing
7 changed files
with
144 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Import the Next.js API route handler | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { graphqlRequest, GraphQLRequest } from '@/util/GraphQLQuery'; | ||
|
||
// Define the API route handler | ||
export default async function onPublishEnd(req: NextApiRequest, res: NextApiResponse) { | ||
// Check if the api_key query parameter matches the WEBHOOK_API_KEY environment variable | ||
if (req.query.api_key !== process.env.WEBHOOK_API_KEY) { | ||
return res.status(401).json({ message: 'Unauthorized' }); | ||
} | ||
|
||
// If the request method is not POST, return an error | ||
if (req.method !== 'POST') { | ||
return res.status(405).json({ message: 'Method not allowed' }); | ||
} | ||
|
||
let data; | ||
try { | ||
// Try to parse the JSON data from the request body | ||
//console.log('Req body:\n' + JSON.stringify(req.body)); | ||
data = req.body; | ||
} catch (error) { | ||
console.log('Bad Request: ', error); | ||
return res.status(400).json({ message: 'Bad Request. Check incoming data.' }); | ||
} | ||
|
||
// Loop over all the entries in updates | ||
for (const update of data.updates) { | ||
// Check if the entity_definition is LayoutData | ||
if (update.entity_definition === 'LayoutData') { | ||
// Extract the GUID portion of the identifier | ||
const guid = update.identifier.split('-')[0] | ||
|
||
try { | ||
// Create the GraphQL request | ||
const request: GraphQLRequest = { | ||
query: itemQuery, | ||
variables: { id: guid }, | ||
}; | ||
console.log('Getting GQL Data for item ' + guid); | ||
// Invoke the GraphQL query with the request | ||
const result = await graphqlRequest(request); | ||
|
||
console.log('Item Data:\n' + JSON.stringify(result)); | ||
|
||
// TODO: Handle the result of the GraphQL query | ||
// 1. Make sure we got some data from GQL | ||
// 2. Check if it's in the right site (the webhook fires for every site) by comparing the path | ||
// 3. Send the json data to the Yext Push API endpoint | ||
|
||
} catch (error) { | ||
// If an error occurs while invoking the GraphQL query, return a 500 error | ||
return res.status(500).json({ message: 'Internal Server Error: GraphQL query failed' }) | ||
} | ||
} | ||
} | ||
|
||
// Send a response | ||
return res.status(200).json({ message: 'Webhook event received' }) | ||
} | ||
|
||
const itemQuery = ` | ||
query ($id: String!) { | ||
item(path: $id, language: "en") { | ||
id | ||
name | ||
path | ||
url { | ||
path | ||
url | ||
} | ||
fields { | ||
name | ||
jsonValue | ||
} | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function GetDate(): string { | ||
const today = new Date(); | ||
const yyyy = today.getFullYear(); | ||
const mm = today.getMonth() + 1; // Months start at 0! | ||
const dd = today.getDate(); | ||
let MM = mm.toString(); | ||
let DD = dd.toString(); | ||
|
||
if (dd < 10) { | ||
DD = '0' + dd; | ||
} | ||
|
||
if (mm < 10) { | ||
MM = '0' + mm; | ||
} | ||
|
||
// Return date in YYYYMMDD format for Yext param | ||
return yyyy + MM + DD; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import fetch from 'node-fetch'; | ||
|
||
export interface GraphQLRequest { | ||
query: string; | ||
variables?: Record<string, any>; | ||
} | ||
|
||
// Define the expected shape of the GraphQL response data | ||
interface GraphQLResponse { | ||
data?: any; | ||
errors?: { message: string }[]; | ||
} | ||
|
||
export async function graphqlRequest(request: GraphQLRequest): Promise<any> { | ||
const endpoint = process.env.GRAPH_QL_ENDPOINT ?? ''; | ||
const apiKey = process.env.SITECORE_API_KEY ?? ''; | ||
|
||
const response = await fetch(endpoint, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'sc_apikey': apiKey | ||
}, | ||
body: JSON.stringify(request), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`GraphQL request failed: ${response.status} ${response.statusText}`); | ||
} | ||
|
||
// Type the body variable using the GraphQLResponse interface | ||
const body = await response.json() as GraphQLResponse; | ||
|
||
if (body.errors) { | ||
throw new Error(`GraphQL request failed: ${body.errors.map(error => error.message).join(', ')}`); | ||
} | ||
|
||
return body.data; | ||
} |