-
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 a simple api wrap querying Yext for blog posts entities.
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
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,66 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
type ResponseData = { | ||
message: string | ||
} | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<ResponseData> | ||
) { | ||
try { | ||
if (!req) { | ||
return; | ||
} | ||
const filter = req.query['filter']?.toString(); | ||
console.log('filter param=' + filter); | ||
|
||
console.log('api key=' + process.env.YEXT_SANDBOX_API_KEY); | ||
|
||
const yextApiUrl = 'https://api.yextapis.com/v2/accounts/me/entities'; | ||
const yextApiParams = { | ||
filter : filter ? decodeURIComponent(filter) : '{}', | ||
entityTypes : 'ce_blogPost', | ||
api_key: process.env.YEXT_SANDBOX_API_KEY, | ||
v: GetDate() | ||
} | ||
// Construct the query string from the parameters | ||
const queryString = Object.entries(yextApiParams) | ||
.map(([key, value]) => `${key}=${encodeURIComponent((value ?? '').toString())}`) | ||
.join('&') | ||
|
||
|
||
const requestUri = `${yextApiUrl}?${queryString}`; | ||
console.log('Request uri: ' + requestUri); | ||
|
||
const response = await (await fetch(requestUri)).json(); | ||
console.log('RESPONSE FROM FETCH REQUEST', response?.response); | ||
res.status(200).json(response?.response); | ||
} | ||
catch (err) { | ||
console.log('ERROR DURING FETCH REQUEST', err); | ||
} | ||
finally { | ||
|
||
} | ||
}; | ||
|
||
function GetDate() : string | ||
{ | ||
var today = new Date(); | ||
var yyyy = today.getFullYear(); | ||
var mm = today.getMonth() + 1; // Months start at 0! | ||
var dd = today.getDate(); | ||
var MM = mm.toString(); | ||
var DD = dd.toString(); | ||
|
||
if (dd < 10) { | ||
DD = '0' + dd; | ||
} | ||
|
||
if (mm < 10) { | ||
MM = '0' + mm; | ||
} | ||
|
||
return yyyy + MM + DD; | ||
} |