feat: first version

This commit is contained in:
MrDiro 2024-06-23 06:52:10 -05:00
parent 5f6525eaf2
commit 0a3d8ed883
40 changed files with 458 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
#folders
node_modules/

114
README.MD Normal file
View File

@ -0,0 +1,114 @@
# ModiLiteJS
ModiLiteJS is a library for writing modular code with components and dependency injection. It simplifies the development process by allowing you to easily define and manage modules, components, and services, enabling a clean and organized codebase.
## Installation
You can install the library using npm:
```bash
npm install app-factory
```
## Usage
Below is a basic example of how to use the library:
### Decorators
The library provides three main decorators:
`Component`
Defines a class as a component.
```typescript
@Component()
export class MyComponent {
constructor(private readonly myService: MyService) {
this.myService.greet();
}
}
```
`Injectable`
Defines a class as an injectable service.
```typescript
@Injectable()
export class MyService {
public greet() {
console.log('Hello, I am a service');
}
}
```
`Module`
Defines a module that can group components, services, and other modules.
```typescript
@Module({
components: [MyComponent],
providers: [MyService],
imports: [AnotherModule]
})
export class MyModule {}
```
### Libraries
`AppFactory`
Creates the application instance from a root module.
```typescript
function bootstrap() {
AppFactory.create(MyModule).catch((err: Error) => {
console.error(`[App] Error:`, err.message);
});
}
bootstrap();
```
## Complete Example
```typescript
import { Component, Injectable, Module, AppFactory } from 'app-factory';
@Injectable()
export class AppService {
public greeting() {
console.log('Hello, I am a service');
}
}
@Component()
export class AppComponent {
constructor(private readonly service: AppService) {
this.service.greeting();
}
}
@Module({
components: [
AppComponent
],
providers: [
AppService
]
})
export class AppModule {}
function bootstrap() {
AppFactory.create(AppModule).catch((err: Error) => {
console.error(`[App] Error:`, err.message);
});
}
bootstrap();
```
## Contributions
Contributions are welcome. Please open an issue or submit a pull request.
## License
This library is licensed under the MIT License. You can see more details in the LICENSE file.

View File

@ -0,0 +1,2 @@
import 'reflect-metadata';
export declare function Component(): (target: Function) => void;

11
dist/decorators/component.decorator.js vendored Normal file
View File

@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Component = Component;
const prefix_target_enum_1 = require("../enums/prefix-target.enum");
require("reflect-metadata");
function Component() {
return function (target) {
Reflect.defineMetadata('prefix', prefix_target_enum_1.PrefixTarget.COMPONENT, target);
};
}
//# sourceMappingURL=component.decorator.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"component.decorator.js","sourceRoot":"","sources":["../../src/decorators/component.decorator.ts"],"names":[],"mappings":";;AAGA,8BAIC;AAPD,oEAA2D;AAC3D,4BAA0B;AAE1B,SAAgB,SAAS;IACvB,OAAO,UAAU,MAAgB;QAC/B,OAAO,CAAC,cAAc,CAAC,QAAQ,EAAE,iCAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACnE,CAAC,CAAA;AACH,CAAC"}

View File

@ -0,0 +1,2 @@
import 'reflect-metadata';
export declare function Injectable(): (target: Function) => void;

11
dist/decorators/injectable.decorator.js vendored Normal file
View File

@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Injectable = Injectable;
const prefix_target_enum_1 = require("../enums/prefix-target.enum");
require("reflect-metadata");
function Injectable() {
return function (target) {
Reflect.defineMetadata('prefix', prefix_target_enum_1.PrefixTarget.SERVICE, target);
};
}
//# sourceMappingURL=injectable.decorator.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"injectable.decorator.js","sourceRoot":"","sources":["../../src/decorators/injectable.decorator.ts"],"names":[],"mappings":";;AAGA,gCAIC;AAPD,oEAA2D;AAC3D,4BAA0B;AAE1B,SAAgB,UAAU;IACxB,OAAO,UAAU,MAAgB;QAC/B,OAAO,CAAC,cAAc,CAAC,QAAQ,EAAE,iCAAY,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACjE,CAAC,CAAA;AACH,CAAC"}

3
dist/decorators/module.decorator.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
import { ModuleOptions } from "../types/module-options.type";
import 'reflect-metadata';
export declare function Module(options: ModuleOptions): (target: Function) => void;

28
dist/decorators/module.decorator.js vendored Normal file
View File

