UNPKG

2.09 kBPlain TextView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4import { AppStartHandler, AppTerminateHandler, PostInvocationHandler, PreInvocationHandler } from '@azure/functions';
5import * as coreTypes from '@azure/functions-core';
6import { Disposable } from '../utils/Disposable';
7import { tryGetCoreApiLazy } from '../utils/tryGetCoreApiLazy';
8import { AppStartContext } from './AppStartContext';
9import { AppTerminateContext } from './AppTerminateContext';
10import { PostInvocationContext } from './PostInvocationContext';
11import { PreInvocationContext } from './PreInvocationContext';
12
13function registerHook(hookName: string, callback: coreTypes.HookCallback): coreTypes.Disposable {
14 const coreApi = tryGetCoreApiLazy();
15 if (!coreApi) {
16 console.warn(
17 `WARNING: Skipping call to register ${hookName} hook because the "@azure/functions" package is in test mode.`
18 );
19 return new Disposable(() => {
20 console.warn(
21 `WARNING: Skipping call to dispose ${hookName} hook because the "@azure/functions" package is in test mode.`
22 );
23 });
24 } else {
25 return coreApi.registerHook(hookName, callback);
26 }
27}
28
29export function appStart(handler: AppStartHandler): Disposable {
30 return registerHook('appStart', (coreContext) => {
31 return handler(new AppStartContext(coreContext));
32 });
33}
34
35export function appTerminate(handler: AppTerminateHandler): Disposable {
36 return registerHook('appTerminate', (coreContext) => {
37 return handler(new AppTerminateContext(coreContext));
38 });
39}
40
41export function preInvocation(handler: PreInvocationHandler): Disposable {
42 return registerHook('preInvocation', (coreContext) => {
43 return handler(new PreInvocationContext(coreContext));
44 });
45}
46
47export function postInvocation(handler: PostInvocationHandler): Disposable {
48 return registerHook('postInvocation', (coreContext) => {
49 return handler(new PostInvocationContext(coreContext));
50 });
51}