Skip to main content

@resolve-js/client

The @resolve-js/client library exposes an interface that you can use to communicate with the reSolve backend from JavaScript code. To initialize the client, call the library's getClient function:

import { getClient } from '@resolve-js/client'

const main = async resolveContext => {
const client = getClient(resolveContext)
...

The getClient function takes a reSolve context as a parameter and returns an initialized client object. This object exposes the following functions:

Function NameDescription
commandSends an aggregate command to the backend.
queryQueries a Read Model.
getStaticAssetUrlGets a static file's full URL.
getOriginPathReturns an absolute URL within the application for the given relative path.
subscribeSubscribes to View Model updates.
unsubscribeUnsubscribes from View Model updates.

commandā€‹

Sends an aggregate command to the backend.

Argumentsā€‹

Argument NameDescription
cmdAn object that describes a command to send to the server.
optionsAn object that contains additional options for command execution.
callbackA callback to call on the server response or error.

The returned value is a promise that resolves to the command execution result.

Exampleā€‹

client.command(
{
aggregateName: 'Chat',
type: 'postMessage',
aggregateId: chatRoom,
payload: {
userName,
message,
},
},
{
middleware: {
error: [
createRetryOnErrorMiddleware({
attempts: 3,
errors: [500],
debug: true,
period: 500,
}),
],
},
},
(err) => {
if (err) {
console.warn(`Error sending a command: ${err}`)
}
}
)

queryā€‹

Queries a Read Model.

Argumentsā€‹

Argument NameDescription
qrAn object that describes a query.
optionsAn object that contains additional query options.
callbackA callback to call on the server response or error.

The returned value is a promise that resolves to the query result.

Exampleā€‹

const { data } = await client.query({
name: 'chat',
aggregateIds: '*',
})

getStaticAssetUrlā€‹

Gets a static file's full URL.

Argumentsā€‹

Argument NameDescription
assetPathA string that specifies a relative URL path.

The returned value is a string that contains a full URL.

Exampleā€‹

var imagePath = client.getStaticAssetUrl('/account/image.jpg')

getOriginPathā€‹

Returns an absolute URL within the application for the given relative path.

Argumentsā€‹

Argument NameDescription
pathA string that specifies a relative URL path.

The returned value is a string that contains a full URL.

Exampleā€‹

var commandsApiPath = client.getOriginPath('/api/commands')

subscribeā€‹

Subscribes to View Model updates.

Argumentsā€‹

Argument NameDescription
urlA URL used to establish a WebSocket connection to a view model.
cursorThe data cursor used to traverse the events included into the query result set.
viewModelNameA string that specifies the name of a view model.
aggregateIdsA list of aggregate IDs for which to receive events.
handlerA function that handles incoming events.
subscribeCallbackA callback called on a successful subscription or an error.
resubscribeCallbackA callback called on a successful resubscription or an error.

The returned value is a promise that resolves to a subscription object.

Exampleā€‹

const chatViewModelUpdater = (event) => {
const eventType = event != null && event.type != null ? event.type : null
const eventHandler = chatViewModel.projection[eventType]

if (typeof eventHandler === 'function') {
chatViewModelState = eventHandler(chatViewModelState, event)
}

setImmediate(updateUI.bind(null, chatViewModelState))
}

await client.subscribe('chat', '*', chatViewModelUpdater)

unsubscribeā€‹

Unsubscribes from View Model updates.

Argumentsā€‹

Argument NameDescription
subscriptionAn object returned by the subscribe function.

Exampleā€‹

await client.unsubscribe(subscription)