Skip to content

PURISTA API


PURISTA API / @purista/core / ServiceBuilder

Class: ServiceBuilder<S>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:67

This class is used to build a service. The ServiceBuilder class is used to build a service. It has a few methods that are used to add command definitions and subscription definitions to the service. It also has a method that is used to create an instance of the service class.

Type Parameters

S

S extends ServiceBuilderTypes = ServiceBuilderTypes

Constructors

Constructor

new ServiceBuilder<S>(info): ServiceBuilder<S>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:86

Parameters

info

ServiceInfoType

Returns

ServiceBuilder<S>

Properties

info

info: ServiceInfoType

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:86


SClass

SClass: Newable<S["ServiceClassType"], ServiceClassTypes<S["ConfigType"], S["Resources"]>> = Service

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:83

Methods

addCommandDefinition()

addCommandDefinition(...commands): ServiceBuilder<S>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:136

addCommandDefinition adds a list of command definitions to the service builder

Parameters

commands

...CommandDefinitionList<S["ServiceClassType"]>

CommandDefinitionList

Returns

ServiceBuilder<S>

The service builder


addSubscriptionDefinition()

addSubscriptionDefinition(...subscription): ServiceBuilder<S>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:152

It adds a subscription definition to the service builder

Parameters

subscription

...SubscriptionDefinitionList<S["ServiceClassType"]>

SubscriptionDefinitionList

Returns

ServiceBuilder<S>

The service builder


defineResource()

defineResource<ResourceName, ResourcesType>(): ServiceBuilder<SetNewTypeValue<S, "Resources", S["Resources"] & { [K in string]: InstanceOrType<ResourcesType> }>>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:204

Define the resources of the service. Resources are available within commands and subscriptions.

Type Parameters

ResourceName

ResourceName extends string

ResourcesType

ResourcesType

Returns

ServiceBuilder<SetNewTypeValue<S, "Resources", S["Resources"] & { [K in string]: InstanceOrType<ResourcesType> }>>

The builder with defined types for resources

Example

ts
serviceBuilder.defineResources<'resource_name',ResourceType>()

getCommandBuilder()

getCommandBuilder<T, N>(commandName, description, eventName?): CommandDefinitionBuilder<S["ServiceClassType"], CommandDefinitionBuilderTypes<Schema, Schema, Schema, Schema, Schema, Schema, S["Resources"], InvokeList, Record<string, Schema>>>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:335

Create a CommandDefinitionBuilder for defining a command of this service.

Type Parameters

T

T extends string

N

N extends string

Parameters

commandName

NonEmptyString<T>

The name of the command to create.

description

string

A short description of what the command does.

eventName?

NonEmptyString<N>

Optional event name emitted on success.

Returns

CommandDefinitionBuilder<S["ServiceClassType"], CommandDefinitionBuilderTypes<Schema, Schema, Schema, Schema, Schema, Schema, S["Resources"], InvokeList, Record<string, Schema>>>

A new command builder instance

Example

ts
const cmd = serviceBuilder.getCommandBuilder('login', 'Authenticate user')
  .addPayloadSchema(z.object({ user: z.string() }))
  .setCommandFunction(async function () {})

getCommandDefinitions()

getCommandDefinitions(): CommandDefinitionListResolved<S["ServiceClassType"]>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:387

Returns

CommandDefinitionListResolved<S["ServiceClassType"]>

the definition of registered commands


getCustomClass()

getCustomClass(): Newable<S["ServiceClassType"], ServiceClassTypes<S["ConfigType"], S["Resources"]>>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:228

Get the service class used when creating instances.

Returns

Newable<S["ServiceClassType"], ServiceClassTypes<S["ConfigType"], S["Resources"]>>

The constructor function of the service


getFullServiceDefinition()

getFullServiceDefinition(): Promise<{ commands: CommandDefinitionListResolved<S["ServiceClassType"]>; deprecated: boolean; serviceDescription: string; serviceName: string; serviceVersion: string; subscriptions: SubscriptionDefinitionListResolved<S["ServiceClassType"]>; }>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:461

Returns the service definition. This includes information about commands and subscriptions.

Returns

Promise<{ commands: CommandDefinitionListResolved<S["ServiceClassType"]>; deprecated: boolean; serviceDescription: string; serviceName: string; serviceVersion: string; subscriptions: SubscriptionDefinitionListResolved<S["ServiceClassType"]>; }>


getInstance()

getInstance(eventBridge, options?): Promise<S["ServiceClassType"]>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:249

Instantiate the service with the provided EventBridge and optional configuration.

All command and subscription definitions are resolved automatically before the instance is created.

Parameters

eventBridge

EventBridge

The event bridge implementation to use.

options?

{ [K in string | number | symbol]: ({ configStore?: ConfigStore; logger?: Logger; logLevel?: LogLevelName; secretStore?: SecretStore; spanProcessor?: SpanProcessor; stateStore?: StateStore } & (keyof S["Resources"] extends NeverObject ? { resources?: undefined } : { resources: S["Resources"] }) & (keyof S["ConfigInputType"] extends NeverObject ? { serviceConfig?: undefined } : { serviceConfig?: S["ConfigInputType"] }))[K] }

