Skip to content

Commit 2154db3

Browse files
committed
docs: adds an article on how to execute requests
this also merges part information found in other articles
1 parent 20a72e7 commit 2154db3

File tree

4 files changed

+80
-42
lines changed

4 files changed

+80
-42
lines changed

16/umbraco-cms/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
* [Fetching Data](customizing/foundation/fetching-data/README.md)
187187
* [Fetch API](customizing/foundation/fetching-data/fetch-api.md)
188188
* [HTTP Client](customizing/foundation/fetching-data/http-client.md)
189+
* [Executing Requests](customizing/foundation/fetching-data/try-execute.md)
189190
* [Working with Data](customizing/foundation/working-with-data/README.md)
190191
* [Repositories](customizing/foundation/working-with-data/repositories.md)
191192
* [Context API](customizing/foundation/working-with-data/context-api.md)

16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ The above example serves to illustrate some of the process to make a request to
114114

115115
Regardless of method, you can execute the fetch requests through Umbraco's [tryExecute](https://apidocs.umbraco.com/v16/ui-api/classes/packages_core_auth.UmbAuthContext.html#tryexecute) function. This function will handle any errors that occur during the request and will automatically refresh the token if it is expired. If the session is expired, the function will also make sure the user logs in again.
116116

117+
**Example:**
118+
117119
```javascript
118120
import { tryExecute } from '@umbraco-cms/backoffice/resources';
119121

@@ -127,12 +129,14 @@ if (response.error) {
127129
}
128130
```
129131

130-
{% hint style="info" %}
131-
The above example requires a host element illustrated by the use of `this`. This is typically a custom element that extends the `UmbLitElement` class.
132-
{% endhint %}
132+
You can read more about the `tryExecute` function in this article:
133+
134+
{% content-ref url="../try-execute.md" %}
135+
[try-execute.md](../try-execute.md)
136+
{% endcontent-ref %}
133137

134138
## Conclusion
135139

136140
The Fetch API is a powerful and flexible way to make network requests in JavaScript. It is available in all modern browsers and is the recommended way to make network requests in JavaScript. The Fetch API can be used in Umbraco to make network requests to the server. It can also be used to make requests to the Management API controllers. You can use the Fetch API to make requests to any endpoint in the Management API. You can also use it to handle responses in a variety of formats. This is especially useful, if you have but a few requests to make.
137141

