Skip to content

Commit 5661fcc

Browse files
committed
docs: updates code samples after testing
1 parent a129be6 commit 5661fcc

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ import type { UmbClassInterface } from '@umbraco-cms/backoffice/class-api';
9595
*/
9696
async function makeRequest(host: UmbClassInterface, url: string, method = 'GET', body?: any) {
9797
const authContext = await host.getContext(UMB_AUTH_CONTEXT);
98-
const token = await authContext.getLatestToken();
98+
const token = await authContext?.getLatestToken();
9999
const response = await fetch(url, {
100100
method,
101101
body: body ? JSON.stringify(body) : undefined,
@@ -115,25 +115,15 @@ The above example serves to illustrate some of the process to make a request to
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

117117
```javascript
118-
import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
119118
import { tryExecute } from '@umbraco-cms/backoffice/resources';
120119

121-
const authContext = await this.getContext(UMB_AUTH_CONTEXT);
122-
const token = await authContext.getLatestToken();
123-
const request = fetch(url, {
124-
method,
125-
body: body ? JSON.stringify(body) : undefined,
126-
headers: {
127-
'Content-Type': 'application/json',
128-
'Authorization': `Bearer ${token}`,
129-
},
130-
});
131-
const { data, error } = await tryExecute(this, request);
120+
const request = makeRequest(this, '/umbraco/management/api/v1/server/status');
121+
const response = await tryExecute(this, request);
132122

133-
if (error) {
123+
if (response.error) {
134124
console.error('There was a problem with the fetch operation:', error);
135125
} else {
136-
console.log(data); // Do something with the data
126+
console.log(response); // Do something with the data
137127
}
138128
```
139129

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Umbraco provides a built-in HTTP client that you can use to make network request
1010
import { umbHttpClient } from '@umbraco-cms/backoffice/http-client';
1111

1212
const { data } = await umbHttpClient.get({
13-
url: '/umbraco/api/management/v1/server/status'
13+
url: '/umbraco/management/api/v1/server/status'
1414
});
1515

1616
if (data) {
@@ -31,7 +31,7 @@ import { umbHttpClient } from '@umbraco-cms/backoffice/http-client';
3131
import { tryExecute } from '@umbraco-cms/backoffice/resources';
3232

3333
const { data, error } = await tryExecute(this, umbHttpClient.get({
34-
url: '/umbraco/api/management/v1/server/status'
34+
url: '/umbraco/management/api/v1/server/status'
3535
}));
3636

3737
if (error) {
@@ -43,6 +43,30 @@ if (error) {
4343

4444
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.
4545

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:
57+
58+
```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+
```
69+
4670
## Custom Generated Client
4771

4872
The HTTP client is generated using the [@hey-api/openapi-ts](https://heyapi.dev/openapi-ts/get-started) library. This library allows anyone to generate a TypeScript client from an OpenAPI specification. The generated client provides a convenient way to make requests to that specific API with type-safety without having to manually write the requests yourself. You can consider generating a client. This can save you a lot of time and effort when working with custom API controllers.
@@ -76,7 +100,7 @@ import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
76100
import { client } from './my-client/client.gen';
77101

78102
export const onInit = (host) => {
79-
host.consumeContext(UMB_AUTH_CONTEXT, async (authContext) => {
103+
host.consumeContext(UMB_AUTH_CONTEXT, (authContext) => {
80104
// Get the token info from Umbraco
81105
const config = authContext?.getOpenApiConfiguration();
82106

@@ -111,7 +135,7 @@ import { tryExecute } from '@umbraco-cms/backoffice/resources';
111135
import { umbHttpClient } from '@umbraco-cms/backoffice/http-client';
112136

113137
const { data } = await tryExecute(this, getMyControllerAction({
114-
client: umbHttpClient,
138+
client: umbHttpClient, // Use Umbraco's HTTP client
115139
}));
116140

117141
if (data) {

0 commit comments

Comments
 (0)