feat: add url utils

This commit is contained in:
Julien Oculi 2024-06-04 14:57:31 +02:00
parent f6343c7db4
commit b99bfabc23

156
src/url_utils.ts Normal file
View file

@ -0,0 +1,156 @@
import { COHABIT_GIT_URL, PORTFOLIO_BASE_URL } from '../config.ts'
import { User } from './user_utils.ts'
/**
* Get raw files repository base url.
*
* @param user Login of the owner user.
* @returns Repository url for raw files.
*
* @example
* ```ts
* import { getRepoUrl } from './url_utils.ts'
*
* const repoUrl = getRepoUrl('john.doe')
* console.assert(repoUrl.href === 'https://git.cohabit.fr/john.doe/.portfolio/raw/branch/main/')
* ```
*/
export function getRepoUrl(user: User): URL {
const userRepoUrl = new URL(`./${user}/`, COHABIT_GIT_URL)
return new URL(PORTFOLIO_BASE_URL, userRepoUrl)
}
/**
* Get portfolio index URL depending of the presence of `/dist/index.html`.
*
* @param baseUrl Repository base url.
* @returns Index url, `./dist/index.html` is available else './README.md'.
*
* @example Basic WYSIWYG index
* ```ts
* import { getRepoUrl, getIndexUrl } from './url_utils.ts'
*
* const repoUrl = getRepoUrl('john.doe')
* const index = getIndexUrl(repoUrl)
*
* console.assert(index.href === new URL('./README.md', repoUrl).href)
* ```
*
* @example Generated index
* ```ts
* import { getRepoUrl, getIndexUrl } from './url_utils.ts'
*
* const repoUrl = getRepoUrl('john.doe')
* const baseUrl = new URL('./dist/', repoUrl)
* const index = getIndexUrl(baseUrl)
*
* console.assert(index.href === new URL('./dist/index.html', repoUrl).href)
* ```
*/
export function getIndexUrl(baseUrl: URL): URL {
if (baseUrl.pathname.endsWith('./dist/')) {
return new URL('./index.html', baseUrl)
}
return new URL('./README.md', baseUrl)
}
/**
* Get portfolio source URL from request URL.
*
* @param filePath request pathname of the file.
* @param baseUrl Repository base url.
* @returns URL of the source file.
*
* @example Basic WYSIWYG index
* ```ts
* import { getRepoUrl, getSourceUrl } from './url_utils.ts'
*
* const repoUrl = getRepoUrl('john.doe')
* const index = getSourceUrl('/', repoUrl)
* const file = getSourceUrl('/dir/path/file.any', repoUrl)
*
* console.assert(index.href === new URL('./README.md', repoUrl).href)
* console.assert(file.href === new URL('./dir/path/file.any', repoUrl).href)
* ```
*
* @example Generated index
* ```ts
* import { getRepoUrl, getSourceUrl } from './url_utils.ts'
*
* const repoUrl = getRepoUrl('john.doe')
* const baseUrl = new URL('./dist/', repoUrl)
* const index = getSourceUrl('/', baseUrl)
* const file = getSourceUrl('/dir/path/file.any', baseUrl)
*
* console.assert(index.href === new URL('./dist/index.html', repoUrl).href)
* console.assert(file.href === new URL('./dist/dir/path/file.any', repoUrl).href)
* ```
*/
export function getSourceUrl(filePath: string, baseUrl: URL): URL {
if (filePath === '/') {
return getIndexUrl(baseUrl)
}
return new URL(filePath, baseUrl)
}
/**
* Determine portfolio base URL from the repository structure.
* Check the presence of `./dist/index.html` to return either `./dist/` either `./` as base path.
*
* @param user User login of owner of the portfolio.
* @returns Portfolio base URL.
*
* @example Basic WYSIWYG index
*
* # Portfolio structure
*
* ```
* /
* README.md
* *
* ```
*
* # Resolved pathname
*
* ```ts
* import { getBaseUrl } from './url_utils.ts'
*
* const baseUrl = await getBaseUrl('john.doe')
*
* console.assert(baseUrl.pathname === '/')
* ```
*
* @example Generated index
*
* # Portfolio structure
*
* ```
* /
* dist
* index.html
* *
* *
* ```
*
* # Resolved pathname
*
* ```ts
* import { getBaseUrl } from './url_utils.ts'
*
* const baseUrl = await getBaseUrl('john.doe')
*
* console.assert(baseUrl.pathname === '/dist/')
* ```
*/
export async function getBaseUrl(user: User): Promise<URL> {
//TODO check network errors before last return
const repoUrl = getRepoUrl(user)
const response = await fetch(new URL('./dist/index.html', repoUrl))
if (response.ok) {
return new URL('./dist/', repoUrl)
}
return repoUrl
}