Skip to content

Commit

Permalink
split target function into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
zchsh committed Sep 9, 2024
1 parent b7223fe commit e9add7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
23 changes: 23 additions & 0 deletions build-libs/get-latest-content-ref-for-product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Fetch the latest ref from the content API to ensure the redirects are accurate.
*
* TODO: refactor to fetch from known nav-data, rather than from version
* metadata. Intent being to allow upstream changes to version metadata such
* that version metadata `ref` will only be updated when version metadata is
* actually updated (as opposed to on every content upload, as is currently
* the case).
*
* @param {string} product
* @returns {Promise<string>}
*/
async function getLatestContentRefForProduct(product) {
const contentUrl = new URL('https://content.hashicorp.com')
contentUrl.pathname = `/api/content/${product}/version-metadata/latest`
const latestRef = await fetch(contentUrl.toString())
.then((resp) => resp.json())
.then((json) => json.result.ref)

return latestRef
}

module.exports = getLatestContentRefForProduct
16 changes: 1 addition & 15 deletions build-libs/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path')

const { isDeployPreview } = require('../src/lib/env-checks')
const fetchGithubFile = require('./fetch-github-file')
const getLatestContentRefForProduct = require('./get-latest-content-ref-for-product')
const { getTutorialRedirects } = require('./tutorial-redirects')
const {
getDocsDotHashiCorpRedirects,
Expand All @@ -26,21 +27,6 @@ const HOSTNAME_MAP = {
'test-st.hashi-mktg.com': 'sentinel',
}

/**
* Fetch the latest ref from the content API to ensure the redirects are accurate.
*
* @TODO save the redirects to the content database and expose them directly via the API
*/
async function getLatestContentRefForProduct(product) {
const contentUrl = new URL('https://content.hashicorp.com')
contentUrl.pathname = `/api/content/${product}/version-metadata/latest`
const latestRef = await fetch(contentUrl.toString())
.then((resp) => resp.json())
.then((json) => json.result.ref)

return latestRef
}

/**
* Load redirects from a content repository.
*
Expand Down

0 comments on commit e9add7a

Please sign in to comment.