vue3-router-middlewares 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Quentin Juarez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
File without changes
@@ -0,0 +1,3 @@
1
+ import type { NavigationGuardWithThis, RouteLocationNormalized, NavigationGuardNext } from "vue-router";
2
+ declare function evaluateGuards(guards: NavigationGuardWithThis<any>[], to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): void;
3
+ export default evaluateGuards;
package/dist/guards.js ADDED
@@ -0,0 +1,17 @@
1
+ function evaluateGuards(guards, to, from, next) {
2
+ const guardsLeft = guards.slice(0); // Clone the array
3
+ const nextGuard = guardsLeft.shift();
4
+ if (!nextGuard) {
5
+ next();
6
+ return;
7
+ }
8
+ const applyNext = (nextArg) => {
9
+ if (!nextArg) {
10
+ evaluateGuards(guardsLeft, to, from, next);
11
+ return;
12
+ }
13
+ next(nextArg);
14
+ };
15
+ nextGuard(to, from, applyNext);
16
+ }
17
+ export default evaluateGuards;
@@ -0,0 +1,10 @@
1
+ import type { App } from "vue";
2
+ import type { Router } from "vue-router";
3
+ import applyMiddlewares from "./middlewares";
4
+ declare const _default: {
5
+ install(_app: App, options: {
6
+ router: Router;
7
+ }): void;
8
+ };
9
+ export default _default;
10
+ export { applyMiddlewares };
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import applyMiddlewares from "./middlewares";
2
+ export default {
3
+ install(_app, options) {
4
+ if (!options.router) {
5
+ throw new Error("vue3-router-middlewares: Router is required");
6
+ }
7
+ options.router.beforeEach(applyMiddlewares);
8
+ },
9
+ };
10
+ export { applyMiddlewares };
@@ -0,0 +1,3 @@
1
+ import type { RouteLocationNormalized, NavigationGuardNext } from "vue-router";
2
+ declare const applyMiddlewares: (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => void;
3
+ export default applyMiddlewares;
@@ -0,0 +1,20 @@
1
+ import evaluateGuards from "./guards";
2
+ const checkMiddlewaresValidity = (middlewares) => {
3
+ if (!Array.isArray(middlewares)) {
4
+ throw new Error("vue3-router-middlewares: Middlewares should be an array");
5
+ }
6
+ middlewares.forEach((middleware) => {
7
+ if (typeof middleware !== "function") {
8
+ throw new Error("vue3-router-middlewares: Middleware should be a function");
9
+ }
10
+ });
11
+ return middlewares;
12
+ };
13
+ const applyMiddlewares = (to, from, next) => {
14
+ if (!to.meta.middlewares) {
15
+ return next();
16
+ }
17
+ const guards = checkMiddlewaresValidity(to.meta.middlewares);
18
+ return evaluateGuards(guards, to, from, next);
19
+ };
20
+ export default applyMiddlewares;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "vue3-router-middlewares",
3
+ "version": "0.0.2",
4
+ "description": "A middleware handler for Vue 3 router using meta fields.",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.esm.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "src/types.d.ts"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "test": "jest"
15
+ },
16
+ "dependencies": {
17
+ "vue": "^3.x",
18
+ "vue-router": "^4.x"
19
+ },
20
+ "devDependencies": {
21
+ "@types/jest": "^29.5.12",
22
+ "jest": "^29.7.0",
23
+ "ts-jest": "^29.2.4",
24
+ "typescript": "^5.5.4"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/quentinjuarez/vue3-router-middlewares.git"
29
+ },
30
+ "keywords": [
31
+ "vue",
32
+ "vue3",
33
+ "vue-router",
34
+ "middlewares",
35
+ "plugin"
36
+ ],
37
+ "author": "quentinjuarez",
38
+ "license": "MIT",
39
+ "packageManager": "yarn@3.5.0"
40
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export {};
2
+
3
+ declare module "vue-router" {
4
+ interface RouteMeta {
5
+ middlewares?: NavigationGuard[];
6
+ }
7
+ }