diff --git a/src/lib/datocms-bypass.ts b/src/lib/datocms-bypass.ts index 7d762e3..7ff8e01 100644 --- a/src/lib/datocms-bypass.ts +++ b/src/lib/datocms-bypass.ts @@ -1,46 +1,44 @@ import cms from 'lib/cms' -//import { BlogPostRecord } from 'lib/cms/generated' import { getBlogPosts as serverGetBlogPosts, getBlogPostsCategories as serverGetBlogPostsCategories } from 'lib/blog' -import path from 'path'; -import { promises as fs } from 'fs'; +import path from 'path' +import { promises as fs } from 'fs' //-----misc helpers function datocmsDateToIng(date : string) : number { - let dateInt = 0; + let dateInt = 0 - date = date.replace(/\D/g,''); - dateInt = parseInt(date); + date = date.replace(/\D/g,'') + dateInt = parseInt(date) if (Number.isInteger(dateInt) !== true) { - dateInt = 0; + dateInt = 0 } - return dateInt; + return dateInt } - //-----graphql query interception export async function datocmsQueryIntercept(query : any) { - let parent = pluckFirstParentFromQuery(query); - let pageQueryWhitelist = getPageQueryBypassWhitelist(); - let listingQueryWhitelist = getListingQueryDirectories(); - let interceptData = {}; + let parent = pluckFirstParentFromQuery(query) + let pageQueryWhitelist = getPageQueryBypassWhitelist() + let listingQueryWhitelist = getListingQueryDirectories() + let interceptData = {} if (listingQueryWhitelist.includes(parent)) { - interceptData = await datocmsListingQueryIntercept(query); + interceptData = await datocmsListingQueryIntercept(query) } else if (pageQueryWhitelist.includes(parent)) { - interceptData = await datocmsPageQueryIntercept(query); + interceptData = await datocmsPageQueryIntercept(query) } else { - let errorText = 'Unable to intercept datocms query. No viable JSON alternative was defined.'; - interceptData = {error: errorText}; + let errorText = 'Unable to intercept datocms query. No viable JSON alternative was defined.' + interceptData = {error: errorText} } - return interceptData; + return interceptData } function getPageQueryBypassWhitelist() : string[] { @@ -48,765 +46,715 @@ function getPageQueryBypassWhitelist() : string[] { //-----does not include iterative stuff like blog posts or events etc... //-----this may only end up being scaffolding. - let whitelist : string [] = []; + let whitelist : string [] = [] - whitelist.push('aboutPage'); - whitelist.push('careersPage'); - whitelist.push('communityPage'); - whitelist.push('contactPage'); - whitelist.push('homePage'); - whitelist.push('partnersPage'); - whitelist.push('pressPage'); - whitelist.push('privacyPage'); - whitelist.push('productsPage'); - whitelist.push('termsPage'); + whitelist.push('aboutPage') + whitelist.push('careersPage') + whitelist.push('communityPage') + whitelist.push('contactPage') + whitelist.push('homePage') + whitelist.push('partnersPage') + whitelist.push('pressPage') + whitelist.push('privacyPage') + whitelist.push('productsPage') + whitelist.push('termsPage') - whitelist.push('header'); - whitelist.push('footer'); + whitelist.push('header') + whitelist.push('footer') - return whitelist; + return whitelist } function getListingQueryDirectories() : string[] { //-----whitelist the listying types of content... - let directories : string[] = []; + let directories : string[] = [] - directories.push('allEvents'); - directories.push('allPositions'); - directories.push('allPressReleases'); - directories.push('allTeams'); - directories.push('allTestimonials'); - - return directories; + directories.push('allEvents') + directories.push('allPositions') + directories.push('allPressReleases') + directories.push('allTeams') + directories.push('allTestimonials') + return directories } function pluckFirstParentFromQuery(query : any) : string { //-----only plucks the FIRST parent, if there are multiple parents in the query they will be ignored. - let parent : string = ((query.replace(/[\W_]+/g," ")).trim()).split(" ")[0]; - return parent; - + let parent : string = ((query.replace(/[\W_]+/g," ")).trim()).split(" ")[0] + return parent } async function datocmsPageQueryIntercept(query : any) : Promise { - let parent : string = pluckFirstParentFromQuery(query); - let jsonDirectory : string = path.join(process.cwd(), 'json/site_content'); - let jsonPath : string = jsonDirectory + '/' + parent + '.json'; - let fileRawContents : string = ''; + let parent : string = pluckFirstParentFromQuery(query) + let jsonDirectory : string = path.join(process.cwd(), 'json/site_content') + let jsonPath : string = jsonDirectory + '/' + parent + '.json' + let fileRawContents : string = '' let fileJson : any = {} let jsonData : any = {} try { - fileRawContents = await fs.readFile(jsonPath, 'utf8'); - fileJson = JSON.parse(fileRawContents); - jsonData = fileJson.data; + fileRawContents = await fs.readFile(jsonPath, 'utf8') + fileJson = JSON.parse(fileRawContents) + jsonData = fileJson.data } catch (e) { - console.log('Failed to parse JSON for file ' + jsonPath); - console.log('JSON parse attempt resulted in the following error:'); - console.log(e); + console.log('Failed to parse JSON for file ' + jsonPath) + console.log('JSON parse attempt resulted in the following error:') + console.log(e) } - return jsonData; - + return jsonData } async function datocmsListingQueryIntercept(query : any) : Promise { - - let parent : string = pluckFirstParentFromQuery(query); - let jsonData : any = {}; + + let parent : string = pluckFirstParentFromQuery(query) + let jsonData : any = {} switch(parent) { case 'allBlogPosts': //-----blog listing is handled in its own annoyingly special way //-----could build it out here to be verbose but will not in the intrest of time/budget - //-----might need to add/remove some parent nodes in the JSON returned from getAllBlogPostsJson(); to match the would-be expected format. - //jsonData = await getAllBlogPostsJson(); - jsonData = {}; - break; + //-----might need to add/remove some parent nodes in the JSON returned from getAllBlogPostsJson() to match the would-be expected format. + //jsonData = await getAllBlogPostsJson() + jsonData = {} + break case 'allCategories': //-----category listing is handled in its own annoyingly special way //-----could build it out here to be verbose but will not in the intrest of time/budget - //-----might need to add/remove some parent nodes in the JSON returned from getAllBlogCategoriesJson(); to match the would-be expected format. - //jsonData = await getAllBlogCategoriesJson(); - jsonData = {}; - break; + //-----might need to add/remove some parent nodes in the JSON returned from getAllBlogCategoriesJson() to match the would-be expected format. + //jsonData = await getAllBlogCategoriesJson() + jsonData = {} + break case 'allEvents': - jsonData = await getAllEventsJson(); - break; + jsonData = await getAllEventsJson() + break case 'allPositions': - jsonData = await getAllPositionsJson(); - break; + jsonData = await getAllPositionsJson() + break case 'allPressReleases': - jsonData = await getAllPressReleasesJson(); - break; + jsonData = await getAllPressReleasesJson() + break case 'allTeams': - jsonData = await getAllTeamsJson(); - break; + jsonData = await getAllTeamsJson() + break case 'allTestimonials': - jsonData = await getAllTestimonialsJson(); - break; + jsonData = await getAllTestimonialsJson() + break default: - jsonData = {}; - break; + jsonData = {} + break } - return jsonData; - + return jsonData } - //-----json traversal function jsonFilePathIsValid(filePath : string) { if (filePath.startsWith("_")) { - return false; - } - - if (!filePath.endsWith(".json")) { - return false; + return false } - return true; + if (!filePath.endsWith(".json")) { + return false + } + + return true } function jsonNodeExists(jsonData : any, node : string) { - //-----this condition is unneccessary... so THATS what typescript is for XD - //if (typeof node === 'string' || node instanceof String) { if (node in jsonData === true) { - return true; + return true } - //} - return false; + return false } function jsonNodesExist(jsonData : any, nodes : any) { //-----permit basic validation of the json we are trying to spit out by checking for existence of first order nodes - let node = ''; - let nodeGroup : any = []; - let nodeGroupNode = ''; - let nodeGroupValid = false; - - for (let i = 0; i < nodes.length; i++) { - node = nodes[i]; + let node = '' + let nodeGroup : any = [] + let nodeGroupNode = '' + let nodeGroupValid = false + + for (let i = 0 i < nodes.length i++) { + node = nodes[i] if (typeof node === 'string') { //-----string means that PRIMARY node must exist... no checking for children etc... if (jsonNodeExists(jsonData, node) === false) { - return false; + return false } } else if (Array.isArray(node)) { //-----array means json is valid if ANY of the nodes in that array exist - nodeGroup = node; - nodeGroupValid = false; - for (let j = 0; j < nodeGroup.length; j++) { - nodeGroupNode = nodeGroup[j]; + nodeGroup = node + nodeGroupValid = false + for (let j = 0 j < nodeGroup.length j++) { + nodeGroupNode = nodeGroup[j] if (jsonNodeExists(jsonData, nodeGroupNode) === true) { - nodeGroupValid = true; + nodeGroupValid = true } } if (nodeGroupValid === false) { //-----none of the nodes in the group exist. - return false; + return false } } else { //-----lets not get too crazy here. - return false; + return false } } - return true; - + return true } export async function getJsonItemsFromDirectory(jsonDirectory : string, pluckerFunction : any, validationNodes : any[]) : Promise { - let jsonFiles = await fs.readdir(jsonDirectory); - let returnJson : any[] = []; + let jsonFiles = await fs.readdir(jsonDirectory) + let returnJson : any[] = [] - for (let i = 0; i < jsonFiles.length; i++) { - let jsonFile = jsonFiles[i]; + for (let i = 0 i < jsonFiles.length i++) { + let jsonFile = jsonFiles[i] if (jsonFilePathIsValid(jsonFile) === true) { - let jsonPath = path.join(jsonDirectory, jsonFile); - let fileRawContents = await fs.readFile(jsonPath, 'utf8'); - let fileJsonContents = {}; - let jsonParseSuccess = false; + let jsonPath = path.join(jsonDirectory, jsonFile) + let fileRawContents = await fs.readFile(jsonPath, 'utf8') + let fileJsonContents = {} + let jsonParseSuccess = false try { - fileJsonContents = JSON.parse(fileRawContents); - jsonParseSuccess = true; + fileJsonContents = JSON.parse(fileRawContents) + jsonParseSuccess = true } catch (e) { - jsonParseSuccess = false; - console.log('Failed to parse JSON for file ' + jsonPath); - console.log('JSON parse attempt resulted in the following error:'); - console.log(e); + jsonParseSuccess = false + console.log('Failed to parse JSON for file ' + jsonPath) + console.log('JSON parse attempt resulted in the following error:') + console.log(e) } if (jsonParseSuccess === true) { - fileJsonContents = JSON.parse(fileRawContents); - let jsonData = pluckerFunction(fileJsonContents); - jsonData.sourceFile = jsonFile; + fileJsonContents = JSON.parse(fileRawContents) + let jsonData = pluckerFunction(fileJsonContents) + jsonData.sourceFile = jsonFile if (jsonNodesExist(jsonData, validationNodes) === true) { - returnJson.push(jsonData); + returnJson.push(jsonData) } } } } - return returnJson; + return returnJson } //-----blogPosts function getBlogJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/blogPost'); + return path.join(process.cwd(), 'json/site_content/blogPost') } function pluckBlogPostData(json : any) { - //let plucked : BlogPostRecord = {}; - let plucked : any = {}; + //let plucked : BlogPostRecord = {} + let plucked : any = {} if('data' in json){ if('blogPost' in json.data) { - plucked = json.data.blogPost; + plucked = json.data.blogPost } } - return plucked; - + return plucked } function getRequiedBlogPostNodes() : any[] { - let nodes = []; - let contentNodes = []; + let nodes = [] + let contentNodes = [] - contentNodes.push('content'); - contentNodes.push('htmlContent'); + contentNodes.push('content') + contentNodes.push('htmlContent') //-----slug is now set per the JSON filename to avoid mismatches in routing - //nodes.push('slug'); - nodes.push('date'); - nodes.push('title'); - nodes.push(contentNodes); - - return nodes; + //nodes.push('slug') + nodes.push('date') + nodes.push('title') + nodes.push(contentNodes) + return nodes } function forceBlogPostsJsonSlugIntegrity(json : any[]) { //-----this is used to force the blog post slug to match that of its parent file name. - let returnJson : any[] = []; + let returnJson : any[] = [] - for (let i = 0; i < json.length; i++) { - let blogPost = json[i]; - let sourceFile = blogPost.sourceFile; - let newSlug = path.parse(sourceFile).name; - blogPost.slug = newSlug; - returnJson.push(blogPost); + for (let i = 0 i < json.length i++) { + let blogPost = json[i] + let sourceFile = blogPost.sourceFile + let newSlug = path.parse(sourceFile).name + blogPost.slug = newSlug + returnJson.push(blogPost) } - return returnJson; - + return returnJson } function sortBlogPostsJsonByDate(json : any[]) { - let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.date) - datocmsDateToIng(a.date)); + let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.date) - datocmsDateToIng(a.date)) - return sortedJson; + return sortedJson } function getFakePaginationData(json : any) { - let pagination : any = {}; - let totalPosts = json.length; + let pagination : any = {} + let totalPosts = json.length - pagination.nextPage = null; - pagination.page = 1; - pagination.step = totalPosts; - pagination.total = totalPosts; - pagination.totalPages = 1; + pagination.nextPage = null + pagination.page = 1 + pagination.step = totalPosts + pagination.total = totalPosts + pagination.totalPages = 1 - return pagination; + return pagination } async function getAllBlogPostsJson() { - let jsonDirectory = getBlogJsonDirectoryPath(); - let validationNodes = getRequiedBlogPostNodes(); - let blogPosts = await getJsonItemsFromDirectory(jsonDirectory, pluckBlogPostData, validationNodes); - let pagination = getFakePaginationData(blogPosts); - let processedBlogPosts = forceBlogPostsJsonSlugIntegrity(blogPosts); - let sortedBlogPosts : any[] = sortBlogPostsJsonByDate(processedBlogPosts); - let allBlogPostsJson = {pagination: pagination, data: sortedBlogPosts}; + let jsonDirectory = getBlogJsonDirectoryPath() + let validationNodes = getRequiedBlogPostNodes() + let blogPosts = await getJsonItemsFromDirectory(jsonDirectory, pluckBlogPostData, validationNodes) + let pagination = getFakePaginationData(blogPosts) + let processedBlogPosts = forceBlogPostsJsonSlugIntegrity(blogPosts) + let sortedBlogPosts : any[] = sortBlogPostsJsonByDate(processedBlogPosts) + let allBlogPostsJson = {pagination: pagination, data: sortedBlogPosts} - return allBlogPostsJson; + return allBlogPostsJson } export async function getAllBlogPostsSlugsFromSource() { - let jsonDirectory = getBlogJsonDirectoryPath(); - let jsonFiles = await fs.readdir(jsonDirectory); - let slugs = []; + let jsonDirectory = getBlogJsonDirectoryPath() + let jsonFiles = await fs.readdir(jsonDirectory) + let slugs = [] - for (let i = 0; i < jsonFiles.length; i++) { - let jsonFile = jsonFiles[i]; + for (let i = 0 i < jsonFiles.length i++) { + let jsonFile = jsonFiles[i] if (jsonFilePathIsValid(jsonFile) === true) { - let slug = path.parse(jsonFile).name; - let node = {slug : slug}; - slugs.push(node); + let slug = path.parse(jsonFile).name + let node = {slug : slug} + slugs.push(node) } } - return slugs; - + return slugs } export async function getAllBlogPostsFromSource(datocmsFilters : any) : Promise { - let allBlogPostsFromSource : any = {}; + let allBlogPostsFromSource : any = {} if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { - allBlogPostsFromSource = await getAllBlogPostsJson(); + allBlogPostsFromSource = await getAllBlogPostsJson() } else { - allBlogPostsFromSource = serverGetBlogPosts(datocmsFilters); + allBlogPostsFromSource = serverGetBlogPosts(datocmsFilters) } - return allBlogPostsFromSource; - + return allBlogPostsFromSource } async function getSingleBlogPostJsonBySlug(slug : string) : Promise { - let jsonDirectory = getBlogJsonDirectoryPath(); - let jsonFile = slug + '.json'; - let jsonPath = path.join(jsonDirectory, jsonFile); - let blogPost : any = {}; + let jsonDirectory = getBlogJsonDirectoryPath() + let jsonFile = slug + '.json' + let jsonPath = path.join(jsonDirectory, jsonFile) + let blogPost : any = {} try { - let fileRawContents = await fs.readFile(jsonPath, 'utf8'); - let fileJsonContents = JSON.parse(fileRawContents); - blogPost = pluckBlogPostData(fileJsonContents); + let fileRawContents = await fs.readFile(jsonPath, 'utf8') + let fileJsonContents = JSON.parse(fileRawContents) + blogPost = pluckBlogPostData(fileJsonContents) //-----continue to enforce standard set by forceBlogPostsJsonSlugIntegrity() - blogPost.slug = slug; + blogPost.slug = slug } catch (e) { - console.log('Failed to open or parse JSON for file ' + jsonPath); - console.log('JSON parse attempt resulted in the following error:'); - console.log(e); + console.log('Failed to open or parse JSON for file ' + jsonPath) + console.log('JSON parse attempt resulted in the following error:') + console.log(e) } - return blogPost; - + return blogPost } export async function getSingleBlogPostBySlugFromSource(slug : string) : Promise { - let blogPostJson : any = {}; + let blogPostJson : any = {} if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { - try { - //-----old highly inefficent way, - //-----used before we forced slug to be re written per filename via forceBlogPostsJsonSlugIntegrity() - /* - let blogPosts = await getAllBlogPostsJson(); - let blogPostsData = blogPosts.data; - let slugMatches = blogPostsData.filter( - function(blogPostsData) { - return blogPostsData.slug == slug; - } - ); - blogPostJson = slugMatches[0]; - */ - blogPostJson = await getSingleBlogPostJsonBySlug(slug); + try { + blogPostJson = await getSingleBlogPostJsonBySlug(slug) } catch (e) { - console.log('Failed pull blog post with slug ' + slug); - console.log('The attempt resulted in the following error:'); - console.log(e); + console.log('Failed pull blog post with slug ' + slug) + console.log('The attempt resulted in the following error:') + console.log(e) } } else { blogPostJson = await (await cms().SingleBlogPost({ slug })).blogPost } - return blogPostJson; - + return blogPostJson } function getBlogPostCategorySlugs(blogPostJson : any) { - let categories = []; + let categories = [] try { - let categoryNode = blogPostJson.category; - for (let i = 0; i < categoryNode.length; i++) { - let category = categoryNode[i].slug; - categories.push(category); + let categoryNode = blogPostJson.category + for (let i = 0 i < categoryNode.length i++) { + let category = categoryNode[i].slug + categories.push(category) } } catch (e) { - console.log('Failed pull blog post categories'); - console.log('The attempt resulted in the following error:'); - console.log(e); + console.log('Failed pull blog post categories') + console.log('The attempt resulted in the following error:') + console.log(e) } - return categories; - + return categories } export async function getRelatedBlogPosts(blogPostJson : any, matchCount : number) : Promise { - let relatedBlogPosts = []; + let relatedBlogPosts = [] let reservedSlugs = [] - let matchedCount = 0; - let matchCategories = getBlogPostCategorySlugs(blogPostJson); - let blogPosts = await getAllBlogPostsJson(); - let blogPostsData = blogPosts.data; - let candidateBlogPost : any = {}; - let candidateBlogPostSlug = ''; - let slug = blogPostJson.slug; + let matchedCount = 0 + let matchCategories = getBlogPostCategorySlugs(blogPostJson) + let blogPosts = await getAllBlogPostsJson() + let blogPostsData = blogPosts.data + let candidateBlogPost : any = {} + let candidateBlogPostSlug = '' + let slug = blogPostJson.slug if (matchCategories.length > 0) { - reservedSlugs.push(slug); - for (let i = 0; i < blogPostsData.length; i++) { - candidateBlogPost = blogPostsData[i]; - candidateBlogPostSlug = candidateBlogPost.slug; + reservedSlugs.push(slug) + for (let i = 0 i < blogPostsData.length i++) { + candidateBlogPost = blogPostsData[i] + candidateBlogPostSlug = candidateBlogPost.slug if (matchedCount >= matchCount) { - break; + break } if (reservedSlugs.includes(candidateBlogPostSlug) === false) { - let candidateCategories = getBlogPostCategorySlugs(candidateBlogPost); - for (let j = 0; j < matchCategories.length; j++) { - let matchCategory = matchCategories[j]; + let candidateCategories = getBlogPostCategorySlugs(candidateBlogPost) + for (let j = 0 j < matchCategories.length j++) { + let matchCategory = matchCategories[j] if (candidateCategories.includes(matchCategory) === true && reservedSlugs.includes(candidateBlogPostSlug) === false) { - relatedBlogPosts.push(candidateBlogPost); + relatedBlogPosts.push(candidateBlogPost) reservedSlugs.push(candidateBlogPostSlug) - matchedCount++; - break; + matchedCount++ + break } - } - + } } } } if (matchedCount < matchCount) { //-----if not enough matches, pull in anything thats left... - for (let i = 0; i < blogPostsData.length; i++) { - candidateBlogPost = blogPostsData[i]; - candidateBlogPostSlug = candidateBlogPost.slug; + for (let i = 0 i < blogPostsData.length i++) { + candidateBlogPost = blogPostsData[i] + candidateBlogPostSlug = candidateBlogPost.slug if (matchedCount >= matchCount) { - break; + break } if (reservedSlugs.includes(candidateBlogPostSlug) === false) { - relatedBlogPosts.push(candidateBlogPost); + relatedBlogPosts.push(candidateBlogPost) reservedSlugs.push(candidateBlogPostSlug) - matchedCount++; + matchedCount++ } } } - return relatedBlogPosts; - + return relatedBlogPosts } - //-----category function getBlogCategoriesJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/category'); + return path.join(process.cwd(), 'json/site_content/category') } function pluckBlogCategoriesData(json : any) { - let plucked = {}; + let plucked = {} if('data' in json){ if('category' in json.data) { - plucked = json.data.category; + plucked = json.data.category } } - return plucked; - + return plucked } function getRequiedBlogBlogCategoryNodes() { - let nodes = []; + let nodes = [] - nodes.push('slug'); - nodes.push('title'); - - return nodes; + nodes.push('slug') + nodes.push('title') + return nodes } export async function getAllBlogCategoriesJson() : Promise { - let jsonDirectory = getBlogCategoriesJsonDirectoryPath(); - let validationNodes = getRequiedBlogBlogCategoryNodes(); + let jsonDirectory = getBlogCategoriesJsonDirectoryPath() + let validationNodes = getRequiedBlogBlogCategoryNodes() let allBlogCategoriesJson : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckBlogCategoriesData, validationNodes) - return allBlogCategoriesJson; + return allBlogCategoriesJson } export async function getAllBlogPostsCategoriesFromSource() : Promise { - let allBlogPostsCategoriesFromSource : any[] = []; + let allBlogPostsCategoriesFromSource : any[] = [] if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { - allBlogPostsCategoriesFromSource = await getAllBlogCategoriesJson(); - //let requestResults = await request(AllCategoriesQuery); - //allBlogPostsCategoriesFromSource = requestResults.allCategories; + allBlogPostsCategoriesFromSource = await getAllBlogCategoriesJson() + //let requestResults = await request(AllCategoriesQuery) + //allBlogPostsCategoriesFromSource = requestResults.allCategories } else { - allBlogPostsCategoriesFromSource = await serverGetBlogPostsCategories(); + allBlogPostsCategoriesFromSource = await serverGetBlogPostsCategories() } - return allBlogPostsCategoriesFromSource; - + return allBlogPostsCategoriesFromSource } - //-----event function getEventsJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/event'); + return path.join(process.cwd(), 'json/site_content/event') } function pluckEventData(json : any) { - let plucked = {}; + let plucked = {} if('data' in json){ if('event' in json.data) { - plucked = json.data.event; + plucked = json.data.event } } - return plucked; - + return plucked } function getRequiedEventNodes() { - let nodes = []; + let nodes = [] - nodes.push('title'); - nodes.push('eventLocation'); - nodes.push('eventStartdate'); - nodes.push('eventEnddate'); - - return nodes; + nodes.push('title') + nodes.push('eventLocation') + nodes.push('eventStartdate') + nodes.push('eventEnddate') + return nodes } function sortEventsJsonByStartDate(json : any[]) { - let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.eventStartdate) - datocmsDateToIng(a.eventStartdate)); + let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.eventStartdate) - datocmsDateToIng(a.eventStartdate)) - return sortedJson; + return sortedJson } async function getAllEventsJson() : Promise { - let jsonDirectory = getEventsJsonDirectoryPath(); - let validationNodes = getRequiedEventNodes(); - let events : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckEventData, validationNodes); - let sortedEvents : any[] = sortEventsJsonByStartDate(events); - let allEventsJson = {allEvents: sortedEvents}; + let jsonDirectory = getEventsJsonDirectoryPath() + let validationNodes = getRequiedEventNodes() + let events : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckEventData, validationNodes) + let sortedEvents : any[] = sortEventsJsonByStartDate(events) + let allEventsJson = {allEvents: sortedEvents} - return allEventsJson; + return allEventsJson } - //-----position function getPositionsJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/position'); + return path.join(process.cwd(), 'json/site_content/position') } function pluckPositionData(json : any) { - let plucked = {}; + let plucked = {} if('data' in json){ if('position' in json.data) { - plucked = json.data.position; + plucked = json.data.position } } - return plucked; - + return plucked } function getRequiedPositionNodes() { - let nodes = []; + let nodes = [] - nodes.push('id'); - nodes.push('positionName'); - nodes.push('positionLink'); - - return nodes; + nodes.push('id') + nodes.push('positionName') + nodes.push('positionLink') + return nodes } function sortPositionsJsonById(json : any[]) { - let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); + let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)) - return sortedJson; + return sortedJson } async function getAllPositionsJson() : Promise { - let jsonDirectory = getPositionsJsonDirectoryPath(); - let validationNodes = getRequiedPositionNodes(); - let positions : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckPositionData, validationNodes); - let sortedPositions : any[] = sortPositionsJsonById(positions); - let allPositionsJson = {allPositions: sortedPositions}; + let jsonDirectory = getPositionsJsonDirectoryPath() + let validationNodes = getRequiedPositionNodes() + let positions : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckPositionData, validationNodes) + let sortedPositions : any[] = sortPositionsJsonById(positions) + let allPositionsJson = {allPositions: sortedPositions} - return allPositionsJson; + return allPositionsJson } - //-----pressRelease function getPressReleasesJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/pressRelease'); + return path.join(process.cwd(), 'json/site_content/pressRelease') } function pluckPressReleaseData(json : any) { - let plucked = {}; + let plucked = {} if('data' in json){ if('pressRelease' in json.data) { - plucked = json.data.pressRelease; + plucked = json.data.pressRelease } } - return plucked; - + return plucked } function getRequiedPressReleaseNodes() { - let nodes = []; + let nodes = [] - nodes.push('id'); - nodes.push('title'); - nodes.push('date'); - nodes.push('link'); - - return nodes; + nodes.push('id') + nodes.push('title') + nodes.push('date') + nodes.push('link') + return nodes } function sortPressReleasesJsonByDate(json : any[]) { - let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.date) - datocmsDateToIng(a.date)); + let sortedJson : any[] = json.slice().sort((a : any, b: any) => datocmsDateToIng(b.date) - datocmsDateToIng(a.date)) - return sortedJson; + return sortedJson } async function getAllPressReleasesJson() : Promise { - let jsonDirectory = getPressReleasesJsonDirectoryPath(); - let validationNodes = getRequiedPressReleaseNodes(); - let pressReleases : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckPressReleaseData, validationNodes); - let sortedPressReleases : any[] = sortPressReleasesJsonByDate(pressReleases); - let allPressReleasesJson = {allPressReleases: sortedPressReleases}; + let jsonDirectory = getPressReleasesJsonDirectoryPath() + let validationNodes = getRequiedPressReleaseNodes() + let pressReleases : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckPressReleaseData, validationNodes) + let sortedPressReleases : any[] = sortPressReleasesJsonByDate(pressReleases) + let allPressReleasesJson = {allPressReleases: sortedPressReleases} - return allPressReleasesJson; + return allPressReleasesJson } - //-----team function getTeamsJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/team'); + return path.join(process.cwd(), 'json/site_content/team') } function pluckTeamData(json : any) { - let plucked = {}; + let plucked = {} if('data' in json){ if('team' in json.data) { - plucked = json.data.team; + plucked = json.data.team } } - return plucked; - + return plucked } function getRequiedTeamNodes() { - let nodes = []; + let nodes = [] - nodes.push('id'); - nodes.push('memberName'); - - return nodes; + nodes.push('id') + nodes.push('memberName') + return nodes } function sortTeamsJsonById(json : any[]) { - let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); + let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)) - return sortedJson; + return sortedJson } async function getAllTeamsJson() : Promise { - let jsonDirectory = getTeamsJsonDirectoryPath(); - let validationNodes = getRequiedTeamNodes(); - let teams : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckTeamData, validationNodes); - let sortedTeams : any[] = sortTeamsJsonById(teams); - let allTeamsJson = {allTeams: sortedTeams}; + let jsonDirectory = getTeamsJsonDirectoryPath() + let validationNodes = getRequiedTeamNodes() + let teams : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckTeamData, validationNodes) + let sortedTeams : any[] = sortTeamsJsonById(teams) + let allTeamsJson = {allTeams: sortedTeams} - return allTeamsJson; + return allTeamsJson } - //-----testimonial function getTestimonialsJsonDirectoryPath() { - return path.join(process.cwd(), 'json/site_content/testimonial'); + return path.join(process.cwd(), 'json/site_content/testimonial') } function pluckTestimonialData(json : any) { - let plucked = {}; + let plucked = {} if('data' in json){ if('testimonial' in json.data) { - plucked = json.data.testimonial; + plucked = json.data.testimonial } } - return plucked; - + return plucked } function getRequiedTestimonialNodes() { - let nodes = []; + let nodes = [] - nodes.push('id'); - nodes.push('testimonialImage'); - nodes.push('testimonialText'); - nodes.push('testimonialUsername'); - - return nodes; + nodes.push('id') + nodes.push('testimonialImage') + nodes.push('testimonialText') + nodes.push('testimonialUsername') + return nodes } function sortTestimonialsJsonById(json : any[]) { - let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)); + let sortedJson : any[] = json.slice().sort((a : any, b: any) => parseInt(a.id) - parseInt(b.id)) - return sortedJson; + return sortedJson } async function getAllTestimonialsJson() : Promise { - let jsonDirectory = getTestimonialsJsonDirectoryPath(); - let validationNodes = getRequiedTestimonialNodes(); - let testimonials : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckTestimonialData, validationNodes); - let sortedTestimonals : any[] = sortTestimonialsJsonById(testimonials); - let allTestimonialsJson = {allTestimonials: sortedTestimonals}; + let jsonDirectory = getTestimonialsJsonDirectoryPath() + let validationNodes = getRequiedTestimonialNodes() + let testimonials : any[] = await getJsonItemsFromDirectory(jsonDirectory, pluckTestimonialData, validationNodes) + let sortedTestimonals : any[] = sortTestimonialsJsonById(testimonials) + let allTestimonialsJson = {allTestimonials: sortedTestimonals} - return allTestimonialsJson; -} + return allTestimonialsJson +} \ No newline at end of file diff --git a/src/lib/datocms.ts b/src/lib/datocms.ts index 98cdc55..6a583da 100644 --- a/src/lib/datocms.ts +++ b/src/lib/datocms.ts @@ -2,9 +2,7 @@ import tiny from 'tiny-json-http' //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== -import { - datocmsQueryIntercept -} from 'lib/datocms-bypass' +import { datocmsQueryIntercept } from 'lib/datocms-bypass' //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== @@ -45,14 +43,14 @@ export async function request({ query, variables, preview }: props) { */ if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { - let interceptData : any = await datocmsQueryIntercept(query); + let interceptData : any = await datocmsQueryIntercept(query) if (interceptData.error) { - console.error(interceptData.error); - interceptData = {}; - return interceptData; + console.error(interceptData.error) + interceptData = {} + return interceptData } else { - return interceptData; + return interceptData } } else { let endpoint = 'https://graphql.datocms.com' @@ -78,8 +76,6 @@ export async function request({ query, variables, preview }: props) { } return body.data - } - + } //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ==================================================== - -} +} \ No newline at end of file diff --git a/src/pages/blog/[slug].tsx b/src/pages/blog/[slug].tsx index 85826af..b62960f 100644 --- a/src/pages/blog/[slug].tsx +++ b/src/pages/blog/[slug].tsx @@ -68,17 +68,17 @@ export const getStaticPaths: GetStaticPaths = async () => { //const posts = await getAllBlogPostsSlugs() async function postsSlugsJsonFromSource() : Promise { - let postsSlugsJson : any = {}; + let postsSlugsJson : any = {} if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { - postsSlugsJson = await getAllBlogPostsSlugsFromSource(); + postsSlugsJson = await getAllBlogPostsSlugsFromSource() } else { - postsSlugsJson = await getAllBlogPostsSlugs(); + postsSlugsJson = await getAllBlogPostsSlugs() } - return postsSlugsJson; + return postsSlugsJson } - const posts = await postsSlugsJsonFromSource(); + const posts = await postsSlugsJsonFromSource() /* const paths = posts?.map((post) => ({ @@ -104,7 +104,7 @@ export const getStaticProps: GetStaticProps = async ( //===== \/ START NEXT_PUBLIC_DATOCMS_BYPASS \/ ==================================================== //const post = await (await cms().SingleBlogPost({ slug })).blogPost - const post : any = await getSingleBlogPostBySlugFromSource(slug); + const post : any = await getSingleBlogPostBySlugFromSource(slug) /* const allBlogPosts = await serverGetBlogPosts({ @@ -121,16 +121,16 @@ export const getStaticProps: GetStaticProps = async ( async function getRelatedBlogPostsFromSource() : Promise { - let relatedBlogPosts = {}; + let relatedBlogPosts = {} if (process.env.NEXT_PUBLIC_DATOCMS_BYPASS_TYPE === 'local_json') { try { - relatedBlogPosts = await getRelatedBlogPosts(post, 3); + relatedBlogPosts = await getRelatedBlogPosts(post, 3) } catch (e) { - console.log('Failed pull related blog posts.'); - console.log('The attempt resulted in the following error:'); - console.log(e); + console.log('Failed pull related blog posts.') + console.log('The attempt resulted in the following error:') + console.log(e) } } else { @@ -146,13 +146,13 @@ export const getStaticProps: GetStaticProps = async ( ) ) - relatedBlogPosts = relatedPosts; + relatedBlogPosts = relatedPosts } - return relatedBlogPosts; + return relatedBlogPosts } - const relatedPosts = await getRelatedBlogPostsFromSource(); + const relatedPosts = await getRelatedBlogPostsFromSource() //===== /\ FINISH NEXT_PUBLIC_DATOCMS_BYPASS /\ ====================================================