Skip to content

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

typescript
class MyService extends Service {

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

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

Type parameters

NameType
ConfigTypeunknown

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:91

Properties

commandDefinitionList

commandDefinitionList: CommandDefinitionListResolved<any>

Defined in

core/Service/Service.impl.ts:85


commands

Protected commands: Map<string, CommandDefinition>

Defined in

core/Service/Service.impl.ts:83


config

config: ConfigType

Implementation of

ServiceClass.config

Defined in

core/Service/Service.impl.ts:87


configSchema

Protected configSchema: undefined | Schema<any, any, any, ""> | ZodType<any, ZodTypeDef, any>

Inherited from

ServiceBaseClass.configSchema

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:42


configStore

Protected configStore: ConfigStore

Inherited from

ServiceBaseClass.configStore

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:39


eventBridge

Protected eventBridge: EventBridge

Inherited from

ServiceBaseClass.eventBridge

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:30


info

Readonly info: ServiceInfoType

Inherited from

ServiceBaseClass.info

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:28


isStarted

isStarted: boolean = false

Defined in

core/Service/Service.impl.ts:89


logger

Protected logger: Logger

Inherited from

ServiceBaseClass.logger

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:32


secretStore

Protected secretStore: SecretStore

Inherited from

ServiceBaseClass.secretStore

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:38


spanProcessor

spanProcessor: undefined | SpanProcessor

Inherited from

ServiceBaseClass.spanProcessor

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:34


stateStore

Protected stateStore: StateStore

Inherited from

ServiceBaseClass.stateStore

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:40


subscriptionDefinitionList

subscriptionDefinitionList: SubscriptionDefinitionListResolved<any>

Defined in

core/Service/Service.impl.ts:86


subscriptions

Protected subscriptions: Map<string, SubscriptionDefinition>

Defined in

core/Service/Service.impl.ts:82


traceProvider

traceProvider: NodeTracerProvider

Inherited from

ServiceBaseClass.traceProvider

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:36

Accessors

name

get name(): string

Returns

string

Defined in

core/Service/Service.impl.ts:108


serviceInfo

get serviceInfo(): ServiceInfoType

Get service info

Returns

ServiceInfoType

Inherited from

ServiceBaseClass.serviceInfo

Defined in

core/Service/ServiceBaseClass/ServiceBaseClass.impl.ts:102

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:1056


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:24


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: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: 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: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: 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: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; sender: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: 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:597


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: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: 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: { instanceId: string ; serviceName: string ; serviceTarget: string ; serviceVersion: string } ; tenantId?: string ; timestamp: number ; traceId?: string }, "id" | "timestamp">>

Defined in

core/Service/Service.impl.ts:814


getContextFunctions

getContextFunctions(logger): ContextBase

Parameters

NameType
loggerLogger

Returns

ContextBase

Defined in

core/Service/Service.impl.ts:424


getEmitFunction

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

Type parameters

NameType
EmitList{}

Parameters

NameType
serviceTargetstring
traceId?string
principalId?string
tenantId?string
emitList?EmitList

Returns

fn

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

Type parameters
NameType
Kextends string | number | symbol
PayloadEmitList[K]
Parameters
NameTypeDefault value
eventNameKundefined
eventPayload?Payloadundefined
contentTypestring'application/json'
contentEncodingstring'utf-8'
Returns

Promise<void>

Defined in

core/Service/Service.impl.ts:346


getInvokeFunction

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

Parameters

NameType
serviceTargetstring
traceId?string
principalId?string
tenantId?string
invokes?Record<string, Record<string, Record<string, { outputSchema?: Schema<any, any, any, ""> | ZodType<any, ZodTypeDef, any> ; parameterSchema?: Schema<any, any, any, ""> | ZodType<any, ZodTypeDef, any> ; payloadSchema?: Schema<any, any, any, ""> | ZodType<any, ZodTypeDef, any> }>>>

Returns

fn

▸ (receiver, invokePayload, invokeparameter, contentType?, contentEncoding?): Promise<any>

Parameters
NameTypeDefault value
receiverEBMessageAddressundefined
invokePayloadunknownundefined
invokeparameterunknownundefined
contentTypestring'application/json'
contentEncodingstring'utf-8'
Returns

Promise<any>

Defined in

core/Service/Service.impl.ts:208


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:111


initializeEventbridgeConnect

initializeEventbridgeConnect(commandDefinitionList, subscriptions): Promise<void>

Connect service to event bridge to receive commands and command responses

Parameters

NameType
commandDefinitionListCommandDefinitionListResolved<any>
subscriptionsSubscriptionDefinitionListResolved<any>

Returns

Promise<void>

Defined in

core/Service/Service.impl.ts:154


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:20


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:16


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:787


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:1020


removeAllListeners

removeAllListeners(): void

Returns

void

Inherited from

ServiceBaseClass.removeAllListeners

Defined in

core/types/GenericEventEmitter.ts:28


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:188


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:115


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:126


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:176