# spiel-build

> router builder for diferents framworks

Latest version **1.1.0** (published 2018-04-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install spiel-build
pnpm add spiel-build
yarn add spiel-build
bun add spiel-build
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-04-28 |
| First published | 2018-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 379.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Francisco Jesus spieljs@gmail.com |
| Maintainers | dancespiele |

## Links

- npm: https://www.npmjs.com/package/spiel-build
- Repository: https://github.com/spieljs/spiel-build
- Homepage: https://github.com/spieljs/spiel-build#readme
- Issues: https://github.com/spieljs/spiel-build/issues
- npm.io page: https://npm.io/package/spiel-build

## Dependencies (2)

- [navigo](https://npm.io/package/navigo.md) ^7.1.2
- [@types/navigo](https://npm.io/package/@types/navigo.md) ^7.0.1

## Recent versions

- 1.1.0 (latest) — 2018-04-28
- 1.0.5 — 2018-04-27
- 1.0.4 — 2018-04-27
- 1.0.3 — 2018-04-26
- 1.0.2 — 2018-04-26
- 1.0.1 — 2018-04-26
- 1.0.0 — 2018-04-26

## README

# spiel-build

[![Travis CI](https://travis-ci.org/spieljs/spiel-build.svg?branch=master)](https://travis-ci.org/spieljs/spiel-build)
[![npm](https://img.shields.io/npm/v/spiel-build.svg)](https://www.npmjs.org/package/spiel-build)


Router builder for different frameworks based in states

## Api documentation
* [Spiel Build](https://spieljs.github.io/spiel-build/)

## How build your own router builder

### First instance the Router class:

```typescript
import { IRoutes, Router } from "../src";

const builder = new Router(configRouter.rootPath, configRouter.useHash, configRouter.hash);
```

### After call the build method

```typescript
builder.build(configRouter.routes, this.setPatch, null, element, extraParams);
```

### Simple example with Ultradom 2

```typescript
import { h, patch, VNode } from "ultradom";
import { IRoutes, Router } from "../src";
import { render } from "./render";

export type Keys = string;
export type State = {[k in Keys]: any};
export type View = (state: any) => VNode<any>;

export interface IPage {
    state: State;
    view: View;
}

export interface IRoutesExample extends IRoutes {
    page: IPage;
}

export interface IAdditionalSetting {
    defaultProps: string;
}

export interface IConfigRouter {
    rootPath?: string;
    defaultProps?: string;
    root?: string;
    useHash?: boolean;
    hash?: string;
    routes?: Array<{[Route in keyof IRoutesExample] : any}>;
}

export class ExampleBuilder {
    public builder!: Router;
    private configRouter!: IConfigRouter;
    private root!: string;

    public setRouters(configRouter: IConfigRouter) {
        this.configRouter = configRouter || {};
        this.root = configRouter.root || "app";
        this.builder = new Router(configRouter.rootPath, configRouter.useHash, configRouter.hash);
        const element = this.createRootElement();

        if (configRouter.routes) {
            this.builder.build(configRouter.routes, this.setPatch, null, element, configRouter.defaultProps);
        }

        this.builder.router.resolve();
    }

    private createRootElement() {
        const rootElement = document.getElementById(this.root);
        const node = h("div", {});
        let element;
        if (!rootElement) {
            const elm = document.createElement("div");
            elm.setAttribute("id", this.root);
            document.body.appendChild(elm);
            element = patch(node, document.getElementById(this.root));
        } else {
            element = patch(node, document.getElementById(this.root));
        }

        return element;
    }

    private setPatch(route: IRoutesExample, params: object, query: string,
                     rootElement?: Element, defaultProps?: string) {
        const page = route.page;
        const state: State = {};

        Object.assign(state, page.state);
        state.params = params;
        state.query = query;
        state.defaultProps = defaultProps;
        render(page.view, state, rootElement);
    }
}

export const exampleBuilder = new ExampleBuilder();
```

## Run Spiel Builder tests
* `yarn test` or `npm test`

## License
Spiel Builder is MIT licensed. See [license](LICENSE).

---
_Source: https://npm.io/package/spiel-build · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
