-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add setContext, helper for injecting other dependencies (#117)
- Loading branch information
Showing
31 changed files
with
452 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
|
||
```ts | ||
import { container } from '../container'; | ||
import { func } from '../nammatham'; | ||
import { injector } from '@di-extra/inversify'; | ||
import { DataService } from '../services/data.service'; | ||
import type { InferHandler } from '@nammatham/core'; | ||
|
||
const trigger = func | ||
.httpGet('hello', { | ||
route: 'hello-world', | ||
}) | ||
.setContext({ | ||
// prettier-ignore | ||
services: injector(container) | ||
.inject('dataService', DataService).to<DataService>() | ||
.resolve(), | ||
}); | ||
|
||
export const _handler: InferHandler<typeof trigger> = ({ trigger, context, services }) => { | ||
context.log('HTTP trigger function processed a request.'); | ||
|
||
return { | ||
jsonBody: { | ||
data: 'hello world' + services.dataService.getData(), | ||
}, | ||
}; | ||
}; | ||
|
||
export default trigger.handler(_handler); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @examples/azure-functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"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 NAMMATHAM_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/azure-functions": "2.0.0-alpha.8", | ||
"@nammatham/core": "2.0.0-alpha.8", | ||
"@nammatham/express": "2.0.0-alpha.8", | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
26 changes: 26 additions & 0 deletions
26
examples/azure-functions-with-inversify/src/functions/hello.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { container } from '../container'; | ||
import { func } from '../nammatham'; | ||
import { injector } from '@di-extra/inversify'; | ||
import { DataService } from '../services/data.service'; | ||
|
||
// prettier-ignore | ||
const services = injector(container) | ||
.inject('dataService', DataService).to<DataService>() | ||
.resolve(); | ||
|
||
export default func | ||
.httpGet('hello', { | ||
route: 'hello-world', | ||
}) | ||
.setContext({ | ||
services, | ||
}) | ||
.handler(async ({ trigger, context, services }) => { | ||
context.log('HTTP trigger function processed a request.'); | ||
|
||
return { | ||
jsonBody: { | ||
data: 'hello world' + services.dataService.getData(), | ||
}, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'reflect-metadata'; | ||
import { expressPlugin } from '@nammatham/express'; | ||
import hello from './functions/hello'; | ||
import { app } from './nammatham'; | ||
|
||
app.addFunctions(hello); | ||
|
||
app.register(expressPlugin()); | ||
app.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { initNammatham } from '@nammatham/core'; | ||
import { AzureFunctionsAdapter } from '@nammatham/azure-functions'; | ||
|
||
const n = initNammatham.create(new AzureFunctionsAdapter()); | ||
n.func; | ||
// ^? | ||
export const func = n.func; | ||
export const app = n.app; |
9 changes: 9 additions & 0 deletions
9
examples/azure-functions-with-inversify/src/services/data.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @examples/azure-functions |
26 changes: 26 additions & 0 deletions
26
examples/azure-functions-with-test/__test__/helloFunction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { expect, test } from 'vitest'; | ||
import helloFunc from '../src/functions/hello'; | ||
import { HttpRequest, InvocationContext } from '@azure/functions'; | ||
|
||
test('helloFunc', async () => { | ||
const handler = helloFunc.getHandler(); | ||
const result = await handler({ | ||
context: new InvocationContext(), | ||
trigger: new HttpRequest({ | ||
method: 'GET', | ||
url: 'http://localhost:3000/api/hello-world', | ||
query: { | ||
name: 'world', | ||
}, | ||
}), | ||
}); | ||
|
||
expect(result).toEqual({ | ||
jsonBody: { | ||
data: { | ||
name: 'world', | ||
message: 'Hello, world!', | ||
}, | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@examples/azure-functions-with-test", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "dist/src/main.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"lint": "tsc --noEmit", | ||
"start": "tsc && func start", | ||
"dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts" | ||
}, | ||
"author": "Thada Wangthammang", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@azure/functions": "^4.1.0", | ||
"@nammatham/azure-functions": "2.0.0-alpha.8", | ||
"@nammatham/core": "2.0.0-alpha.8", | ||
"@nammatham/express": "2.0.0-alpha.8" | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^7.0.3", | ||
"npm-run-all": "^4.1.5", | ||
"tsx": "^4.7.0", | ||
"typescript": "^5.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { func } from '../nammatham'; | ||
|
||
export default func | ||
.httpGet('hello', { | ||
route: 'hello-world', | ||
}) | ||
.handler(async ({ trigger, context }) => { | ||
context.log('HTTP trigger function processed a request.'); | ||
context.debug(`Http function processed request for url "${trigger.url}"`); | ||
const name = trigger.query.get('name') || (await trigger.text()) || 'world'; | ||
if (name === 'error') { | ||
throw new Error('this is an error'); | ||
} | ||
const result = { | ||
data: { | ||
name: name, | ||
message: `Hello, ${name}!` | ||
} | ||
} | ||
return { | ||
jsonBody: result, | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { expressPlugin } from '@nammatham/express'; | ||
import hello from './functions/hello'; | ||
import { app } from './nammatham'; | ||
|
||
app.addFunctions(hello); | ||
|
||
app.register(expressPlugin()); | ||
app.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { initNammatham } from '@nammatham/core'; | ||
import { AzureFunctionsAdapter } from '@nammatham/azure-functions'; | ||
|
||
const n = initNammatham.create(new AzureFunctionsAdapter()); | ||
|
||
export const func = n.func; | ||
export const app = n.app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.