@financial-times/dotcom-server-react-jsx 7.3.1 → 7.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@financial-times/dotcom-server-react-jsx",
3
- "version": "7.3.1",
3
+ "version": "7.3.2",
4
4
  "description": "",
5
5
  "main": "dist/node/index.js",
6
6
  "types": "src/index.ts",
@@ -26,7 +26,8 @@
26
26
  "npm": "7.x || 8.x"
27
27
  },
28
28
  "files": [
29
- "dist/"
29
+ "dist/",
30
+ "src/"
30
31
  ],
31
32
  "repository": {
32
33
  "type": "git",
@@ -0,0 +1,52 @@
1
+ import { createElement } from 'react'
2
+ import { renderToString, renderToStaticMarkup } from 'react-dom/server'
3
+ import interopRequire from './interopRequire'
4
+ import { Renderable, RenderCallback } from './types'
5
+
6
+ export interface TPageKitReactJSXOptions {
7
+ useStaticRendering?: boolean,
8
+ includeDoctype?: boolean
9
+ }
10
+
11
+ const defaultOptions: TPageKitReactJSXOptions = {
12
+ useStaticRendering: false,
13
+ includeDoctype: true
14
+ }
15
+
16
+ export class PageKitReactJSX {
17
+ public options: TPageKitReactJSXOptions
18
+ public engine: Function
19
+
20
+ constructor(userOptions: TPageKitReactJSXOptions = {}) {
21
+ this.options = { ...defaultOptions, ...userOptions }
22
+ this.engine = this.renderView.bind(this)
23
+ }
24
+
25
+ async render(component: Renderable, templateContext: any, includeDoctype: boolean = this.options.includeDoctype): Promise<string> {
26
+ if (typeof component.getInitialProps === 'function') {
27
+ templateContext = await component.getInitialProps(templateContext)
28
+ }
29
+
30
+ const outputPrefix = includeDoctype ? '<!DOCTYPE html>' : ''
31
+ const renderMethod = this.options.useStaticRendering ? renderToStaticMarkup : renderToString
32
+ const outputHTML = renderMethod(createElement(component, templateContext))
33
+
34
+ return outputPrefix + outputHTML
35
+ }
36
+
37
+ async renderView(viewPath: string, templateContext: any, callback: RenderCallback): Promise<void> {
38
+ try {
39
+ const element = interopRequire(viewPath) as Renderable
40
+
41
+ if (typeof element !== 'function') {
42
+ throw Error(`The module ${viewPath} requires a default export.`)
43
+ }
44
+
45
+ const output = await this.render(element, templateContext)
46
+
47
+ callback(null, output)
48
+ } catch (error) {
49
+ callback(error)
50
+ }
51
+ }
52
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './PageKitReactJSX'
@@ -0,0 +1,4 @@
1
+ export default function interopRequire(path: string): any {
2
+ const obj = require(path)
3
+ return obj && obj.__esModule ? obj['default'] : obj
4
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import React from 'react'
2
+
3
+ // Extend @types/react to support optional additional .getInitialProps() method.
4
+ // This is adapted from the @types/next:
5
+ // <https://github.com/DefinitelyTyped/DefinitelyTyped/blob/HEAD/types/next/index.d.ts>
6
+
7
+ /**
8
+ * Next.js style counterpart of React.FunctionComponent.
9
+ *
10
+ * @template P Component props.
11
+ * @template C Context passed to getInitialProps.
12
+ */
13
+ type NextFunctionComponent<P = {}, C = {}> = React.FunctionComponent<P> & NextStaticLifecycle<P, C>
14
+
15
+ /**
16
+ * Next.js style counterpart of React.ComponentClass.
17
+ *
18
+ * @template P Component props.
19
+ * @template C Context passed to getInitialProps.
20
+ */
21
+ type NextComponentClass<P = {}, C = {}> = React.ComponentClass<P> & NextStaticLifecycle<P, C>
22
+
23
+ /**
24
+ * Next.js style lifecycle methods.
25
+ *
26
+ * @template P Props returned from getInitialProps and passed to the component.
27
+ * @template C Context passed to getInitialProps.
28
+ */
29
+ interface NextStaticLifecycle<P, C> {
30
+ getInitialProps?: (templateContext: C) => Promise<P> | P
31
+ }
32
+
33
+ export type Renderable = NextComponentClass | NextFunctionComponent
34
+
35
+ export type RenderCallback = (error?: Error, output?: string) => any