{"version":3,"file":"index.mjs","sources":["../lib/base.ts","../lib/avatar.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: GPL-3.0-or-later\n */\n\n/**\n * Options for URL parameter replacement\n */\nexport interface UrlOptions {\n\t/**\n\t * Set to false if parameters should not be URL encoded\n\t * @default true\n\t */\n\tescape?: boolean\n\n\t/**\n\t * True if you want to force index.php being added\n\t * @default false\n\t */\n\tnoRewrite?: boolean\n\n\t/**\n\t * OCS version to use\n\t * @default 2\n\t */\n\tocsVersion?: number\n\n\t/**\n\t * URL to use as a base (defaults to current instance)\n\t * @default ''\n\t */\n\tbaseURL?: string\n}\n\n/**\n * Get an url with webroot to a file in an app\n *\n * @param app - The id of the app the file belongs to\n * @param file the file path relative to the app folder\n * @return URL with webroot to a file\n */\nexport function linkTo(app: string, file: string): string {\n\treturn generateFilePath(app, '', file)\n}\n\n/**\n * Creates a relative url for remote use\n *\n * @param {string} service id\n * @return {string} the url\n */\nconst linkToRemoteBase = (service: string) => '/remote.php/' + service\n\n/**\n * Creates an absolute url for remote use\n * @param {string} service id\n * @return {string} the url\n * @param {UrlOptions} [options] options for the parameter replacement\n */\nexport const generateRemoteUrl = (service: string, options?: UrlOptions) => {\n\tconst baseURL = options?.baseURL ?? getBaseUrl()\n\treturn baseURL + linkToRemoteBase(service)\n}\n\n/**\n * Get the base path for the given OCS API service\n *\n * @param {string} url OCS API service url\n * @param {object} params parameters to be replaced into the service url\n * @param {UrlOptions} options options for the parameter replacement\n * @return {string} Absolute path for the OCS URL\n */\nexport const generateOcsUrl = (url: string, params?: object, options?: UrlOptions) => {\n\tconst allOptions = Object.assign({\n\t\tocsVersion: 2,\n\t}, options || {})\n\n\tconst version = (allOptions.ocsVersion === 1) ? 1 : 2\n\tconst baseURL = options?.baseURL ?? getBaseUrl()\n\n\treturn baseURL + '/ocs/v' + version + '.php' + _generateUrlPath(url, params, options)\n}\n\n/**\n * Generate a url path, which can contain parameters\n *\n * Parameters will be URL encoded automatically\n *\n * @param {string} url address (can contain placeholders e.g. /call/{token} would replace {token} with the value of params.token\n * @param {object} params parameters to be replaced into the address\n * @param {UrlOptions} options options for the parameter replacement\n * @return {string} Path part for the given URL\n */\nconst _generateUrlPath = (url: string, params?: object, options?: UrlOptions) => {\n\tconst allOptions = Object.assign({\n\t\tescape: true,\n\t}, options || {})\n\n\tconst _build = function(text: string, vars: Record<string, unknown>) {\n\t\tvars = vars || {}\n\t\treturn text.replace(/{([^{}]*)}/g,\n\t\t\tfunction(a: string, b: string) {\n\t\t\t\tconst r = vars[b]\n\t\t\t\tif (allOptions.escape) {\n\t\t\t\t\treturn (typeof r === 'string' || typeof r === 'number') ? encodeURIComponent(r.toString()) : encodeURIComponent(a)\n\t\t\t\t} else {\n\t\t\t\t\treturn (typeof r === 'string' || typeof r === 'number') ? r.toString() : a\n\t\t\t\t}\n\t\t\t},\n\t\t)\n\t}\n\n\tif (url.charAt(0) !== '/') {\n\t\turl = '/' + url\n\t}\n\n\treturn _build(url, (params || {}) as Record<string, unknown>)\n}\n\n/**\n * Generate the url with webroot for the given relative url, which can contain parameters\n * If options.baseURL is provided, generate the absolute url pointing ro remote server\n *\n * Parameters will be URL encoded automatically\n *\n * @param {string} url address (can contain placeholders e.g. /call/{token} would replace {token} with the value of params.token\n * @param {object} params parameters to be replaced into the url\n * @param {UrlOptions} options options for the parameter replacement\n * @return {string} URL with webroot for the given relative URL\n */\nexport const generateUrl = (url: string, params?: object, options?: UrlOptions) => {\n\tconst allOptions = Object.assign({\n\t\tnoRewrite: false,\n\t}, options || {})\n\n\tconst baseOrRootURL = options?.baseURL ?? getRootUrl()\n\n\tif (window?.OC?.config?.modRewriteWorking === true && !allOptions.noRewrite) {\n\t\treturn baseOrRootURL + _generateUrlPath(url, params, options)\n\t}\n\n\treturn baseOrRootURL + '/index.php' + _generateUrlPath(url, params, options)\n}\n\n/**\n * Get the path with webroot to an image file\n * if no extension is given for the image, it will automatically add .svg\n *\n * @param {string} app the app id to which the image belongs\n * @param {string} file the name of the image file\n * @return {string}\n */\nexport const imagePath = (app: string, file: string) => {\n\tif (!file.includes('.')) {\n\t\t// if no extension is given, use svg\n\t\treturn generateFilePath(app, 'img', `${file}.svg`)\n\t}\n\n\treturn generateFilePath(app, 'img', file)\n}\n\n/**\n * Get the url with webroot for a file in an app\n *\n * @param {string} app the id of the app\n * @param {string} type the type of the file to link to (e.g. css,img,ajax.template)\n * @param {string} file the filename\n * @return {string} URL with webroot for a file in an app\n */\nexport const generateFilePath = (app: string, type: string, file: string) => {\n\tconst isCore = window?.OC?.coreApps?.includes(app) ?? false\n\tconst isPHP = file.slice(-3) === 'php'\n\tlet link = getRootUrl()\n\tif (isPHP && !isCore) {\n\t\tlink += `/index.php/apps/${app}`\n\t\tif (type) {\n\t\t\tlink += `/${encodeURI(type)}`\n\t\t}\n\t\tif (file !== 'index.php') {\n\t\t\tlink += `/${file}`\n\t\t}\n\t} else if (!isPHP && !isCore) {\n\t\tlink = getAppRootUrl(app)\n\t\tif (type) {\n\t\t\tlink += `/${type}/`\n\t\t}\n\t\tif (link.at(-1) !== '/') {\n\t\t\tlink += '/'\n\t\t}\n\t\tlink += file\n\t} else {\n\t\tif ((app === 'settings' || app === 'core' || app === 'search') && type === 'ajax') {\n\t\t\tlink += '/index.php'\n\t\t}\n\t\tif (app) {\n\t\t\tlink += `/${app}`\n\t\t}\n\t\tif (type) {\n\t\t\tlink += `/${type}`\n\t\t}\n\t\tlink += `/${file}`\n\t}\n\treturn link\n}\n\n/**\n * Return the full base URL where this Nextcloud instance\n * is accessible, with a web root included.\n * For example \"https://company.com/nextcloud\".\n *\n * @return {string} base URL\n */\nexport const getBaseUrl = () => window.location.protocol + '//' + window.location.host + getRootUrl()\n\n/**\n * Return the web root path where this Nextcloud instance\n * is accessible, with a leading slash.\n * For example \"/nextcloud\".\n *\n * @return {string} web root path\n */\nexport function getRootUrl(): string {\n\tlet webroot = window._oc_webroot\n\n\tif (typeof webroot === 'undefined') {\n\t\twebroot = location.pathname\n\t\tconst pos = webroot.indexOf('/index.php/')\n\t\tif (pos !== -1) {\n\t\t\twebroot = webroot.slice(0, pos)\n\t\t} else {\n\t\t\tconst index = webroot.indexOf('/', 1)\n\t\t\t// Make sure to not cut end of path if there is just the webroot like `/nextcloud`\n\t\t\twebroot = webroot.slice(0, index > 0 ? index : undefined)\n\t\t}\n\t}\n\treturn webroot\n}\n\n/**\n * Return the web root path for a given app\n * @param {string} app The ID of the app\n */\nexport function getAppRootUrl(app: string): string {\n\tconst webroots = window._oc_appswebroots ?? {}\n\treturn webroots[app] ?? ''\n}\n","/*!\n * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: GPL-3.0-or-later\n */\n\nimport { generateUrl } from './base.ts'\n\nexport interface AvatarUrlOptions {\n\t/**\n\t * Should a dark theme variant of the avatar be used.\n\t *\n\t * @default false\n\t */\n\tisDarkTheme?: boolean\n\n\t/**\n\t * If the given user is is a guest user.\n\t * This is needed as guest users use a different API endpoint.\n\t *\n\t * @default false\n\t */\n\tisGuestUser?: boolean\n\n\t/**\n\t * Avatar image size.\n\t * The backend only supports 64px and 512px.\n\t *\n\t * @default 64\n\t */\n\tsize?: 64 | 512\n}\n\n/**\n * Get the avatar URL for a given user.\n *\n * @param user - The user id to lookup the avatar of\n * @param options - Options for configuring the avatar\n * @return Relative URL for the avatar\n */\nexport function generateAvatarUrl(user: string, options?: AvatarUrlOptions): string {\n\t// backend only supports 64 and 512px\n\t// so we only request the needed size for better caching of the request.\n\tconst size = (options?.size || 64) <= 64\n\t\t? 64\n\t\t: 512\n\n\tconst guestUrl = options?.isGuestUser\n\t\t? '/guest'\n\t\t: ''\n\tconst themeUrl = options?.isDarkTheme\n\t\t? '/dark'\n\t\t: ''\n\n\treturn generateUrl(`/avatar${guestUrl}/{user}/{size}${themeUrl}`, {\n\t\tuser,\n\t\tsize,\n\t})\n}\n"],"names":[],"mappings":"AA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