|
| 1 | +# Spiel Client |
| 2 | + |
| 3 | +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: [Picodom](https://github.com/picodom/picodom) and [Navigo](https://github.com/krasimir/navigo). |
| 4 | + |
| 5 | +## Api documentation |
| 6 | +* [Spiel client API](https://spiel-framework.github.io/spiel-client/) |
| 7 | + |
| 8 | +## How use it |
| 9 | + |
| 10 | +### First config you routes |
| 11 | + |
| 12 | +```typescript |
| 13 | +import {ConfigRouters, Router} from 'spiel-client'; |
| 14 | + |
| 15 | +import {genericHooks, hooks} from './hooks'; |
| 16 | + |
| 17 | +import {page1, page2, page3, page4, notFound} from './Pages' |
| 18 | + |
| 19 | +const configSettings: ConfigRouters = { |
| 20 | + rootPath: 'http://localhost:9876/', |
| 21 | + default: '/home', |
| 22 | + useHash: true, |
| 23 | + genericHooks: genericHooks, |
| 24 | + defaultProps: 'default', |
| 25 | + notFound: true, |
| 26 | + hash: '#!', |
| 27 | + notFoundPath: '/not-found', |
| 28 | + routers: [{ |
| 29 | + path: '/home', |
| 30 | + page: page1, |
| 31 | + routers: [{ |
| 32 | + path: '/child/:number', |
| 33 | + page: page2, |
| 34 | + alias: 'child', |
| 35 | + hooks: hooks, |
| 36 | + routers: [{ |
| 37 | + path: '/child2/:word', |
| 38 | + alias: 'grandchild', |
| 39 | + page: page3 |
| 40 | + }] |
| 41 | + },{ |
| 42 | + path: '/brother', |
| 43 | + defaultProps: 'my own prop', |
| 44 | + page: page4 |
| 45 | + }] |
| 46 | + }, |
| 47 | + { |
| 48 | + path: '/not-found', |
| 49 | + page: notFound |
| 50 | + }] |
| 51 | +}; |
| 52 | + |
| 53 | +router.setRouters(configDefault).resolve(); |
| 54 | +``` |
| 55 | + |
| 56 | +You can also declare the routes manually and after resolve it: |
| 57 | + |
| 58 | +```typescript |
| 59 | +router.setRouters() |
| 60 | + .onMultiple({ |
| 61 | + '/user/:id': { |
| 62 | + as: 'user', uses: (params, query) => { |
| 63 | + Object.assign(page2.state, params); |
| 64 | + render(page2.view, page2.state); |
| 65 | + } |
| 66 | + } |
| 67 | + }) |
| 68 | + .resolve(); |
| 69 | +``` |
| 70 | + |
| 71 | +Assign alias if you want generate links: |
| 72 | + |
| 73 | +```typescript |
| 74 | +const configDefault: ConfigRouters = { |
| 75 | + routers: [{ |
| 76 | + path: '/user/:id', |
| 77 | + default: '/user' |
| 78 | + page: page1, |
| 79 | + alias: 'user' |
| 80 | + }] |
| 81 | +}; |
| 82 | + |
| 83 | +router.setRouters(configDefault).resolve(); |
| 84 | +console.log(router.generate('user', {id: 4})); // #/user/4 |
| 85 | +``` |
| 86 | + |
| 87 | +Set your generic hooks for all the routes: |
| 88 | + |
| 89 | +```typescript |
| 90 | +export const genericHooks = { |
| 91 | + before: (done: (suppress?: boolean) => void, params: Params) => { |
| 92 | + if(params && params.number) params.number = +params.number + 2; |
| 93 | + done(); |
| 94 | + }, |
| 95 | + after: (params: Params) => { |
| 96 | + if(params && params.number) params.number = +params.number + 2; |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +And your hooks for expecific route: |
| 102 | + |
| 103 | +```typescript |
| 104 | +export const hooks = { |
| 105 | + before: (done: (suppress?: boolean) => void, params: Params) => { |
| 106 | + if(params) params.number = +params.number + 2; |
| 107 | + done(); |
| 108 | + }, |
| 109 | + after: (params: Params) => { |
| 110 | + if(params) params.number = +params.number + 2 |
| 111 | + }, |
| 112 | + leave: (params: Params) => { |
| 113 | + if(params) params.number = +params.number + 2 |
| 114 | + }, |
| 115 | + already: (params: Params) => { |
| 116 | + if(params) params.number = +params.number + 2 |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | +### Create your page components |
| 121 | + |
| 122 | +```typescript |
| 123 | +import { h, Component, Page, State, VNode, Children, JSXElements } from 'spiel-client'; |
| 124 | + |
| 125 | +interface Show { |
| 126 | + value: string |
| 127 | +} |
| 128 | + |
| 129 | +export class Page1 implements Page { |
| 130 | + state = { |
| 131 | + text: 'This is a Page component' |
| 132 | + } |
| 133 | + |
| 134 | + view(state: State): JSXElements { |
| 135 | + return( |
| 136 | + <Show value={state.text}> |
| 137 | + <span>And this is its component</span> |
| 138 | + </Show> |
| 139 | + ) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +function Show ({value}: Show, children: Children) { |
| 144 | + return ( |
| 145 | + <div> |
| 146 | + <span>{value}</span> |
| 147 | + <div id='child'>{children}</div> |
| 148 | + </div> |
| 149 | + ) |
| 150 | +} |
| 151 | + |
| 152 | +export const page1 = new Page1(); |
| 153 | +``` |
| 154 | + |
| 155 | +**Note:** you need to export the singleton of the class and always import `h` and `Component` to render the views |
| 156 | + |
| 157 | +To know more about the views see more in [picodom](https://github.com/picodom/picodom). |
| 158 | +All the picodom functionalities like `patch`, `h` etc.. are available in spiel-client |
| 159 | + |
| 160 | +### Config your project |
| 161 | + |
| 162 | +This is a tsconfig example: |
| 163 | + |
| 164 | +```typescript |
| 165 | +{ |
| 166 | + "compilerOptions": { |
| 167 | + "target": "es6", |
| 168 | + "module": "commonjs", |
| 169 | + "sourceMap": true, |
| 170 | + "strict": true, |
| 171 | + "emitDecoratorMetadata": true, |
| 172 | + "experimentalDecorators": true, |
| 173 | + "declaration": true, |
| 174 | + "outDir": "./lib", |
| 175 | + "rootDir": ".", |
| 176 | + "jsx": "react", |
| 177 | + "jsxFactory": "h" |
| 178 | + }, |
| 179 | + "include": [ |
| 180 | + "./src", |
| 181 | + "./example" |
| 182 | + ], |
| 183 | + "exclude": [ |
| 184 | + "node_modules" |
| 185 | + ] |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +Remember always to put `h` in the `jsxFactory` option. |
| 190 | + |
| 191 | +### Test your code |
| 192 | + |
| 193 | +Create your mocks: |
| 194 | + |
| 195 | +```typescript |
| 196 | +import { h, Component, Page, State, VNode, Children, JSXElements } from 'spiel-client'; |
| 197 | + |
| 198 | +interface Show { |
| 199 | + value: string |
| 200 | +} |
| 201 | + |
| 202 | +export class ComponentTest implements Page { |
| 203 | + state = { |
| 204 | + text: 'This is a component' |
| 205 | + } |
| 206 | + |
| 207 | + view(state: State): JSXElements { |
| 208 | + return( |
| 209 | + <Show value={state.text}> |
| 210 | + <span>And this is its child</span> |
| 211 | + </Show> |
| 212 | + ) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +function Show ({value}: Show, children: Children) { |
| 217 | + return ( |
| 218 | + <div> |
| 219 | + <span>{value}</span> |
| 220 | + <div id='child'>{children}</div> |
| 221 | + </div> |
| 222 | + ) |
| 223 | +} |
| 224 | + |
| 225 | +export const componentTest = new ComponentTest(); |
| 226 | +``` |
| 227 | + |
| 228 | +and your file spec: |
| 229 | + |
| 230 | +```typescript |
| 231 | +import { h, VNode } from "spiel-client"; |
| 232 | +import { expect, assert } from "chai"; |
| 233 | + |
| 234 | +import {componentTest} from './mocks'; |
| 235 | + |
| 236 | +describe('Component', () => { |
| 237 | + let nodes: VNode<any> |
| 238 | + before(()=> { |
| 239 | + nodes = h(componentTest.view, componentTest.state); |
| 240 | + }); |
| 241 | + it('has to be created', ()=> { |
| 242 | + const text: any = nodes.children.find((node: any) => node.type === 'span'); |
| 243 | + expect(text.children[0]).has.to.be.equal('This is a component') |
| 244 | + }); |
| 245 | + |
| 246 | + it('has to exist its children', ()=> { |
| 247 | + const child: any = nodes.children.find((node: any) => node.type === 'div'); |
| 248 | + const text: any = child.children.find((node: any) => node.type === 'span'); |
| 249 | + expect(text.children[0]).has.to.be.equal("And this is its child"); |
| 250 | + }); |
| 251 | +}) |
| 252 | +``` |
| 253 | + |
| 254 | +## License |
| 255 | + |
| 256 | +Spiel Client use MIT license. See [License](LICENSE.md) |
0 commit comments