npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@push.rocks/smartrouter

v1.3.2

Published

A JavaScript library providing routing capabilities for web applications.

Downloads

381

Readme

@push.rocks/smartrouter

a router for routing on websites

Install

To install @push.rocks/smartrouter, run the following command in your project directory:

npm install @push.rocks/smartrouter --save

This will add @push.rocks/smartrouter to your project's dependencies and enable you to use it within your application.

Usage

@push.rocks/smartrouter provides a versatile routing solution for websites, leveraging modern Web APIs to manipulate browser history and handle URL paths intelligently. Below are examples demonstrating how to use @push.rocks/smartrouter effectively in a TypeScript project, taking advantage of ESM syntax.

Basic Setup

First, ensure you've installed the package as described in the Install section above. Next, import SmartRouter from @push.rocks/smartrouter in your application's entry point or any module where routing is required.

import { SmartRouter } from '@push.rocks/smartrouter';

Initialize the Router

Create an instance of SmartRouter and optionally provide configuration options. If your application requires debugging information, debug can be set to true.

const router = new SmartRouter({
  debug: true, // Enables debugging. Optional and false by default.
});

Define Routes

Define your application routes using the on method, which takes a URL pattern and a handler function. The handler function will be called when the application navigates to a URL that matches the pattern.

router.on('/home', async (routeInfo) => {
  console.log('Home route accessed', routeInfo);
  // Handle the home route
  // You can load a page component, change document title, etc.
});

router.on('/about', async (routeInfo) => {
  console.log('About route accessed', routeInfo);
  // Handle the about route
});

Path Parameters

@push.rocks/smartrouter supports dynamic path parameters. Define path parameters within your route strings using the : prefix, and access their values from the routeInfo.params object in your handler function.

router.on('/user/:userId', async (routeInfo) => {
  console.log(`User Profile for ID: ${routeInfo.params.userId}`, routeInfo);
  // Load and display user profile based on userId
});

Query Parameters

Query parameters can be accessed through the routeInfo.queryParams object, making it easy to handle complex routing scenarios with optional parameters.

router.on('/search', async (routeInfo) => {
  console.log('Search Query:', routeInfo.queryParams.query);
  // Perform a search operation using the provided query parameter
});

Programmatic Navigation

Navigate programmatically using the pushUrl method. This method allows you to change the URL without reloading the page, and optionally pass state information.

// Navigate to the about page
router.pushUrl('/about');

// Navigate to a user profile with URL parameters
router.pushUrl('/user/12345');

Managing Query Parameters

@push.rocks/smartrouter provides methods for managing URL query parameters, enabling dynamic URL manipulation for filter settings, pagination, and other use cases.

// Set a query parameter
router.queryParams.setQueryParam('key', 'value');

// Get a query parameter
const value = router.queryParams.getQueryParam('key');

// Delete a query parameter
router.queryParams.deleteQueryParam('key');

Selection Dimensions

@push.rocks/smartrouter introduces the concept of selection dimensions, allowing you to manage stateful selections across routes. This is especially useful for complex navigation flows that depend on prior selections.

await router.createSelectionDimension({
  routeArg: '/select/:option',
  keyArg: 'mySelection',
  options: [
    {
      key: 'option1',
      detail: { /* some data */ },
      action: async () => { /* action for option1 */ }
    },
    {
      key: 'option2',
      detail: { /* some data */ },
      action: async () => { /* action for option2 */ }
    }
  ]
});

// Navigate to a selection option
router.pushUrl('/select/option1');

This module enables complex routing scenarios, simplifying the handling of navigational logic in modern web applications. By leveraging @push.rocks/smartrouter, developers can implement detailed routing mechanisms, manipulate browser history thoughtfully, and maintain cleaner URL structures, enhancing the user experience and making web apps more accessible.

License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at [email protected].

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.