@ -0,0 +1,28 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Module = Module;
const prefix_target_enum_1 = require("../enums/prefix-target.enum");
const inject_lib_1 = require("../libs/inject.lib");
require("reflect-metadata");
function Module(options) {
return function (target) {
const { components, providers, imports } = options;
Reflect.defineMetadata('prefix', prefix_target_enum_1.PrefixTarget.MODULE, target);
if (imports) {
for (const module of imports) {
(0, inject_lib_1.inject)(module);
}
}
if (providers) {
for (const service of providers) {
(0, inject_lib_1.inject)(service);
}
}
if (components) {
for (const component of components) {
(0, inject_lib_1.inject)(component);
}
}
};
}
//# sourceMappingURL=module.decorator.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"module.decorator.js","sourceRoot":"","sources":["../../src/decorators/module.decorator.ts"],"names":[],"mappings":";;AAMA,wBAwBC;AA7BD,oEAA2D;AAE3D,mDAA4C;AAC5C,4BAA0B;AAE1B,SAAgB,MAAM,CAAC,OAAsB;IAC3C,OAAO,UAAU,MAAgB;QAC/B,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;QAEnD,OAAO,CAAC,cAAc,CAAC,QAAQ,EAAE,iCAAY,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAE9D,IAAI,OAAO,EAAE,CAAC;YACZ,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;gBAC7B,IAAA,mBAAM,EAAC,MAAM,CAAC,CAAC;YACjB,CAAC;QACH,CAAC;QAED,IAAI,SAAS,EAAE,CAAC;YACd,KAAK,MAAM,OAAO,IAAI,SAAS,EAAE,CAAC;gBAChC,IAAA,mBAAM,EAAC,OAAO,CAAC,CAAC;YAClB,CAAC;QACH,CAAC;QAED,IAAI,UAAU,EAAE,CAAC;YACf,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;gBACnC,IAAA,mBAAM,EAAC,SAAS,CAAC,CAAC;YACpB,CAAC;QACH,CAAC;IACH,CAAC,CAAA;AACH,CAAC"}

5
dist/enums/prefix-target.enum.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
export declare enum PrefixTarget {
COMPONENT = "component",
SERVICE = "service",
MODULE = "module"
}

11
dist/enums/prefix-target.enum.js vendored Normal file
View File

@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrefixTarget = void 0;
var PrefixTarget;
(function (PrefixTarget) {
PrefixTarget["COMPONENT"] = "component";
PrefixTarget["SERVICE"] = "service";
PrefixTarget["MODULE"] = "module";
})(PrefixTarget || (exports.PrefixTarget = PrefixTarget = {}));
;
//# sourceMappingURL=prefix-target.enum.js.map

1
dist/enums/prefix-target.enum.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"prefix-target.enum.js","sourceRoot":"","sources":["../../src/enums/prefix-target.enum.ts"],"names":[],"mappings":";;;AAAA,IAAY,YAIX;AAJD,WAAY,YAAY;IACtB,uCAAuB,CAAA;IACvB,mCAAmB,CAAA;IACnB,iCAAiB,CAAA;AACnB,CAAC,EAJW,YAAY,4BAAZ,YAAY,QAIvB;AAAA,CAAC"}

8
dist/index.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
export * from './decorators/component.decorator';
export * from './decorators/injectable.decorator';
export * from './decorators/module.decorator';
export * from './enums/prefix-target.enum';
export * from './libs/app-factory.lib';
export * from './libs/dependency-container.lib';
export * from './libs/inject.lib';
export * from './types/module-options.type';

25
dist/index.js vendored Normal file
View File

@ -0,0 +1,25 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./decorators/component.decorator"), exports);
__exportStar(require("./decorators/injectable.decorator"), exports);
__exportStar(require("./decorators/module.decorator"), exports);
__exportStar(require("./enums/prefix-target.enum"), exports);
__exportStar(require("./libs/app-factory.lib"), exports);
__exportStar(require("./libs/dependency-container.lib"), exports);
__exportStar(require("./libs/inject.lib"), exports);
__exportStar(require("./types/module-options.type"), exports);
//# sourceMappingURL=index.js.map

1
dist/index.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mEAAiD;AACjD,oEAAkD;AAClD,gEAA8C;AAC9C,6DAA2C;AAC3C,yDAAuC;AACvC,kEAAgD;AAChD,oDAAkC;AAClC,8DAA4C"}

