generated from UCLALibrary/nuxt3-starter-template
-
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.
feat: APPS-3023 Create composable useContentIndexer to index craft da…
…ta used on all routes for FTVA (#67) * create composable and add it to all slug pages * add the composable * delete extraneous composable import in blog * make FAILED TO INDEX console message easier to read in console * linting * delete console satement * fix path to composable * fix path to composable * move gql and composable inside of blog/article * add helpers after gql and composables in blog * move comments in the useContentIndexer composable * standardize imports * standardize imports * standardize imports * standardize imports * get rid of comment * get rid of comment * revert to NOT using $fetch * remove $fetch * linting
- Loading branch information
1 parent
cb89674
commit e76144d
Showing
9 changed files
with
188 additions
and
13 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,85 @@ | ||
export function useContentIndexer() { | ||
const esIndex = useRuntimeConfig().public.esTempIndex | ||
const esURL = useRuntimeConfig().public.esURL | ||
const esReadKey = useRuntimeConfig().public.esReadKey | ||
const esWriteKey = useRuntimeConfig().esWriteKey | ||
|
||
async function indexContent(data, slug) { | ||
try { | ||
if (data && slug && esIndex) { | ||
/* console.log( | ||
"this is the elasticsearch plugin: " + JSON.stringify(data) | ||
) */ | ||
// console.log(`Requesting URL: ${esURL}/${esIndex}/_doc/${slug}`) | ||
const docExists = await fetch( | ||
`${esURL}/${esIndex}/_doc/${slug}`, | ||
{ | ||
headers: { | ||
Authorization: `ApiKey ${esReadKey}`, | ||
}, | ||
} | ||
) | ||
|
||
const body = await docExists.text() | ||
const docExistsResponseValue = JSON.parse(body) | ||
// console.log('Existing data in ES', docExistsResponseValue) | ||
|
||
if (docExistsResponseValue && docExistsResponseValue._source) { | ||
// console.log('GET-RESPONSE: ' + slug) | ||
const updateUrl = `${esURL}/${esIndex}/_update/${slug}` | ||
// console.log('ES update url', updateUrl) | ||
const postBody = { | ||
doc: data | ||
} | ||
// console.log('postBody', JSON.stringify(postBody)) | ||
const updateResponse = await fetch( | ||
`${esURL}/${esIndex}/_update/${slug}`, | ||
{ | ||
headers: { | ||
Authorization: `ApiKey ${esWriteKey}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify(postBody), | ||
} | ||
) | ||
// console.log('Update document in ES', updateResponse) | ||
const updateJson = await updateResponse.text() | ||
console.log('Update in ES', updateJson) | ||
} else { | ||
const response = await fetch( | ||
`${esURL}/${esIndex}/_doc/${slug}`, | ||
{ | ||
headers: { | ||
Authorization: `ApiKey ${esWriteKey}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify(data), | ||
} | ||
) | ||
|
||
// console.log('Create a new document in ES:', await response.text()) | ||
} | ||
} else { | ||
console.warn('not indexing anything') | ||
} | ||
} catch (e) { | ||
console.error('skip indexing if connection times out during builds in the mean time: ' + e.message) | ||
console.warn('skip indexing if connection times out during builds in the mean time: ' + e.message) | ||
throw new Error('Elastic Search Indexing failed ' + e) // TODO uncomment when cause is clear | ||
} | ||
} | ||
|
||
return { | ||
indexContent | ||
} | ||
} | ||
|
||
// Checks the page slug | ||
// Calls ES (Elastic Search) | ||
// Checks if the document exists in ES | ||
// if the document already exists | ||
// it overwrites/replaces it in the index | ||
// if the document doesn't exist | ||
// it adds it to the index |
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
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
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
e76144d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://test-ftva.library.ucla.edu as production
🚀 Deployed on https://672ec4d6e3f3379224e4a1e5--test-ftva.netlify.app