Skip to main content

Class: Service<ConfigType>


PURISTA API / Modules / @purista/core / Service

Class: Service<ConfigType>

@purista/core.Service

Base class for all services. This class provides base functions to work with the event bridge, logging and so on

Every service should extend this class and should not directly access the eventbridge or other service

class MyService extends Service {

  async start() {
    await super.start()
    // your custom implementation
  }

  async destroy() {
    // your custom implementation
   await super.destroy()
  }
}

Type parameters

NameType
ConfigTypeunknown | undefined

Hierarchy

  • ServiceBaseClass

    Service

Implements

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new Service<ConfigType>(config): Service<ConfigType>

Type parameters

NameType
ConfigTypeunknown

Parameters

NameType
configServiceConstructorInput<ConfigType>

Returns

Service<ConfigType>

Overrides

ServiceBaseClass.constructor

Defined in

core/Service/Service.impl.ts:84open in new window

Properties

commandDefinitionList

commandDefinitionList: CommandDefinitionList<any>

Defined in

core/Service/Service.impl.ts:80open in new window


commands

Protected commands: Map<string, CommandDefinition>

Defined in

core/Service/Service.impl.ts:78open in new window


config

config: ConfigType

Implementation of

ServiceClass.config

Defined in

core/Service/Service.impl.ts:82open in new window


configStore

configStore: ConfigStore

Inherited from

ServiceBaseClass.configStore

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:35open in new window


eventBridge

eventBridge: EventBridge

Inherited from

ServiceBaseClass.eventBridge

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:26open in new window


info

Readonly info: ServiceInfoType

Inherited from

ServiceBaseClass.info

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:24open in new window


logger

logger: Logger

Inherited from

ServiceBaseClass.logger

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:28open in new window


secretStore

secretStore: SecretStore

Inherited from

ServiceBaseClass.secretStore

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:34open in new window


spanProcessor

spanProcessor: undefined | SpanProcessor

Inherited from

ServiceBaseClass.spanProcessor

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:30open in new window


stateStore

stateStore: StateStore

Inherited from

ServiceBaseClass.stateStore

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:36open in new window


subscriptionDefinitionList

subscriptionDefinitionList: SubscriptionDefinitionList<any>

Defined in

core/Service/Service.impl.ts:81open in new window


subscriptions

Protected subscriptions: Map<string, SubscriptionDefinition>

Defined in

core/Service/Service.impl.ts:77open in new window


traceProvider

traceProvider: NodeTracerProvider

Inherited from

ServiceBaseClass.traceProvider

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:32open in new window

Accessors

name

get name(): string

Returns

string

Defined in

core/Service/Service.impl.ts:100open in new window


serviceInfo

get serviceInfo(): ServiceInfoType

Get service info

Returns

ServiceInfoType

Inherited from

ServiceBaseClass.serviceInfo

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:94open in new window

Methods

destroy

destroy(): Promise<void>

Stop and destroy the current service

Returns

Promise<void>

Implementation of

ServiceClass.destroy

Overrides

ServiceBaseClass.destroy

Defined in

core/Service/Service.impl.ts:785open in new window


emit

emit<K>(eventName, parameter?): void

Type parameters

NameType
Kextends EventKey<ServiceEvents>

Parameters

NameType
eventNameK
parameter?ServiceEvents[K]

Returns

void

Inherited from

ServiceBaseClass.emit

Defined in

core/types/GenericEventEmitter.ts:24open in new window


executeCommand

executeCommand(message): Promise<Readonly<Omit<{ contentEncoding: "utf-8" ; contentType: "application/json" ; correlationId: string ; eventName?: string ; id: string ; isHandledError: boolean ; messageType: CommandErrorResponse ; otp?: string ; payload: { data?: unknown ; message: string ; status: StatusCode } ; principalId?: string ; receiver: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; sender: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?: string ; timestamp: number ; traceId?: string }, "instanceId">> | { contentEncoding: string ; contentType: string ; correlationId: string ; eventName?: string ; id: string ; messageType: CommandSuccessResponse ; otp: string ; payload: unknown ; principalId?: string ; receiver: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; tenantId?: string ; timestamp: number ; traceId?: string }>

Called when a command is received by the service

Parameters

NameType
messageReadonly<{ contentEncoding: string ; contentType: string ; correlationId: string ; eventName?: string ; id: string ; messageType: Command ; otp?: string ; payload: { parameter: unknown ; payload: unknown } ; principalId?: string ; receiver: EBMessageAddress ; sender: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?: string ; timestamp: number ; traceId?: string }>

Returns

Promise<Readonly<Omit<{ contentEncoding: "utf-8" ; contentType: "application/json" ; correlationId: string ; eventName?: string ; id: string ; isHandledError: boolean ; messageType: CommandErrorResponse ; otp?: string ; payload: { data?: unknown ; message: string ; status: StatusCode } ; principalId?: string ; receiver: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; sender: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?: string ; timestamp: number ; traceId?: string }, "instanceId">> | { contentEncoding: string ; contentType: string ; correlationId: string ; eventName?: string ; id: string ; messageType: CommandSuccessResponse ; otp: string ; payload: unknown ; principalId?: string ; receiver: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; tenantId?: string ; timestamp: number ; traceId?: string }>

Defined in

core/Service/Service.impl.ts:426open in new window


executeSubscription

executeSubscription(message, subscriptionName): Promise<undefined | Omit<{ contentEncoding: string ; contentType: string ; correlationId?: string ; eventName: string ; id: string ; messageType: CustomMessage ; otp?: string ; payload?: unknown ; principalId?: string ; receiver?: EBMessageAddress ; sender: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?: string ; timestamp: number ; traceId?: string }, "id" | "timestamp">>

Parameters

NameType
messageReadonly<EBMessage>
subscriptionNamestring

Returns

Promise<undefined | Omit<{ contentEncoding: string ; contentType: string ; correlationId?: string ; eventName: string ; id: string ; messageType: CustomMessage ; otp?: string ; payload?: unknown ; principalId?: string ; receiver?: EBMessageAddress ; sender: { serviceName: string; serviceVersion: string; serviceTarget: string; instanceId: string; } ; tenantId?: string ; timestamp: number ; traceId?: string }, "id" | "timestamp">>

Defined in

core/Service/Service.impl.ts:587open in new window


getContextFunctions

getContextFunctions(logger): ContextBase

Parameters

NameType
loggerLogger

Returns

ContextBase

Defined in

core/Service/Service.impl.ts:253open in new window


getEmitFunction

getEmitFunction(serviceTarget, traceId, principalId?, tenantId?): <Payload>(eventName: string, eventPayload?: Payload, contentType: string, contentEncoding: string) => Promise<void>

Parameters

NameType
serviceTargetstring
traceIdstring
principalId?string
tenantId?string

Returns

fn

▸ <Payload>(eventName, eventPayload?, contentType?, contentEncoding?): Promise<void>

Type parameters
Name
Payload
Parameters
NameTypeDefault value
eventNamestringundefined
eventPayload?Payloadundefined
contentTypestring'application/json'
contentEncodingstring'utf-8'
Returns

Promise<void>

Defined in

core/Service/Service.impl.ts:218open in new window


getInvokeFunction

getInvokeFunction(serviceTarget, traceId, principalId?, tenantId?): (receiver: EBMessageAddress, eventPayload: unknown, parameter: unknown, contentType: string, contentEncoding: string) => Promise<any>

Parameters

NameType
serviceTargetstring
traceIdstring
principalId?string
tenantId?string

Returns

fn

▸ (receiver, eventPayload, parameter, contentType?, contentEncoding?): Promise<any>

Parameters
NameTypeDefault value
receiverEBMessageAddressundefined
eventPayloadunknownundefined
parameterunknownundefined
contentTypestring'application/json'
contentEncodingstring'utf-8'
Returns

Promise<any>

Defined in

core/Service/Service.impl.ts:182open in new window


getTracer

getTracer(name?, version?): Tracer

Returns open telemetry tracer of this service

Parameters

NameType
name?string
version?string

Returns

Tracer

Tracer

Implementation of

ServiceClass.getTracer

Inherited from

ServiceBaseClass.getTracer

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:103open in new window


initializeEventbridgeConnect

initializeEventbridgeConnect(commandDefinitionList, subscriptions): Promise<void>

Connect service to event bridge to receive commands and command responses

Parameters

NameType
commandDefinitionListCommandDefinitionList<any>
subscriptionsSubscriptionDefinition[]

Returns

Promise<void>

Defined in

core/Service/Service.impl.ts:128open in new window


off

off<K>(eventName, fn): void

Type parameters

NameType
Kextends EventKey<ServiceEvents>

Parameters

NameType
eventNameK
fnEventReceiver<ServiceEvents[K]>

Returns

void

Inherited from

ServiceBaseClass.off

Defined in

core/types/GenericEventEmitter.ts:20open in new window


on

on<K>(eventName, fn): void

Type parameters

NameType
Kextends EventKey<ServiceEvents>

Parameters

NameType
eventNameK
fnEventReceiver<ServiceEvents[K]>

Returns

void

Inherited from

ServiceBaseClass.on

Defined in

core/types/GenericEventEmitter.ts:16open in new window


registerCommand

registerCommand(commandDefinition): Promise<void>

Registers a new command for the service

Parameters

NameTypeDescription
commandDefinitionCommandDefinitionthe service command definition

Returns

Promise<void>

Implementation of

ServiceClass.registerCommand

Defined in

core/Service/Service.impl.ts:560open in new window


registerSubscription

registerSubscription(subscriptionDefinition): Promise<void>

Registers a new subscription for the service

Parameters

NameTypeDescription
subscriptionDefinitionSubscriptionDefinitionthe subscription definition

Returns

Promise<void>

Implementation of

ServiceClass.registerSubscription

Defined in

core/Service/Service.impl.ts:749open in new window


removeAllListeners

removeAllListeners(): void

Returns

void

Inherited from

ServiceBaseClass.removeAllListeners

Defined in

core/types/GenericEventEmitter.ts:28open in new window


sendServiceInfo

sendServiceInfo(infoType, target?, payload?): Promise<Readonly<EBMessage>>

Broadcast service info message

Parameters

NameTypeDescription
infoTypeInfoMessageTypetype of info message
target?stringfunction name is need in messages like InfoServiceFunctionAdded
payload?Record<string, unknown>-

Returns

Promise<Readonly<EBMessage>>

Defined in

core/Service/Service.impl.ts:162open in new window


start

start(): Promise<void>

It connects to the event bridge and subscribes to the topics that are in the subscription list.

Returns

Promise<void>

Implementation of

ServiceClass.start

Defined in

core/Service/Service.impl.ts:107open in new window


startActiveSpan

startActiveSpan<F>(name, opts, context?, fn): Promise<F>

Start a child span for opentelemetry tracking

Type parameters

Name
F

Parameters

NameTypeDefault valueDescription
namestringundefinedname of span
optsSpanOptionsundefinedspan options
contextundefined | Contextundefinedoptional context
fn(span: Span) => Promise<F>undefinedfunction to be executed within the span

Returns

Promise<F>

return value of fn

Implementation of

ServiceClass.startActiveSpan

Inherited from

ServiceBaseClass.startActiveSpan

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:118open in new window


wrapInSpan

wrapInSpan<F>(name, opts, fn, context?): Promise<F>

Start span for opentelemetry tracking on same level. The created span will not become the "active" span within opentelemetry!

This means during logging and similar the spanId of parent span is logged.

Use wrapInSpan for marking points in flow of one bigger function, but not to trace the program flow itself

Type parameters

Name
F

Parameters

NameTypeDescription
namestringname of span
optsSpanOptionsspan options
fn(span: Span) => Promise<F>function te be executed in the span
context?Contextspan context

Returns

Promise<F>

return value of fn

Implementation of

ServiceClass.wrapInSpan

Inherited from

ServiceBaseClass.wrapInSpan

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:168open in new window

Last update:
Contributors: Sebastian Wessel