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

sophie-router

v1.0.8-beta

Published

Provides simple components in order to have routing in your application

Downloads

19

Readme

Sophie Router

Provides simple components in order to have routing in your application. Ideal for single page applications where you need to handle the routing.

Part of the SPA framework Sophie

npm version npm dependencies npm downloads

INSTALLATION

yarn add sophie-router

USAGE

Basically consist of an Application class you need to extend from. This will be your shared state across different pages.

Then you need to instantiate Resource Routers and associate to them Resources the first ones will match a wildcard for a path while the second ones are the concrete resource to run for a matching path. For example we have an application that consists of the home page, and two other pages.

We need to handle the following paths:

  • /
  • /page1
  • /page2

So first we can create the ResourceRouter that will handle the root path

HomeResourceRouter.ts

import { AbstractResourceRouter } from 'sophie-router';

export const HOME_PATH = /^\/+$/g;

export class HomeResourceRouter extends AbstractResourceRouter
{
	protected canRoute (pathName: string): boolean
	{
		return !!pathName.match(HOME_PATH);
	}
}

Then the Resource

HomeResource.ts

import { AbstractApplication, AbstractResource } from 'sophie-router';

import { HOME_PATH } from './HomeResourceRouter';

export class HomeResource extends AbstractResource
{
	public getPath (): RegExp
	{
		return HOME_PATH;
	}

	public run (app: AbstractApplication, pathName?: string, search?: string): void
	{
		console.log('Run Homepage');
	}
}

The same for the other pages

PageResourceRouter.ts

import { AbstractResourceRouter } from 'sophie-router';

export const PAGE_PATH = /page\d?/g;

export class PageResourceRouter extends AbstractResourceRouter
{
    protected canRoute (pathName: string): boolean
    {
        return !!pathName.match(PAGE_PATH);
    }
}

Page1Resource.ts

import { AbstractApplication, AbstractResource } from 'sophie-router';

export class Page1Resource extends AbstractResource
{
	public getPath (): RegExp
	{
		return /page1/g;
	}

	public run (app: AbstractApplication, pathName?: string, search?: string): void
	{
		console.log('Run Page 1');
	}
}

Page2Resource.ts

import { AbstractApplication, AbstractResource } from 'sophie-router';

export class Page2Resource extends AbstractResource
{
    public getPath (): RegExp
    {
        return /page2/g;
    }

    public run (app: AbstractApplication, pathName?: string, search?: string): void
    {
        console.log('Run Page 2');
    }
}

Now we can create our Application, the one that we get in the run methods from the Resources

Application.ts

import { AbstractApplication } from 'sophie-router';

export class Application extends AbstractApplication
{
// Extend me ...
}

To finish we just need to create our index or entry point

index.ts

import {
	AbstractApplication,
    Framework,
    AbstractResourceRouter,
    AbstractResource
} from 'sophie-router';

import { Application } from './Application';
import { HomeResourceRouter } from './HomeResourceRouter';
import { PageResourceRouter } from './PageResourceRouter';
import { HomeResource } from './HomeResource';
import { Page1Resource } from './Page1Resource';
import { Page2Resource } from './Page2Resource';

// Instantiate objects
const application: AbstractApplication = new Application(); // Create our application
const framework: Framework = Framework.createInstance(application); // Create the framework that will handle the routing

// Create the resource routers
const homeResourceRouter: AbstractResourceRouter = new HomeResourceRouter();
const pageResourceRouter: AbstractResourceRouter = new PageResourceRouter();

// Create some resources for the routers
const homePage: AbstractResource = new HomeResource();
const page1: AbstractResource = new Page1Resource();
const page2: AbstractResource = new Page2Resource();

// Add resources to routers
homeResourceRouter.addResource(homePage);
pageResourceRouter.addResource(page1);
pageResourceRouter.addResource(page2);

// Register the routers in the framework
framework.registerResourceRouter(homeResourceRouter);
framework.registerResourceRouter(pageResourceRouter);

// Run the application
framework.run(location.pathname, location.search);

TIP: You need to handle the navigation in your application in order to have a SPA to avoid normal navigation and instead use the router, for that I recommend the following snippet

index.ts

// Add this to the imports
import { bubbleUpToTag } from "sophie-helpers";

// Add the next to the end of index.ts
const HTTP_PROTOCOL: string = 'http://';
const HTTPS_PROTOCOL: string = 'https://';
const MAIL_TO_PROTOCOL: string = 'mailto:';

document.addEventListener('click', (event) =>
{
    const link = bubbleUpToTag(event.target as HTMLElement || event.srcElement as HTMLElement, 'a');

    if (link)
    {
        const href = (link as HTMLLinkElement).getAttribute('href');
        if (
            href &&
            href.substr(0, HTTP_PROTOCOL.length) !== HTTP_PROTOCOL &&
            href.substr(0, HTTPS_PROTOCOL.length) !== HTTPS_PROTOCOL &&
            href.substr(0, MAIL_TO_PROTOCOL.length) !== MAIL_TO_PROTOCOL)
        {
            event.stopPropagation();
            event.preventDefault();

            history.pushState({}, '', href);

            framework.run(href);
        }
    }
});

window.onpopstate = () => {
    framework.run(location.pathname, location.search);
};

Changelog

Changelog

Contributing

contributions welcome

License

This software is licensed under the terms of the MIT license. See LICENSE for the full license.