From 798c4987f85486c3e50a954e4933d35ac7a98264 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Wed, 8 May 2019 10:54:05 +1200 Subject: [PATCH] Extract http into separate parameter --- .../client/src/renderer/ClientRenderer.tsx | 23 +++++++++++++------ packages/client/src/renderer/IRenderer.ts | 7 +++++- .../client/src/renderer/ServerRenderer.tsx | 21 +++++++++++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/packages/client/src/renderer/ClientRenderer.tsx b/packages/client/src/renderer/ClientRenderer.tsx index ed3ade1..0847050 100644 --- a/packages/client/src/renderer/ClientRenderer.tsx +++ b/packages/client/src/renderer/ClientRenderer.tsx @@ -1,21 +1,28 @@ import React from 'react' import ReactDOM from 'react-dom' import {Action} from 'redux' +import {IAPIDef} from '@rondo/common' import {IClientConfig} from './IClientConfig' +import {IHTTPClient, HTTPClient} from '../http' import {IRenderer} from './IRenderer' -import {TStoreFactory} from './TStoreFactory' import {Provider} from 'react-redux' import {Router} from 'react-router-dom' +import {TStoreFactory} from './TStoreFactory' import {createBrowserHistory} from 'history' -export interface IClientRendererParams { +export interface IClientRendererParams< + State, A extends Action, D extends IAPIDef> { readonly createStore: TStoreFactory, - readonly RootComponent: React.ComponentType<{config: IClientConfig}>, + readonly RootComponent: React.ComponentType<{ + config: IClientConfig, + http: IHTTPClient + }>, readonly target?: HTMLElement } -export class ClientRenderer implements IRenderer { - constructor(readonly params: IClientRendererParams) {} +export class ClientRenderer + implements IRenderer { + constructor(readonly params: IClientRendererParams) {} render( url: string, @@ -28,6 +35,8 @@ export class ClientRenderer implements IRenderer { target = document.getElementById('container'), } = this.params + const http = new HTTPClient(config.baseUrl) + const history = createBrowserHistory({ basename: config.baseUrl, }) @@ -37,7 +46,7 @@ export class ClientRenderer implements IRenderer { ReactDOM.hydrate( - + , target, @@ -47,7 +56,7 @@ export class ClientRenderer implements IRenderer { ReactDOM.render( - + , target, diff --git a/packages/client/src/renderer/IRenderer.ts b/packages/client/src/renderer/IRenderer.ts index e82c5f9..2aa76f0 100644 --- a/packages/client/src/renderer/IRenderer.ts +++ b/packages/client/src/renderer/IRenderer.ts @@ -1,5 +1,10 @@ +import {IAPIDef} from '@rondo/common' import {IClientConfig} from './IClientConfig' export interface IRenderer { - render(url: string, config: IClientConfig, state?: any): any + render( + url: string, + config: IClientConfig, + state?: any, + ): any } diff --git a/packages/client/src/renderer/ServerRenderer.tsx b/packages/client/src/renderer/ServerRenderer.tsx index c105d05..bd19181 100644 --- a/packages/client/src/renderer/ServerRenderer.tsx +++ b/packages/client/src/renderer/ServerRenderer.tsx @@ -1,21 +1,32 @@ import React from 'react' import {Action} from 'redux' +import {IAPIDef} from '@rondo/common' import {IClientConfig} from './IClientConfig' +import {IHTTPClient, HTTPClient} from '../http' import {IRenderer} from './IRenderer' -import {TStoreFactory} from './TStoreFactory' import {Provider} from 'react-redux' import {StaticRouterContext} from 'react-router' import {StaticRouter} from 'react-router-dom' +import {TStoreFactory} from './TStoreFactory' import {renderToNodeStream} from 'react-dom/server' -export class ServerRenderer implements IRenderer { +export class ServerRenderer + implements IRenderer { constructor( readonly createStore: TStoreFactory, - readonly RootComponent: React.ComponentType<{config: IClientConfig}>, + readonly RootComponent: React.ComponentType<{ + config: IClientConfig, + http: IHTTPClient + }>, ) {} - render(url: string, config: IClientConfig, state?: any) { + render( + url: string, + config: IClientConfig, + state?: any, + ) { const {RootComponent} = this const store = this.createStore(state) + const http = new HTTPClient(config.baseUrl) const context: StaticRouterContext = {} const stream = renderToNodeStream( @@ -25,7 +36,7 @@ export class ServerRenderer implements IRenderer { location={url} context={context} > - + , )