@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 Name | Description |
|---|---|
command | Sends an aggregate command to the backend. |
query | Queries a Read Model. |
getStaticAssetUrl | Gets a static file's full URL. |
getOriginPath | Returns an absolute URL within the application for the given relative path. |
subscribe | Subscribes to View Model updates. |
unsubscribe | Unsubscribes from View Model updates. |
commandā
Sends an aggregate command to the backend.
Argumentsā
| Argument Name | Description |
|---|---|
cmd | An object that describes a command to send to the server. |
options | An object that contains additional options for command execution. |
callback | A 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 Name | Description |
|---|---|
qr | An object that describes a query. |
options | An object that contains additional query options. |
callback | A 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 Name | Description |
|---|---|
assetPath | A 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 Name | Description |
|---|---|
path | A 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 Name | Description |
|---|---|
url | A URL used to establish a WebSocket connection to a view model. |
cursor | The data cursor used to traverse the events included into the query result set. |
viewModelName | A string that specifies the name of a view model. |
aggregateIds | A list of aggregate IDs for which to receive events. |
handler | A function that handles incoming events. |
subscribeCallback | A callback called on a successful subscription or an error. |
resubscribeCallback | A 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 Name | Description |
|---|---|
subscription | An object returned by the subscribe function. |
Exampleā
await client.unsubscribe(subscription)