4
dist/libs/app-factory.lib.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
export declare class AppFactory {
private static instance;
static create(module: any): Promise<unknown>;
}

17
dist/libs/app-factory.lib.js vendored Normal file
View File

@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppFactory = void 0;
const dependency_container_lib_1 = require("./dependency-container.lib");
class AppFactory {
static create(module) {
return new Promise((resolve, reject) => {
if (!this.instance) {
const moduleInstance = dependency_container_lib_1.DependencyContainer.resolve(module);
this.instance = moduleInstance;
}
resolve(this.instance);
});
}
}
exports.AppFactory = AppFactory;
//# sourceMappingURL=app-factory.lib.js.map

1
dist/libs/app-factory.lib.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"app-factory.lib.js","sourceRoot":"","sources":["../../src/libs/app-factory.lib.ts"],"names":[],"mappings":";;;AAAA,yEAAiE;AAEjE,MAAa,UAAU;IAGrB,MAAM,CAAC,MAAM,CAAC,MAAW;QACvB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACnB,MAAM,cAAc,GAAG,8CAAmB,CAAC,OAAO,CAAM,MAAM,CAAC,CAAC;gBAChE,IAAI,CAAC,QAAQ,GAAG,cAAc,CAAC;YACjC,CAAC;YAED,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACzB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAbD,gCAaC"}

View File

@ -0,0 +1,5 @@
import 'reflect-metadata';
export declare class DependencyContainer {
private static instances;
static resolve<T>(target: any): T;
}

18
dist/libs/dependency-container.lib.js vendored Normal file
View File

@ -0,0 +1,18 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DependencyContainer = void 0;
require("reflect-metadata");
class DependencyContainer {
static resolve(target) {
if (!this.instances.has(target)) {
const tokens = Reflect.getMetadata('design:paramtypes', target) || [];
const injections = tokens.map((token) => DependencyContainer.resolve(token));
const instance = new target(...injections);
this.instances.set(target, instance);
}
return this.instances.get(target);
}
}
exports.DependencyContainer = DependencyContainer;
DependencyContainer.instances = new Map();
//# sourceMappingURL=dependency-container.lib.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"dependency-container.lib.js","sourceRoot":"","sources":["../../src/libs/dependency-container.lib.ts"],"names":[],"mappings":";;;AAAA,4BAA0B;AAE1B,MAAa,mBAAmB;IAG9B,MAAM,CAAC,OAAO,CAAI,MAAW;QAC3B,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;YAChC,MAAM,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,mBAAmB,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;YACtE,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAU,EAAE,EAAE,CAAC,mBAAmB,CAAC,OAAO,CAAM,KAAK,CAAC,CAAC,CAAC;YACvF,MAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC;YAC3C,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QACvC,CAAC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC;;AAZH,kDAaC;AAZgB,6BAAS,GAAG,IAAI,GAAG,EAAE,CAAC"}

2
dist/libs/inject.lib.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
import 'reflect-metadata';
export declare function inject(target: any): any;

16
dist/libs/inject.lib.js vendored Normal file
View File

@ -0,0 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.inject = inject;
const dependency_container_lib_1 = require("../libs/dependency-container.lib");
const prefix_target_enum_1 = require("../enums/prefix-target.enum");
require("reflect-metadata");
function inject(target) {
const prefix = Reflect.getMetadata('prefix', target);
if (!prefix || !Object.values(prefix_target_enum_1.PrefixTarget).includes(prefix)) {
throw new Error(`
${target.name} class doesn't has a valid decorator.
`);
}
return dependency_container_lib_1.DependencyContainer.resolve(target);
}
//# sourceMappingURL=inject.lib.js.map

1
dist/libs/inject.lib.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"inject.lib.js","sourceRoot":"","sources":["../../src/libs/inject.lib.ts"],"names":[],"mappings":";;AAIA,wBAUC;AAdD,+EAAuE;AACvE,oEAA2D;AAC3D,4BAA0B;AAE1B,SAAgB,MAAM,CAAC,MAAW;IAChC,MAAM,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;IAErD,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,iCAAY,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7D,MAAM,IAAI,KAAK,CAAC;QACZ,MAAM,CAAC,IAAI;KACd,CAAC,CAAC;IACL,CAAC;IAED,OAAO,8CAAmB,CAAC,OAAO,CAAM,MAAM,CAAC,CAAC;AAClD,CAAC"}

