Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Inversify Plugin #132

Merged
merged 7 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/azure-functions-with-context/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @examples/azure-functions
15 changes: 15 additions & 0 deletions examples/azure-functions-with-context/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.15.0, 4.0.0)"
}
}
9 changes: 9 additions & 0 deletions examples/azure-functions-with-context/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
},
"ConnectionStrings": {}
}
27 changes: 27 additions & 0 deletions examples/azure-functions-with-context/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@examples/azure-functions-with-inversify",
"version": "1.0.0",
"description": "",
"main": "dist/src/main.js",
"scripts": {
"build": "tsc",
"lint": "tsc --noEmit",
"start": "tsc && func start",
"dev": "cross-env NODE_ENV=development tsx watch src/main.ts"
},
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.4.0",
"@di-extra/inversify": "^0.2.0",
"nammatham": "2.0.0-alpha.13",
"inversify": "^6.0.2",
"reflect-metadata": "^0.2.1"
},
"devDependencies": {
"cross-env": "^7.0.3",
"npm-run-all": "^4.1.5",
"tsx": "^4.7.0",
"typescript": "^5.0.2"
}
}
6 changes: 6 additions & 0 deletions examples/azure-functions-with-context/src/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Container } from 'inversify';
import { DataService } from './services/data.service';

export const container = new Container();
container.bind<DataService>(DataService).toSelf();

10 changes: 10 additions & 0 deletions examples/azure-functions-with-context/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'reflect-metadata';
import { expressPlugin } from 'nammatham';
import hello from './functions/hello';
import { app } from './nammatham';

app.addFunctions(hello);

const dev =process.env.NODE_ENV === 'development';
app.register(expressPlugin({ dev }));
app.start();
7 changes: 7 additions & 0 deletions examples/azure-functions-with-context/src/nammatham.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { initNammatham } from 'nammatham';

const n = initNammatham.create();
n.func;
// ^?
export const func = n.func;
export const app = n.app;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { injectable } from 'inversify';

@injectable()
export class DataService {

public getData() {
return `Data from DataService`;
}
}
13 changes: 13 additions & 0 deletions examples/azure-functions-with-context/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
},
"exclude": ["node_modules", "**/*.test.ts"]
}
5 changes: 3 additions & 2 deletions examples/azure-functions-with-inversify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.1.0",
"@azure/functions": "^4.4.0",
"@di-extra/inversify": "^0.2.0",
"nammatham": "2.0.0-alpha.13",
"@nammatham/inversify": "2.0.0-alpha.13",
"inversify": "^6.0.2",
"nammatham": "2.0.0-alpha.13",
"reflect-metadata": "^0.2.1"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion examples/azure-functions-with-inversify/src/container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Container } from 'inversify';
import { DataService } from './services/data.service';
import { HomeController } from './controllers/home.controller';

export const container = new Container();
container.bind<DataService>(DataService).toSelf();

container.bind<HomeController>(HomeController).toSelf();
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { inject, injectable } from 'inversify';
import { func } from '../nammatham';
import { DataService } from '../services/data.service';

@injectable()
export class HomeController {
constructor(@inject(DataService) public dataService: DataService) {}

hello = func
.httpGet('myHello', {
route: 'hello-world',
})
.handler(async c => {
c.context.log('HTTP trigger function processed a request.');

return c.json({
data: 'hello world' + this.dataService.getData(),
});
});

timer = func
.timer('myTimer', {
schedule: '0 */5 * * * *',
})
.handler(async c => {
c.context.log('Timer trigger function processed a request.');
});
}
15 changes: 12 additions & 3 deletions examples/azure-functions-with-inversify/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import 'reflect-metadata';
import { expressPlugin } from 'nammatham';
import hello from './functions/hello';
import { HomeController } from './controllers/home.controller';
import { app } from './nammatham';
import { container } from './container';

app.addFunctions(hello);
// Import the inversify plugin
import { inverisfyPlugin } from '@nammatham/inversify';

const dev =process.env.NODE_ENV === 'development';
// Manually register controllers
// const homeController = new HomeController(container.get(DataService));
// app.addFunctions(homeController.hello);

// Automatically register controllers
app.register(inverisfyPlugin({ container, services: [HomeController] }));

const dev = process.env.NODE_ENV === 'development';
app.register(expressPlugin({ dev }));
app.start();
2 changes: 1 addition & 1 deletion examples/azure-functions-with-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.1.0",
"@azure/functions": "^4.4.0",
"nammatham": "2.0.0-alpha.13"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-functions-with-trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.1.0",
"@azure/functions": "^4.4.0",
"nammatham": "2.0.0-alpha.13",
"@nammatham/trpc-azure-functions": "2.0.0-alpha.13",
"@trpc/client": "^10.45.0",
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"scripts": {
"test": "vitest",
"test:coverage": "vitest run --coverage",
"dev": "nx run @nammatham/core:build && nx run-many -t dev --projects nammatham @nammatham/* --parallel=5",
"dev": "nx run @nammatham/core:build && nx run-many -t dev --projects nammatham @nammatham/* --parallel=10",
"pre-local": "tsx ./scripts/pre-local.ts",
"post-local": "tsx ./scripts/post-local.ts",
"release": "run-s build releaseOnly",
"releaseOnly": "tsx ./scripts/release.ts",
"format": "nx run-many -t format --projects=@nammatham/* --parallel=5",
"lint": "nx run-many -t lint --projects=@nammatham/* --parallel=5",
"lint:fix": "nx run-many -t lint:fix --projects=@nammatham/* --parallel=5",
"format": "nx run-many -t format --projects=@nammatham/* --parallel=10",
"lint": "nx run-many -t lint --projects=@nammatham/* --parallel=10",
"lint:fix": "nx run-many -t lint:fix --projects=@nammatham/* --parallel=10",
"build": "nx run-many -t build --parallel=10",
"azurite": "pnpx azurite --silent --location ./.azurite --debug ./.azurite/debug.log"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/azure-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.1.0",
"@azure/functions": "^4.4.0",
"@nammatham/core": "2.0.0-alpha.13",
"colorette": "^2.0.20",
"express": "^4.18.2",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/bases/base-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NammamthamEndpoint } from '../types';

export abstract class BaseHandler<Handler extends (...args: any[]) => any> {
public readonly __baseHandler: boolean = true;
abstract build(): NammamthamEndpoint;
abstract handler(func: Handler): this;
abstract getHandler(): Handler;
Expand Down
148 changes: 148 additions & 0 deletions packages/inversify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<p align="center">
<a href="http://thadaw.com/" target="blank"><img src="https://i.ibb.co/QmTh7x4/Nammatham-Logo-v2.png" width="120" alt="Nammatham Logo" /></a>
</p>

<p align="center">
Type-safe Serverless Library for Azure Functions and friends
</p>

<p align="center"><a href="https://www.npmjs.com/package/nammatham"><img alt="NPM Version (with dist tag)" src="https://img.shields.io/npm/v/nammatham/alpha">
<a href="https://www.npmjs.com/package/nammatham"><img src="https://img.shields.io/npm/dt/nammatham" alt="npm download"></a></p>


> 🚧 Alpha Stage: Internal Use Only 🚧
>
> Please note that Nammatham v2 is currently in its Alpha stage and is intended for internal use only. As we actively develop and refine the platform, be aware that the API may undergo frequent changes. [Tracking v2 Roadmap](https://github.com/thaitype/nammatham/issues?q=is%3Aissue+is%3Aopen+label%3Av2-blocker)
>
> Note: [Nammatham v1](https://www.npmjs.com/package/nammatham) is currently in maintenance mode. no new features are actively being developed

You're reading v2 docs


| Version | Status | Azure Functions <br>Node.js Lib | Branch | Build Status |
| ------- | ----------- | ----------------------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| v1.x | Maintenance | v3.x | [v1.x][v1.x] | [![Build & Test](https://github.com/thaitype/nammatham/actions/workflows/test.yml/badge.svg?branch=v1.x)](https://github.com/thaitype/nammatham/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/thaitype/nammatham/branch/v1.x/graph/badge.svg?token=Y7ZMDKFPAN)](https://codecov.io/gh/thaitype/nammatham) |
| v2.x | Alpha | v4.x | [main][main] | [![Build & Test](https://github.com/thaitype/nammatham/actions/workflows/test.yml/badge.svg?branch=main.unittest)](https://github.com/thaitype/nammatham/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/thaitype/nammatham/branch/main/graph/badge.svg?token=Y7ZMDKFPAN)](https://codecov.io/gh/thaitype/nammatham) |

[v1.x]: https://github.com/thaitype/nammatham/tree/v1.x
[main]: https://github.com/thaitype/nammatham/tree/main

## Description
Nammatham (นามธรรม in Thai, pronounced `/naam ma tham/`, means **abstract** in Thai) is Azure Function Nodejs.

## Getting Started for Azure Functions

### Install

```bash
# Including all packages
npm install nammatham@alpha
```

### Example

You can see [examples](examples) or follow the minimal app getting started below:

> `initNammatham.create()` is a factory function for creating Nammatham App, it's a wrapper for Azure Functions App.

```typescript
import { initNammatham, expressPlugin } from 'nammatham';

const n = initNammatham.create();
const func = n.func;
const app = n.app;

const helloFunction = func
.httpGet('hello', {
route: 'hello-world',
})
.handler(async c => {
c.context.log('HTTP trigger function processed a request.');
c.context.debug(`Http function processed request for url "${c.trigger.url}"`);
const name = c.trigger.query.get('name') || (await c.trigger.text()) || 'world';
return c.text(`Hello, ${name}!`);
});

app.addFunctions(helloFunction);

const dev = process.env.NODE_ENV === 'development';
app.register(expressPlugin({ dev }));
app.start();
```

Then edit `package.json` like this;

```json
{
"main": "dist/src/main.js",
"scripts": {
"dev": "cross-env NODE_ENV=development tsx watch src/main.ts",
"start": "tsc && func start"
}
}
```

Run Dev Server on locally, (For dev server use `tsx watch` for reloading run dev server using `express` )

```
npm run dev
```

Run Azure Functions on locally (Using Official Azure Functions Node.js)

```
npm start
```


## Nammatham Packages

- [core][@nammatham/core], Nammatham Core package for initializing Nammatham App

### Available Adatpers

- [azure-functions][@nammatham/azure-functions], Azure Functions Adapter for Nammatham, internally, Azure Functions in local dev mode is dependend on Express.js.

### Available Plugins

- [express][@nammatham/express], Express Plugin for run server. Nammatham itself doesn't contain any server, enabling this plugin to provide better DX than the original server e.g. Azure Functions Runtime
- [trpc-azure-functions][@nammatham/trpc-azure-functions], provide [tRPC](https://trpc.io/) Plugin for Azure Functions, inclduing [express][@nammatham/express] server for local testing.

[@nammatham/core]: packages/core
[@nammatham/azure-functions]: packages/azure-functions
[@nammatham/express]: packages/express
[@nammatham/trpc-azure-functions]: packages/trpc-azure-functions


## Talks
Empowering TypeScript on Azure Functions with Nammatham, Azure Open Source Day @ Microsoft Thailand, 25 Mar 2023
[![](docs/imgs/azure-open-source-day-2023.png)](https://www.youtube.com/watch?v=n6B4-5Lt2h0) (Thai speech, subtitle will added later)
- Slides: https://docs.google.com/presentation/d/1WUIXaUxXaiixZ2bgGCfx-f4Gdrmjl4RfbwKaEfAC6t4/edit?usp=sharing


<!-- ## What's different with Azure Functions v4 (Official Library) -->

## Local Dev Setup

```bash
# Install dependencies
pnpm install
# Before dev (Update workspace to local dependencies)
pnpm pre-local && pnpm install
# While dev
pnpm dev
# After dev before submitting PRs (Update workspace to actual dependencies), `pnpm install` for making sure lockfile is correct.
pnpm post-local && pnpm install
# Release package
pnpm release
```

## Inspiration
- [Azure Functions .NET](https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-csharp?tabs=azure-cli%2Cin-process)
- [inversify-express-utils](https://github.com/inversify/inversify-express-utils) - We use inversify as a Dependency Injection Tool.
- [Nestjs](https://nestjs.com/)
- [typestack/routing-controllers](https://github.com/typestack/routing-controllers)
- [azure-middleware](https://github.com/emanuelcasco/azure-middleware) - Azure Functions Middleware Libray

## Author
- Thada Wangthammang, Software Engineer, Thailand
Loading
Loading