Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisit client API docs #2145

Merged
merged 21 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/api/client/entry-point.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Client Entry Point
description: The entry point is a function that is the first to be called when the client script runs.
---

The entry point is a function that is the first to be called when the client script runs. It takes a reSolve context object as a parameter.
The entry point is a function that is called first when the client script runs. It takes a reSolve context object as a parameter.

##### client/index.js:

Expand All @@ -17,4 +17,4 @@ export default main

The `resolveContext` object contains data used internally by reSolve client libraries to communicate with the backend.

See the [Client Application Entry Point](frontend.md#client-application-entry-point) section of the [Frontend](frontend.md) article for more information.
See the [Client Application Entry Point](../../frontend.md#client-application-entry-point) section of the [Frontend](frontend.md) article for more information.
17 changes: 17 additions & 0 deletions docs/api/client/http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,20 @@ const queryViewModel = async (viewModelName, aggregateIds, jwt) => {
return await res.json()
}
```

## Authorization

To authorize your request on a reSolve server, specify a Bearer Token in your request's Authorization header:

```js
await fetch(Url, {
...
headers: {
Authorization: `Bearer ${jwt}`,
...
},
...
})
```

See the [Authentication and Authorization](../../authentication-and-authorization.md) topic for information on how authorization is handled on the server.
95 changes: 83 additions & 12 deletions docs/api/client/resolve-client.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
id: resolve-client
title: '@resolve-js/client'
description: The @resolve-js/client library provides an interface that you can use to communicate with the reSolve backend from JavaScript code.
description: The @resolve-js/client library exposes an interface that you can use to communicate with the reSolve backend from JavaScript code.
---

The **@resolve-js/client** library provides 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:
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:

```js
import { getClient } from '@resolve-js/client'
Expand All @@ -29,19 +29,44 @@ The `getClient` function takes a reSolve context as a parameter and returns an i

Sends an aggregate command to the backend.

##### Example
#### 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

```js
client.command(
{
aggregateName: 'Chat',
type: 'postMessage',
aggregateId: userName,
payload: message,
aggregateId: chatRoom,
payload: {
userName,
message,
},
},
{
middleware: {
error: [
createRetryOnErrorMiddleware({
attempts: 3,
errors: [500],
debug: true,
period: 500,
}),
],
},
},
(err) => {
if (err) {
console.warn(`Error while sending command: ${err}`)
console.warn(`Error sending a command: ${err}`)
}
}
)
Expand All @@ -51,7 +76,17 @@ client.command(

Queries a Read Model.

##### Example
#### 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

```js
const { data } = await client.query({
Expand All @@ -64,7 +99,15 @@ const { data } = await client.query({

Gets a static file's full URL.

##### Example
#### 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

```js
var imagePath = client.getStaticAssetUrl('/account/image.jpg')
Expand All @@ -74,17 +117,39 @@ var imagePath = client.getStaticAssetUrl('/account/image.jpg')

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

##### Example
#### 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

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

### subscribe

Subscribes to View Model updates. Returns a promise that resolves to a **subscription** object.
Subscribes to View Model updates.

#### Arguments

##### Example
| 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

```js
const chatViewModelUpdater = (event) => {
Expand All @@ -105,7 +170,13 @@ await client.subscribe('chat', '*', chatViewModelUpdater)

Unsubscribes from View Model updates.

##### Example
#### Arguments

| Argument Name | Description |
| ------------- | ----------------------------------------------------------- |
| subscription | An object returned by the [subscribe](#subscribe) function. |

#### Example

```js
await client.unsubscribe(subscription)
Expand Down
97 changes: 72 additions & 25 deletions docs/api/client/resolve-react-hooks.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
---
id: resolve-react-hooks
title: '@resolve-js/react-hooks'
description: The @resolve-js/react-hooks library provides React hooks that you can use to connect React components to a reSolve backend.
description: The @resolve-js/react-hooks library exposes React hooks that you can use to connect React components to a reSolve backend.
---

The **@resolve-js/react-hooks** library provides React hooks that you can use to connect React components to a reSolve backend. The following hooks are provided.
The **@resolve-js/react-hooks** library exposes React hooks that you can use to connect React components to a reSolve backend. The following hooks are included:

| Hook | Description |
| --------------------------------------- | ------------------------------------------------------------------------- |
| [useCommand](#usecommand) | Initializes a command that can be passed to the backend. |
| [useCommandBuilder](#usecommandbuilder) | Allows a component to generate commands based on input parameters. |
| [useViewModel](#useviewmodel) | Establishes a WebSocket connection to a reSolve View Model. |
| [useQuery](#usequery) | Allows a component to send queries to a reSolve Read Model or View Model. |
| [useOriginResolver](#useoriginresolver) | Resolves a relative path to an absolute URL within the application. |
| Hook | Description |
| --------------------------------------- | ------------------------------------------------------------------------------ |
| [useClient](#useclient) | Returns the [@resolve-js/client](resolve-client.md) library's `client` object. |
| [useCommand](#usecommand) | Initializes a command that can be passed to the backend. |
| [useCommandBuilder](#usecommandbuilder) | Allows a component to generate commands based on input parameters. |
| [useViewModel](#useviewmodel) | Establishes a WebSocket connection to a reSolve View Model. |
| [useQuery](#usequery) | Allows a component to send queries to a reSolve Read Model or View Model. |
| [useQueryBuilder](#usequerybuilder) | Allows a component to generate queries based on input parameters. |
| [useOriginResolver](#useoriginresolver) | Resolves a relative path to an absolute URL within the application. |
| [useStaticResolver](#usestaticresolver) | Resolves a relative path to a static resource's full URL. |

### useClient

Returns the [@resolve-js/client](resolve-client.md) library's `client` object.

#### Example

```js
const client = useClient()

client.command(
{
aggregateName: 'Chat',
type: 'postMessage',
aggregateId: userName,
payload: message,
},
(err) => {
if (err) {
console.warn(`Error while sending command: ${err}`)
}
}
)
```

### useCommand

Initializes a command that can be passed to the backend.

##### Example
#### Example

```js
const ShoppingList = ({
Expand Down Expand Up @@ -50,7 +77,7 @@ const ShoppingList = ({

Allows a component to generate commands based on input parameters.

##### Example
#### Example

```js
const ShoppingList = ({
Expand Down Expand Up @@ -89,7 +116,7 @@ const ShoppingList = ({

Establishes a WebSocket connection to a reSolve View Model.

##### Example
#### Example

```js
const ShoppingList = ({
Expand Down Expand Up @@ -130,7 +157,7 @@ const ShoppingList = ({

Allows a component to send queries to a reSolve Read Model or View Model.

##### Example
#### Example

```js
const MyLists = () => {
Expand All @@ -146,28 +173,48 @@ const MyLists = () => {
}, [])

...
}
```

onCreateSuccess={(err, result) => {
const nextLists = { ...lists }
nextLists.data.push({
name: result.payload.name,
createdAt: result.timestamp,
id: result.aggregateId
})
setLists(nextLists)
}}
### useQueryBuilder

...
}
Allows a component to generate queries based on input parameters.

#### Example

```js
const getPage = useQueryBuilder(
(page) => ({
name: 'MessageList',
resolver: 'paginated',
args: { page },
}),
(error, result) => {
setMessages(result.data)
}
)

useEffect(() => {
getPage(page)
}, [])
```

### useOriginResolver

Resolves a relative path to an absolute URL within the application.

##### Example
#### Example

```js
var resolver = useOriginResolver()
var commandApiPath = resolver('/api/commands')
```

### useStaticResolver

Resolves a relative path to a static resource's full URL.

```js
var staticResolver = useStaticResolver()
var imagePath = staticResolver('/account/image.jpg')
```
Loading