Optional configuration such as logger or stores.

Returns

Promise<S["ServiceClassType"]>

The initialized service instance

Example

ts
const svc = await serviceBuilder.getInstance(eventBridge, { logger })
svc.start()

getSubscriptionBuilder()

getSubscriptionBuilder<T>(subscriptionName, description): SubscriptionDefinitionBuilder<S["ServiceClassType"], SubscriptionDefinitionBuilderTypes<any, any, any, any, any, any, S["Resources"]>>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:371

Create a SubscriptionDefinitionBuilder for defining a subscription.

Type Parameters

T

T extends string

Parameters

subscriptionName

NonEmptyString<T>

The name of the subscription.

description

string

A human readable description.

Returns

SubscriptionDefinitionBuilder<S["ServiceClassType"], SubscriptionDefinitionBuilderTypes<any, any, any, any, any, any, S["Resources"]>>

A subscription builder instance

Example

ts
const sub = serviceBuilder
  .getSubscriptionBuilder('userCreated', 'React on user creation')
  .subscribeToEvent('UserCreated')

getSubscriptionDefinitions()

getSubscriptionDefinitions(): SubscriptionDefinitionListResolved<S["ServiceClassType"]>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:400

Returns

SubscriptionDefinitionListResolved<S["ServiceClassType"]>

the definition of registered subscriptions


markAsDeprecated()

markAsDeprecated(): ServiceBuilder<S>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:126

Mark this service as deprecated

Returns

ServiceBuilder<S>

The ServiceBuilder instance


resolveDefinitions()

resolveDefinitions(): Promise<{ commands: CommandDefinitionListResolved<S["ServiceClassType"]>; subscriptions: SubscriptionDefinitionListResolved<S["ServiceClassType"]>; }>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:172

Resolve all added command and subscription definitions.

The resolved definitions are cached and subsequent calls will return the cached result. No new definitions can be added after this method was executed.

Returns

Promise<{ commands: CommandDefinitionListResolved<S["ServiceClassType"]>; subscriptions: SubscriptionDefinitionListResolved<S["ServiceClassType"]>; }>

The resolved command and subscription definitions


setConfigSchema()

setConfigSchema<T>(schema): ServiceBuilder<SetNewTypeValues<S, { ConfigInputType: InferIn<T> extends Record<string, unknown> ? InferIn<InferIn<T>> : NeverObject; ConfigType: Infer<T> extends Record<string, unknown> ? Infer<Infer<T>> : NeverObject; ServiceClassType: Service<ServiceClassTypes<Infer<T> extends Record<string, unknown> ? Infer<Infer<T>> : EmptyObject, S["Resources"]>>; }>>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:94

"This function sets the config schema for the service builder."

Type Parameters

T

T extends Schema

Parameters

schema

T

The schema that will be used to validate the config.

Returns

ServiceBuilder<SetNewTypeValues<S, { ConfigInputType: InferIn<T> extends Record<string, unknown> ? InferIn<InferIn<T>> : NeverObject; ConfigType: Infer<T> extends Record<string, unknown> ? Infer<Infer<T>> : NeverObject; ServiceClassType: Service<ServiceClassTypes<Infer<T> extends Record<string, unknown> ? Infer<Infer<T>> : EmptyObject, S["Resources"]>>; }>>

ServiceBuilder


setCustomClass()

setCustomClass<T>(customClass): ServiceBuilder<SetNewTypeValue<S, "ServiceClassType", T>>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:216

It sets the class type of the service.

Type Parameters

T

T extends Service<ServiceClassTypes<S["ConfigType"], S["Resources"]>>

Parameters

customClass

Newable<T, ServiceClassTypes<S["ConfigType"], S["Resources"]>>

A class which extends the Service class

Returns

ServiceBuilder<SetNewTypeValue<S, "ServiceClassType", T>>

The builder itself, but with the type of the service class changed.


setDefaultConfig()

setDefaultConfig(config): this

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:117

"This function sets the default configuration for the service."

Parameters

config

Complete<S["ConfigType"]>

ConfigType - The default configuration for the service.

Returns

this

The ServiceBuilder instance

Deprecated

Use a default value in the config validation schema instead


testServiceSetup()

testServiceSetup(): Promise<boolean>

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:422

Simple helper to verify the service configuration during tests.

It resolves the definitions and checks for duplicated command or subscription names. Useful to ensure that your service setup is correct before starting it.

Returns

Promise<boolean>

Example

ts
await serviceBuilder.testServiceSetup()

validateCommandDefinitions()

validateCommandDefinitions(): void

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:486

Returns

void

Deprecated

Use testServiceSetup() instead


validateCommands()

protected validateCommands(commandDefinitions): void

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:431

Parameters

commandDefinitions

CommandDefinitionListResolved<any>

Returns

void


validateSubscriptionDefinitions()

validateSubscriptionDefinitions(): void

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:494

Returns

void

Deprecated

Use testServiceSetup() instead


validateSubscriptions()

protected validateSubscriptions(subscriptionDefinitions): void

Defined in: ServiceBuilder/ServiceBuilder.impl.ts:471

Parameters

subscriptionDefinitions

SubscriptionDefinitionListResolved<any>

Returns

void