@nihasoft/the-waydeprecated

We are a framework to help the development life to be better.

Usage no npm install needed!

<script type="module">
  import nihasoftTheWay from 'https://cdn.skypack.dev/@nihasoft/the-way';
</script>

README

Documentation Exampples npm version License EsLint Build Status codecov Donate

The Way

To improve the life cicle of the development, we created this framework to help your application to be more clean and intelligible with a loot of decorators. With this you can use an express server with improvements (best practices from express) with swagger (when provided the swagger.json), inject classes (as singleton), overridde classes, automatic configurations provided by @Configuration, destroy, you can map and use REST method's more intuitive and more clean with the Rest & Decorators. You can define some properties overriding the default properties and putting in this file yours custom properties to use in your classes injecting the class PropertiesConfiguration.

You also can customize some behaviors of this framework with decorators @Configuration and @Service.

Note: When you use the @Configure you MUST extends the class AbstractConfiguration.

Note: By default, we start an express http server. To change this behavior you must provide an application.properties.yml in the root path of your project or with the command line argument: --properties=/path/to/your/properties/application.properties.yml

the-way:
    server:
        enabled: false

Table of Contents

Installation

To use this framework you must have a project with Node.js and configurated to use Typescript, you can see how to do this here.

With a project in Node.Js configurated with Typescript you only need to install this framework with commando below:

npm i @nihasoft/the-way

Note: This library use the @types/node and the rxjs. So, your application must have this libraries as devDependency and as dependency respectively.

Usage

With the framework installed you can now do the magic. You only need to create a "main class" that extends the TheWayApplication and decorate your class with the @Application()

The example below can be viewed here

The Main Class

import { TheWayApplication, Application } from '@nihasoft/the-way'

@Application()
export class Main extends TheWayApplication {
    public start(): void {
        console.log('Yeah, now I\'m The Way application');
    }
}

The Command to run

In your project:

tsc && path_to_your_dist/main.js

This example above, will enable the application to use Rest & Decorators and the Application Decorators. Will be used the default configurations like properties, security, configurations and services. You can check all the default properties and beheviors here.

Inject decorator

Some times we want to use a singleton and import/inject this class into another class. You can to this using the decorator @Inject(). The example below can be viewed here

Creating a class to be injected

export class InjectionExample {
    public helloWorld() {
        console.log('hello world');
    }
}

Injecting another class into you main

import { TheWayApplication, Application, Inject } from '@nihasoft/the-way'
import { InjectionExample } from './injection-example';

@Application()
export class Main extends TheWayApplication {

    @Inject() injectionExample: InjectionExample;

    public start(): void {
        this.injectionExample.helloWorld();
    }
}

Rest & Decorator

In this section we will create a rest class with some methods enabled to be called. The example below, can be viewed here

Note: We will concat your path in decorators with the property the-way.server.path. To change this behavior you must provide an application.properties.yml in the root path of your project or with the command line argument: --properties=/path/to/your/properties/application.properties.yml

the-way:
    server:
        path: /api

Note: By default, we start an express http server. To change this behavior you must provide an application.properties.yml in the root path of your project or with the command line argument: --properties=/path/to/your/properties/application.properties.yml

the-way:
    server:
        enabled: false

Creating a rest class with some rest methods

import { Get } from '@nihasoft/the-way';
import { Observable, of } from 'rxjs';

export class HeroRest {
    @Get('/hero')
    public getHero(): Observable<{id: number, name: string, power: number}> {
        return of({
            id: 10,
            name: 'Batman',
            power: 10000
        })
    }
}

Importing that class into main

import { TheWayApplication, Application, Inject } from '@nihasoft/the-way'
import { InjectionExample } from './injection-example';
import { HeroRest } from './hero.rest';

@Application()
export class Main extends TheWayApplication {

    @Inject() injectionExample: InjectionExample;
    @Inject() heroRest: HeroRest;

    public start(): void {
        this.injectionExample.helloWorld();
    }
}

ApplicationProperties

We use an application.propertles.yml to be configurable by parameters, you can provide in the root path of your project application.properties.yml or with command line argument: --properties=/path/to/your/properties/application.properties.yml. You can see more here. You can also pass command line properties. Properties passed in argument line will be the priority. Example: --the-way.server.port=8090 if in my application properties the the-way.server.port is 8081 but if is passed the the-way.server.port in command line so the value will be the value passed in command line (like the example above, the value will be 8090).

The current default properties are:

the-way:
    core:
        log: true
    log:
        level: 0
    server:
        enabled: true
        port: 8081
        path: /api
        swagger:
            enabled: false
            path: /swagger
            filePath: './swagger.json'
        security:
            user-key: A2345678901234567890123456789012
            token-key: B2345678901234567890123456789034
            token-expiration: '3 days'
        file:
            enabled: false
            fallback: false
            full: false
            path: ''
            static: 
                path: ''
                full: false
            assets: 
                path: ''
                full: false

Issues

If you have an idea, a bug, a feature or anything else, please help us to growth creating an issue for that here

Documentation

You can access the full documentation here