Skip to content

Commit

Permalink
node-render (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaziuk authored Jul 19, 2017
1 parent ba206be commit 8bdcdae
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 272 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ deploy:
secure: "imp1U7Pl6pYcMxwg6boBQlRWf08hKl/9SyXcKIeG1HuL27DDsuvlsYZfdVBLPvHEwgGH89fFhdPv5AKpTmt8bBES/t3/LLTfCO4Wx0inXeUI+558yDB+RpWN8dtiKfOWGDqeDFL01zYnPfVTclzLR+mDVgAcQxqRov7YpANpD0E1WIAzXpxCYyO3SgEkJlZ0s0R4zfG2Ibm/nJ7caaHbJJqwjxZsDI2IRdSLdl/WucaoSnRbFW8hTLP5IaTNkahFIeqhP2jLl0S1YIxrV4B3Bt7f0PCqfhELAQNkJ/hcu4whtJ0EMzmQO/brHPC3mKSPDDThaQLJf7pnFbZhL355BhvMwpai8SlHTIxxG6hpqjyf0KZp/7tA9lxyGEz9JYOQwe/msPnGtYXlM4tkp5k3xELxU4BVI4kPE9DMPy10wg/FJw+Jxg5CelDvXhKHWF6/AkZpzwV3CgxDib2iopZkouevy1illJ+n555Q2yaF6b0ZDf6Ymf4feqhICvNYOI01ATC+c0wzctyFAV546UCQjJGv5HRsuOzwoOBnOzhnTBhHC+OljW+jsrAQpqDvUbmacvlIzR2g0dgZJOK/A3NkuCJ2wEb8z9USnVx8TRoRETqFS8mswW34sgLtXcF3TYfdGK+x1acNlNS4E+rxYL8gcJzWsOKnIKwBJ8BHBKnd4YQ="
on:
tags: true
branch: master
repo: tlaziuk/mithril-express-middleware
4 changes: 1 addition & 3 deletions mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import {
expect,
} from "chai";

import {
mock,
} from "./mock";
import mock from "./mock";

describe(mock.name, () => {
it("should mock window", () => {
Expand Down
2 changes: 1 addition & 1 deletion mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import * as browserMock from "mithril/test-utils/browserMock";

export function mock(env: object) {
export function mock(env: object = global) {
browserMock(env);
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mithril-express-middleware",
"version": "1.1.3",
"version": "1.1.4",
"description": "express middleware for mithril.js projects",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -43,6 +43,7 @@
"dependencies": {
"@types/express": "^4.0.0",
"@types/mithril": "~1.1.0",
"mithril-render": "~1.1.0",
"tslib": "~1.7.0"
}
}
17 changes: 1 addition & 16 deletions render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,10 @@ import {
mock,
} from "./mock";

import {
isComponent,
render,
} from "./render";
import render from "./render";

mock(global);

describe(isComponent.name, () => {
it("should be a function", () => {
expect(isComponent).to.be.a("function");
});
it("should return boolean", () => {
expect(isComponent(() => undefined)).to.be.a("boolean");
expect(isComponent(null)).to.be.a("boolean");
expect(isComponent(undefined)).to.be.a("boolean");
expect(isComponent({})).to.be.a("boolean");
});
});

describe(render.name, () => {
it("should return Promise", () => {
expect(render({ view: () => "div" })).to.be.instanceOf(Promise);
Expand Down
24 changes: 13 additions & 11 deletions render.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
// tslint:disable-next-line:no-reference
/// <reference path="./types/mithril-node-render.d.ts" />
/// <reference path="./types/mithril.d.ts" />

import {
Attributes,
Children,
ClassComponent,
Component,
ComponentTypes,
CVnode,
FactoryComponent,
Lifecycle,
RouteResolver,
Vnode,
} from "mithril";

import mithrilRender from "./renderer";
import mithrilRender, {
isComponentType,
} from "mithril-render";

export type Defs = ComponentTypes<any, any> | RouteResolver<any, any>;

export function isComponent(thing: any): thing is ComponentTypes<Attributes, Lifecycle<Attributes, object>> {
return !!(thing && (thing.view || typeof thing === "function"));
}

export async function render(
payload: Defs,
params: { [_: string]: any } = {},
path: string = "",
): Promise<string> {
if (isComponent(payload)) {
return await mithrilRender(payload, params);
// tslint:disable-next-line:whitespace
const m = await import("mithril/render/hyperscript");
if (isComponentType(payload)) {
return await mithrilRender(m(payload, params));
} else {
// tslint:disable-next-line:whitespace
const m = await import("mithril");
let component: Vnode<any, any>;
if (payload.onmatch) {
component = m(await payload.onmatch(params, path), params);
} else {
component = m("div");
}
if (payload.render) {
return await mithrilRender(payload.render(component), params);
return await mithrilRender(payload.render(component));
}
}
return "";
Expand Down
238 changes: 0 additions & 238 deletions renderer.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/mithril-node-render.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions types/mithril.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
declare module "mithril/querystring/parse";
declare module "mithril/test-utils/browserMock";
declare module "mithril/render/hyperscript";

0 comments on commit 8bdcdae

Please sign in to comment.