5
dist/types/module-options.type.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
export type ModuleOptions = {
components?: any[];
providers?: any[];
imports?: any[];
};

3
dist/types/module-options.type.js vendored Normal file
View File

@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=module-options.type.js.map

1
dist/types/module-options.type.js.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"module-options.type.js","sourceRoot":"","sources":["../../src/types/module-options.type.ts"],"names":[],"mappings":""}

View File

@ -0,0 +1,8 @@
import { PrefixTarget } from '../enums/prefix-target.enum';
import 'reflect-metadata';
export function Component() {
return function (target: Function) {
Reflect.defineMetadata('prefix', PrefixTarget.COMPONENT, target);
}
}

View File

@ -0,0 +1,8 @@
import { PrefixTarget } from '../enums/prefix-target.enum';
import 'reflect-metadata';
export function Injectable() {
return function (target: Function) {
Reflect.defineMetadata('prefix', PrefixTarget.SERVICE, target);
}
}

View File

@ -0,0 +1,32 @@
import { PrefixTarget } from "../enums/prefix-target.enum";
import { ModuleOptions } from "../types/module-options.type";
import { inject } from "../libs/inject.lib";
import 'reflect-metadata';
export function Module(options: ModuleOptions) {
return function (target: Function) {
const { components, providers, imports } = options;
Reflect.defineMetadata('prefix', PrefixTarget.MODULE, target);
if (imports) {
for (const module of imports) {
inject(module);
}
}
if (providers) {
for (const service of providers) {
inject(service);
}
}
if (components) {
for (const component of components) {
inject(component);
}
}
}
}

View File

@ -0,0 +1,5 @@
export enum PrefixTarget {
COMPONENT = 'component',
SERVICE = 'service',
MODULE = 'module',
};

8
src/index.ts Normal file
View File

@ -0,0 +1,8 @@
export * from './decorators/component.decorator';
export * from './decorators/injectable.decorator';
export * from './decorators/module.decorator';
export * from './enums/prefix-target.enum';
export * from './libs/app-factory.lib';
export * from './libs/dependency-container.lib';
export * from './libs/inject.lib';
export * from './types/module-options.type';

View File

@ -0,0 +1,16 @@
import { DependencyContainer } from "./dependency-container.lib";
export class AppFactory {
private static instance: AppFactory;
static create(module: any) {
return new Promise((resolve, reject) => {
if (!this.instance) {
const moduleInstance = DependencyContainer.resolve<any>(module);
this.instance = moduleInstance;
}
resolve(this.instance);
});
}
}

View File

@ -0,0 +1,16 @@
import 'reflect-metadata';
export class DependencyContainer {
private static instances = new Map();
static resolve<T>(target: any): T {
if (!this.instances.has(target)) {
const tokens = Reflect.getMetadata('design:paramtypes', target) || [];
const injections = tokens.map((token: any) => DependencyContainer.resolve<any>(token));
const instance = new target(...injections);
this.instances.set(target, instance);
}
return this.instances.get(target);
}
}

15
src/libs/inject.lib.ts Normal file
View File

@ -0,0 +1,15 @@
import { DependencyContainer } from "../libs/dependency-container.lib";
import { PrefixTarget } from "../enums/prefix-target.enum";
import 'reflect-metadata';
export function inject(target: any) {
const prefix = Reflect.getMetadata('prefix', target);
if (!prefix || !Object.values(PrefixTarget).includes(prefix)) {
throw new Error(`
${target.name} class doesn't has a valid decorator.
`);
}
return DependencyContainer.resolve<any>(target);
}

View File

@ -0,0 +1,5 @@
export type ModuleOptions = {
components?: any[],
providers?: any[],
imports?: any[],
};

43
tsconfig.json Normal file
View File

@ -0,0 +1,43 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
"incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
/* Language and Environment */
"target": "ES2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
/* Modules */
"module": "CommonJS", /* Specify what module code is generated. */
"rootDir": "./src", /* Specify the root folder within your source files. */
"moduleResolution": "Node", /* Specify how TypeScript looks up a file from a given module specifier. */
"baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
/* JavaScript Support */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
/* Emit */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
"removeComments": true, /* Disable emitting comments. */
/* Interop Constraints */
"allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
"strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
"strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
"noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
"noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
"noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
"noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
/* Completeness */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": [
"node_modules",
"dist"
]
}

1
tsconfig.tsbuildinfo Normal file

File diff suppressed because one or more lines are too long