1
1
import ReactDOM from 'react-dom' ;
2
+ import type { ReactElement , Component } from 'react' ;
3
+ import type { Store } from 'redux' ;
2
4
3
5
import * as ClientStartup from './clientStartup' ;
4
6
import handleError from './handleError' ;
@@ -9,20 +11,33 @@ import buildConsoleReplay from './buildConsoleReplay';
9
11
import createReactElement from './createReactElement' ;
10
12
import Authenticity from './Authenticity' ;
11
13
import context from './context' ;
14
+ import type {
15
+ RegisteredComponent ,
16
+ RenderParams ,
17
+ ErrorOptions ,
18
+ ComponentOrRenderFunction ,
19
+ AuthenticityHeaders ,
20
+ StoreGenerator
21
+ } from './types/index' ;
12
22
13
23
const ctx = context ( ) ;
14
24
25
+ if ( ctx === undefined ) {
26
+ throw new Error ( "The context (usually Window or NodeJS's Global) is undefined." ) ;
27
+ }
28
+
15
29
const DEFAULT_OPTIONS = {
16
30
traceTurbolinks : false ,
17
31
} ;
18
32
19
33
ctx . ReactOnRails = {
34
+ options : { } ,
20
35
/**
21
36
* Main entry point to using the react-on-rails npm package. This is how Rails will be able to
22
37
* find you components for rendering.
23
38
* @param components (key is component name, value is component)
24
39
*/
25
- register ( components ) {
40
+ register ( components : { [ id : string ] : ComponentOrRenderFunction } ) : void {
26
41
ComponentRegistry . register ( components ) ;
27
42
} ,
28
43
@@ -32,7 +47,7 @@ ctx.ReactOnRails = {
32
47
* the setStore API is different in that it's the actual store hydrated with props.
33
48
* @param stores (keys are store names, values are the store generators)
34
49
*/
35
- registerStore ( stores ) {
50
+ registerStore ( stores : { [ id : string ] : Store } ) : void {
36
51
if ( ! stores ) {
37
52
throw new Error ( 'Called ReactOnRails.registerStores with a null or undefined, rather than ' +
38
53
'an Object with keys being the store names and the values are the store generators.' ) ;
@@ -50,7 +65,7 @@ ctx.ReactOnRails = {
50
65
* there is no store with the given name.
51
66
* @returns Redux Store, possibly hydrated
52
67
*/
53
- getStore ( name , throwIfMissing = true ) {
68
+ getStore ( name : string , throwIfMissing = true ) : Store | undefined {
54
69
return StoreRegistry . getStore ( name , throwIfMissing ) ;
55
70
} ,
56
71
@@ -59,7 +74,7 @@ ctx.ReactOnRails = {
59
74
* Available Options:
60
75
* `traceTurbolinks: true|false Gives you debugging messages on Turbolinks events
61
76
*/
62
- setOptions ( newOptions ) {
77
+ setOptions ( newOptions : { traceTurbolinks : boolean } ) : void {
63
78
if ( 'traceTurbolinks' in newOptions ) {
64
79
this . options . traceTurbolinks = newOptions . traceTurbolinks ;
65
80
@@ -69,7 +84,7 @@ ctx.ReactOnRails = {
69
84
70
85
if ( Object . keys ( newOptions ) . length > 0 ) {
71
86
throw new Error (
72
- ' Invalid options passed to ReactOnRails.options: ' , JSON . stringify ( newOptions ) ,
87
+ ` Invalid options passed to ReactOnRails.options: ${ JSON . stringify ( newOptions ) } ` ,
73
88
) ;
74
89
}
75
90
} ,
@@ -80,7 +95,7 @@ ctx.ReactOnRails = {
80
95
* More details can be found here:
81
96
* https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/turbolinks.md
82
97
*/
83
- reactOnRailsPageLoaded ( ) {
98
+ reactOnRailsPageLoaded ( ) : void {
84
99
ClientStartup . reactOnRailsPageLoaded ( ) ;
85
100
} ,
86
101
@@ -89,7 +104,7 @@ ctx.ReactOnRails = {
89
104
* @returns String or null
90
105
*/
91
106
92
- authenticityToken ( ) {
107
+ authenticityToken ( ) : string | null {
93
108
return Authenticity . authenticityToken ( ) ;
94
109
} ,
95
110
@@ -99,7 +114,7 @@ ctx.ReactOnRails = {
99
114
* @returns {* } header
100
115
*/
101
116
102
- authenticityHeaders ( otherHeaders = { } ) {
117
+ authenticityHeaders ( otherHeaders : { [ id : string ] : string } = { } ) : AuthenticityHeaders {
103
118
return Authenticity . authenticityHeaders ( otherHeaders ) ;
104
119
} ,
105
120
@@ -112,7 +127,7 @@ ctx.ReactOnRails = {
112
127
* @param key
113
128
* @returns option value
114
129
*/
115
- option ( key ) {
130
+ option ( key : string ) : string | number | boolean | undefined {
116
131
return this . options [ key ] ;
117
132
} ,
118
133
@@ -122,7 +137,7 @@ ctx.ReactOnRails = {
122
137
* @param name
123
138
* @returns Redux Store generator function
124
139
*/
125
- getStoreGenerator ( name ) {
140
+ getStoreGenerator ( name : string ) : StoreGenerator {
126
141
return StoreRegistry . getStoreGenerator ( name ) ;
127
142
} ,
128
143
@@ -131,15 +146,15 @@ ctx.ReactOnRails = {
131
146
* @param name
132
147
* @returns Redux Store, possibly hydrated
133
148
*/
134
- setStore ( name , store ) {
149
+ setStore ( name : string , store : Store ) : void {
135
150
return StoreRegistry . setStore ( name , store ) ;
136
151
} ,
137
152
138
153
/**
139
154
* Clears hydratedStores to avoid accidental usage of wrong store hydrated in previous/parallel
140
155
* request.
141
156
*/
142
- clearHydratedStores ( ) {
157
+ clearHydratedStores ( ) : void {
143
158
StoreRegistry . clearHydratedStores ( ) ;
144
159
} ,
145
160
@@ -156,72 +171,72 @@ ctx.ReactOnRails = {
156
171
* @param hydrate Pass truthy to update server rendered html. Default is falsy
157
172
* @returns {virtualDomElement } Reference to your component's backing instance
158
173
*/
159
- render ( name , props , domNodeId , hydrate ) {
174
+ render ( name : string , props : Record < string , string > , domNodeId : string , hydrate : boolean ) : void | Element | Component {
160
175
const componentObj = ComponentRegistry . get ( name ) ;
161
176
const reactElement = createReactElement ( { componentObj, props, domNodeId } ) ;
162
177
163
178
const render = hydrate ? ReactDOM . hydrate : ReactDOM . render ;
164
179
// eslint-disable-next-line react/no-render-return-value
165
- return render ( reactElement , document . getElementById ( domNodeId ) ) ;
180
+ return render ( reactElement as ReactElement , document . getElementById ( domNodeId ) ) ;
166
181
} ,
167
182
168
183
/**
169
184
* Get the component that you registered
170
185
* @param name
171
186
* @returns {name, component, generatorFunction, isRenderer }
172
187
*/
173
- getComponent ( name ) {
188
+ getComponent ( name : string ) : RegisteredComponent {
174
189
return ComponentRegistry . get ( name ) ;
175
190
} ,
176
191
177
192
/**
178
193
* Used by server rendering by Rails
179
194
* @param options
180
195
*/
181
- serverRenderReactComponent ( options ) {
196
+ serverRenderReactComponent ( options : RenderParams ) : string {
182
197
return serverRenderReactComponent ( options ) ;
183
198
} ,
184
199
185
200
/**
186
201
* Used by Rails to catch errors in rendering
187
202
* @param options
188
203
*/
189
- handleError ( options ) {
204
+ handleError ( options : ErrorOptions ) : string | undefined {
190
205
return handleError ( options ) ;
191
206
} ,
192
207
193
208
/**
194
209
* Used by Rails server rendering to replay console messages.
195
210
*/
196
- buildConsoleReplay ( ) {
211
+ buildConsoleReplay ( ) : string {
197
212
return buildConsoleReplay ( ) ;
198
213
} ,
199
214
200
215
/**
201
216
* Get an Object containing all registered components. Useful for debugging.
202
217
* @returns {* }
203
218
*/
204
- registeredComponents ( ) {
219
+ registeredComponents ( ) : Map < string , RegisteredComponent > {
205
220
return ComponentRegistry . components ( ) ;
206
221
} ,
207
222
208
223
/**
209
224
* Get an Object containing all registered store generators. Useful for debugging.
210
225
* @returns {* }
211
226
*/
212
- storeGenerators ( ) {
227
+ storeGenerators ( ) : Map < string , Function > {
213
228
return StoreRegistry . storeGenerators ( ) ;
214
229
} ,
215
230
216
231
/**
217
232
* Get an Object containing all hydrated stores. Useful for debugging.
218
233
* @returns {* }
219
234
*/
220
- stores ( ) {
235
+ stores ( ) : Map < string , Store > {
221
236
return StoreRegistry . stores ( ) ;
222
237
} ,
223
238
224
- resetOptions ( ) {
239
+ resetOptions ( ) : void {
225
240
this . options = Object . assign ( { } , DEFAULT_OPTIONS ) ;
226
241
} ,
227
242
} ;
0 commit comments