Giter Club home page Giter Club logo

nammatham's Introduction

Nammatham

Nammatham (นามธรรม in Thai, pronounced /naam ma tham/) is Azure Function Nodejs Lightweight frameworks with Dependency Injection,

npm version npm download

Installation

You can install nammatham using npm:

npm install nammatham inversify reflect-metadata --save

The nammatham type definitions are included in the npm module and require TypeScript 2.0 and above. Please refer to the InversifyJS documentation to learn more about the installation process.

Features

Motivation

.NET is a first-class supported in Azure Function which ... (Write Later)

We heavily get inspired from Azure Functions .NET version which provide clearly project strucutre, built-in Azure Function Configuration with the Code, and also provide built-in Dependency Injection.

Pain Point

  • Ugly Project Structure - Azure Functions Node.js provide only basic library to connect with Azure Function Runtime. All Functions Endpoint be in the root of the project and accept only one export in the index.js which is the Azure Function Runtime will inject runtime object for that such as Context. The other code like services, constrant, middleware must be same level of the Function Endpoints.
  • Separate JS Code and the Function configuration - Seperately JS Code and the Function configuration make a bit hard to read how to function app works, however, .NET version also provide configuration inline of the C# Code
    • In order to create an Azure Function endpoint, they requires 2 files
      1. index.js which must have only one export.
      2. function.json which is plain JSON, no autocomplete how to config, it requires to open the document to config it.
  • No Dependecy Injection - Azure Function Node.js doesn't provide any Dependecy Injection tool, however, in .NET Azure Function provides built-in Dependency Injection

Usage

Please see in example directory

  1. define bootstrap.ts
  2. Write controller, extend with BaseController we will auto inject Azure Function's Context
    import {
      AuthorizationLevel,
      BaseController,
      controller,
      functionName,
      httpTrigger,
    } from "nammatham";
    import { HttpRequest } from "@azure/functions";
    
    @controller()
    export class UserController extends BaseController {
    
      @functionName("GetUsers", httpTrigger(AuthorizationLevel.Anonymous, ["get"]))
      public getUsers(req: HttpRequest): void {
        const name = req.query.name;  
        this.context.log('Context Log');
    
        // this.res.send(`hello get user with ${name}`);
        this.res.json({
          data: `hello get user with ${name}`
        });
      }
    }

How it work

it will autogenerate, 2 files per function

  1. function.json

    {
      "bindings": [
        {
          "authLevel": "anonymous",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        }
      ],
      "scriptFile": "../dist/src/controllers/user.controller.js"
    }
  2. index.ts

    import 'reflect-metadata';
    import { AzureFunction, Context } from '@azure/functions';
    import { funcBootstrap } from 'nammatham';
    import { UserController } from '../src/controllers/user.controller';
    
    const GetUsers: AzureFunction = async function (
      context: Context,
      ...args: any[]
    ): Promise<void> {
      funcBootstrap({
        classTarget: UserController,
        methodName: 'getUsers',
        azureFunctionParams: [context, ...args]
      });
    };
    
    export default GetUsers;

TODO

  • Add Log at boostrap level
  • Inject Context in Class Dependency
  • @controller() should accept prefix path, e.g. @controller('users')
  • allow to add Middleware
  • Unit Test
  • functionName must be unique
  • Clean generated function endpoint (already remove controller)
  • Cannot Resolve Serivce from Bootstrap script

Inspiration

Author

  • Thada Wangthammang, Software Engineer, Thailand

nammatham's People

Contributors

mildronize avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.