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

spiel-client

v1.2.1

Published

Frontend framework to make easier to write frontend applications

Downloads

15

Readme

Spiel Client

Travis CI npm

Spiel client is a flexible and light frontend framework to make easier create a complex and modern applications for the client side. It joins two light but powerful libraries: Ultradom and Navigo.

Api documentation

Examples

How use it

First config you routes

import {IConfigRouters} from "spiel-client";
import {genericHooks, hooks} from "./hooks";
import {page1, page2, page3, page4, notFound} from './Pages'

export const configSettings: IConfigRouters = {
    default: "/home",
    defaultProps: "default",
    genericHooks,
    hash: "#!",
    notFound: true,
    notFoundPath: "/not-found",
    rootPath: "http://localhost:9876/",
    routers: [{
        page: Page1,
        path: "/home",
        routers: [{
            alias: "child",
            hooks,
            page: Page2,
            path: "/child/:number",
            routers: [{
                alias: "grandchild",
                page: Page3,
                path: "/child2/:word",
            }],
        }, {
            defaultProps: "my own prop",
            page: Page4,
            path: "/brother",
        }],
    },
    {
        page: notFound,
        path: "/not-found",
    }],
    useHash: true,
};

srouter.setRouters(configDefault).resolve();

You can also use the navigo API directly:


srouter.setRouters();

const router = srouter.router;
router.on({
  '/user/:id': {
    as: 'user', uses: (params, query) => {
      Object.assign(page2.state, params);
      render(page2.view, page2.state);
    }
  }
});
router.resolve();

Assign alias if you want generate links:

const configDefault: IConfigRouters = {
    routers: [{
        alias: 'user'
        default: '/user'
        page: page1,
        path: '/user/:id',
    }]
};

srouter.setRouters(configDefault).resolve();
const router = srouter.router;
console.log(router.generate('user', {id: 4})); // #/user/4

Pass object by url

You can pass object with srouter.go and recover it with lastState state property:

export class TestPage4 {
    public state = {
        title: "Hello brother",
    };

    public view(state: State): JSXElements {
        return (
            <div>
                <h1>{state.title}</h1>
                <h2>{state.lastState}</h2>
                <button
                    onclick ={() => {
                        state.title = "Yes brother";
                        srouter.go("/home/brother", {title: state.title});
                        render(testPage4.view, state);
                    }}
                >Change Title</button>
                <a href="/home" data-navigo>go to root</a>
            </div>
        );
    }
}

Set your generic hooks for all the routes:

export const genericHooks: IGenericHooks = {
    after: (params: Params) => {
        if (params && params.number) {
            params.number = +params.number + 2;
        }
    },
    before: (done: (suppress?: boolean) => void, params: Params) => {
        if (params && params.number) {
            params.number = +params.number + 2;
        }
        done();
    },
};

And your hooks for expecific route:

export const hooks: IHooks = {
    after: (params: Params) => {
        if (params) {
            params.number = +params.number + 2;
        }
    },
    already: (params: Params) => {
        if (params) {
            params.number = +params.number + 2;
        }
    },
    before: (done: (suppress?: boolean) => void, params: Params) => {
        if (params) {
            params.number = +params.number + 2;
        }
        done();
    },
    leave: (params: Params) => {
        if (params) {
            params.number = +params.number + 2;
        }
    },
};  

Create your page components

import { Children, h, IPage, JSXElements, srouter, State, VNode } from "../../../src";

interface IShow {
    value: string;
    onGo: () => void;
}

export class Page5 implements IPage {
    public state = {
        text: "This is a component",
    };

    public view(state: State): JSXElements {
        return(
            <Show value={state.text} onGo={() => srouter.go("/")}>
                <span>And this is its child</span>
            </Show>
        );
    }
}

function Show({value, onGo}: IShow, children: Children) {
    return (
        <div>
            <span>{value}</span>
            <div id="child">{children}</div>
            <button id="go-parent" onclick={() => srouter.go("/")}></button>
        </div>
    );
}

export const page5 = new page5();

Note: you need to export the singleton of the class and always import h to render the views

To know more about the views see more in ultradom. All the ultradom functionalities like patch, h etc... are available in spiel-client

Config your project

This is a tsconfig example:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "sourceMap": true,
        "strict": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "declaration": true,
        "outDir": "./lib",
        "rootDir": ".",
        "jsx": "react",
        "jsxFactory": "h"
    },
    "include": [
        "./src",
        "./example"
    ],
    "exclude": [
        "node_modules"
    ]
}

Remember always to put h in the jsxFactory option.

Test your code

Create your mocks:

import { Children, h, IPage, JSXElements, State, VNode } from "../../src";

interface IShow {
    value: string;
}

export class ComponentTest implements IPage {
    public state = {
        text: "This is a component",
    };

    public view(state: State): JSXElements {
        return(
            <Show value={state.text}>
                <span>And this is its child</span>
            </Show>
        );
    }

}

function Show({value}: IShow, children: Children) {
    return (
        <div>
            <span>{value}</span>
            <div id="child">{children}</div>
        </div>
    );
}

export const componentTest = new ComponentTest();

and your file spec:

import { assert, expect } from "chai";
import { h, patch, VNode} from "../../src";

import {componentTest} from "./mocks";

describe("Component", () => {
    let nodes: VNode<any>;
    before(() => {
        nodes = h(componentTest.view, componentTest.state);
    });
    it("has to be created", () => {
        const text: any = nodes.children.find((node: any) => node.nodeName === "span");
        expect(text.children[0]).has.to.be.equal("This is a component");
    });

    it("has to exist its children", () => {
        const child: any = nodes.children.find((node: any) => node.nodeName === "div");
        const text: any = child.children.find((node: any) => node.nodeName === "span");
        expect(text.children[0]).has.to.be.equal("And this is its child");
    });
});

Make compatible with ES5 browsers

tsconfig.json:

{
	"compilerOptions": {
		"target": "es5",
		"module": "commonjs",
		"sourceMap": true,
		"strict": true,
		"emitDecoratorMetadata": true,
		"experimentalDecorators": true,
		"declaration": true,
		"outDir": "./lib",
		"rootDir": ".",
		"jsx": "react",
		"jsxFactory": "h"
	},
	"include": [
		"./src",
		"./example"
	],
	"exclude": [
		"node_modules"
	]
}

In your code:

import 'es6-shim'; // or every polyfill which you need
import { srouter, IConfigRouters } from 'spiel-client';

Run Spiel Client tests

npm test

License

Spiel Client is MIT licensed. See license