{"version":3,"file":"index.mjs","sources":["../lib/index.ts"],"sourcesContent":["/**\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 {string} app the id of the app the file belongs to\n * @param {string} file the file path relative to the app folder\n * @return {string} URL with webroot to a file\n */\nexport const linkTo = (app: string, file: string) => generateFilePath(app, '', file)\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) {\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)\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"],"names":["linkTo","app","file","generateFilePath","linkToRemoteBase","service","generateRemoteUrl","options","_a","getBaseUrl","generateOcsUrl","url","params","version","_generateUrlPath","allOptions","_build","text","vars","a","b","r","generateUrl","_b","_c","baseOrRootURL","getRootUrl","imagePath","type","isCore","isPHP","link","getAppRootUrl","webroot","pos","index"],"mappings":"AAoCO,MAAMA,IAAS,CAACC,GAAaC,MAAiBC,EAAiBF,GAAK,IAAIC,CAAI,GAQ7EE,IAAmB,CAACC,MAAoB,iBAAiBA,GAQlDC,IAAoB,CAACD,GAAiBE,MAAyB;AApD5E,MAAAC;AAsDQ,WADSA,IAAAD,KAAA,OAAA,SAAAA,EAAS,YAAT,OAAAC,IAAoBC,EAAW,KAC9BL,EAAiBC,CAAO;AAC1C,GAUaK,IAAiB,CAACC,GAAaC,GAAiBL,MAAyB;AAjEtF,MAAAC;AAsEC,QAAMK,IAJa,OAAO,OAAO;AAAA,IAChC,YAAY;AAAA,EAAA,GACVN,KAAW,CAAA,CAAE,EAEY,eAAe,IAAK,IAAI;AAGpD,WAFgBC,IAAAD,KAAA,OAAA,SAAAA,EAAS,YAAT,OAAAC,IAAoBC,EAAW,KAE9B,WAAWI,IAAU,SAASC,EAAiBH,GAAKC,GAAQL,CAAO;AACrF,GAYMO,IAAmB,CAACH,GAAaC,GAAiBL,MAAyB;AAC1E,QAAAQ,IAAa,OAAO,OAAO;AAAA,IAChC,QAAQ;AAAA,EAAA,GACNR,KAAW,CAAA,CAAE,GAEVS,IAAS,SAASC,GAAcC,GAA+B;AACpE,WAAAA,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