├── .gitignore ├── .travis.yml ├── package.json ├── readme.md ├── src ├── index.test.ts └── index.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/git,node,macos,sublimetext,visualstudio 3 | # Edit at https://www.gitignore.io/?templates=git,node,macos,sublimetext,visualstudio 4 | 5 | ### Git ### 6 | # Created by git for backups. To disable backups in Git: 7 | # $ git config --global mergetool.keepBackup false 8 | *.orig 9 | 10 | # Created by git when using merge tools for conflicts 11 | *.BACKUP.* 12 | *.BASE.* 13 | *.LOCAL.* 14 | *.REMOTE.* 15 | *_BACKUP_*.txt 16 | *_BASE_*.txt 17 | *_LOCAL_*.txt 18 | *_REMOTE_*.txt 19 | 20 | ### macOS ### 21 | # General 22 | .DS_Store 23 | .AppleDouble 24 | .LSOverride 25 | 26 | # Icon must end with two \r 27 | Icon 28 | 29 | # Thumbnails 30 | ._* 31 | 32 | # Files that might appear in the root of a volume 33 | .DocumentRevisions-V100 34 | .fseventsd 35 | .Spotlight-V100 36 | .TemporaryItems 37 | .Trashes 38 | .VolumeIcon.icns 39 | .com.apple.timemachine.donotpresent 40 | 41 | # Directories potentially created on remote AFP share 42 | .AppleDB 43 | .AppleDesktop 44 | Network Trash Folder 45 | Temporary Items 46 | .apdisk 47 | 48 | ### Node ### 49 | # Logs 50 | logs 51 | *.log 52 | npm-debug.log* 53 | yarn-debug.log* 54 | yarn-error.log* 55 | lerna-debug.log* 56 | 57 | # Diagnostic reports (https://nodejs.org/api/report.html) 58 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 59 | 60 | # Runtime data 61 | pids 62 | *.pid 63 | *.seed 64 | *.pid.lock 65 | 66 | # Directory for instrumented libs generated by jscoverage/JSCover 67 | lib-cov 68 | 69 | # Coverage directory used by tools like istanbul 70 | coverage 71 | *.lcov 72 | 73 | # nyc test coverage 74 | .nyc_output 75 | 76 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 77 | .grunt 78 | 79 | # Bower dependency directory (https://bower.io/) 80 | bower_components 81 | 82 | # node-waf configuration 83 | .lock-wscript 84 | 85 | # Compiled binary addons (https://nodejs.org/api/addons.html) 86 | build/Release 87 | 88 | # Dependency directories 89 | node_modules/ 90 | jspm_packages/ 91 | 92 | # TypeScript v1 declaration files 93 | typings/ 94 | 95 | # TypeScript cache 96 | *.tsbuildinfo 97 | 98 | # Optional npm cache directory 99 | .npm 100 | 101 | # Optional eslint cache 102 | .eslintcache 103 | 104 | # Optional REPL history 105 | .node_repl_history 106 | 107 | # Output of 'npm pack' 108 | *.tgz 109 | 110 | # Yarn Integrity file 111 | .yarn-integrity 112 | 113 | # dotenv environment variables file 114 | .env 115 | .env.test 116 | 117 | # parcel-bundler cache (https://parceljs.org/) 118 | .cache 119 | 120 | # next.js build output 121 | .next 122 | 123 | # nuxt.js build output 124 | .nuxt 125 | 126 | # vuepress build output 127 | .vuepress/dist 128 | 129 | # Serverless directories 130 | .serverless/ 131 | 132 | # FuseBox cache 133 | .fusebox/ 134 | 135 | # DynamoDB Local files 136 | .dynamodb/ 137 | 138 | ### SublimeText ### 139 | # Cache files for Sublime Text 140 | *.tmlanguage.cache 141 | *.tmPreferences.cache 142 | *.stTheme.cache 143 | 144 | # Workspace files are user-specific 145 | *.sublime-workspace 146 | 147 | # Project files should be checked into the repository, unless a significant 148 | # proportion of contributors will probably not be using Sublime Text 149 | # *.sublime-project 150 | 151 | # SFTP configuration file 152 | sftp-config.json 153 | 154 | # Package control specific files 155 | Package Control.last-run 156 | Package Control.ca-list 157 | Package Control.ca-bundle 158 | Package Control.system-ca-bundle 159 | Package Control.cache/ 160 | Package Control.ca-certs/ 161 | Package Control.merged-ca-bundle 162 | Package Control.user-ca-bundle 163 | oscrypto-ca-bundle.crt 164 | bh_unicode_properties.cache 165 | 166 | # Sublime-github package stores a github token in this file 167 | # https://packagecontrol.io/packages/sublime-github 168 | GitHub.sublime-settings 169 | 170 | ### VisualStudio ### 171 | ## Ignore Visual Studio temporary files, build results, and 172 | ## files generated by popular Visual Studio add-ons. 173 | ## 174 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 175 | 176 | # User-specific files 177 | *.rsuser 178 | *.suo 179 | *.user 180 | *.userosscache 181 | *.sln.docstates 182 | 183 | # User-specific files (MonoDevelop/Xamarin Studio) 184 | *.userprefs 185 | 186 | # Mono auto generated files 187 | mono_crash.* 188 | 189 | # Build results 190 | [Dd]ebug/ 191 | [Dd]ebugPublic/ 192 | [Rr]elease/ 193 | [Rr]eleases/ 194 | x64/ 195 | x86/ 196 | [Aa][Rr][Mm]/ 197 | [Aa][Rr][Mm]64/ 198 | bld/ 199 | [Bb]in/ 200 | [Oo]bj/ 201 | [Ll]og/ 202 | 203 | # Visual Studio 2015/2017 cache/options directory 204 | .vs/ 205 | # Uncomment if you have tasks that create the project's static files in wwwroot 206 | #wwwroot/ 207 | 208 | # Visual Studio 2017 auto generated files 209 | Generated\ Files/ 210 | 211 | # MSTest test Results 212 | [Tt]est[Rr]esult*/ 213 | [Bb]uild[Ll]og.* 214 | 215 | # NUnit 216 | *.VisualState.xml 217 | TestResult.xml 218 | nunit-*.xml 219 | 220 | # Build Results of an ATL Project 221 | [Dd]ebugPS/ 222 | [Rr]eleasePS/ 223 | dlldata.c 224 | 225 | # Benchmark Results 226 | BenchmarkDotNet.Artifacts/ 227 | 228 | # .NET Core 229 | project.lock.json 230 | project.fragment.lock.json 231 | artifacts/ 232 | 233 | # StyleCop 234 | StyleCopReport.xml 235 | 236 | # Files built by Visual Studio 237 | *_i.c 238 | *_p.c 239 | *_h.h 240 | *.ilk 241 | *.meta 242 | *.obj 243 | *.iobj 244 | *.pch 245 | *.pdb 246 | *.ipdb 247 | *.pgc 248 | *.pgd 249 | *.rsp 250 | *.sbr 251 | *.tlb 252 | *.tli 253 | *.tlh 254 | *.tmp 255 | *.tmp_proj 256 | *_wpftmp.csproj 257 | *.vspscc 258 | *.vssscc 259 | .builds 260 | *.pidb 261 | *.svclog 262 | *.scc 263 | 264 | # Chutzpah Test files 265 | _Chutzpah* 266 | 267 | # Visual C++ cache files 268 | ipch/ 269 | *.aps 270 | *.ncb 271 | *.opendb 272 | *.opensdf 273 | *.sdf 274 | *.cachefile 275 | *.VC.db 276 | *.VC.VC.opendb 277 | 278 | # Visual Studio profiler 279 | *.psess 280 | *.vsp 281 | *.vspx 282 | *.sap 283 | 284 | # Visual Studio Trace Files 285 | *.e2e 286 | 287 | # TFS 2012 Local Workspace 288 | $tf/ 289 | 290 | # Guidance Automation Toolkit 291 | *.gpState 292 | 293 | # ReSharper is a .NET coding add-in 294 | _ReSharper*/ 295 | *.[Rr]e[Ss]harper 296 | *.DotSettings.user 297 | 298 | # JustCode is a .NET coding add-in 299 | .JustCode 300 | 301 | # TeamCity is a build add-in 302 | _TeamCity* 303 | 304 | # DotCover is a Code Coverage Tool 305 | *.dotCover 306 | 307 | # AxoCover is a Code Coverage Tool 308 | .axoCover/* 309 | !.axoCover/settings.json 310 | 311 | # Visual Studio code coverage results 312 | *.coverage 313 | *.coveragexml 314 | 315 | # NCrunch 316 | _NCrunch_* 317 | .*crunch*.local.xml 318 | nCrunchTemp_* 319 | 320 | # MightyMoose 321 | *.mm.* 322 | AutoTest.Net/ 323 | 324 | # Web workbench (sass) 325 | .sass-cache/ 326 | 327 | # Installshield output folder 328 | [Ee]xpress/ 329 | 330 | # DocProject is a documentation generator add-in 331 | DocProject/buildhelp/ 332 | DocProject/Help/*.HxT 333 | DocProject/Help/*.HxC 334 | DocProject/Help/*.hhc 335 | DocProject/Help/*.hhk 336 | DocProject/Help/*.hhp 337 | DocProject/Help/Html2 338 | DocProject/Help/html 339 | 340 | # Click-Once directory 341 | publish/ 342 | 343 | # Publish Web Output 344 | *.[Pp]ublish.xml 345 | *.azurePubxml 346 | # Note: Comment the next line if you want to checkin your web deploy settings, 347 | # but database connection strings (with potential passwords) will be unencrypted 348 | *.pubxml 349 | *.publishproj 350 | 351 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 352 | # checkin your Azure Web App publish settings, but sensitive information contained 353 | # in these scripts will be unencrypted 354 | PublishScripts/ 355 | 356 | # NuGet Packages 357 | *.nupkg 358 | # NuGet Symbol Packages 359 | *.snupkg 360 | # The packages folder can be ignored because of Package Restore 361 | **/[Pp]ackages/* 362 | # except build/, which is used as an MSBuild target. 363 | !**/[Pp]ackages/build/ 364 | # Uncomment if necessary however generally it will be regenerated when needed 365 | #!**/[Pp]ackages/repositories.config 366 | # NuGet v3's project.json files produces more ignorable files 367 | *.nuget.props 368 | *.nuget.targets 369 | 370 | # Microsoft Azure Build Output 371 | csx/ 372 | *.build.csdef 373 | 374 | # Microsoft Azure Emulator 375 | ecf/ 376 | rcf/ 377 | 378 | # Windows Store app package directories and files 379 | AppPackages/ 380 | BundleArtifacts/ 381 | Package.StoreAssociation.xml 382 | _pkginfo.txt 383 | *.appx 384 | *.appxbundle 385 | *.appxupload 386 | 387 | # Visual Studio cache files 388 | # files ending in .cache can be ignored 389 | *.[Cc]ache 390 | # but keep track of directories ending in .cache 391 | !?*.[Cc]ache/ 392 | 393 | # Others 394 | ClientBin/ 395 | ~$* 396 | *~ 397 | *.dbmdl 398 | *.dbproj.schemaview 399 | *.jfm 400 | *.pfx 401 | *.publishsettings 402 | orleans.codegen.cs 403 | 404 | # Including strong name files can present a security risk 405 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 406 | #*.snk 407 | 408 | # Since there are multiple workflows, uncomment next line to ignore bower_components 409 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 410 | #bower_components/ 411 | 412 | # RIA/Silverlight projects 413 | Generated_Code/ 414 | 415 | # Backup & report files from converting an old project file 416 | # to a newer Visual Studio version. Backup files are not needed, 417 | # because we have git ;-) 418 | _UpgradeReport_Files/ 419 | Backup*/ 420 | UpgradeLog*.XML 421 | UpgradeLog*.htm 422 | ServiceFabricBackup/ 423 | *.rptproj.bak 424 | 425 | # SQL Server files 426 | *.mdf 427 | *.ldf 428 | *.ndf 429 | 430 | # Business Intelligence projects 431 | *.rdl.data 432 | *.bim.layout 433 | *.bim_*.settings 434 | *.rptproj.rsuser 435 | *- [Bb]ackup.rdl 436 | *- [Bb]ackup ([0-9]).rdl 437 | *- [Bb]ackup ([0-9][0-9]).rdl 438 | 439 | # Microsoft Fakes 440 | FakesAssemblies/ 441 | 442 | # GhostDoc plugin setting file 443 | *.GhostDoc.xml 444 | 445 | # Node.js Tools for Visual Studio 446 | .ntvs_analysis.dat 447 | 448 | # Visual Studio 6 build log 449 | *.plg 450 | 451 | # Visual Studio 6 workspace options file 452 | *.opt 453 | 454 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 455 | *.vbw 456 | 457 | # Visual Studio LightSwitch build output 458 | **/*.HTMLClient/GeneratedArtifacts 459 | **/*.DesktopClient/GeneratedArtifacts 460 | **/*.DesktopClient/ModelManifest.xml 461 | **/*.Server/GeneratedArtifacts 462 | **/*.Server/ModelManifest.xml 463 | _Pvt_Extensions 464 | 465 | # Paket dependency manager 466 | .paket/paket.exe 467 | paket-files/ 468 | 469 | # FAKE - F# Make 470 | .fake/ 471 | 472 | # CodeRush personal settings 473 | .cr/personal 474 | 475 | # Python Tools for Visual Studio (PTVS) 476 | __pycache__/ 477 | *.pyc 478 | 479 | # Cake - Uncomment if you are using it 480 | # tools/** 481 | # !tools/packages.config 482 | 483 | # Tabs Studio 484 | *.tss 485 | 486 | # Telerik's JustMock configuration file 487 | *.jmconfig 488 | 489 | # BizTalk build output 490 | *.btp.cs 491 | *.btm.cs 492 | *.odx.cs 493 | *.xsd.cs 494 | 495 | # OpenCover UI analysis results 496 | OpenCover/ 497 | 498 | # Azure Stream Analytics local run output 499 | ASALocalRun/ 500 | 501 | # MSBuild Binary and Structured Log 502 | *.binlog 503 | 504 | # NVidia Nsight GPU debugger configuration file 505 | *.nvuser 506 | 507 | # MFractors (Xamarin productivity tool) working folder 508 | .mfractor/ 509 | 510 | # Local History for Visual Studio 511 | .localhistory/ 512 | 513 | # BeatPulse healthcheck temp database 514 | healthchecksdb 515 | 516 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 517 | MigrationBackup/ 518 | 519 | # End of https://www.gitignore.io/api/git,node,macos,sublimetext,visualstudio 520 | 521 | 522 | dist/ 523 | types/ 524 | .env 525 | package-lock.json 526 | locale/ 527 | .vscode -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 13 4 | - 12 5 | - 10 6 | - 8 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@grafikri/vue-middleware", 3 | "version": "1.0.0", 4 | "description": "Middleware for Vue", 5 | "main": "dist/index.js", 6 | "types": "types/index.d.ts", 7 | "author": "Serhan Coşkun", 8 | "scripts": { 9 | "dev": "tsc --watch", 10 | "build": "tsc", 11 | "prepare": "npm run build", 12 | "test": "jest", 13 | "pretest": "npm run prepare", 14 | "prepublish": "npm run test", 15 | "test:watch": "jest --watch" 16 | }, 17 | "devDependencies": { 18 | "@types/jest": "^26.0.15", 19 | "nodemon": "^2.0.4", 20 | "typescript": "^4.0.2", 21 | "vue": "^2.6.12", 22 | "vue-router": "^3.4.9", 23 | "jest": "^24.9.0", 24 | "ts-jest": "^24.3.0" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "https://github.com/grafikri/vue-middleware.git" 29 | }, 30 | "bugs": { 31 | "url": "https://github.com/grafikri/vue-middleware/issues" 32 | }, 33 | "files": [ 34 | "dist", 35 | "types" 36 | ], 37 | "jest": { 38 | "preset": "ts-jest", 39 | "testEnvironment": "node" 40 | }, 41 | "keywords": [ 42 | "middleware", 43 | "vue", 44 | "router", 45 | "route" 46 | ], 47 | "license": "ISC" 48 | } 49 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/grafikri/vue-middleware.svg?branch=master)](https://travis-ci.org/grafikri/vue-middleware) 2 | [![install size](https://packagephobia.com/badge?p=@grafikri/vue-middleware)](https://packagephobia.com/result?p=@grafikri/vue-middleware) 3 | 4 | # Middleware for Vue 5 | 6 | ## Main features 7 | 8 | - Injecting custom object for middlewares 9 | - Adjusting multiple middleware rules 10 | - Easy implementation 11 | 12 | ### Installation 13 | 14 | ```bash 15 | $ npm i @grafikri/vue-middleware 16 | ``` 17 | 18 | ### Usage in 3 steps 19 | 20 | #### 1. Register the module in the main file 21 | 22 | You can also inject any object to a module to take it in middleware method(recommended vuex store, it will be mentioned below) 23 | 24 | ```js 25 | // main.js 26 | 27 | import router from "./router" 28 | import middleware from "@grafikri/vue-middleware" 29 | 30 | router.beforeEach(middleware()) 31 | ``` 32 | 33 | #### 2. Specify logic 34 | 35 | You can put your middleware methods under any folder. There is no rule for this. 36 | 37 | ```js 38 | // middleware/authentication.js 39 | 40 | export default ({ to, from, next }) => { 41 | // Your custom if statement 42 | if (!userLoggedIn) { 43 | next("/login") 44 | return false 45 | } 46 | next() 47 | } 48 | ``` 49 | 50 | > Mentioned params to, from, and next completely same with Vue Router [navigation guard params](https://router.vuejs.org/guide/advanced/navigation-guards.html#global-before-guards) 51 | 52 | #### 3. Adjust middleware for any route under meta key 53 | 54 | ```js 55 | // router/index.js 56 | 57 | import authentication from "../middleware/authentication" 58 | 59 | const routes = [ 60 | { 61 | path: "/user-profile", 62 | meta: { 63 | middleware: [authentication], 64 | }, 65 | }, 66 | ] 67 | ``` 68 | 69 | ### Injecting custom object (recommended vuex store) 70 | 71 | You can inject any object 72 | 73 | ```js 74 | // main.js 75 | 76 | import router from "./router" 77 | import store from "./store" 78 | 79 | import middleware from "@grafikri/vue-middleware" 80 | 81 | router.beforeEach(middleware({ store })) 82 | ``` 83 | 84 | to retrive it inside of middleware method 85 | 86 | ```js 87 | // middleware/authentication.js 88 | 89 | export default ({ store, next }) => { 90 | if (!store.state.user.loggedIn) { 91 | next("/login") 92 | return false 93 | } 94 | next() 95 | } 96 | ``` 97 | 98 | > There is one important rule for chaining that you must return `false` if you want to stop the next middleware method. 99 | 100 | ### Defining multiple middlewares 101 | 102 | You can define more than one middleware methods that will be invoked respectively. 103 | 104 | ```js 105 | // router/index.js 106 | 107 | import authentication from "../middleware/authentication" 108 | import authorization from "../middleware/authorization" 109 | 110 | const routes = [ 111 | { 112 | path: "/payments", 113 | meta: { 114 | middleware: [authentication, authorization], 115 | }, 116 | }, 117 | ] 118 | ``` 119 | -------------------------------------------------------------------------------- /src/index.test.ts: -------------------------------------------------------------------------------- 1 | import middleware from './index' 2 | 3 | describe('Middleware', () => { 4 | 5 | const store = { name: 'john' } 6 | const customObject = { sum: () => {} } 7 | 8 | const from: any = {} 9 | 10 | test('no middleware', () => { 11 | const to: any = {} 12 | const next = jest.fn() 13 | middleware()(to, from, next) 14 | 15 | expect(next.mock.calls.length).toBe(1) 16 | }) 17 | 18 | test('one middleware', () => { 19 | 20 | const dummyMethod = jest.fn() 21 | const next = jest.fn() 22 | const to: any = { 23 | meta: { 24 | middleware: [dummyMethod] 25 | } 26 | } 27 | 28 | middleware({ store, customObject })(to, from, next) 29 | expect(dummyMethod).toHaveBeenCalledWith({ store, customObject, to, from, next }) 30 | expect(dummyMethod.mock.calls.length).toBe(1) 31 | expect(next.mock.calls.length).toBe(0) 32 | 33 | }) 34 | 35 | test('infinite defined middleware', () => { 36 | 37 | const dummySumMethod = jest.fn() 38 | const dummySubstractMethod = jest.fn() 39 | const dummyMultiplicationMethod = jest.fn() 40 | const next = jest.fn() 41 | const to: any = { 42 | meta: { 43 | middleware: [dummySumMethod, dummySubstractMethod, dummyMultiplicationMethod] 44 | } 45 | } 46 | 47 | middleware({ store, customObject })(to, from, next) 48 | expect(dummySumMethod).toHaveBeenCalledWith({ store, customObject, to, from, next }) 49 | expect(dummySubstractMethod).toHaveBeenCalledWith({ store, customObject, to, from, next }) 50 | expect(dummyMultiplicationMethod).toHaveBeenCalledWith({ store, customObject, to, from, next }) 51 | 52 | expect(dummySumMethod.mock.calls.length).toBe(1) 53 | expect(dummySubstractMethod.mock.calls.length).toBe(1) 54 | expect(dummyMultiplicationMethod.mock.calls.length).toBe(1) 55 | 56 | expect(next.mock.calls.length).toBe(0) 57 | 58 | }) 59 | 60 | 61 | test('last middleware must not be invoked', () => { 62 | 63 | const dummySumMethod = jest.fn() 64 | const dummySubstractMethod = jest.fn().mockReturnValue(false) 65 | const dummyMultiplicationMethod = jest.fn() 66 | const next = jest.fn() 67 | const to: any = { 68 | meta: { 69 | middleware: [dummySumMethod, dummySubstractMethod, dummyMultiplicationMethod] 70 | } 71 | } 72 | 73 | middleware({ store, customObject })(to, from, next) 74 | expect(dummySumMethod).toHaveBeenCalledWith({ store, customObject, to, from, next }) 75 | expect(dummySubstractMethod).toHaveBeenCalledWith({ store, customObject, to, from, next }) 76 | expect(dummyMultiplicationMethod).not.toHaveBeenCalled() 77 | 78 | expect(dummySumMethod.mock.calls.length).toBe(1) 79 | expect(dummySubstractMethod.mock.calls.length).toBe(1) 80 | expect(dummyMultiplicationMethod.mock.calls.length).toBe(0) 81 | 82 | expect(next.mock.calls.length).toBe(0) 83 | 84 | }) 85 | 86 | 87 | test('last two middleware must not be invoked', () => { 88 | 89 | const dummySumMethod = jest.fn().mockReturnValue(false) 90 | const dummySubstractMethod = jest.fn() 91 | const dummyMultiplicationMethod = jest.fn() 92 | const next = jest.fn() 93 | const to: any = { 94 | meta: { 95 | middleware: [dummySumMethod, dummySubstractMethod, dummyMultiplicationMethod] 96 | } 97 | } 98 | 99 | middleware({ store, customObject })(to, from, next) 100 | expect(dummySumMethod).toHaveBeenCalledWith({ store, customObject, to, from, next }) 101 | expect(dummySubstractMethod).not.toHaveBeenCalled() 102 | expect(dummyMultiplicationMethod).not.toHaveBeenCalled() 103 | 104 | expect(dummySumMethod.mock.calls.length).toBe(1) 105 | expect(dummySubstractMethod.mock.calls.length).toBe(0) 106 | expect(dummyMultiplicationMethod.mock.calls.length).toBe(0) 107 | 108 | expect(next.mock.calls.length).toBe(0) 109 | 110 | }) 111 | 112 | }) -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { Route, NavigationGuardNext } from 'vue-router' 2 | 3 | export default (datas?: any) => async (to: Route, from: Route, next: NavigationGuardNext) => { 4 | 5 | if (to.meta?.middleware?.length) { 6 | const arr = to.meta.middleware 7 | for (let index = 0; index < arr.length; index++) { 8 | const method: Function = arr[index]; 9 | const result = method({...datas, to, from, next}) 10 | if (result === false) { 11 | break 12 | } 13 | } 14 | return 15 | } 16 | 17 | return next() 18 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "sourceMap": false, 4 | "module": "commonjs", 5 | "moduleResolution": "node", 6 | "declaration": true, 7 | "declarationDir": "./types", 8 | "outDir": "./dist", 9 | "strict": true, 10 | "target": "es6", 11 | "allowJs": false, 12 | "noImplicitAny": true 13 | }, 14 | "include": [ 15 | "src/**/*" 16 | ], 17 | "exclude": [ 18 | "node_modules", 19 | "**/*.spec.ts", 20 | "**/*.test.ts" 21 | ] 22 | } --------------------------------------------------------------------------------