Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Router Plugins #397

Merged
merged 9 commits into from
Dec 31, 2024
Merged

New Feature: Router Plugins #397

merged 9 commits into from
Dec 31, 2024

Conversation

pleek91
Copy link
Contributor

@pleek91 pleek91 commented Dec 29, 2024

Description

Plugins add the ability to predefine router functionality to be used when creating a router. Plugins can add routes, rejections, and global hooks to a router.

Creating a plugin

Any object that satisfies the RouterPlugin type can be used. But the new createRouterPlugin utility can be used to easily create plugins in a type safe manor.

import { createRouterPlugin, createRoute } from '@kitbag/router'

const plugin = creatRouterPlugin({
  routes: [
    createRoute(...),
    createRoute(...),
  ],
  rejections: {
    PluginRejection: { ... }
  },
  onBeforeRouteEnter: () => { ... }
})

Installing plugins

Multiple plugins can be installed when creating a router using the 3rd argument of createRouter

const routes = [...] as const

const router = createRouter(routes, {}, [plugin1, plugin2]) 

Type Safety

Any routes or rejections added by plugins will be available when navigating. Use router.push, router.reject, and components like RouterLink like normal.

Copy link

netlify bot commented Dec 29, 2024

Deploy Preview for kitbag-router ready!

Name Link
🔨 Latest commit 2db9a91
🔍 Latest deploy log https://app.netlify.com/sites/kitbag-router/deploys/67746713f5b3ac0008aa9fb7
😎 Deploy Preview https://deploy-preview-397--kitbag-router.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@pleek91
Copy link
Contributor Author

pleek91 commented Dec 29, 2024

@stackoverfloweth couple tests are failing around global hooks. I realized when writing tests for plugins that global hooks didn't work like I expected. I could update the tests to reflect the current functionality, but thought it was worth discussing first.

@pleek91 pleek91 changed the title New Feature: Plugins New Feature: Router Plugins Dec 29, 2024
Copy link
Contributor

@stackoverfloweth stackoverfloweth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been wanting this for so long and assumed it would be incredibly difficult. I'm blown away by how simple this appears to be, so kudos on that. SUPER excited about what this will unlock for us ❤️

src/services/createRouter.ts Show resolved Hide resolved
src/services/createRouterPlugin.ts Outdated Show resolved Hide resolved
src/services/createRouter.ts Outdated Show resolved Hide resolved
src/services/createRouter.spec.ts Outdated Show resolved Hide resolved
@pleek91 pleek91 marked this pull request as ready for review December 31, 2024 03:04
@stackoverfloweth stackoverfloweth added the enhancement New feature or request label Dec 31, 2024
@pleek91 pleek91 merged commit 0cf3f80 into main Dec 31, 2024
6 checks passed
@pleek91 pleek91 deleted the plugins branch December 31, 2024 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants