diff --git a/examples/azure-functions-with-context/README.md b/examples/azure-functions-with-context/README.md new file mode 100644 index 00000000..0aec29f0 --- /dev/null +++ b/examples/azure-functions-with-context/README.md @@ -0,0 +1 @@ +# @examples/azure-functions diff --git a/examples/azure-functions-with-context/host.json b/examples/azure-functions-with-context/host.json new file mode 100644 index 00000000..852b7b7a --- /dev/null +++ b/examples/azure-functions-with-context/host.json @@ -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)" + } +} \ No newline at end of file diff --git a/examples/azure-functions-with-context/local.settings.json b/examples/azure-functions-with-context/local.settings.json new file mode 100644 index 00000000..6199cf10 --- /dev/null +++ b/examples/azure-functions-with-context/local.settings.json @@ -0,0 +1,9 @@ +{ + "IsEncrypted": false, + "Values": { + "FUNCTIONS_WORKER_RUNTIME": "node", + "AzureWebJobsFeatureFlags": "EnableWorkerIndexing", + "AzureWebJobsStorage": "UseDevelopmentStorage=true" + }, + "ConnectionStrings": {} +} \ No newline at end of file diff --git a/examples/azure-functions-with-context/package.json b/examples/azure-functions-with-context/package.json new file mode 100644 index 00000000..e5039cdf --- /dev/null +++ b/examples/azure-functions-with-context/package.json @@ -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.1.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" + } +} \ No newline at end of file diff --git a/examples/azure-functions-with-context/src/container.ts b/examples/azure-functions-with-context/src/container.ts new file mode 100644 index 00000000..5ac6234e --- /dev/null +++ b/examples/azure-functions-with-context/src/container.ts @@ -0,0 +1,6 @@ +import { Container } from 'inversify'; +import { DataService } from './services/data.service'; + +export const container = new Container(); +container.bind(DataService).toSelf(); + diff --git a/examples/azure-functions-with-inversify/src/functions/hello.ts b/examples/azure-functions-with-context/src/functions/hello.ts similarity index 100% rename from examples/azure-functions-with-inversify/src/functions/hello.ts rename to examples/azure-functions-with-context/src/functions/hello.ts diff --git a/examples/azure-functions-with-context/src/main.ts b/examples/azure-functions-with-context/src/main.ts new file mode 100644 index 00000000..a116f49b --- /dev/null +++ b/examples/azure-functions-with-context/src/main.ts @@ -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(); diff --git a/examples/azure-functions-with-context/src/nammatham.ts b/examples/azure-functions-with-context/src/nammatham.ts new file mode 100644 index 00000000..f321be24 --- /dev/null +++ b/examples/azure-functions-with-context/src/nammatham.ts @@ -0,0 +1,7 @@ +import { initNammatham } from 'nammatham'; + +const n = initNammatham.create(); +n.func; +// ^? +export const func = n.func; +export const app = n.app; diff --git a/examples/azure-functions-with-context/src/services/data.service.ts b/examples/azure-functions-with-context/src/services/data.service.ts new file mode 100644 index 00000000..ed289e29 --- /dev/null +++ b/examples/azure-functions-with-context/src/services/data.service.ts @@ -0,0 +1,9 @@ +import { injectable } from 'inversify'; + +@injectable() +export class DataService { + + public getData() { + return `Data from DataService`; + } +} \ No newline at end of file diff --git a/examples/azure-functions-with-context/tsconfig.json b/examples/azure-functions-with-context/tsconfig.json new file mode 100644 index 00000000..f970fa21 --- /dev/null +++ b/examples/azure-functions-with-context/tsconfig.json @@ -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"] +} \ No newline at end of file diff --git a/examples/azure-functions-with-inversify/src/controllers/home.controller.ts b/examples/azure-functions-with-inversify/src/controllers/home.controller.ts new file mode 100644 index 00000000..ddd6371c --- /dev/null +++ b/examples/azure-functions-with-inversify/src/controllers/home.controller.ts @@ -0,0 +1,18 @@ +import { func } from '../nammatham'; +import { DataService } from '../services/data.service'; + +export class HomeController { + constructor(public dataService: DataService) {} + + hello = func + .httpGet('hello', { + route: 'hello-world', + }) + .handler(async c => { + c.context.log('HTTP trigger function processed a request.'); + + return c.json({ + data: 'hello world' + this.dataService.getData(), + }); + }); +} diff --git a/examples/azure-functions-with-inversify/src/main.ts b/examples/azure-functions-with-inversify/src/main.ts index a116f49b..7421374b 100644 --- a/examples/azure-functions-with-inversify/src/main.ts +++ b/examples/azure-functions-with-inversify/src/main.ts @@ -1,10 +1,13 @@ 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'; +import { DataService } from './services/data.service'; -app.addFunctions(hello); +const homeController = new HomeController(container.get(DataService)); +app.addFunctions(homeController.hello); -const dev =process.env.NODE_ENV === 'development'; +const dev = process.env.NODE_ENV === 'development'; app.register(expressPlugin({ dev })); app.start();