| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 16:52:45 +02:00
										 |  |  | const getDefaultContentTypeConfig = () => ({ | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  |   enabled: true, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mutations: true, | 
					
						
							|  |  |  |   queries: true, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   disabledActions: [], | 
					
						
							| 
									
										
										
										
											2021-09-22 16:52:45 +02:00
										 |  |  |   fields: new Map(), | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const getDefaultFieldConfig = () => ({ | 
					
						
							|  |  |  |   enabled: true, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   input: true, | 
					
						
							|  |  |  |   output: true, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   filters: true, | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ALL_ACTIONS = '*'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = () => { | 
					
						
							|  |  |  |   const configs = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |   return (uid) => { | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  |     if (!configs.has(uid)) { | 
					
						
							| 
									
										
										
										
											2021-09-22 16:52:45 +02:00
										 |  |  |       configs.set(uid, getDefaultContentTypeConfig()); | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       isEnabled() { | 
					
						
							|  |  |  |         return configs.get(uid).enabled; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isDisabled() { | 
					
						
							|  |  |  |         return !this.isEnabled(); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       areQueriesEnabled() { | 
					
						
							|  |  |  |         return configs.get(uid).queries; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       areQueriesDisabled() { | 
					
						
							|  |  |  |         return !this.areQueriesEnabled(); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       areMutationsEnabled() { | 
					
						
							|  |  |  |         return configs.get(uid).mutations; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       areMutationsDisabled() { | 
					
						
							|  |  |  |         return !this.areMutationsEnabled(); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isActionEnabled(action) { | 
					
						
							|  |  |  |         const matchingActions = [action, ALL_ACTIONS]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |         return configs | 
					
						
							|  |  |  |           .get(uid) | 
					
						
							|  |  |  |           .disabledActions.every((action) => !matchingActions.includes(action)); | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       isActionDisabled(action) { | 
					
						
							|  |  |  |         return !this.isActionEnabled(action); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       disable() { | 
					
						
							|  |  |  |         configs.get(uid).enabled = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       disableQueries() { | 
					
						
							|  |  |  |         configs.get(uid).queries = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       disableMutations() { | 
					
						
							|  |  |  |         configs.get(uid).mutations = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       disableAction(action) { | 
					
						
							|  |  |  |         const config = configs.get(uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!config.disabledActions.includes(action)) { | 
					
						
							|  |  |  |           config.disabledActions.push(action); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       disableActions(actions = []) { | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |         actions.forEach((action) => this.disableAction(action)); | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return this; | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-09-22 16:52:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       field(fieldName) { | 
					
						
							|  |  |  |         const { fields } = configs.get(uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!fields.has(fieldName)) { | 
					
						
							|  |  |  |           fields.set(fieldName, getDefaultFieldConfig()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           isEnabled() { | 
					
						
							|  |  |  |             return fields.get(fieldName).enabled; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           hasInputEnabled() { | 
					
						
							|  |  |  |             return fields.get(fieldName).input; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           hasOutputEnabled() { | 
					
						
							|  |  |  |             return fields.get(fieldName).output; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           hasFiltersEnabeld() { | 
					
						
							|  |  |  |             return fields.get(fieldName).filters; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           disable() { | 
					
						
							|  |  |  |             fields.set(fieldName, { | 
					
						
							|  |  |  |               enabled: false, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               output: false, | 
					
						
							|  |  |  |               input: false, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               filters: false, | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return this; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           disableOutput() { | 
					
						
							|  |  |  |             fields.get(fieldName).output = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return this; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           disableInput() { | 
					
						
							|  |  |  |             fields.get(fieldName).input = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return this; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           disableFilters() { | 
					
						
							|  |  |  |             fields.get(fieldName).filters = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return this; | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-09-15 15:44:42 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; |