structured-fw 0.8.21 → 0.8.42

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/README.md CHANGED
@@ -5,6 +5,9 @@ Framework allows the developer to develop self-contained components which are re
5
5
 
6
6
  It works with Node.js and Deno runtimes. Other runtimes are not tested.
7
7
 
8
+ > [!NOTE]
9
+ > While Structured framework is in development for a couple of years and is production tested, the npm package is introduced recently and there were still issues that came up with it in the past few versions. Since version 0.8.3 all should be functional. Please update to latest version and check for updates regularly until the npm package is as stable as the framework itself. Feel free to open issues on the github page if you have any issues with the npm package or the framework. _Structured followed versioning x.y.z where z was a single digit 0-9, but since there were a couple of versions that introduced no changes to the framework and were just npm package tweaks, I decided to make an exception to the rule and allow myself to use 2 digits for such updates._
10
+
8
11
  - [Why Structured](#why-structured)
9
12
  - [Audience](#audience)
10
13
  - [Getting started](#getting-started)
@@ -110,13 +113,13 @@ new Application(config);
110
113
 
111
114
  ### Methods
112
115
  - `init(): Promise<void>` - initializes application, you only need to run this if you set `autoInit = false` in config, otherwise this will be ran when you create the Application instance
113
- - `on(evt: ApplicationEvents, callback: RequestCallback|((payload?: any) => void))` - allows you to add event listeners for specific `ApplicationEvenets`:
114
- - `serverStarted` - executed once the built-in http server is started and running. Callback receives no arguments
116
+ - `on(evt: ApplicationEvents, callback: RequestCallback|((payload?: any) => void))` - allows you to add event listeners for specific `ApplicationEvents`:
117
+ - `serverStarted` - executed once the built-in http server is started and running. Callback receives Server (exported from node:http) instance as the first argument
115
118
  - `beforeRequestHandler` - runs before any request handler (route) is executed. Callback receives `RequestContext` as the first argument. Useful for example to set `RequestContext.data: RequestContextData` (user defined data, to make it available to routes and components)
116
119
  - `afterRequestHandler` - runs after any request handler (route) is executed. Callback receives `RequestContext` as the first argument
117
120
  - `afterRoutes` - runs after all routes are loaded from `StructuredConfig.routes.path`. Callback receives no arguments
118
- - `beforeComponentLoad` - runs before components are loaded from `StructuredConfig.components.path`. Callback receives no arguments
119
- - `afterComponentLoad` - runs after all components are loaded from `StructuredConfig.components.path`. Callback receives no arguments
121
+ - `beforeComponentsLoad` - runs before components are loaded from `StructuredConfig.components.path`. Callback receives no arguments
122
+ - `afterComponentsLoaded` - runs after all components are loaded from `StructuredConfig.components.path`. Callback receives instance of Components as the first argument
120
123
  - `documentCreated` - runs whenever an instance of a [Document](#document) is created. Callback receives the Document instance as the first argument. You will often use this, for example if you want to include a CSS file to all pages `Document.head.addCSS(...)`
121
124
  - `beforeAssetAccess` - runs when assets are being accessed, before response is sent. Callback receives `RequestContext` as the first argument
122
125
  - `afterAssetAccess` - runs when assets are being accessed, after response is sent. Callback receives `RequestContext` as the first argument
@@ -173,7 +176,7 @@ app.exportContextFields('user');
173
176
  ```
174
177
 
175
178
  ### Session
176
- Session allows you to store temporary data for the users of your web application. You don't need to create an instance of Session, you will always use the instace `Application.session`.
179
+ Session allows you to store temporary data for the users of your web application. You don't need to create an instance of Session, you will always use the instance `Application.session`.
177
180
 
178
181
  Session data is tied to a visitor via sessionId, which is always available on `RequestContext`, which means you can interact with session data from routes and server side code of your components.
179
182
 
@@ -321,16 +324,16 @@ In some edge cases you may need more control of when a route is executed, in whi
321
324
  > email: string,
322
325
  > password: string,
323
326
  > age: number
324
- >}>('POST', '/users/create', asyc (ctx) => {
327
+ >}>('POST', '/users/create', async (ctx) => {
325
328
  > ctx.body.email // string
326
329
  > ctx.body.age // number
327
- > const doc = new Document(ctx, 'User', app);
330
+ > const doc = new Document(app, 'User', ctx);
328
331
  > return doc; // error if we return anything but Document
329
332
  > });
330
333
  > ```
331
334
 
332
335
  ## Document
333
- Document does not differ much from a component, in fact, it extends Component. It has a more user-firendly API than Component. Each Document represents a web page. It has a head and body. Structured intentionally does not differentiate between a page and a Component - page is just a component that loads many other components in a desired layout. DocumentHead (each document has one at Document.head) allows adding content to `<head>` section of the output HTML page.
336
+ Document does not differ much from a component, in fact, it extends Component. It has a more user-friendly API than Component. Each Document represents a web page. It has a head and body. Structured intentionally does not differentiate between a page and a Component - page is just a component that loads many other components in a desired layout. DocumentHead (each document has one at Document.head) allows adding content to `<head>` section of the output HTML page.
334
337
 
335
338
  Creating a document:
336
339
  `const doc = new Document(app, 'HelloWorld page', ctx);`
@@ -344,13 +347,25 @@ app.request.on('GET', '/home', async (ctx) => {
344
347
  });
345
348
  ```
346
349
 
350
+ > [!TIP]
351
+ > Since version 0.8.4 Document extends EventEmitter, and "componentCreated" event is emitted whenever a component instance is created within the Document.\
352
+ > This makes the following possible:
353
+ > ```
354
+ > app.on('documentCreated', (doc) => {
355
+ > doc.on('componentCreated', (component) => {
356
+ > // do something with the document or the component
357
+ > })
358
+ > })
359
+ > ```
360
+
347
361
  ## Component
348
- A component is comprised of 1-3 files. It always must include one HTML file, while server side and client side files are optional.
349
- * HTML file preobably requires no explanation
362
+ A component is comprised of [1-3 files](#component-parts). It always must include one HTML file, while server side and client side files are optional.
363
+ * HTML file probably requires no explanation
350
364
  * server side file, code that runs on the server and makes data available to HTML and client side code
351
365
  * client side file, code that runs on the client (in the browser)
352
366
 
353
- You should never need to instantiate a Component on your own. You will always load a Component representing your page into a document (using `Document.loadComponent(componentName: string)`), which will know what to do from there.
367
+ > [!TIP]
368
+ > You should never need to instantiate a Component on your own. You will always load a Component representing your page into a document (using `Document.loadComponent(componentName: string)`), which will know what to do from there.
354
369
 
355
370
  Example component files:
356
371
  - `/app/views/`
@@ -367,9 +382,15 @@ It is recommended, but not necessary, that you contain each component in it's ow
367
382
  \
368
383
  **Component rules:**
369
384
  - **Component names must be unique**
370
- - Components HTML file can have a `.hbs` extension (which allows for better Handlebars sytax highlighting)
385
+ - Components HTML file can have a `.hbs` extension (which allows for better Handlebars syntax highlighting)
371
386
  - Components can reside at any depth in the file structure
372
387
 
388
+ ### Component parts
389
+ - [Component HTML](#component-html) (_ComponentName.html_)
390
+ - [Component server-side code](#component-server-side-code) (_ComponentName.ts_)
391
+ - [Component client-side code](#component-client-side-code) (_ComponentName.client.ts_)
392
+
393
+ ### Component HTML
373
394
  Let's create a HelloWorld Component `/app/views/HelloWorld/HelloWorld.html`:\
374
395
  `Hello, World!`
375
396
 
@@ -389,7 +410,12 @@ export default function(app: Application) {
389
410
  You can now run the app and if you open /hello/world in the browser you will see:\
390
411
  `Hello, World!` - which came from your HelloWorld component.
391
412
 
392
- That was the simplest possible example, let's make it more interesting.
413
+ > [!TIP]
414
+ > It is recommended to use .hbs (Handlebars) extension as you will get better syntax highlighting in most IDEs. Other than syntax highlighting there is no difference between using html or hbs extension.
415
+
416
+ That was the simplest possible example, let's make it more interesting by adding some server-side code.
417
+
418
+ ### Component server-side code
393
419
  Create a new file `/app/views/HelloWorld/HelloWorld.ts` (server side component code):
394
420
  ```
395
421
  import { ComponentScaffold } from 'system/Types.js';
@@ -423,7 +449,15 @@ Your lucky number is [a number from 0-100]
423
449
  This demonstrates the use of a *server side component code* to make data available to HTML.
424
450
  We just generated a random number, but the data could be anything and will more often come from a database, session, or be provided by the parent component.
425
451
 
452
+ > [!IMPORTANT]
453
+ > Server side `getData` will receive the following arguments:
454
+ > - `data: LooseObject` any data passed in (either by attributes, ClientComponent.add or ClientComponent.redraw)
455
+ > - `ctx: RequestContext` - current `RequestContext`, you will often use this to access for example ctx.data (`RequestContextData`) or ctx.sessionId to interact with session
456
+ > - `app: Application` - your Application instance. You can use it to, for example, access the session in combination with ctx.sessionId
457
+
426
458
  Let's make it even more interesting by adding some client side code to it.
459
+
460
+ ### Component client-side code
427
461
  Create `/app/views/HelloWorld/HelloWorld.client.ts`:
428
462
  ```
429
463
  import { InitializerFunction } from 'system/Types.js';
@@ -499,19 +533,13 @@ What we did is, we accepted the number provided by parent component, and returne
499
533
  betterNumber: number
500
534
  }
501
535
  ```
502
- which is now avaialble in `AnotherComponent` HTML, we assigned the received number to `parentSuggests`, while `betterNumber` is `parentSuggests + 5`, we now have these 2 available and ready to use in our HTML template.
536
+ which is now available in `AnotherComponent` HTML, we assigned the received number to `parentSuggests`, while `betterNumber` is `parentSuggests + 5`, we now have these 2 available and ready to use in our HTML template.
503
537
 
504
538
  What about client side? **By default, data returned by server side code is not available in client side code** for obvious reasons, let's assume your server side code returns sensitive data such as user's password, you would not like that exposed on the client side, hence exporting data needs to be explicitly requested in the server side code. There are two ways to achieve this, setting `exportData = true` (exports all data), or `exportFields: Array<string> = [...keysToExport]` (export only given fields).
505
539
 
506
540
  > [!NOTE]
507
541
  > Whenever a component with server-side code is rendered, `getData` is automatically called and anything it returns is available in HTML. You can export all returned data to client-side code by setting `exportData = true` or you can export some of the fields by setting `exportFields = ["field1", "field2", ...]` as a direct property of the class. To access the exported data from client-side use `ClientComponent`.`getData(key: string)` which will be `this.getData(key:string)` within client side code.
508
542
 
509
- > [!IMPORTANT]
510
- > Server side `getData` will receive the following arguments:
511
- > - `data: LooseObject` any data passed in (either by attributes, ClientComponent.add or ClientComponent.redraw)
512
- > - `ctx: RequestContext` - current `RequestContext`, you will often use this to access for example ctx.data (`RequestContextData`) or ctx.sessionId to interact with session
513
- > - `app: Application` - your Application instance. You can use it to, for example, access the session in combination with ctx.sessionId
514
-
515
543
  Let's create a client side code for `AnotherComponent` and export the `betterNumber` to it, create `/app/views/AnotherComponent/AnotherComponent.client.ts`:
516
544
  ```
517
545
  import { InitializerFunction } from 'system/Types.js';
@@ -555,7 +583,7 @@ export const init: InitializerFunction = async function() {
555
583
  ```
556
584
  Here we accessed the `parent` and obtained it's `name`.
557
585
 
558
- *"But we did not send any data to the parent here"* - correct, we did not, and we won't, instead we can inform them we have some data available, or that an event they might be interested in has ocurred, and if they care, so be it:
586
+ *"But we did not send any data to the parent here"* - correct, we did not, and we won't, instead we can inform them we have some data available, or that an event they might be interested in has occurred, and if they care, so be it:
559
587
  ```
560
588
  import { InitializerFunction } from 'system/Types.js';
561
589
  export const init: InitializerFunction = async function() {
@@ -585,7 +613,7 @@ export const init: InitializerFunction = async function() {
585
613
  }
586
614
  ```
587
615
 
588
- That's it. If there is `AnotherComponent` found within `HelloWorld` (which there is in our case) we are subscribing to "truth" event and capturing the payload. Payload is optional, sometimes we just want to inform anyone interested that a certain event has ocurred, without the need to pass any extra data with it. We used `this.find(componentName: string)`, this will recursively find the first instance of a component with `componentName`, optionally you can make it non-recursive by passing `false` as the second argument to `find` method in which case it will look for a direct child with given name.
616
+ That's it. If there is `AnotherComponent` found within `HelloWorld` (which there is in our case) we are subscribing to "truth" event and capturing the payload. Payload is optional, sometimes we just want to inform anyone interested that a certain event has occurred, without the need to pass any extra data with it. We used `this.find(componentName: string)`, this will recursively find the first instance of a component with `componentName`, optionally you can make it non-recursive by passing `false` as the second argument to `find` method in which case it will look for a direct child with given name.
589
617
 
590
618
  We have only scratched the surface of what client-side code of a component is capable of. Which brings us to `this`. In client-side code of a component, `this` is the instance of a `ClientComponent`.
591
619
 
@@ -605,15 +633,70 @@ Methods:
605
633
  - `store.set(key: string, value: any)` - set data in client side data store
606
634
  - `find(componentName: string, recursive: boolean = true): ClientComponent | null` - find a child component
607
635
  - `findParent(componentName: string): ClientComponent | null` - find the first parent with given name
608
- - `query(componentName: string, recursive: boolean = true): Array<ClientComponent>` - return all components with given name found within this component, if `recurive = false`, only direct children are considered
636
+ - `query(componentName: string, recursive: boolean = true): Array<ClientComponent>` - return all components with given name found within this component, if `recursive = false`, only direct children are considered
609
637
  - `ref<T>(refName: string): T` - get a HTMLElement or ClientComponent that has attribute `ref="[refName]"`
610
638
  - `arrayRef<T>(refName: string): Array<T>` - get an array of HTMLElement or ClientComponent that have attribute `array:ref="[refName]"`
611
- - `add(appendTo: HTMLElement, componentName: string, data?: LooseObject)` - add `componentName` component to `appendTo` element, optionally passing `data` to the component when it's being rendered
639
+ - `add(appendTo: HTMLElement, componentName: string, data?: LooseObject): Promise<ClientComponent | null>` - add `componentName` component to `appendTo` element, optionally passing `data` to the component when it's being rendered. Returns a promise that resolves with added ClientComponent or null if something went wrong
640
+ - `redraw(data?: LooseObject): Promise<void>` - redraw the component, optionally provide data which will be available server side
641
+
642
+ ### Conditionals
643
+ You can make any DOM node within your components conditionally shown/hidden using `data-if` attribute.\
644
+ For example:
645
+ ```
646
+ <div data-if="showDiv"></div>
647
+ ```
648
+ Above div will only be shown if store.showDiv = true
649
+
650
+ You can also use `!` to invert the value, `!showDiv` in which case div would be shown if showDiv is false.
651
+
652
+ You can also use comparison:
653
+ ```
654
+ <div data-if="val === 1"></div>
655
+ <div data-if="val == 1"></div>
656
+ <div data-if="val !== 1"></div>
657
+ <div data-if="val != 1"></div>
658
+ <div data-if="val > 1"></div>
659
+ <div data-if="val < 1"></div>
660
+ <div data-if="val <= 1"></div>
661
+ <div data-if="val >= 1"></div>
662
+ ```
663
+
664
+ The right hand side of the comparison does not have to be boolean or number. It can be a string or any primitive value, but the numeric comparisons don't make sense in such case.
665
+
666
+ You can also define callbacks and use them as the condition, in you ComponentName.client.ts:
667
+ ```
668
+ import { InitializerFunction } from 'structured-fw/Types';
669
+ export const init: InitializerFunction = async function() {
670
+ this.conditionalCallback('showDiv', () => {
671
+ // return a boolean here
672
+ });
673
+ }
674
+ ```
675
+
676
+ then in ComponentName.html:
677
+ ```
678
+ <div data-if="showDiv()"></div>
679
+ ```
680
+
681
+ **Basic animation/transitions**\
682
+ If you use conditionals on any DOM node, you may also enable basic animations/transitions using following attributes:
683
+ - Enable transition:
684
+ - `data-transition-show-slide="durationMilliseconds"` - when DOM node is shown, slide it in
685
+ - `data-transition-hide-slide="durationMilliseconds"` - when DOM node is hidden, slide it out
686
+ - `data-transition-show-fade="durationMilliseconds"` - fade DOM node in
687
+ - `data-transition-hide-fade="durationMilliseconds"` - fade DOM node out
688
+ - Modify transition (slide only)
689
+ - `data-transform-origin-show="CSS transform origin"` - from where does the component slide in for example `0% 50%` to slide it in from mid-left
690
+ - `data-transform-origin-hide="CSS transform origin"` - where does the component slide out to for example `100% 100%` to slide it out to bottom-right
691
+ - `data-transition-axis-show="X | Y"` - slide animation axis
692
+ - `data-transition-axis-hide="X | Y"` - slide animation axis
612
693
 
613
694
  ## Good to know
614
- - [Uing CSS frameworks](#css-frameworks)
695
+ - [Using CSS frameworks](#css-frameworks)
615
696
  - [Using JS runtimes other than Node.js](#runtimes)
616
697
  - [Why not JSR](#jsr)
698
+ - [Best practices](#best-practices)
699
+ - [Having an issue?](#issues-and-feedback)
617
700
 
618
701
  ### CSS frameworks
619
702
  We rarely write all CSS from scratch, usually we use a CSS framework to speed us up. Structured allows you to work with any CSS frameworks such as Tailwind, PostCSS or Bootstrap.
@@ -721,6 +804,46 @@ Run application using `deno main.ts`
721
804
  It would make a lot of sense to have Structured hosted on JSR (JavaScript Registry) given Structured is a TypeScript framework, and JSR is a TypeScript-first registry, however, the issue is that Deno imposes [limitations with dynamic imports](https://docs.deno.com/deploy/api/dynamic-import/) with JSR-imported dependencies, which are required for the framework (to dynamically import your routes and components).\
722
805
  This does not stop the framework from working with Deno, but for the time being, we have to stick with good old npm.
723
806
 
807
+ ### Best practices
808
+
809
+ **Entry point:**\
810
+ I suggest the following setup for your entry point:
811
+ 1) Set `autoInit = false` in your `/Config.ts`
812
+ 2) If you are using ENV variables, define a type `EnvConf` in `/app/Types.ts`
813
+ 3) In `/index.ts`, only create the Application instance and import ENV using `importEnv`, exporting both, as follows:
814
+ ```
815
+ import { EnvConf } from './app/Types.js';
816
+ import { Application } from 'structured-fw/Application';
817
+ import { config } from './Config.js';
818
+
819
+ export const app = new Application(config);
820
+ export const env = app.importEnv<EnvConf>();
821
+ ```
822
+ 4) Create `/main.ts` and import `app` and `env` from `/index.ts`, add `main.ts` to tsconfig.json include array, add any event listeners, and load helpers from within `/main.ts`. This makes sure you can use env in any imported modules in main.ts without having to use dynamic imports. You can later import `env` and `app` from `index.ts` wherever you want to use them.
823
+
824
+ \
825
+ **Component directories**\
826
+ You should always place your components in a directory named same as your component. While this is not required, it will keep things organized. You might think your component will only have a HTML part, but at some point you may decide you want to add client/server code to it, so it's better to start out with a directory.\
827
+ Feel free to group your components in directories and subdirectories. Structured loads all components recursively when Application is initialized, and allows you to load any existing component from any component/Document. You can even move your components to other directory later without having to worry about updating the imports.
828
+
829
+ **Type definitions**\
830
+ I suggest keeping your general type definitions in /app/Types.ts, but for more specific types you should probably create /app/types/[entity].types.ts to keep things clean easy to maintain.\
831
+ For example:\
832
+ `export type BooleanInt = 0 | 1;` - this is fine
833
+ in /app/Types.ts\
834
+ `export type User = {email: string, password: string}` - you should probably create /app/types/users.types.ts for this one
835
+
836
+ **Models**\
837
+ If you ran `npx structured init`, it has created /app/models for you. Structured does not use this directory, but I suggest keeping your models interfacing the DB/APIs there. While Structured framework is not an MVC in a traditional sense, it's a good idea to keep your models in one place, as you will want to import the same model from many routes and components.
838
+
839
+ > [!IMPORTANT]
840
+ > while it's true that with Structured, components take care of their own data, it does not mean that they need to contain the code to fetch said data, instead you are encouraged to keep data logic in your models, and use those models in components/routes.
841
+
842
+ You can create additional code separation, for example, it would make sense to have /app/lib for code that interfaces an API, or have /app/Util.ts where you export utility functions. Structured boilerplate does not include these as not all applications will need them.
843
+
844
+ ### Issues and feedback
845
+ If you have any issues with the framework or the npm package, please don't hesitate to open an issue on [github](https://github.com/julijan/structured). Feedback is also welcome!
846
+
724
847
  ## Why Structured
725
848
  Framework was developed by someone who has been a web developer for almost 20 years (me), and did not like the path web development has taken.
726
849
  \
package/build/index.js CHANGED
@@ -1,3 +1,8 @@
1
1
  import { Application } from "structured-fw/Application";
2
2
  import { config } from './Config.js';
3
- new Application(config);
3
+ const app = new Application(config);
4
+ app.on('documentCreated', (document) => {
5
+ document.on('componentCreated', (component) => {
6
+ document.head.add(`<script>console.log('${component.name}')</script>`);
7
+ });
8
+ });
@@ -0,0 +1,7 @@
1
+ import { EventEmitterCallback } from "./Types.js";
2
+ export declare class EventEmitter<T extends Record<string, any> = Record<string, any>> {
3
+ protected listeners: Partial<Record<keyof T, Array<EventEmitterCallback<any>>>>;
4
+ on<K extends keyof T>(eventName: K, callback: EventEmitterCallback<T[K]>): void;
5
+ emit(eventName: keyof T, payload?: any): void;
6
+ unbind(eventName: keyof T, callback: EventEmitterCallback<any>): void;
7
+ }
@@ -0,0 +1,31 @@
1
+ export class EventEmitter {
2
+ constructor() {
3
+ this.listeners = {};
4
+ }
5
+ on(eventName, callback) {
6
+ if (!Array.isArray(this.listeners[eventName])) {
7
+ this.listeners[eventName] = [];
8
+ }
9
+ this.listeners[eventName].push(callback);
10
+ }
11
+ emit(eventName, payload) {
12
+ if (Array.isArray(this.listeners[eventName])) {
13
+ this.listeners[eventName].forEach((callback) => {
14
+ callback(payload);
15
+ });
16
+ }
17
+ }
18
+ unbind(eventName, callback) {
19
+ if (Array.isArray(this.listeners[eventName])) {
20
+ while (true) {
21
+ const index = this.listeners[eventName].indexOf(callback);
22
+ if (index > -1) {
23
+ this.listeners[eventName].splice(index, 1);
24
+ }
25
+ else {
26
+ break;
27
+ }
28
+ }
29
+ }
30
+ }
31
+ }
@@ -127,7 +127,7 @@ export interface ComponentScaffold {
127
127
  [key: string]: any;
128
128
  }
129
129
  export type LooseObject = Record<string, any>;
130
- export type ApplicationEvents = 'serverStarted' | 'beforeRequestHandler' | 'afterRequestHandler' | 'beforeRoutes' | 'afterRoutes' | 'beforeComponentLoad' | 'afterComponentLoad' | 'documentCreated' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound';
130
+ export type ApplicationEvents = 'serverStarted' | 'beforeRequestHandler' | 'afterRequestHandler' | 'beforeRoutes' | 'afterRoutes' | 'beforeComponentsLoad' | 'afterComponentsLoaded' | 'documentCreated' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound';
131
131
  export type SessionEntry = {
132
132
  sessionId: string;
133
133
  lastRequest: number;
@@ -166,4 +166,4 @@ export type ClientComponentTransition = {
166
166
  };
167
167
  export type ClientComponentTransitionEvent = 'show' | 'hide';
168
168
  export type ClientComponentTransitions = Record<ClientComponentTransitionEvent, ClientComponentTransition>;
169
- export type EventEmitterCallback = (payload: any) => void;
169
+ export type EventEmitterCallback<T> = (payload: T) => void;
@@ -2,7 +2,7 @@ import { LooseObject } from '../Types.js';
2
2
  import { DataStoreView } from './DataStoreView.js';
3
3
  import { DataStore } from './DataStore.js';
4
4
  import { Net } from './Net.js';
5
- import { EventEmitter } from './EventEmitter.js';
5
+ import { EventEmitter } from '../EventEmitter.js';
6
6
  export declare class ClientComponent extends EventEmitter {
7
7
  readonly name: string;
8
8
  children: Array<ClientComponent>;
@@ -2,7 +2,7 @@ import { attributeValueFromString, attributeValueToString, mergeDeep, objectEach
2
2
  import { DataStoreView } from './DataStoreView.js';
3
3
  import { Net } from './Net.js';
4
4
  import { NetRequest } from './NetRequest.js';
5
- import { EventEmitter } from './EventEmitter.js';
5
+ import { EventEmitter } from '../EventEmitter.js';
6
6
  export class ClientComponent extends EventEmitter {
7
7
  constructor(parent, name, domNode, store, runInitializer = true) {
8
8
  super();
@@ -20,7 +20,7 @@ export declare class Application {
20
20
  constructor(config: StructuredConfig);
21
21
  init(): Promise<void>;
22
22
  private start;
23
- on<E extends ApplicationEvents>(evt: E, callback: (payload: E extends 'beforeRequestHandler' | 'afterRequestHandler' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound' ? RequestContext : E extends 'documentCreated' ? Document : undefined) => void): void;
23
+ on<E extends ApplicationEvents>(evt: E, callback: (payload: E extends 'beforeRequestHandler' | 'afterRequestHandler' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound' ? RequestContext : E extends 'documentCreated' ? Document : E extends 'afterComponentsLoaded' ? Components : E extends 'serverStarted' ? Server : undefined) => void): void;
24
24
  emit(eventName: ApplicationEvents, payload?: any): Promise<Array<any>>;
25
25
  importEnv<T extends LooseObject>(smartPrimitives?: boolean): T;
26
26
  exportContextFields(...fields: Array<keyof RequestContextData>): void;
@@ -35,12 +35,12 @@ export class Application {
35
35
  catch (e) {
36
36
  console.error(e.message);
37
37
  }
38
- await this.emit('beforeComponentLoad');
38
+ await this.emit('beforeComponentsLoad');
39
39
  this.components.loadComponents();
40
- await this.emit('afterComponentLoad');
40
+ await this.emit('afterComponentsLoaded', this.components);
41
41
  await this.emit('beforeRoutes');
42
42
  await this.request.loadHandlers();
43
- await this.emit('afterRoutes');
43
+ await this.emit('afterRoutes', this.request);
44
44
  if (this.config.url.componentRender !== false) {
45
45
  this.request.on('POST', `${this.config.url.componentRender}`, async (ctx) => {
46
46
  const input = ctx.body;
@@ -49,16 +49,18 @@ export class Application {
49
49
  }
50
50
  this.request.on('GET', /^\/assets\/client-js/, async ({ request, response }) => {
51
51
  const uri = request.url?.substring(18);
52
- const filePath = path.resolve('./system/', uri);
52
+ if (uri.includes('..')) {
53
+ return '';
54
+ }
55
+ const filePath = path.resolve(import.meta.dirname, '..', uri);
53
56
  if (existsSync(filePath)) {
54
57
  response.setHeader('Content-Type', 'application/javascript');
55
- response.write(readFileSync(filePath));
56
- response.end();
58
+ return readFileSync(filePath);
57
59
  }
58
60
  else {
59
61
  response.statusCode = 404;
60
62
  }
61
- return;
63
+ return '';
62
64
  }, this, true);
63
65
  await this.start();
64
66
  }
@@ -69,7 +71,7 @@ export class Application {
69
71
  });
70
72
  this.server.listen(this.config.http.port, this.config.http.host || '127.0.0.1', async () => {
71
73
  const address = (this.config.http.host !== undefined ? this.config.http.host : '') + ':' + this.config.http.port;
72
- await this.emit('serverStarted');
74
+ await this.emit('serverStarted', this.server);
73
75
  console.log(`Server started on ${address}`);
74
76
  resolve();
75
77
  });
@@ -1,7 +1,10 @@
1
1
  import { Document } from './Document.js';
2
2
  import { ComponentEntry, LooseObject } from '../Types.js';
3
3
  import { DOMNode } from './dom/DOMNode.js';
4
- export declare class Component {
4
+ import { EventEmitter } from '../EventEmitter.js';
5
+ export declare class Component<Events extends Record<string, any> = {
6
+ 'componentCreated': Component;
7
+ }> extends EventEmitter<Events> {
5
8
  id: string;
6
9
  name: string;
7
10
  document: Document;
@@ -20,8 +23,8 @@ export declare class Component {
20
23
  private initChildren;
21
24
  protected importedParentData(parentData: LooseObject): LooseObject;
22
25
  protected initAttributesData(domNode?: DOMNode): void;
23
- private attributePreffix;
26
+ private attributePrefix;
24
27
  private attributeDataType;
25
- private attributeUnpreffixed;
28
+ private attributeUnprefixed;
26
29
  protected fillData(data: LooseObject): void;
27
30
  }
@@ -1,13 +1,16 @@
1
1
  import { Document } from './Document.js';
2
2
  import { attributeValueFromString, attributeValueToString, objectEach, toCamelCase } from '../Util.js';
3
3
  import { DOMFragment } from './dom/DOMFragment.js';
4
- export class Component {
4
+ import { EventEmitter } from '../EventEmitter.js';
5
+ export class Component extends EventEmitter {
5
6
  constructor(name, node, parent, autoInit = true) {
7
+ super();
6
8
  this.children = [];
7
9
  this.path = [];
8
10
  this.attributesRaw = {};
9
11
  this.attributes = {};
10
12
  this.data = {};
13
+ const isDocument = this instanceof Document;
11
14
  this.name = name;
12
15
  if (name === 'root') {
13
16
  this.dom = new DOMFragment();
@@ -21,7 +24,7 @@ export class Component {
21
24
  }
22
25
  this.isRoot = false;
23
26
  }
24
- if (this instanceof Document) {
27
+ if (isDocument) {
25
28
  this.document = this;
26
29
  }
27
30
  else {
@@ -42,6 +45,9 @@ export class Component {
42
45
  else {
43
46
  this.entry = null;
44
47
  }
48
+ if (!isDocument) {
49
+ this.document.emit('componentCreated', this);
50
+ }
45
51
  }
46
52
  async init(html, data) {
47
53
  this.initAttributesData();
@@ -180,7 +186,7 @@ export class Component {
180
186
  }
181
187
  for (let i = 0; i < domNode.attributes.length; i++) {
182
188
  const attrNameRaw = domNode.attributes[i].name;
183
- const attrNameUnprefixed = this.attributeUnpreffixed(attrNameRaw);
189
+ const attrNameUnprefixed = this.attributeUnprefixed(attrNameRaw);
184
190
  if (attrNameUnprefixed.indexOf('data-') === 0) {
185
191
  const attrDataType = this.attributeDataType(attrNameRaw);
186
192
  const dataDecoded = attributeValueFromString(domNode.attributes[i].value.toString());
@@ -214,7 +220,7 @@ export class Component {
214
220
  this.attributesRaw[attrNameRaw] = domNode.attributes[i].value;
215
221
  }
216
222
  }
217
- attributePreffix(attrName) {
223
+ attributePrefix(attrName) {
218
224
  const index = attrName.indexOf(':');
219
225
  if (index < 0) {
220
226
  return null;
@@ -222,7 +228,7 @@ export class Component {
222
228
  return attrName.substring(0, index);
223
229
  }
224
230
  attributeDataType(attrName) {
225
- const prefix = this.attributePreffix(attrName);
231
+ const prefix = this.attributePrefix(attrName);
226
232
  if (prefix === 'string' ||
227
233
  prefix === 'number' ||
228
234
  prefix === 'object' ||
@@ -231,7 +237,7 @@ export class Component {
231
237
  }
232
238
  return 'any';
233
239
  }
234
- attributeUnpreffixed(attrName) {
240
+ attributeUnprefixed(attrName) {
235
241
  const index = attrName.indexOf(':');
236
242
  if (index < 0) {
237
243
  return attrName;
@@ -3,7 +3,9 @@ import { Initializers, LooseObject, RequestContext } from '../../system/Types.js
3
3
  import { Application } from './Application.js';
4
4
  import { DocumentHead } from './DocumentHead.js';
5
5
  import { Component } from './Component.js';
6
- export declare class Document extends Component {
6
+ export declare class Document extends Component<{
7
+ 'componentCreated': Component;
8
+ }> {
7
9
  head: DocumentHead;
8
10
  language: string;
9
11
  application: Application;
package/index.ts CHANGED
@@ -1,4 +1,21 @@
1
1
  import { Application } from "structured-fw/Application";
2
2
  import { config } from './Config.js';
3
3
 
4
- new Application(config);
4
+ const app = new Application(config);
5
+
6
+ // app.on('afterComponentsLoaded', (components) => {
7
+ // components.componentNames.forEach((componentName) => {
8
+ // console.log(componentName)
9
+ // console.log(components.getByName(componentName));
10
+ // });
11
+ // })
12
+
13
+ // app.on('componentCreated', (component) => {
14
+ // console.log(component.document.id);
15
+ // })
16
+
17
+ app.on('documentCreated', (document) => {
18
+ document.on('componentCreated', (component) => {
19
+ document.head.add(`<script>console.log('${component.name}')</script>`);
20
+ });
21
+ })
package/package.json CHANGED
@@ -14,12 +14,13 @@
14
14
  "license": "MIT",
15
15
  "type": "module",
16
16
  "main": "build/index",
17
- "version": "0.8.21",
17
+ "version": "0.8.42",
18
18
  "scripts": {
19
19
  "develop": "tsc --watch",
20
20
  "startDev": "cd build && nodemon --watch '../app/**/*' --watch '../build/**/*' -e js,html,css index.js",
21
21
  "start": "cd build && node index.js",
22
- "prepublish": "tsc"
22
+ "prepublish": "tsc",
23
+ "postinstall": "rm tsconfig.json"
23
24
  },
24
25
  "bin": {
25
26
  "structured": "./build/system/bin/structured.js"
@@ -1,10 +1,10 @@
1
- import { EventEmitterCallback } from "../Types.js";
1
+ import { EventEmitterCallback } from "./Types.js";
2
2
 
3
- export class EventEmitter {
4
- protected listeners: Record<string, Array<EventEmitterCallback>> = {}
3
+ export class EventEmitter<T extends Record<string, any> = Record<string, any>> {
4
+ protected listeners: Partial<Record<keyof T, Array<EventEmitterCallback<any>>>> = {}
5
5
 
6
6
  // add event listener
7
- public on(eventName: string, callback: EventEmitterCallback): void {
7
+ public on<K extends keyof T>(eventName: K, callback: EventEmitterCallback<T[K]>): void {
8
8
  if (! Array.isArray(this.listeners[eventName])) {
9
9
  this.listeners[eventName] = [];
10
10
  }
@@ -13,7 +13,7 @@ export class EventEmitter {
13
13
  }
14
14
 
15
15
  // emit event with given payload
16
- public emit(eventName: string, payload?: any): void {
16
+ public emit(eventName: keyof T, payload?: any): void {
17
17
  if (Array.isArray(this.listeners[eventName])) {
18
18
  this.listeners[eventName].forEach((callback) => {
19
19
  callback(payload);
@@ -22,7 +22,7 @@ export class EventEmitter {
22
22
  }
23
23
 
24
24
  // remove event listener
25
- public unbind(eventName: string, callback: EventEmitterCallback): void {
25
+ public unbind(eventName: keyof T, callback: EventEmitterCallback<any>): void {
26
26
  if (Array.isArray(this.listeners[eventName])) {
27
27
  while (true) {
28
28
  const index = this.listeners[eventName].indexOf(callback);
package/system/Types.ts CHANGED
@@ -177,7 +177,7 @@ export interface ComponentScaffold {
177
177
 
178
178
  export type LooseObject = Record<string, any>
179
179
 
180
- export type ApplicationEvents = 'serverStarted'|'beforeRequestHandler'|'afterRequestHandler'|'beforeRoutes'|'afterRoutes'|'beforeComponentLoad'|'afterComponentLoad'|'documentCreated'|'beforeAssetAccess'|'afterAssetAccess'|'pageNotFound';
180
+ export type ApplicationEvents = 'serverStarted'|'beforeRequestHandler'|'afterRequestHandler'|'beforeRoutes'|'afterRoutes'|'beforeComponentsLoad'|'afterComponentsLoaded'|'documentCreated'|'beforeAssetAccess'|'afterAssetAccess'|'pageNotFound';
181
181
 
182
182
  export type SessionEntry = {
183
183
  sessionId : string,
@@ -229,4 +229,4 @@ export type ClientComponentTransition = {
229
229
  export type ClientComponentTransitionEvent = 'show' | 'hide';
230
230
  export type ClientComponentTransitions = Record<ClientComponentTransitionEvent, ClientComponentTransition>;
231
231
 
232
- export type EventEmitterCallback = (payload: any) => void
232
+ export type EventEmitterCallback<T> = (payload: T) => void
@@ -4,7 +4,7 @@ import { DataStoreView } from './DataStoreView.js';
4
4
  import { DataStore } from './DataStore.js';
5
5
  import { Net } from './Net.js';
6
6
  import { NetRequest } from './NetRequest.js';
7
- import { EventEmitter } from './EventEmitter.js';
7
+ import { EventEmitter } from '../EventEmitter.js';
8
8
 
9
9
  export class ClientComponent extends EventEmitter {
10
10
  readonly name: string;
@@ -59,14 +59,14 @@ export class Application {
59
59
  console.error(e.message);
60
60
  }
61
61
 
62
- await this.emit('beforeComponentLoad');
62
+ await this.emit('beforeComponentsLoad');
63
63
  this.components.loadComponents();
64
- await this.emit('afterComponentLoad');
64
+ await this.emit('afterComponentsLoaded', this.components);
65
65
 
66
66
 
67
67
  await this.emit('beforeRoutes');
68
68
  await this.request.loadHandlers();
69
- await this.emit('afterRoutes');
69
+ await this.emit('afterRoutes', this.request);
70
70
 
71
71
  if (this.config.url.componentRender !== false) {
72
72
  // special request handler, executed when ClientComponent.redraw is called
@@ -84,15 +84,15 @@ export class Application {
84
84
  // special request handler, serve the client side JS
85
85
  this.request.on('GET', /^\/assets\/client-js/, async ({ request, response }) => {
86
86
  const uri = request.url?.substring(18) as string;
87
- const filePath = path.resolve('./system/', uri);
87
+ if (uri.includes('..')) {return '';} // disallow having ".." in the URL
88
+ const filePath = path.resolve(import.meta.dirname, '..', uri);
88
89
  if (existsSync(filePath)) {
89
90
  response.setHeader('Content-Type', 'application/javascript');
90
- response.write(readFileSync(filePath));
91
- response.end();
91
+ return readFileSync(filePath);
92
92
  } else {
93
93
  response.statusCode = 404;
94
94
  }
95
- return;
95
+ return '';
96
96
  }, this, true);
97
97
 
98
98
  await this.start();
@@ -106,7 +106,7 @@ export class Application {
106
106
  });
107
107
  this.server.listen(this.config.http.port, this.config.http.host || '127.0.0.1', async () => {
108
108
  const address = (this.config.http.host !== undefined ? this.config.http.host : '') + ':' + this.config.http.port;
109
- await this.emit('serverStarted');
109
+ await this.emit('serverStarted', this.server);
110
110
  console.log(`Server started on ${address}`);
111
111
  resolve();
112
112
  });
@@ -120,6 +120,8 @@ export class Application {
120
120
  payload:
121
121
  E extends 'beforeRequestHandler' | 'afterRequestHandler' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound' ? RequestContext :
122
122
  E extends 'documentCreated' ? Document :
123
+ E extends 'afterComponentsLoaded' ? Components :
124
+ E extends 'serverStarted' ? Server :
123
125
  undefined
124
126
  ) => void
125
127
  ): void {
@@ -144,7 +146,7 @@ export class Application {
144
146
  });
145
147
  }
146
148
 
147
- // load envirnment variables
149
+ // load environment variables
148
150
  // if this.config.envPrefix is a string, load all ENV variables starting with [envPrefix]_
149
151
  // the method is generic, so user can define the expected return type
150
152
  public importEnv<T extends LooseObject>(smartPrimitives: boolean = true): T {
@@ -3,8 +3,9 @@ import { attributeValueFromString, attributeValueToString, objectEach, toCamelCa
3
3
  import { ComponentEntry, LooseObject } from '../Types.js';
4
4
  import { DOMFragment } from './dom/DOMFragment.js';
5
5
  import { DOMNode } from './dom/DOMNode.js';
6
+ import { EventEmitter } from '../EventEmitter.js';
6
7
 
7
- export class Component {
8
+ export class Component<Events extends Record<string, any> = {'componentCreated' : Component}> extends EventEmitter<Events> {
8
9
  id: string;
9
10
  name: string;
10
11
  document: Document;
@@ -29,6 +30,8 @@ export class Component {
29
30
  isRoot: boolean;
30
31
 
31
32
  constructor(name: string, node?: DOMNode, parent?: Document|Component, autoInit: boolean = true) {
33
+ super();
34
+ const isDocument = this instanceof Document;
32
35
  this.name = name;
33
36
 
34
37
  if (name === 'root') {
@@ -43,7 +46,7 @@ export class Component {
43
46
  this.isRoot = false;
44
47
  }
45
48
 
46
- if (this instanceof Document) {
49
+ if (isDocument) {
47
50
  // this will only happen if an instance of Document, as it extends component
48
51
  this.document = this;
49
52
  } else {
@@ -71,16 +74,20 @@ export class Component {
71
74
  } else {
72
75
  this.entry = null;
73
76
  }
77
+
78
+ if (! isDocument) {
79
+ this.document.emit('componentCreated', this);
80
+ }
74
81
  }
75
82
 
76
83
  // load component's data and fill it
77
84
  // load any nested components recursively
78
85
  public async init(html: string, data?: LooseObject): Promise<void> {
79
86
 
80
- // extract data-atributes and encode non-encoded attributes
87
+ // extract data-attributes and encode non-encoded attributes
81
88
  this.initAttributesData();
82
89
 
83
- // create component container replacng the original tag name with a div
90
+ // create component container replacing the original tag name with a div
84
91
  // (or whatever is set as renderTagName on ComponentEntry)
85
92
  this.dom.tagName = this.entry?.renderTagName || 'div';
86
93
 
@@ -88,7 +95,7 @@ export class Component {
88
95
  this.dom.innerHTML = html;
89
96
 
90
97
 
91
- // re-apply attributes the orignal tag had
98
+ // re-apply attributes the original tag had
92
99
  // no need to encode values at this point
93
100
  // any non-encoded attributes got encoded earlier by initAttributesData
94
101
  this.setAttributes(this.attributesRaw, '', false);
@@ -110,7 +117,7 @@ export class Component {
110
117
  this.id = this.attributes.componentId;
111
118
  }
112
119
 
113
- // export RequestContext.data fields specified in Application.exporteRequestContextData
120
+ // export RequestContext.data fields specified in Application.exportedRequestContextData
114
121
  const exportedContextData = this.document.application.exportedRequestContextData.reduce((prev, field) => {
115
122
  if (! this.document.ctx) {return prev;}
116
123
  if (field in this.document.ctx.data) {
@@ -295,7 +302,7 @@ export class Component {
295
302
 
296
303
  // attributes can have a data prefix eg. number:data-num="3"
297
304
  // return unprefixed attribute name
298
- const attrNameUnprefixed = this.attributeUnpreffixed(attrNameRaw);
305
+ const attrNameUnprefixed = this.attributeUnprefixed(attrNameRaw);
299
306
 
300
307
  if (attrNameUnprefixed.indexOf('data-') === 0) {
301
308
  // only attributes starting with data- are stored to this.attributes
@@ -353,7 +360,7 @@ export class Component {
353
360
 
354
361
  // component attributes can have a data type prefix [prefix]:data-[name]="[val]"
355
362
  // returns the prefix
356
- private attributePreffix(attrName: string): string|null {
363
+ private attributePrefix(attrName: string): string|null {
357
364
  const index = attrName.indexOf(':');
358
365
  if (index < 0) {
359
366
  return null;
@@ -364,7 +371,7 @@ export class Component {
364
371
  // returns the user defined data type of given attribute
365
372
  // for example number:data-total returns 'number'
366
373
  private attributeDataType(attrName: string): 'string'|'number'|'object'|'boolean'|'any' {
367
- const prefix = this.attributePreffix(attrName);
374
+ const prefix = this.attributePrefix(attrName);
368
375
 
369
376
  if (
370
377
  prefix === 'string' ||
@@ -375,13 +382,13 @@ export class Component {
375
382
  return prefix;
376
383
  }
377
384
 
378
- // unrecognized attribute preffix
385
+ // unrecognized attribute prefix
379
386
  return 'any';
380
387
  }
381
388
 
382
389
  // removes the data-type prefix from given attribute name
383
390
  // for example number:data-total returns data-total
384
- private attributeUnpreffixed(attrName: string): string {
391
+ private attributeUnprefixed(attrName: string): string {
385
392
  const index = attrName.indexOf(':');
386
393
  if (index < 0) {
387
394
  return attrName;
@@ -9,7 +9,7 @@ import { attributeValueToString, randomString } from '../Util.js';
9
9
  import path from 'node:path';
10
10
  import { existsSync, readFileSync } from 'node:fs';
11
11
 
12
- export class Document extends Component {
12
+ export class Document extends Component<{'componentCreated': Component}> {
13
13
 
14
14
  head: DocumentHead;
15
15
  language = 'en';
package/tsconfig.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "module": "ES2020",
11
11
  "target": "ES2021",
12
12
  "alwaysStrict": true,
13
- "allowSyntheticDefaultImports": true, // albe to do import { default as varname } from 'module'
13
+ "allowSyntheticDefaultImports": true, // able to do import { default as varname } from 'module'
14
14
  "declaration": true,
15
15
  "isolatedDeclarations": true,
16
16
  "noImplicitReturns": true,