Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nextcloud/router

Package Overview
Dependencies
Maintainers
13
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nextcloud/router - npm Package Compare versions

Comparing version 2.2.1 to 3.0.0

__tests__/paths.spec.ts

25

CHANGELOG.md

@@ -5,2 +5,27 @@ # Changelog

## 3.0.0 - 2023-02-01
[Full Changelog](https://github.com/nextcloud-libraries/nextcloud-router/compare/v2.2.1...v3.0.0)
### Enhancements
* Drop `core-js` and move to vite by @susnux in https://github.com/nextcloud-libraries/nextcloud-router/pull/563
* enh(tests): Add unit tests and CI workflow for testing by @susnux in https://github.com/nextcloud-libraries/nextcloud-router/pull/564
* feat: add method to get a base URL, allow to pass remote base by @Antreesy in https://github.com/nextcloud-libraries/nextcloud-router/pull/558
### Fixed
* fix: Adjust `generateFilePath` to include `type` also for `index.php` by @susnux in https://github.com/nextcloud-libraries/nextcloud-router/pull/566
* fix(getRootUrl)!: If not configured use first subdirectory as webroot instead of last by @susnux in https://github.com/nextcloud-libraries/nextcloud-router/pull/570
### Changed
* Clean up code (drop unreachable code) and minor modernization by @susnux in https://github.com/nextcloud-libraries/nextcloud-router/pull/565
* chore: Add ESLint to project and fix issues reported by @susnux in https://github.com/nextcloud-libraries/nextcloud-router/pull/561
* Make README a bit nicer by adding badges for coverage and documentation by @susnux in https://github.com/nextcloud-libraries/nextcloud-router/pull/569
### Dependencies
* build(deps-dev): Bump vitest from 1.2.1 to 1.2.2 by @dependabot in https://github.com/nextcloud-libraries/nextcloud-router/pull/567
* build(deps-dev): Bump @vitest/coverage-v8 from 1.2.1 to 1.2.2 by @dependabot in https://github.com/nextcloud-libraries/nextcloud-router/pull/568
### New Contributors
* @Antreesy made their first contribution in https://github.com/nextcloud-libraries/nextcloud-router/pull/558
## 2.2.1 - 2024-01-24

@@ -7,0 +32,0 @@ [Full Changelog](https://github.com/nextcloud/nextcloud-router/compare/v2.2.0...v2.2.1)

111

dist/index.d.ts
/**
* Get an url with webroot to a file in an app
* Get the url with webroot for a file in an app
*
* @param {string} app the id of the app the file belongs to
* @param {string} file the file path relative to the app folder
* @return {string} URL with webroot to a file
* @param {string} app the id of the app
* @param {string} type the type of the file to link to (e.g. css,img,ajax.template)
* @param {string} file the filename
* @return {string} URL with webroot for a file in an app
*/
export declare const linkTo: (app: string, file: string) => string;
export declare const generateFilePath: (app: string, type: string, file: string) => string;
/**
* @brief Creates an absolute url for remote use
* @param {string} service id
* @return {string} the url
*/
export declare const generateRemoteUrl: (service: string) => string;
/**
* Get the base path for the given OCS API service

@@ -21,14 +17,17 @@ *

* @param {UrlOptions} options options for the parameter replacement
* @param {boolean} options.escape Set to false if parameters should not be URL encoded (default true)
* @param {Number} options.ocsVersion OCS version to use (defaults to 2)
* @return {string} Absolute path for the OCS URL
*/
export declare const generateOcsUrl: (url: string, params?: object, options?: UrlOptions) => string;
export interface UrlOptions {
escape: boolean;
noRewrite: boolean;
ocsVersion: Number;
}
/**
* Creates an absolute url for remote use
* @param {string} service id
* @return {string} the url
* @param {UrlOptions} [options] options for the parameter replacement
*/
export declare const generateRemoteUrl: (service: string, options?: UrlOptions) => string;
/**
* Generate the url with webroot for the given relative url, which can contain parameters
* If options.baseURL is provided, generate the absolute url pointing ro remote server
*

@@ -40,8 +39,31 @@ * Parameters will be URL encoded automatically

* @param {UrlOptions} options options for the parameter replacement
* @param {boolean} options.noRewrite True if you want to force index.php being added
* @param {boolean} options.escape Set to false if parameters should not be URL encoded (default true)
* @return {string} URL with webroot for the given relative URL
*/
export declare const generateUrl: (url: string, params?: object, options?: UrlOptions) => string;
/**
* Return the web root path for a given app
* @param {string} app The ID of the app
*/
export declare function getAppRootUrl(app: string): string;
/**
* Return the full base URL where this Nextcloud instance
* is accessible, with a web root included.
* For example "https://company.com/nextcloud".
*
* @return {string} base URL
*/
export declare const getBaseUrl: () => string;
/**
* Return the web root path where this Nextcloud instance
* is accessible, with a leading slash.
* For example "/nextcloud".
*
* @return {string} web root path
*/
export declare function getRootUrl(): string;
/**
* Get the path with webroot to an image file

@@ -56,23 +78,38 @@ * if no extension is given for the image, it will automatically decide

export declare const imagePath: (app: string, file: string) => string;
/**
* Get the url with webroot for a file in an app
* Get an url with webroot to a file in an app
*
* @param {string} app the id of the app
* @param {string} type the type of the file to link to (e.g. css,img,ajax.template)
* @param {string} file the filename
* @return {string} URL with webroot for a file in an app
* @param {string} app the id of the app the file belongs to
* @param {string} file the file path relative to the app folder
* @return {string} URL with webroot to a file
*/
export declare const generateFilePath: (app: string, type: string, file: string) => string;
export declare const linkTo: (app: string, file: string) => string;
/**
* Return the web root path where this Nextcloud instance
* is accessible, with a leading slash.
* For example "/nextcloud".
*
* @return {string} web root path
* Options for URL parameter replacement
*/
export declare function getRootUrl(): string;
/**
* Return the web root path for a given app
* @param {string} app The ID of the app
*/
export declare function getAppRootUrl(app: string): string;
export declare interface UrlOptions {
/**
* Set to false if parameters should not be URL encoded
* @default true
*/
escape?: boolean;
/**
* True if you want to force index.php being added
* @default false
*/
noRewrite?: boolean;
/**
* OCS version to use
* @default 2
*/
ocsVersion?: number;
/**
* URL to use as a base (defaults to current instance)
* @default ''
*/
baseURL?: string;
}
export { }
/**
* Options for URL parameter replacement
*/
export interface UrlOptions {
/**
* Set to false if parameters should not be URL encoded
* @default true
*/
escape?: boolean
/**
* True if you want to force index.php being added
* @default false
*/
noRewrite?: boolean
/**
* OCS version to use
* @default 2
*/
ocsVersion?: number
/**
* URL to use as a base (defaults to current instance)
* @default ''
*/
baseURL?: string
}
/**
* Get an url with webroot to a file in an app

@@ -16,10 +45,14 @@ *

*/
const linkToRemoteBase = (service: string) => getRootUrl() + '/remote.php/' + service
const linkToRemoteBase = (service: string) => '/remote.php/' + service
/**
* @brief Creates an absolute url for remote use
* Creates an absolute url for remote use
* @param {string} service id
* @return {string} the url
* @param {UrlOptions} [options] options for the parameter replacement
*/
export const generateRemoteUrl = (service: string) => window.location.protocol + '//' + window.location.host + linkToRemoteBase(service)
export const generateRemoteUrl = (service: string, options?: UrlOptions) => {
const baseURL = options?.baseURL ?? getBaseUrl()
return baseURL + linkToRemoteBase(service)
}

@@ -32,22 +65,15 @@ /**

* @param {UrlOptions} options options for the parameter replacement
* @param {boolean} options.escape Set to false if parameters should not be URL encoded (default true)
* @param {Number} options.ocsVersion OCS version to use (defaults to 2)
* @return {string} Absolute path for the OCS URL
*/
export const generateOcsUrl = (url: string, params?: object, options?: UrlOptions) => {
const allOptions = Object.assign({
ocsVersion: 2
}, options || {})
const allOptions = Object.assign({
ocsVersion: 2,
}, options || {})
const version = (allOptions.ocsVersion === 1) ? 1 : 2
const version = (allOptions.ocsVersion === 1) ? 1 : 2
const baseURL = options?.baseURL ?? getBaseUrl()
return window.location.protocol + '//' + window.location.host + getRootUrl() + '/ocs/v' + version + '.php' + _generateUrlPath(url, params, options);
return baseURL + '/ocs/v' + version + '.php' + _generateUrlPath(url, params, options)
}
export interface UrlOptions {
escape: boolean,
noRewrite: boolean,
ocsVersion: Number
}
/**

@@ -64,25 +90,25 @@ * Generate a url path, which can contain parameters

const _generateUrlPath = (url: string, params?: object, options?: UrlOptions) => {
const allOptions = Object.assign({
escape: true
}, options || {})
const allOptions = Object.assign({
escape: true,
}, options || {})
const _build = function (text: string, vars: object) {
vars = vars || {};
return text.replace(/{([^{}]*)}/g,
function (a: string, b: any) {
var r = vars[b];
if (allOptions.escape) {
return (typeof r === 'string' || typeof r === 'number') ? encodeURIComponent(r.toString()) : encodeURIComponent(a);
} else {
return (typeof r === 'string' || typeof r === 'number') ? r.toString() : a;
}
}
);
};
const _build = function(text: string, vars: Record<string, unknown>) {
vars = vars || {}
return text.replace(/{([^{}]*)}/g,
function(a: string, b: string) {
const r = vars[b]
if (allOptions.escape) {
return (typeof r === 'string' || typeof r === 'number') ? encodeURIComponent(r.toString()) : encodeURIComponent(a)
} else {
return (typeof r === 'string' || typeof r === 'number') ? r.toString() : a
}
},
)
}
if (url.charAt(0) !== '/') {
url = '/' + url;
}
if (url.charAt(0) !== '/') {
url = '/' + url
}
return _build(url, params || {});
return _build(url, (params || {}) as Record<string, unknown>)
}

@@ -92,2 +118,3 @@

* Generate the url with webroot for the given relative url, which can contain parameters
* If options.baseURL is provided, generate the absolute url pointing ro remote server
*

@@ -99,16 +126,16 @@ * Parameters will be URL encoded automatically

* @param {UrlOptions} options options for the parameter replacement
* @param {boolean} options.noRewrite True if you want to force index.php being added
* @param {boolean} options.escape Set to false if parameters should not be URL encoded (default true)
* @return {string} URL with webroot for the given relative URL
*/
export const generateUrl = (url: string, params?: object, options?: UrlOptions) => {
const allOptions = Object.assign({
noRewrite: false
}, options || {})
const allOptions = Object.assign({
noRewrite: false,
}, options || {})
if (window?.OC?.config?.modRewriteWorking === true && !allOptions.noRewrite) {
return getRootUrl() + _generateUrlPath(url, params, options);
}
const baseOrRootURL = options?.baseURL ?? getRootUrl()
return getRootUrl() + '/index.php' + _generateUrlPath(url, params, options);
if (window?.OC?.config?.modRewriteWorking === true && !allOptions.noRewrite) {
return baseOrRootURL + _generateUrlPath(url, params, options)
}
return baseOrRootURL + '/index.php' + _generateUrlPath(url, params, options)
}

@@ -126,8 +153,8 @@

export const imagePath = (app: string, file: string) => {
if (file.indexOf('.') === -1) {
//if no extension is given, use svg
return generateFilePath(app, 'img', file + '.svg')
}
if (file.indexOf('.') === -1) {
// if no extension is given, use svg
return generateFilePath(app, 'img', file + '.svg')
}
return generateFilePath(app, 'img', file)
return generateFilePath(app, 'img', file)
}

@@ -144,44 +171,47 @@

export const generateFilePath = (app: string, type: string, file: string) => {
const isCore = window?.OC?.coreApps?.indexOf(app) !== -1
let link = getRootUrl()
if (file.substring(file.length - 3) === 'php' && !isCore) {
link += '/index.php/apps/' + app;
if (file !== 'index.php') {
link += '/'
if (type) {
link += encodeURI(type + '/')
}
link += file
}
} else if (file.substring(file.length - 3) !== 'php' && !isCore) {
link = getAppRootUrl(app)
if (type) {
link += '/' + type + '/'
}
if (link.substring(link.length - 1) !== '/') {
link += '/'
}
link += file
} else {
if ((app === 'settings' || app === 'core' || app === 'search') && type === 'ajax') {
link += '/index.php/'
} else {
link += '/'
}
if (!isCore) {
link += 'apps/'
}
if (app !== '') {
app += '/'
link += app
}
if (type) {
link += type + '/'
}
link += file
}
return link
const isCore = window?.OC?.coreApps?.includes(app) ?? false
const isPHP = file.slice(-3) === 'php'
let link = getRootUrl()
if (isPHP && !isCore) {
link += `/index.php/apps/${app}`
if (type) {
link += `/${encodeURI(type)}`
}
if (file !== 'index.php') {
link += `/${file}`
}
} else if (!isPHP && !isCore) {
link = getAppRootUrl(app)
if (type) {
link += `/${type}/`
}
if (link.at(-1) !== '/') {
link += '/'
}
link += file
} else {
if ((app === 'settings' || app === 'core' || app === 'search') && type === 'ajax') {
link += '/index.php'
}
if (app) {
link += `/${app}`
}
if (type) {
link += `/${type}`
}
link += `/${file}`
}
return link
}
/**
* Return the full base URL where this Nextcloud instance
* is accessible, with a web root included.
* For example "https://company.com/nextcloud".
*
* @return {string} base URL
*/
export const getBaseUrl = () => window.location.protocol + '//' + window.location.host + getRootUrl()
/**
* Return the web root path where this Nextcloud instance

@@ -194,14 +224,16 @@ * is accessible, with a leading slash.

export function getRootUrl(): string {
let webroot = window._oc_webroot
let webroot = window._oc_webroot
if (typeof webroot === 'undefined') {
webroot = location.pathname
const pos = webroot.indexOf('/index.php/')
if (pos !== -1) {
webroot = webroot.substr(0, pos)
} else {
webroot = webroot.substr(0, webroot.lastIndexOf('/'))
}
}
return webroot
if (typeof webroot === 'undefined') {
webroot = location.pathname
const pos = webroot.indexOf('/index.php/')
if (pos !== -1) {
webroot = webroot.slice(0, pos)
} else {
const index = webroot.indexOf('/', 1)
// Make sure to not cut end of path if there is just the webroot like `/nextcloud`
webroot = webroot.slice(0, index > 0 ? index : undefined)
}
}
return webroot
}

@@ -214,4 +246,4 @@

export function getAppRootUrl(app: string): string {
const webroots = window._oc_appswebroots ?? {}
return webroots[app] ?? ''
const webroots = window._oc_appswebroots ?? {}
return webroots[app] ?? ''
}
/// <reference types="@nextcloud/typings" />
declare interface Window {
OC: Nextcloud.v26.OC | Nextcloud.v27.OC;
OC: Nextcloud.v26.OC | Nextcloud.v27.OC;
// Private state directly from server
_oc_webroot?: string
_oc_appswebroots?: Record<string, string|undefined>
// Private state directly from server
_oc_webroot?: string
_oc_appswebroots?: Record<string, string|undefined>
}
{
"name": "@nextcloud/router",
"version": "2.2.1",
"version": "3.0.0",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "babel ./lib --out-dir dist --extensions '.ts,.tsx' --source-maps && tsc --emitDeclarationOnly",
"build:doc": "typedoc --out dist/doc lib && touch dist/doc/.nojekyll",
"check-types": "tsc",
"dev": "babel ./lib --out-dir dist --extensions '.ts,.tsx' --watch",
"test": "echo \"Error: no test specified\" && exit 1"
},
"homepage": "https://github.com/nextcloud/nextcloud-router#readme",
"author": "Christoph Wurst",
"license": "GPL-3.0-or-later",
"keywords": [
"nextcloud"
],
"homepage": "https://github.com/nextcloud/nextcloud-router#readme",
"author": "Christoph Wurst",
"license": "GPL-3.0-or-later",
"repository": {

@@ -24,14 +15,35 @@ "type": "git",

},
"type": "module",
"main": "dist/index.cjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"scripts": {
"build": "vite --mode production build",
"build:doc": "typedoc --out dist/doc lib && touch dist/doc/.nojekyll",
"dev": "vite --mode development build",
"dev:watch": "vite --mode development build --watch",
"lint": "eslint lib",
"lint:fix": "eslint --fix lib",
"test": "vitest run",
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"core-js": "^3.6.4",
"@nextcloud/typings": "^1.7.0"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@babel/preset-typescript": "^7.9.0",
"@nextcloud/browserslist-config": "^3.0.0",
"typedoc": "^0.25.0",
"typescript": "^5.1.3"
"@nextcloud/eslint-config": "^8.3.0",
"@nextcloud/vite-config": "^1.2.0",
"@vitest/coverage-v8": "^1.2.1",
"eslint": "^8.56.0",
"happy-dom": "^13.3.1",
"typedoc": "^0.25.7",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vitest": "^1.2.1"
},

@@ -38,0 +50,0 @@ "browserslist": [

# @nextcloud/router
[![npm](https://img.shields.io/npm/v/@nextcloud/router?style=for-the-badge)](https://www.npmjs.com/package/@nextcloud/router)
[![Code coverage](https://img.shields.io/codecov/c/github/nextcloud-libraries/nextcloud-router/master?style=for-the-badge)](https://app.codecov.io/github/nextcloud-libraries/nextcloud-router)
[![Documentation](https://img.shields.io/badge/Documentation-0082C9?style=for-the-badge)
](https://nextcloud-libraries.github.io/nextcloud-router/)

@@ -5,0 +8,0 @@ Generate URLs for the Nextcloud front-end

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc