Skip to content

Commit 69060c7

Browse files
committed
better example of 'beforeEach' with service
1 parent a4b5fdc commit 69060c7

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

samples/http/app.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { HostOptions } from "../../src/hostConfig"
55
import { InvocationContext } from "./common/interfaces"
66
import { sayHello } from "./functions/hello"
77
import { sayGoodbye } from "./functions/goodbye"
8-
import { CosmosClient } from "./services/fake-clients"
8+
import { getExampleClient } from "./services/fake-clients"
99

1010
const functions: AzureFunctionDefinition[] = [
1111
{
@@ -25,7 +25,7 @@ const functions: AzureFunctionDefinition[] = [
2525
})
2626
],
2727
},
28-
{
28+
{
2929
trigger: new HttpTrigger({
3030
bindingName: "req",
3131
route: "/order",
@@ -65,15 +65,16 @@ const options: HostOptions = {
6565
};
6666

6767
// - local.settings.json??? also host.json not needed??
68-
const app = new FunctionApp(functions, options);
68+
const app = new FunctionApp({
69+
functions,
70+
options
71+
});
6972
// before each function, do this
7073
// beforeeach on trigger type
7174
// for
72-
let cosmosClient = new CosmosClient();
73-
7475
app.beforeEach(async (context: InvocationContext) => {
7576
context.log(`~~ Starting invocation: '${context.invocationId}' ~~`);
76-
context.cosmosClient = cosmosClient;
77+
context.exampleClient = getExampleClient();
7778
});
7879

7980
app.beforeEach(async (context: InvocationContext) => {

samples/http/common/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Context, HttpRequest } from "@azure/functions"
2+
import { ExampleClient } from "../services/fake-clients";
23

34
export interface InvocationContext extends Context {
45
name: string;
5-
cosmosClient: any;
6-
req: CookieRequest;
6+
exampleClient: ExampleClient;
77
}
88

99
interface CookieRequest extends HttpRequest {

samples/http/services/fake-clients.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
export class CosmosClient {
2-
getContainers() { return "Fake cosmos client call"; }
3-
};
1+
// This would typically be from some other library
2+
export class ExampleClient {
3+
getData() { return "Fake data fetched from fake service"; }
4+
};
5+
6+
let _client: ExampleClient;
7+
8+
export function getExampleClient() {
9+
if (!_client) {
10+
_client = new ExampleClient();
11+
}
12+
return _client;
13+
}

src/azure-functions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export class FunctionApp {
2929
};
3030

3131
// host.json and local.settings.json... what to do??
32-
constructor(functions: AzureFunctionDefinition[], options?: HostOptions) {
32+
constructor(setup: { functions: AzureFunctionDefinition[], options?: HostOptions }) {
33+
let { functions, options } = setup;
3334
this._functionConfigurations = functions;
3435
this._hostOptions = Object.assign({}, this.defaultHostOptions, options, this.requiredHostOptions);
3536
for (const func of functions) {

0 commit comments

Comments
 (0)