diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f05c26..2b9e7a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,17 @@ -### [7.0.4](https://github.com/Masquerade-Circus/valyrian.js/compare/7.0.3...7.0.4) (2022-12-17) +## [7.1.0](https://github.com/Masquerade-Circus/valyrian.js/compare/7.0.4...7.1.0) (2022-12-17) + + +### Features + +* **router:** add a way to initialize the router with a prefix ([84770ef](https://github.com/Masquerade-Circus/valyrian.js/commit/84770efac1ce0b2a3a43526b43ae9d0a6d5de78f)) +### [7.0.4](https://github.com/Masquerade-Circus/valyrian.js/compare/7.0.3...7.0.4) (2022-12-17) ### Styles -* add comments to main and improve RouterInterface ([4e40a35](https://github.com/Masquerade-Circus/valyrian.js/commit/4e40a35ade8f4776f5c9218017d50a6f18d2e0b8)) +* add comments to main and improve RouterInterface ([4e40a35](https://github.com/Masquerade-Circus/valyrian.js/commit/4e40a35ade8f4776f5c9218017d50a6f18d2e0b8)) ### [7.0.3](https://github.com/Masquerade-Circus/valyrian.js/compare/7.0.2...7.0.3) (2022-10-24) diff --git a/dist/router/index.d.ts b/dist/router/index.d.ts index 7d805f2..64c38dc 100644 --- a/dist/router/index.d.ts +++ b/dist/router/index.d.ts @@ -42,8 +42,10 @@ export declare class Router implements RouterInterface { path: string; params: Record; matches: string[]; + pathPrefix: string; + constructor(pathPrefix?: string); add(path: string, ...args: Middlewares): Router; - use(...args: Middlewares | Router[]): Router; + use(...args: Middlewares | Router[] | string[]): Router; routes(): string[]; go(path: string, parentComponent?: Component): Promise; getOnClickHandler(url: string): (e: MouseEvent) => void; diff --git a/dist/router/index.d.ts.map b/dist/router/index.d.ts.map index 3040d51..be8cb80 100644 --- a/dist/router/index.d.ts.map +++ b/dist/router/index.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/router/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,aAAa,EACb,uBAAuB,E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AgB,KAAA,EAAE,MAAM,KAAA,QAiBnD"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/router/index.ts"],"names":[],"mappings":"AACA,OAAO,EACL,SAAS,EACT,aAAa,EACb,uBAAuB,EAQxB,MAAM,aAAa,CAAC;AAErB,UAAU,IAAI;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,OAAO;IACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;IAElB,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,SAAS,GAAG,aAAa,GAAG,uBAAuB,KAAK,KAAK,CAAC;CAC1G;AAED,UAAU,UAAU;IAElB,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,CAAC,EAAE,GAAG,GACpB,OAAO,CAAC,GAAG,GAAG,SAAS,GAAG,aAAa,GAAG,uBAAuB,CAAC,GAClE,GAAG,GACH,SAAS,GACT,aAAa,GACb,uBAAuB,CAAC;CAC7B;AAED,UAAU,WAAY,SAAQ,KAAK,CAAC,UAAU,CAAC;CAAG;AAElD,UAAU,eAAe;IACvB,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,SAAS,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAAC;IACnC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IACvC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC7B,GA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AC,IAAI,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IA2C3E,iBAAiB,CAAC,GAAG,EAAE,MAAM,OAChB,UAAU;CAOxB;AAGD,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,OAKnC;AAED,wBAAgB,WAAW,CAAC,gBAAgB,KAAA,EAAE,MAAM,KAAA,QAiBnD"} \ No newline at end of file diff --git a/dist/router/index.js b/dist/router/index.js index baab482..fc195c2 100644 --- a/dist/router/index.js +++ b/dist/router/index.js @@ -125,27 +125,30 @@ var Router = class { path = ""; params = {}; matches = []; + pathPrefix = ""; + constructor(pathPrefix = "") { + this.pathPrefix = pathPrefix; + } add(path, ...args) { - addPath(this, "add", path, args); + addPath(this, "add", `${this.pathPrefix}${path}`, args); return this; } use(...args) { - let path = typeof args[0] === "string" ? args.shift() : "/"; - let i; - let k; - let subrouter; + let path = `${this.pathPrefix}${typeof args[0] === "string" ? args.shift() : "/"}`; let item; let subpath; - for (i = 0; i < args.length; i++) { - subrouter = args[i]; - if (typeof subrouter === "function") { - addPath(this, "use", `${path}.*`, [subrouter]); - } else if (subrouter.paths) { - for (k = 0; k < subrouter.paths.length; k++) { + for (let i = 0; i < args.length; i++) { + if (args[i] instanceof Router) { + let subrouter = args[i]; + for (let k = 0; k < subrouter.paths.length; k++) { item = subrouter.paths[k]; subpath = `${path}${item.path}`.replace(/^\/\//, "/"); addPath(this, item.method, subpath, item.middlewares); } + continue; + } + if (typeof args[i] === "function") { + addPath(this, "use", `${path}.*`, [args[i]]); } } return this; @@ -163,10 +166,11 @@ var Router = class { if (!path) { throw new Error("router.url.required"); } - let parts = path.split("?", 2); + let constructedPath = `${this.pathPrefix}${path}`; + let parts = constructedPath.split("?", 2); let urlParts = parts[0].replace(/(.+)\/$/, "$1"); let queryParts = parts[1]; - this.url = path; + this.url = constructedPath; this.query = parseQuery(queryParts); let middlewares = searchMiddlewares(this, urlParts); let component = await searchComponent(this, middlewares); @@ -174,7 +178,7 @@ var Router = class { return; } if (!component) { - throw new Error(`The url ${path} requested wasn't found`); + throw new Error(`The url ${constructedPath} requested wasn't found`); } if ((0, import_valyrian.isComponent)(parentComponent) || (0, import_valyrian.isVnodeComponent)(parentComponent)) { let childComponent = (0, import_valyrian.isVnodeComponent)(component) ? component : (0, import_valyrian.v)(component, {}); @@ -186,7 +190,7 @@ var Router = class { } } if (!import_valyrian.isNodeJs) { - window.history.pushState(null, "", path); + window.history.pushState(null, "", constructedPath); } if (this.container) { return (0, import_valyrian.mount)(this.container, component); diff --git a/dist/router/index.mjs b/dist/router/index.mjs index 2f76920..d10004d 100644 --- a/dist/router/index.mjs +++ b/dist/router/index.mjs @@ -108,27 +108,30 @@ var Router = class { path = ""; params = {}; matches = []; + pathPrefix = ""; + constructor(pathPrefix = "") { + this.pathPrefix = pathPrefix; + } add(path, ...args) { - addPath(this, "add", path, args); + addPath(this, "add", `${this.pathPrefix}${path}`, args); return this; } use(...args) { - let path = typeof args[0] === "string" ? args.shift() : "/"; - let i; - let k; - let subrouter; + let path = `${this.pathPrefix}${typeof args[0] === "string" ? args.shift() : "/"}`; let item; let subpath; - for (i = 0; i < args.length; i++) { - subrouter = args[i]; - if (typeof subrouter === "function") { - addPath(this, "use", `${path}.*`, [subrouter]); - } else if (subrouter.paths) { - for (k = 0; k < subrouter.paths.length; k++) { + for (let i = 0; i < args.length; i++) { + if (args[i] instanceof Router) { + let subrouter = args[i]; + for (let k = 0; k < subrouter.paths.length; k++) { item = subrouter.paths[k]; subpath = `${path}${item.path}`.replace(/^\/\//, "/"); addPath(this, item.method, subpath, item.middlewares); } + continue; + } + if (typeof args[i] === "function") { + addPath(this, "use", `${path}.*`, [args[i]]); } } return this; @@ -146,10 +149,11 @@ var Router = class { if (!path) { throw new Error("router.url.required"); } - let parts = path.split("?", 2); + let constructedPath = `${this.pathPrefix}${path}`; + let parts = constructedPath.split("?", 2); let urlParts = parts[0].replace(/(.+)\/$/, "$1"); let queryParts = parts[1]; - this.url = path; + this.url = constructedPath; this.query = parseQuery(queryParts); let middlewares = searchMiddlewares(this, urlParts); let component = await searchComponent(this, middlewares); @@ -157,7 +161,7 @@ var Router = class { return; } if (!component) { - throw new Error(`The url ${path} requested wasn't found`); + throw new Error(`The url ${constructedPath} requested wasn't found`); } if (isComponent(parentComponent) || isVnodeComponent(parentComponent)) { let childComponent = isVnodeComponent(component) ? component : v(component, {}); @@ -169,7 +173,7 @@ var Router = class { } } if (!isNodeJs) { - window.history.pushState(null, "", path); + window.history.pushState(null, "", constructedPath); } if (this.container) { return mount(this.container, component); diff --git a/lib/router/index.ts b/lib/router/index.ts index e67c8ce..2462980 100644 --- a/lib/router/index.ts +++ b/lib/router/index.ts @@ -193,7 +193,7 @@ export class Router implements RouterInterface { matches: string[] = []; pathPrefix: string = ""; - constructor(pathPrefix: string) { + constructor(pathPrefix: string = "") { this.pathPrefix = pathPrefix; } diff --git a/package-lock.json b/package-lock.json index a267e53..2009b1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "valyrian.js", - "version": "7.0.4", + "version": "7.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "valyrian.js", - "version": "7.0.4", + "version": "7.1.0", "license": "Apache-2.0", "dependencies": { "clean-css": "^5.2.4", diff --git a/package.json b/package.json index f0848fe..1560bf2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "valyrian.js", - "version": "7.0.4", + "version": "7.1.0", "description": "Lightweight steel to forge PWAs. (Minimal Frontend Framework with server side rendering and other capabilities)", "repository": "git@github.com:Masquerade-Circus/valyrian.js.git", "author": "Masquerade ",