Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.
/ empty-module Public archive

This package provides basics to create a new Hapiness module.

License

Notifications You must be signed in to change notification settings

hapinessjs/empty-module

Repository files navigation

Hapiness

Empty Module

This package provides basics to create a new Hapiness module.

Implementations of Hapiness' route and service are done and related tests too.

Table of contents

Starter

Download this starter and change hapinessjs/empty-module and @hapiness/empty-module, according your module name and repository, in these files:

  • package.json
  • README.md

In README.md, you need to update the documentation to explain what's your module and what it does.

Delete .travis.yml if you don't want to have travis-ci integration.

Back to top

Folders

All files for your module will be in src folder and wrote in Typescript.

All tests files for your module will be in test folder and wrote in Typescript too.

  • Unit tests will be in test/unit folder
  • Integration tests will be in test/integration folder

All packaging files for your module will be in tools folder and wrote in Typescript too.

Back to top

Files

Typescript configuration files

tsconfig.json is used for development process and tsconfig.build.json is used for build process.

In both case, add externals types from @types/{...} inside compilerOptions.types array.

Typescript validation

tslint.json contains all rules for Typescript validation during pretest process.

Yarn

yarn.lock contains fixed packages' versions for all node_modules used in your module.

See yarn cli documentation to know how to use it.

Package definition

package.json contains your module definition with name, description, scripts, dependencies, etc.

To install existing or new dependencies use npm or yarn. We advice to use yarn to have the same version that us.

In scripts part, you have all needed scripts to work. All scripts reference elements in Makefile.

Back to top

NVM

If you want to use nvm, install node version according .nvmrc file and type:

$ cd path/to/hapiness/module
$ nvm use

Back to top

Development

All your module's contents must be inside src/module folder.

Each folder must have a barrels index file.

We have a convention name for each files so follow this guideline to be ok with the next hapiness-cli

Module

Your main module file must be at the root of src/module folder and named {my-module}.module.ts. Class will be {MyModule}Module.

Module needs to have @HapinessModule({...}) decorator.

Export it with barrels index at the root of src/module.

Services

All services files must be inside src/module/services/{my-service} folder and named {my-service}.service.ts. Class will be {MyService}Service.

Service needs to have @Inject() decorator and be added inside module metadata's providers array.

Export it with barrels index at the root of src/module/services.

You can organize services by features and create folders for that. Don't forget to create barrels index for each folder.

If your service needs to be exported by your module to be accessible in the DI of other module, you need to export it with barrels index at the root of src/module and add it inside module metadata's exports array.

Routes

All routes files must be inside src/module/routes folder and organize by resources. In each resource folder, you need to create barrels index and method folder. In each method folder, you need to create barrels index and type file named {type}.route.ts. Class will be {MethodTypeResource}Route.

Route needs to have @Route({...}) decorator and be added inside module metadata's declarations array.

Export it with barrels index at the root of src/module/routes.

Example of path for the route to get one user :

$ src/module/routes/user/get/one.route.ts

Implementation of the route will be :

@Route({
    path: '/user/{id}',
    method: 'GET'
})
export class GetOneUserRoute implements OnGet {}

Libraries

All libraries files must be inside src/module/libraries/{my-library} folder and named {my-library}.library.ts. Class will be {MyLibrary}Library.

Library needs to have @Lib() decorator and be added inside module metadata's declarations array.

Export it with barrels index at the root of src/module/libraries.

You can organize libraries by features and create folders for that. Don't forget to create barrels index for each folder.

Tests

You must unit test each service, route and library.

Your module must be tested with an integration in Hapiness server application.

Each file name will be suffixed by test: {my-module}.module.test.ts, {my-service}.service.test.ts, {resource}.{method}.{type}.route.test.ts or {my-library}.library.test.ts.

Classes will be suffixed by Test: {MyModule}ModuleTest, {MyService}ServiceTest, {MethodTypeResource}RouteTest or {MyLibrary}LibraryTest.

To run your tests, just execute:

$ cd path/to/hapiness/module

$ yarn run test

or

$ npm run test

Coverage result will be inside ./coverage/lcov-report folder. Just open the folder in your browser to see the result.

Back to top

Deployment

Build your project:

$ cd path/to/hapiness/module

$ yarn run build

or

$ npm run build

Packaging will be created inside dist folder. You need to publish only the content of this folder:

$ cd path/to/hapiness/module/dist
$ npm publish (--access public => if scoped package)

Back to top

Using your module inside Hapiness application

yarn or npm it in your package.json

$ npm install --save @hapiness/core @{your_scope}/{your_module} rxjs

or

$ yarn add  @hapiness/core @{your_scope}/{your_module} rxjs
"dependencies": {
    "@hapiness/core": "^1.5.0",
    "@{your_scope}/{your_module}": "^1.0.0",
    "rxjs": "^5.5.8"
    //...
}
//...

import <MyModule>Module from the library

import { Hapiness, HapinessModule } from '@hapiness/core';
import { <MyModule>Module } from '@{your_scope}/{your_module}';

@HapinessModule({
    version: '1.0.0',
    imports: [
        <MyModule>Module
    ]
})
class HapinessModuleApp {}

Hapiness.bootstrap(HapinessModuleApp, [
    HttpServerExt.setConfig({ host: '0.0.0.0', port: 4443 })
]);

use it anywhere

If your module contains route just call specific endpoint to see the result and use service anywhere with dependency injection.

Back to top

Change History

  • v1.1.2 (2018-04-03)
    • Latest packages' versions.
  • v1.1.1 (2017-12-19)
    • Latest packages' versions.
    • New build process
    • Documentation
  • v1.1.0 (2017-10-20)
    • Latest packages' versions.
    • Update tests to match with latest core version.
    • Project version related to core version.

Back to top

Maintainers

tadaweb
Julien Fauville Antoine Gomez Sébastien Ritz Nicolas Jessel

Back to top

License

Copyright (c) 2017 Hapiness Licensed under the MIT license.

Back to top