Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

svelte-guard-history-router

Package Overview
Dependencies
Maintainers
1
Versions
276
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svelte-guard-history-router

svelte router for SPA (history mode only)

  • 2.23.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
426
decreased by-39.49%
Maintainers
1
Weekly downloads
 
Created
Source

Svelte v3 npm License minified size downloads Build Status semantic-release styled with prettier Commitizen friendly Known Vulnerabilities

svelte-guard-history-router

svelte guarded history router

Features

  • Named params
  • Guards to act when entering / leaving a route
  • Automatic route ranking
  • Routes and keys acting as stores
  • Nested Routes
  • Route values
  • Object <=> parameter mapping
  • Create links from objects
  • Standart <a href="/home">Home</a> elements

usage

import { redirectGuard } from 'svelte-guard-history-router';

let session = undefined;

export const enshureSession = redirectGuard("/login", () => !session);
<script>
  import { Router, Route, Outlet } from "svelte-guard-history-router";
  import About from "./About.svelte";
  import Categories from "./Categories.svelte";
  import Category from "./Category.svelte";
  import Articles from "./Articles.svelte";
  import Article from "./Article.svelte";

  import { enshureSession } from "./main.mjs";
</script>

<Router base="/base">
<nav>
  <!-- catch all but link to '/' -->
  <Route href="/" path="*" component={Home}>Router Example</Route>
  <ul class="left">
    <li>
      <Route path="/about" component={About}>About</Route>
    </li>
    <li>
      <Route path="/article" guards={enshureSession} component={Articles}>
        Articles
        <Route path="/:artice" component={Article}/>
      </Route>
    </li>
    <li>
      <Route path="/category" guards={enshureSession} component={Categories}>
        Categories
        <Route path="/:category" component={Category}/>
      </Route>
    </li>
  </ul>
</nav>
<main>
  <Outlet/>
</main>
</Router>

Sample code

Check out the code in the example folder, or the live example.

To run the sample, clone the repository, install the dependencies, and start:

git clone https://github.com/arlac77/svelte-guard-history-router
cd svelte-guard-history-router
npm install
npm start

then navigate to localhost:5000

run tests

export BROWSER=safari|chrome|...
yarn test
or
npm test

API

Table of Contents

Key

Keys also act as svelte stores and can be subscribed.

export const article = derived(
[articles, router.keys.article],
([$articles, $id], set) => {
  set($articles.find(a => a.id === $id));
  return () => {};
}
);

Type: Object

Properties

BaseRouter

key subscriptions:

const aKey = router.keys.aKey;
$aKey // fired if value of aKey changes

Parameters

  • routes Array<Route> (optional, default [])
  • base string url (optional, default "")

Properties

  • linkNodes Set<Node> nodes having their active state updated
  • routes Array<Route>
  • keys Object collected keys of all routes
  • params Object value mapping from keys (from current route)
  • route Route current
  • transition Transition ongoing transition
  • base string url

component

Current component. Either from a redirected transition or from the current route

Returns SvelteComponent

value

Value if the current route

Returns any

replace

Replace current route

Parameters

Returns Object former state

push

Leave current route and enter route for given path The work is done by a Transition

Parameters

Returns Transition running transition

finalizePush

Called from a transition to manifest the new destination. If path is undefined the transition has been aborderd

Parameters

continue

Continue a transition to its original destination. Shortcut for this.transition.continue() Does nothing if there is no transition.

abort

Abort a transition. Shortcut for this.transition.abort() Does nothing if there is no transition.

subscribe

Router subscription Changes in the current route will trigger a update

Parameters

updateActive

Update the active state of a node

Parameters

addRoute

Add a new route.

Parameters
  • route Route

routeFor

Find Route for a given object

Parameters

Returns Route able to support given object

nameValueStore

Create a named object wich can act as a store

Parameters

Properties

  • value any

Returns Store

Transition

Transition between routes

Parameters

  • router Router
  • path string destination

Properties

start

Start the transition

  • leave old route
  • find matching target route @see Router.replace()
  • set params
  • set current route
  • enter new route

end

  • **See: Router.finalizePush **

Cleanup transition Update Nodes active state

redirect

Halt current transition and go to another route. To proceed with the original route by calling continue() The original transition will cept in place and be continued afterwards

Parameters
  • path string new route to enter temporary

continue

Continue a redirected route to its original destination. Does nothing if the transition has not been redirected

abort

Bring back the router into the state before the transition has started

Parameters
  • e Exception

SkeletonRoute

Route

Properties

  • _path string
  • component SvelteComponent target to show
  • linkComponent SvelteComponent content for ObjectLink
  • priority number
  • keys Array<string> as found in the path
  • regex RegEx
  • value any

path

Full path of the Route including all parents

Returns string path

enter

Enter the route from a former one.

Parameters

leave

Leave the route to a new one.

Parameters

propertiesFor

Extract properties from object.

Parameters

Returns (Object | undefined) properties extracted from given objects

propertyMapping

Map properties to objects attributes. Keys are the property names and values are the keys in the resulting object.

Returns Object

objectFor

Deliver object for a given set of properties

Parameters

Returns Object for matching properties

Guard

Enforces conditions of routes Like the presents of values in the context

enter

Called while entering a route (current outlet is not yet set)

Parameters

leave

Called before leaving a route

Parameters

redirectGuard

Redirects to a given url if condition is met

Parameters

sequenceGuard

Execute guards in a sequence

Parameters

parallelGuard

Execute guards in a parallel

Parameters

WaitingGuard

Extends Guard

Shows a component during transition

Parameters

  • component SvelteComponent to show up during th transition
  • rampUpTime number initial delay for the componnt to show up (optional, default 300)

install

With npm do:

npm install svelte-guard-history-router

With yarn do:

yarn add svelte-guard-history-router

SPA integrating with a nginx backend

All unresolvable requests are redirected to /sericeBase/index.html

  • /deploymantLocation is the location of the frontend in the servers file system
  • /serviceBase is the url path as seen from the browser
location /serviceBase {
  alias /deploymantLocation;
  try_files $uri$args $uri$args/ /sericeBase/index.html;
}

license

BSD-2-Clause

Keywords

FAQs

Package last updated on 17 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc