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
Name | Type |
---|---|
ConfigType | unknown |
Hierarchy
ServiceBaseClass
↳
Service
Implements
ServiceClass
<ConfigType
>
Table of contents
Constructors
Properties
- commandDefinitionList
- commands
- config
- configSchema
- configStore
- eventBridge
- info
- isStarted
- logger
- secretStore
- spanProcessor
- stateStore
- subscriptionDefinitionList
- subscriptions
- traceProvider
Accessors
Methods
- destroy
- emit
- executeCommand
- executeSubscription
- getContextFunctions
- getEmitFunction
- getInvokeFunction
- getTracer
- initializeEventbridgeConnect
- off
- on
- registerCommand
- registerSubscription
- removeAllListeners
- sendServiceInfo
- start
- startActiveSpan
- wrapInSpan
Constructors
constructor
• new Service<ConfigType
>(config
): Service
<ConfigType
>
Type parameters
Name | Type |
---|---|
ConfigType | unknown |
Parameters
Name | Type |
---|---|
config | ServiceConstructorInput <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
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
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
Overrides
ServiceBaseClass.destroy
Defined in
core/Service/Service.impl.ts:1056
emit
▸ emit<K
>(eventName
, parameter?
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <ServiceEvents > |
Parameters
Name | Type |
---|---|
eventName | K |
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
Name | Type |
---|---|
message | Readonly <{ 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
Name | Type |
---|---|
message | Readonly <EBMessage > |
subscriptionName | string |
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
Name | Type |
---|---|
logger | Logger |
Returns
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
Name | Type |
---|---|
EmitList | {} |
Parameters
Name | Type |
---|---|
serviceTarget | string |
traceId? | string |
principalId? | string |
tenantId? | string |
emitList? | EmitList |
Returns
fn
▸ <K
, Payload
>(eventName
, eventPayload?
, contentType?
, contentEncoding?
): Promise
<void
>
Type parameters
Name | Type |
---|---|
K | extends string | number | symbol |
Payload | EmitList [K ] |
Parameters
Name | Type | Default value |
---|---|---|
eventName | K | undefined |
eventPayload? | Payload | undefined |
contentType | string | 'application/json' |
contentEncoding | string | '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
Name | Type |
---|---|
serviceTarget | string |
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
Name | Type | Default value |
---|---|---|
receiver | EBMessageAddress | undefined |
invokePayload | unknown | undefined |
invokeparameter | unknown | undefined |
contentType | string | 'application/json' |
contentEncoding | string | '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
Name | Type |
---|---|
name? | string |
version? | string |
Returns
Tracer
Tracer
Implementation of
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
Name | Type |
---|---|
commandDefinitionList | CommandDefinitionListResolved <any > |
subscriptions | SubscriptionDefinitionListResolved <any > |
Returns
Promise
<void
>
Defined in
core/Service/Service.impl.ts:154
off
▸ off<K
>(eventName
, fn
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <ServiceEvents > |
Parameters
Name | Type |
---|---|
eventName | K |
fn | EventReceiver <ServiceEvents [K ]> |
Returns
void
Inherited from
ServiceBaseClass.off
Defined in
core/types/GenericEventEmitter.ts:20
on
▸ on<K
>(eventName
, fn
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <ServiceEvents > |
Parameters
Name | Type |
---|---|
eventName | K |
fn | EventReceiver <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
Name | Type | Description |
---|---|---|
commandDefinition | CommandDefinition | the service command definition |
Returns
Promise
<void
>
Implementation of
Defined in
core/Service/Service.impl.ts:787
registerSubscription
▸ registerSubscription(subscriptionDefinition
): Promise
<void
>
Registers a new subscription for the service
Parameters
Name | Type | Description |
---|---|---|
subscriptionDefinition | SubscriptionDefinition | the 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
Name | Type | Description |
---|---|---|
infoType | InfoMessageType | type of info message |
target? | string | function 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
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
Name | Type | Default value | Description |
---|---|---|---|
name | string | undefined | name of span |
opts | SpanOptions | undefined | span options |
context | undefined | Context | undefined | optional context |
fn | (span : Span ) => Promise <F > | undefined | function to be executed within the span |
Returns
Promise
<F
>
return value of fn
Implementation of
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
Name | Type | Description |
---|---|---|
name | string | name of span |
opts | SpanOptions | span options |
fn | (span : Span ) => Promise <F > | function te be executed in the span |
context? | Context | span context |
Returns
Promise
<F
>
return value of fn
Implementation of
Inherited from
ServiceBaseClass.wrapInSpan