|
1 | | -import async from 'async'; |
2 | | -import _ from 'lodash'; |
3 | | - |
4 | 1 | import settings from '../../resources/settings'; |
5 | 2 | import strings from '../../resources'; |
6 | 3 | import { Config } from '../../types'; |
| 4 | +import { |
| 5 | + GetComponentResult, |
| 6 | + RendererOptions |
| 7 | +} from '../routes/helpers/get-component'; |
7 | 8 |
|
8 | | -type Cb = (err: string | null, data: string) => void; |
9 | | -type Options = { |
| 9 | +interface Options { |
| 10 | + ip?: string; |
10 | 11 | version?: string; |
11 | 12 | name?: string; |
12 | 13 | headers?: Record<string, string>; |
13 | 14 | parameters?: Record<string, string>; |
14 | | -}; |
15 | | -type Params = { |
16 | | - components: Options[]; |
17 | | - options: Options; |
18 | | - callback: Cb; |
19 | | -}; |
20 | | - |
21 | | -const sanitise = { |
22 | | - componentParams(component: string, options: Options | Cb, callback?: Cb) { |
23 | | - return { |
24 | | - ...sanitise.options(options, callback), |
25 | | - componentName: component |
26 | | - }; |
27 | | - }, |
28 | | - componentsParams( |
29 | | - components: Options[], |
30 | | - options: Options | Cb, |
31 | | - callback: Cb |
32 | | - ): Params { |
33 | | - return { |
34 | | - ...sanitise.options(options, callback), |
35 | | - components: components |
36 | | - }; |
37 | | - }, |
38 | | - headers(h = {}) { |
39 | | - return { |
40 | | - ...h, |
41 | | - accept: settings.registry.acceptRenderedHeader |
42 | | - }; |
43 | | - }, |
44 | | - options( |
45 | | - options: Options | Cb, |
46 | | - callback?: Cb |
47 | | - ): { options: Options; callback: Cb } { |
48 | | - const cb = !callback && typeof options === 'function' ? options : callback; |
49 | | - const opts = typeof options === 'function' ? {} : options; |
50 | | - |
51 | | - return { callback: cb!, options: opts }; |
52 | | - } |
53 | | -}; |
54 | | - |
55 | | -const validate = { |
56 | | - callback(c: Cb) { |
57 | | - if (!c || typeof c !== 'function') { |
58 | | - throw new Error( |
59 | | - strings.errors.registry.NESTED_RENDERER_CALLBACK_IS_NOT_VALID |
60 | | - ); |
61 | | - } |
62 | | - }, |
63 | | - componentParams(params: { componentName: string; callback: Cb }) { |
64 | | - if (!params.componentName) { |
65 | | - throw new Error( |
66 | | - strings.errors.registry.NESTED_RENDERER_COMPONENT_NAME_IS_NOT_VALID |
67 | | - ); |
68 | | - } |
69 | | - |
70 | | - validate.callback(params.callback); |
71 | | - }, |
72 | | - componentsParams(params: Params) { |
73 | | - if (_.isEmpty(params.components)) { |
74 | | - throw new Error( |
75 | | - strings.errors.registry.NESTED_RENDERER_COMPONENTS_IS_NOT_VALID |
76 | | - ); |
77 | | - } |
| 15 | +} |
78 | 16 |
|
79 | | - validate.callback(params.callback); |
80 | | - } |
81 | | -}; |
| 17 | +export default function nestedRenderer( |
| 18 | + rendererWithCallback: ( |
| 19 | + options: RendererOptions, |
| 20 | + cb: (result: GetComponentResult) => void |
| 21 | + ) => void, |
| 22 | + conf: Config |
| 23 | +) { |
| 24 | + const renderer = (options: RendererOptions) => |
| 25 | + new Promise<string>((res, rej) => { |
| 26 | + rendererWithCallback(options, result => { |
| 27 | + if (result.response.error) { |
| 28 | + rej(result.response.error); |
| 29 | + } else { |
| 30 | + res(result.response.html!); |
| 31 | + } |
| 32 | + }); |
| 33 | + }); |
82 | 34 |
|
83 | | -export default function nestedRenderer(renderer: any, conf: Config) { |
84 | 35 | return { |
85 | 36 | renderComponent( |
86 | 37 | componentName: string, |
87 | | - renderOptions: Options | Cb, |
88 | | - callback?: Cb |
89 | | - ) { |
90 | | - const p = sanitise.componentParams( |
91 | | - componentName, |
92 | | - renderOptions, |
93 | | - callback |
94 | | - ); |
95 | | - validate.componentParams(p); |
| 38 | + options: Options = {} |
| 39 | + ): Promise<string> { |
| 40 | + if (!componentName) { |
| 41 | + throw new Error( |
| 42 | + strings.errors.registry.NESTED_RENDERER_COMPONENT_NAME_IS_NOT_VALID |
| 43 | + ); |
| 44 | + } |
96 | 45 |
|
97 | | - return renderer( |
98 | | - { |
99 | | - conf: conf, |
100 | | - headers: sanitise.headers(p.options.headers), |
101 | | - name: componentName, |
102 | | - parameters: p.options.parameters || {}, |
103 | | - version: p.options.version || '' |
| 46 | + return renderer({ |
| 47 | + conf: conf, |
| 48 | + ip: options.ip || '', |
| 49 | + headers: { |
| 50 | + ...options.headers, |
| 51 | + accept: settings.registry.acceptRenderedHeader |
104 | 52 | }, |
105 | | - (result: any) => { |
106 | | - if (result.response.error) { |
107 | | - return p.callback(result.response.error, undefined as any); |
108 | | - } else { |
109 | | - return p.callback(null, result.response.html); |
110 | | - } |
111 | | - } |
112 | | - ); |
| 53 | + name: componentName, |
| 54 | + parameters: options.parameters || {}, |
| 55 | + version: options.version || '' |
| 56 | + }); |
113 | 57 | }, |
114 | 58 | renderComponents( |
115 | 59 | components: Options[], |
116 | | - renderOptions: Options, |
117 | | - callback: Cb |
118 | | - ) { |
119 | | - const p = sanitise.componentsParams(components, renderOptions, callback); |
120 | | - validate.componentsParams(p); |
| 60 | + options: Options = {} |
| 61 | + ): Promise<Array<string | Error>> { |
| 62 | + if (!components || !components.length) { |
| 63 | + throw new Error( |
| 64 | + strings.errors.registry.NESTED_RENDERER_COMPONENTS_IS_NOT_VALID |
| 65 | + ); |
| 66 | + } |
121 | 67 |
|
122 | | - async.map( |
123 | | - p.components, |
124 | | - (component, cb) => { |
125 | | - renderer( |
126 | | - { |
127 | | - conf: conf, |
128 | | - headers: sanitise.headers(p.options.headers), |
129 | | - name: component.name, |
130 | | - parameters: { |
131 | | - ...p.options.parameters, |
132 | | - ...component.parameters |
133 | | - }, |
134 | | - version: component.version || '' |
| 68 | + return Promise.all( |
| 69 | + components.map(component => { |
| 70 | + return renderer({ |
| 71 | + conf: conf, |
| 72 | + headers: { |
| 73 | + ...options.headers, |
| 74 | + accept: settings.registry.acceptRenderedHeader |
135 | 75 | }, |
136 | | - (result: any) => { |
137 | | - const error = result.response.error; |
138 | | - cb(null, error ? new Error(error) : result.response.html); |
139 | | - } |
140 | | - ); |
141 | | - }, |
142 | | - p.callback as any |
| 76 | + ip: component.ip || '', |
| 77 | + name: component.name!, |
| 78 | + parameters: { |
| 79 | + ...options.parameters, |
| 80 | + ...component.parameters |
| 81 | + }, |
| 82 | + version: component.version || '' |
| 83 | + }).catch(err => new Error(err)); |
| 84 | + }) |
143 | 85 | ); |
144 | 86 | } |
145 | 87 | }; |
|
0 commit comments