11import { ApplicationConfig , InjectionToken , Newable } from "../domain/types" ;
22import { AppsScriptEventType , RequestMethod } from "../domain/enums" ;
3- import {
4- EventDispatcher ,
5- RequestFactory ,
6- Resolver ,
7- ResponseBuilder ,
8- Router ,
9- RouterExplorer
10- } from "../service" ;
3+ import { EventDispatcher , RequestFactory , Resolver , ResponseBuilder , Router , RouterExplorer } from "../service" ;
4+ import { isSymbol } from "apps-script-utils" ;
115
6+ /**
7+ * Main application class for bootstrapping and handling Google Apps Script events.
8+ */
129export class BootApplication {
1310 private readonly _controllers = new Map < Newable , unknown > ( ) ;
1411 private readonly _providers = new Map < InjectionToken , unknown > ( ) ;
@@ -18,6 +15,11 @@ export class BootApplication {
1815 private readonly _responseBuilder = new ResponseBuilder ( ) ;
1916 private readonly _eventDispatcher : EventDispatcher ;
2017
18+ /**
19+ * Creates a new instance of BootApplication.
20+ *
21+ * @param {ApplicationConfig } config The application configuration.
22+ */
2123 constructor ( config : ApplicationConfig ) {
2224 ( config . controllers || [ ] ) . forEach ( ( c ) => this . _controllers . set ( c , null ) ) ;
2325
@@ -50,26 +52,62 @@ export class BootApplication {
5052 this . _eventDispatcher = new EventDispatcher ( this . _resolver , this . _controllers ) ;
5153 }
5254
55+ /**
56+ * Handles Google Apps Script doGet events.
57+ *
58+ * @param {GoogleAppsScript.Events.DoGet } event The doGet event object.
59+ * @returns {Promise<GoogleAppsScript.HTML.HtmlOutput | GoogleAppsScript.Content.TextOutput> } The response object.
60+ */
5361 public async doGet ( event : GoogleAppsScript . Events . DoGet ) {
5462 return this . handleHttpRequest ( RequestMethod . GET , event ) ;
5563 }
5664
65+ /**
66+ * Handles Google Apps Script doPost events.
67+ *
68+ * @param {GoogleAppsScript.Events.DoPost } event The doPost event object.
69+ * @returns {Promise<GoogleAppsScript.HTML.HtmlOutput | GoogleAppsScript.Content.TextOutput> } The response object.
70+ */
5771 public async doPost ( event : GoogleAppsScript . Events . DoPost ) {
5872 return this . handleHttpRequest ( RequestMethod . POST , event ) ;
5973 }
6074
75+ /**
76+ * Handles Google Apps Script onInstall events.
77+ *
78+ * @param {GoogleAppsScript.Events.AddonOnInstall } event The onInstall event object.
79+ * @returns {Promise<void> }
80+ */
6181 public async onInstall ( event : GoogleAppsScript . Events . AddonOnInstall ) {
6282 await this . _eventDispatcher . dispatch ( AppsScriptEventType . INSTALL , event ) ;
6383 }
6484
85+ /**
86+ * Handles Google Apps Script onOpen events.
87+ *
88+ * @param {GoogleAppsScript.Events.AppsScriptEvent } event The onOpen event object.
89+ * @returns {Promise<void> }
90+ */
6591 public async onOpen ( event : GoogleAppsScript . Events . AppsScriptEvent ) {
6692 await this . _eventDispatcher . dispatch ( AppsScriptEventType . OPEN , event ) ;
6793 }
6894
95+ /**
96+ * Handles Google Apps Script onEdit events.
97+ *
98+ * @param {GoogleAppsScript.Events.SheetsOnEdit } event The onEdit event object.
99+ * @returns {Promise<void> }
100+ */
69101 public async onEdit ( event : GoogleAppsScript . Events . SheetsOnEdit ) {
70102 await this . _eventDispatcher . dispatch ( AppsScriptEventType . EDIT , event ) ;
71103 }
72104
105+ /**
106+ * Handles Google Apps Script onChange events.
107+ *
108+ * @param {GoogleAppsScript.Events.SheetsOnChange } event The onChange event object.
109+ * @returns {Promise<void> }
110+ */
73111 public async onChange ( event : GoogleAppsScript . Events . SheetsOnChange ) {
74112 await this . _eventDispatcher . dispatch ( AppsScriptEventType . CHANGE , event ) ;
75113 }
@@ -79,10 +117,42 @@ export class BootApplication {
79117 // await this.eventDispatcher.dispatch(AppsScriptEventType.SELECTION_CHANGE, event);
80118 // }
81119
120+ /**
121+ * Handles Google Apps Script onFormSubmit events.
122+ *
123+ * @param {GoogleAppsScript.Events.FormsOnFormSubmit } event The onFormSubmit event object.
124+ * @returns {Promise<void> }
125+ */
82126 public async onFormSubmit ( event : GoogleAppsScript . Events . FormsOnFormSubmit ) {
83127 await this . _eventDispatcher . dispatch ( AppsScriptEventType . FORM_SUBMIT , event ) ;
84128 }
85129
130+ /**
131+ * Returns a Proxy object that can be used to handle Google Apps Script menu actions.
132+ *
133+ * @returns {any } A Proxy object.
134+ */
135+ public onMenu ( ) : any {
136+ return new Proxy ( this , {
137+ get ( target , prop , receiver ) {
138+ if ( prop === "inspect" || isSymbol ( prop ) ) {
139+ return Reflect . get ( target , prop , receiver ) ;
140+ }
141+
142+ return ( event : GoogleAppsScript . Events . AppsScriptEvent ) => {
143+ return target . _eventDispatcher . dispatchByName ( prop as string , event ) ;
144+ } ;
145+ }
146+ } ) ;
147+ }
148+
149+ /**
150+ * Handles incoming HTTP requests and routes them to the appropriate controller.
151+ *
152+ * @param {RequestMethod } method The HTTP method (GET or POST).
153+ * @param {GoogleAppsScript.Events.DoGet | GoogleAppsScript.Events.DoPost } event The Apps Script event object.
154+ * @returns {Promise<GoogleAppsScript.HTML.HtmlOutput | GoogleAppsScript.Content.TextOutput> } The response object.
155+ */
86156 private async handleHttpRequest (
87157 method : RequestMethod ,
88158 event : GoogleAppsScript . Events . DoGet | GoogleAppsScript . Events . DoPost
0 commit comments