Implements: IAzureFunctionController, IOpenable, IConfigurable, IReferenceable
Description
The AzureFunctionController class allows you to create a service that receives remove calls via the Azure Function protocol.
Important points
- This service is intended to work inside an AzureFunction container that exposes registered actions externally.
Configuration parameters
- dependencies:
- controller: override for Controller dependency
References
- *:logger:*:*:1.0: (optional) ILogger components to pass log messages.
- *:counters:*:*:1.0: (optional) ICounters components to pass collected measurements.
Constructors
Creates an instance of this service.
publicconstructor(name?: string)
- name: string - name of the service used to generate an action cmd.
Fields
Instance methods
act
Calls registered actions in this Azure Function. The “cmd” parameter in the action parameters determines what action shall be called.
This method shall only be used in testing.
publicact(context: any): Promise<any>
- context: any - context context.
- returns: Promise<any> - returned result
configure
Configures a component by passing its configuration parameters.
publicconfigure(config: ConfigParams): void
- config: ConfigParams - configuration parameters to be set.
applyValidation
Performs a validation.
protectedapplyValidation(schema: Schema, action: (context: any) => Promise<any>): (context: any) => Promise<any>
- schema: Schema - schema used in the validation
- action: (context: any) => Promise<any> - action
- returns: (context: any) => Promise<any> - returned result
close
Closes a component and frees used resources.
publicclose(context: IContext): Promise<void>
- context: IContext - (optional) a context to trace execution through a call chain.
configure
Configures a component by passing its configuration parameters.
publicconfigure(config: ConfigParams): void
- config: ConfigParams - configuration parameters to be set.
generateActionCmd
Adds ‘.cmd’ to a command name
protectedgenerateActionCmd(name: string): string
- name: string - command name
- returns: string - command name with ‘.cmd’ added at its end.
getActions
Get all actions supported by the service.
publicgetActions(): AzureFunctionAction[]
- returns: AzureFunctionAction[] - array with supported actions.
getCommand
Returns a command from the Azure Function context.
This method can be overloaded in child classes.
protectedgetCommand(context: any): string
- context: any - context.
- returns: string - returned command from context.
getTraceId
Returns a traceId from the Azure Function context.
This method can be overloaded in child classes.
protectedgetTraceId(context: any): string
- context: any - context.
- returns: string - returned traceId from context.
instrument
Adds instrumentation to log calls and measures call time. It returns a Timing object that is used to end the time measurement.
protectedinstrument(context: IContext, name: string): InstrumentTiming
- context: IContext - (optional) a context to trace execution through a call chain.
- name: string - method’s name.
- returns: InstrumentTiming - Timing object to end the time measurement.
isOpen
Checks if the component is open.
publicisOpen(): boolean
- returns: boolean - true if the component is open and false otherwise.
open
Opens the component.
publicopen(context: IContext): Promise<void>
- context: IContext - (optional) a context to trace execution through a call chain.
registerAction
Registers an action in Azure Function function.
protectedregisterAction(name: string, schema: Schema, action: (context: any) => Promise<any>): void
- name: string - action name
- schema: Schema - validation schema used to validate received parameters.
- action: (context: any) => Promise<any> - action function that is called when the operation is invoked.
registerActionWithAuth
Registers an action with authorization.
protectedregisterActionWithAuth(name: string, schema: Schema, authorize: (context: any, next: (context: any) => Promise<any>) => Promise<any>, action: (context: any) => Promise<any>): void
- name: string - action’s name
- schema: Schema - validation schema used to validate received parameters.
- authorize: (context: any, next: (context: any) => Promise<any>) => Promise<any> - authorization interceptor
- action: (context: any) => Promise<any> - action function that is called when the operation is invoked.
registerInterceptor
Registers a middleware for actions in Azure Function service.
protectedregisterInterceptor(action: (context: any, next: (context: any) => Promise<any>) => Promise<any>): void
- action: (context: any, next: (context: any) => Promise<any>) => Promise<any> - action function that is called when middleware is invoked.
setReferences
Sets references to dependent components.
publicsetReferences(references: IReferences): void
- references: IReferences - references to locate the component’s dependencies.
Abstract methods
register
Registers all service routes in an HTTP endpoint.
This method is called by the service and must be overridden in child classes.
protected abstractregister(): void
Examples
class MyAzureFunctionController extends AzureFunctionController {
private _controller: IMyController;
...
public constructor() {
base('v1.myservice');
this._dependencyResolver.put(
"controller",
new Descriptor("mygroup","controller","*","*","1.0")
);
}
public setReferences(references: IReferences): void {
base.setReferences(references);
this._controller = this._dependencyResolver.getRequired<IMyController>("controller");
}
public register(): void {
registerAction("get_mydata", null, async (context) => {
let context = context.correlation_id;
let id = context.id;
return await this._controller.getMyData(context, id);
});
...
}
}
let service = new MyAzureFunctionController();
service.configure(ConfigParams.fromTuples(
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", 8080
));
service.setReferences(References.fromTuples(
new Descriptor("mygroup","controller","default","default","1.0"), controller
));
service.open("123");