138-
However, if you have a lot of requests to make, you might want to consider an alternative approach. You could use a library like [@hey-api/openapi-ts](https://heyapi.dev/openapi-ts/get-started) to generate a TypeScript client. The library requires an OpenAPI definition and allows you to make requests to the Management API without having to manually write the requests yourself. The generated client will only need the token once. This can save you a lot of time and effort when working with the Management API. The Umbraco Backoffice itself is running with this library and even exports its internal HTTP client.
142+
However, if you have a lot of requests to make, you might want to consider an alternative approach. You could use a library like [@hey-api/openapi-ts](https://heyapi.dev/openapi-ts/get-started) to generate a TypeScript client. The library requires an OpenAPI definition and allows you to make requests to the Management API without having to manually write the requests yourself. The generated client will only need the token once. This can save you a lot of time and effort when working with the Management API. The Umbraco Backoffice itself is running with this library and even exports its internal HTTP client. You can read more about this in the [HTTP Client](http-client.md) article.

16/umbraco-cms/customizing/foundation/fetching-data/http-client.md

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,19 @@ The above example shows how to use the HTTP client to make a GET request to the
2222

2323
The HTTP client automatically handles authentication and error handling, so you don't have to worry about those details. It also provides a convenient way to parse the response data as JSON.
2424

25-
## Executing the request
25+
## Using the HTTP Client
2626

27-
The Backoffice also provides a `tryExecute` function that you can use to execute requests. This function will additionally wrap the request in a try/catch block and handle any errors that occur during the request. It will also show a notification if the request fails.
27+
The HTTP client is a wrapper around the Fetch API that provides a more convenient way to make network requests. It handles things like request and response parsing, error handling, and retries. The HTTP client is available through the `@umbraco-cms/backoffice/http-client` package, which is included in the Umbraco Backoffice. You can use it to make requests to any endpoint in the Management API or to any other API.
2828

29-
```javascript
30-
import { umbHttpClient } from '@umbraco-cms/backoffice/http-client';
31-
import { tryExecute } from '@umbraco-cms/backoffice/resources';
32-
33-
const { data, error } = await tryExecute(this, umbHttpClient.get({
34-
url: '/umbraco/management/api/v1/server/status'
35-
}));
29+
The recommended approach to use the Umbraco HTTP Client is to use the `tryExecute` function. This function will handle any errors that occur during the request and will automatically refresh the token if it is expired. If the session is expired, the function will also make sure the user logs in again.
3630

37-
if (error) {
38-
console.error('There was a problem with the fetch operation:', error);
39-
} else {
40-
console.log(data); // Do something with the data
41-
}
42-
```
43-
44-
The `tryExecute` function takes the context of the current class or element as the first argument and the request as the second argument. Therefore, the above example can be used in any class or element that extends from either the [UmbController](https://apidocs.umbraco.com/v16/ui-api/interfaces/libs_controller-api.UmbController.html) or [UmbLitElement](https://apidocs.umbraco.com/v16/ui-api/classes/packages_core_lit-element.UmbLitElement.html) classes.
31+
You can read more about the `tryExecute` function in this article:
4532

46-
It is recommended to use the `tryExecute` function instead of the raw HTTP client. It can also be configured not to show notifications, if you want to handle errors yourself:
47-
48-
```javascript
49-
tryExecute(this, request, {
50-
disableNotifications: true,
51-
});
52-
```
53-
54-
### Cancelling requests
55-
56-
The HTTP client also supports cancelling requests. This is useful if you want to cancel a request that is taking too long or if the user navigates away from the page. You can cancel a request by using the [AbortController API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). The `AbortController` API is a built-in API in modern browsers that allows you to cancel requests. You can use it directly with tryExecute:
33+
{% content-ref url="../try-execute.md" %}
34+
[try-execute.md](../try-execute.md)
35+
{% endcontent-ref %}
5736

5837
```javascript
59-
const abortController = new AbortController();
60-
61-
// Cancel the request before starting it for illustration purposes
62-
abortController.abort();
63-
64-
tryExecute(this, request, {
65-
disableNotifications: true,
66-
abortSignal: abortController.signal,
67-
});
68-
```
6938

7039
## Custom Generated Client
7140

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
description:: Learn how to execute requests in the Backoffice.
3+
---
4+
5+
# Executing Requests
6+
7+
When you make a request to the server, you need to execute it. This can be done using the Fetch API or the Umbraco HTTP client. The Backoffice also provides a `tryExecute` function that you can use to execute requests. This function will handle any errors that occur during the request and will automatically refresh the token if it is expired. If the session is expired, the function will also make sure the user logs in again.
8+
9+
{% hint style="info" %}
10+
You can read the technical documentation for the `tryExecute` function in the [UI API Documentation](https://apidocs.umbraco.com/v16/ui-api/functions/packages_core_resources.tryExecute.html) class.
11+
{% endhint %}
12+
13+
## Using the HTTP Client
14+
15+
Here is an example of how to use the `tryExecute` function with the Umbraco HTTP client:
16+
17+
```javascript
18+
import { tryExecute } from '@umbraco-cms/backoffice/resources';
19+
import { umbHttpClient } from '@umbraco-cms/backoffice/http-client';
20+
21+
const { data, error } = await tryExecute(this, umbHttpClient.get({
22+
url: '/umbraco/management/api/v1/server/status'
23+
}));
24+
25+
if (error) {
26+
console.error('There was a problem with the fetch operation:', error);
27+
} else {
28+
console.log(data); // Do something with the data
29+
}
30+
```
31+
32+
The `tryExecute` function takes the context of the current class or element as the first argument and the request as the second argument. Therefore, the above example can be used in any class or element that extends from either the [UmbController](https://apidocs.umbraco.com/v16/ui-api/interfaces/libs_controller-api.UmbController.html) or [UmbLitElement](https://apidocs.umbraco.com/v16/ui-api/classes/packages_core_lit-element.UmbLitElement.html) classes.
33+
34+
{% hint style="info" %}
35+
The above example requires a host element illustrated by the use of `this`. This is typically a custom element that extends the `UmbLitElement` class.
36+
{% endhint %}
37+
38+
It is recommended to always use the `tryExecute` function to wrap HTTP requests.
39+
40+
### Disable Notifications
41+
42+
The `tryExecute` function will automatically show error bubbles if a request fails. There may be valid cases where you want handle errors yourself. This could for instance be if you want to show a custom error message. You can disable the notifications by passing the `disableNotifications` option to the `tryExecute` function:
43+
44+
```javascript
45+
tryExecute(this, request, {
46+
disableNotifications: true,
47+
});
48+
```
49+
50+
### Cancelling requests
51+
52+
The `tryExecute` function also supports cancelling requests. This is useful if you want to cancel a request that is taking too long or if the user navigates away from the page. You can cancel a request by using the [AbortController API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). The `AbortController` API is a built-in API in modern browsers that allows you to cancel requests. You can use it directly with tryExecute:
53+
54+
```javascript
55+
const abortController = new AbortController();
56+
57+
// Cancel the request before starting it for illustration purposes
58+
abortController.abort();
59+
60+
tryExecute(this, request, {
61+
disableNotifications: true,
62+
abortSignal: abortController.signal,
63+
});
64+
```

0 commit comments

Comments
 (0)