From b99bfabc235e4c7cb83bc3ba9ca303a7416ac2af Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Tue, 4 Jun 2024 14:57:31 +0200 Subject: [PATCH] feat: add url utils --- src/url_utils.ts | 156 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 src/url_utils.ts diff --git a/src/url_utils.ts b/src/url_utils.ts new file mode 100644 index 0000000..8c63279 --- /dev/null +++ b/src/url_utils.ts @@ -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 { + //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 +}