-
Notifications
You must be signed in to change notification settings - Fork 811
V16: Documents Fetch and http-client in the Backoffice #7083
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
beaf4d3
docs: adds article on working with the Fetch API
iOvergaard 05c9a9a
docs: adds article on http-client
iOvergaard a129be6
docs: adds overview and main README
iOvergaard 5661fcc
docs: updates code samples after testing
iOvergaard 20a72e7
docs: corrects wrong variable use
iOvergaard 2154db3
docs: adds an article on how to execute requests
iOvergaard bf64093
docs: adds reference to new article
iOvergaard 5d7e975
docs: removes warning
iOvergaard 590dfe5
docs: adds an article on generating your own custom client
iOvergaard 1b2687d
docs: adds bullets to why to choose the http-client
iOvergaard dd9ce4a
docs: name the client the same
iOvergaard ae00ae6
docs: clarify what a custom client is
iOvergaard dc0ad38
docs: clarify what the umbraco http client is
iOvergaard a5177c5
docs: adds an advanced topics section
iOvergaard f90e7c7
docs: adds examples on authentication
iOvergaard 1ad9cee
docs: adds extra bullet
iOvergaard 0666e56
docs: align naming of http client
iOvergaard d4a4236
docs: simplify example
iOvergaard d3e6d16
docs: adds warning about error handling
iOvergaard e60bde1
docs: adds links and other helpful tips
iOvergaard fa5af18
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
iOvergaard cf56998
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
iOvergaard c6a5c94
Apply suggestions from code review
iOvergaard 95e3067
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh ce084da
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh efca7a4
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh 8478a29
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh 1610574
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh fb99c21
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh 8e7b26a
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh 656054a
Update 16/umbraco-cms/customizing/foundation/fetching-data/try-execut…
eshanrnh ed25570
Update 16/umbraco-cms/customizing/foundation/fetching-data/try-execut…
eshanrnh 1e03647
Update 16/umbraco-cms/customizing/foundation/fetching-data/try-execut…
eshanrnh 0459273
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh a4d1712
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh a3b17c3
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh 150a085
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh 538ed00
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh a29574f
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh 3fceda7
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh 89e917f
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh 742d345
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh ed1df4f
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh 16dfc18
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh f957799
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh fcba065
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh 5822ddd
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh ebae84f
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh 7e796c8
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh e7fa87b
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh 97b8ac9
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh c91b4f9
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh ac48356
Update 16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
eshanrnh 7b4d4bf
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh 69b4b52
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh 8131140
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh 9eece0d
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh 9eefa33
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh 930d333
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh b90ba6a
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh 545c7cd
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh d4b5a1a
Update 16/umbraco-cms/customizing/foundation/fetching-data/http-clien…
eshanrnh 3dc3c9e
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh d88e2d2
Update 16/umbraco-cms/customizing/foundation/fetching-data/README.md
eshanrnh 682e137
Update 16/umbraco-cms/customizing/foundation/fetching-data/custom-gen…
eshanrnh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
16/umbraco-cms/customizing/foundation/fetching-data/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Fetching Data | ||
|
|
||
| Learn how to request data when extending the Backoffice. | ||
|
|
||
| ## [Fetch API](fetch-api.md) | ||
|
|
||
| The Fetch API is a modern way to make network requests in JavaScript. It provides a more powerful and flexible feature set than the older XMLHttpRequest. | ||
|
|
||
| ## [HTTP Client](http-client.md) | ||
|
|
||
| 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. | ||
|
|
||
| ## [Working with Data](../working-with-data/README.md) | ||
|
|
||
| Once you have the data using one of the methods above, you can read more about how to work with it here. | ||
138 changes: 138 additions & 0 deletions
138
16/umbraco-cms/customizing/foundation/fetching-data/fetch-api.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| --- | ||
| description: The Fetch API is a modern way to make network requests in JavaScript. It provides a more powerful and flexible feature set than the older XMLHttpRequest. | ||
| --- | ||
|
|
||
| # Fetch API | ||
|
|
||
| The [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) is a Promise-based API that allows you to make network requests similar to XMLHttpRequest. It is a modern way to make network requests in JavaScript and provides a more powerful and flexible feature set than the older XMLHttpRequest. It is available in all modern browsers and is the recommended way to make network requests in JavaScript. | ||
|
|
||
| ## Fetch API in Umbraco | ||
|
|
||
| The Fetch API can also be used in Umbraco to make network requests to the server. Since it is built into the browser, you do not need to install any additional libraries or packages to use it. The Fetch API is available in the global scope and can be used directly in your JavaScript code. | ||
| The Fetch API is a great way to make network requests in Umbraco because it provides a lot of flexibility. You can use it to make GET, POST, PUT, DELETE, and other types of requests to the server. You can also use it to handle responses in a variety of formats, including JSON, text, and binary data. | ||
|
|
||
| ### Example | ||
|
|
||
| For this example, we are using the Fetch API to make a GET request to the `/umbraco/MyApiController/GetData` endpoint. The response is then parsed as JSON and logged to the console. If there is an error with the request, it is caught and logged to the console: | ||
eshanrnh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```javascript | ||
| const data = await fetch('/umbraco/MyApiController/GetData') | ||
| .then(response => { | ||
| if (!response.ok) { | ||
| throw new Error('Network response was not ok'); | ||
| } | ||
| return response.json(); | ||
| }) | ||
| .catch(error => { | ||
| console.error('There was a problem with the fetch operation:', error); | ||
| }); | ||
|
|
||
| if (data) { | ||
| console.log(data); // Do something with the data | ||
| } | ||
| ``` | ||
|
|
||
| {% hint style="info" %} | ||
| The example assumes that you have a controller set up at the `/umbraco/MyApiController/GetData` endpoint that returns JSON data. You can replace this with your own endpoint as needed. Read more about creating a controller in the [Controllers](../../../implementation/controllers.md) article. | ||
| {% endhint %} | ||
eshanrnh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Authentication | ||
|
|
||
| When making requests to the Umbraco API controllers, you may need to include an authorization token in the request headers. This is especially important when making requests to endpoints that require authentication. | ||
|
|
||
| The Fetch API does not automatically include authentication tokens in requests. You need to manually add the authentication token to the request headers. This can be done by adding an `Authorization` header to the request: | ||
|
|
||
| ```javascript | ||
| const token = 'your-auth-token'; | ||
| const data = await fetch('/umbraco/MyApiController/GetData', { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Authorization': `Bearer ${token}`, | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| {% hint style="info" %} | ||
| The example assumes that you have a valid authentication token. You can replace this with your own token as needed. Read more about authentication in the [Security](../../../implementation/security.md) article. | ||
| {% endhint %} | ||
eshanrnh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Management API Controllers | ||
|
|
||
| The Fetch API can also be used to make requests to the Management API controllers. The Management API is a set of RESTful APIs that allow you to interact with Umbraco programmatically. You can use the Fetch API to make requests to the Management API controllers like you would with any other API. The Management API controllers are located in the `/umbraco/api/management` namespace. You can use the Fetch API to make requests to these controllers like you would with any other API. | ||
|
|
||
| ### API User | ||
|
|
||
| You can create an API user in Umbraco to authenticate requests to the Management API. This is useful for making requests from external applications or services. You can create an API user in the Umbraco backoffice by going to the Users section and creating a new user with the "API" role. Once you have created the API user, you can make requests to the Management API using the API user's credentials. You can find these in the Umbraco backoffice. | ||
|
|
||
| You can read more about this concept in the [API Users](../../../fundamentals/data/users/api-users.md) article. | ||
|
|
||
| ### Backoffice Token | ||
|
|
||
| The Fetch API can also be used to make requests to the Management API using a Backoffice token. This is useful for making requests from custom components that are running in the Backoffice. The concept is similar to the API Users, but the Backoffice token represents the current user in the Backoffice. You will share the access policies of the current user, so you can use the token to make requests on behalf of the current user. | ||
|
|
||
| To use the Backoffice access token, you will have to consume the **UMB_AUTH_CONTEXT** context. This context is only available in the Backoffice and includes tools to hook on to the authentication process. You can use the [getOpenApiConfiguration](https://apidocs.umbraco.com/v16/ui-api/classes/packages_core_auth.UmbAuthContext.html#getopenapiconfiguration) method to get a configuration object that includes a few useful properties: | ||
|
|
||
| - `base`: The base URL of the Management API. | ||
| - `credentials`: The credentials to use for the request. | ||
| - `token()`: A function that returns the current access token. | ||
|
|
||
| Read more about this in the [UmbOpenApiConfiguration](https://apidocs.umbraco.com/v16/ui-api/interfaces/packages_core_auth.UmbOpenApiConfiguration.html) interface. | ||
|
|
||
| It is rather tiresome to manually add the token to each request. Therefore, you can wrap the Fetch API in a custom function that automatically adds the token to the request headers. This way, you can use the Fetch API without worrying about adding the token manually: | ||
|
|
||
| ```typescript | ||
| import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth'; | ||
| import type { UmbClassInterface } from '@umbraco-cms/backoffice/class-api'; | ||
|
|
||
| /** | ||
| * Make an authorized request to any Backoffice API. | ||
| * @param host A reference to the host element that can request a context. | ||
| * @param url The URL to request. | ||
| * @param method The HTTP method to use. | ||
| * @param body The body to send with the request (if any). | ||
| * @returns The response from the request as JSON. | ||
| */ | ||
| async function makeRequest(host: UmbClassInterface, url: string, method = 'GET', body?: any) { | ||
| const authContext = await host.getContext(UMB_AUTH_CONTEXT); | ||
| const token = await authContext?.getLatestToken(); | ||
| const response = await fetch(url, { | ||
| method, | ||
| body: body ? JSON.stringify(body) : undefined, | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Authorization': `Bearer ${token}`, | ||
iOvergaard marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| }); | ||
| return response.json(); | ||
| } | ||
| ``` | ||
|
|
||
| The above example serves to illustrate some of the process to make a request to the Management API. You can use this function to make requests to any endpoint in the Management API. The function does not handle errors or responses, so you will need to add that logic yourself, nor does it handle the authentication process. If the token is timed out, you will get a 401 error back, if the `getLatestToken` method failed to refresh the token. | ||
eshanrnh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Executing the request | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| import { tryExecute } from '@umbraco-cms/backoffice/resources'; | ||
|
|
||
| const request = makeRequest(this, '/umbraco/management/api/v1/server/status'); | ||
| const response = await tryExecute(this, request); | ||
|
|
||
| if (response.error) { | ||
| console.error('There was a problem with the fetch operation:', error); | ||
| } else { | ||
| console.log(response); // Do something with the data | ||
| } | ||
| ``` | ||
|
|
||
| {% hint style="info" %} | ||
| The above example requires a host element illustrated by the use of `this`. This is typically a custom element that extends the `UmbLitElement` class. | ||
| {% endhint %} | ||
|
|
||
| ## Conclusion | ||
|
|
||
| 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. | ||
eshanrnh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
155 changes: 155 additions & 0 deletions
155
16/umbraco-cms/customizing/foundation/fetching-data/http-client.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| --- | ||
| description: Learn more about working with the HTTP Client in Umbraco. | ||
| --- | ||
|
|
||
| # HTTP Client | ||
|
|
||
| Umbraco provides a built-in HTTP client that you can use to make network requests. This client is generated using **@hey-api/openapi-ts** around the OpenAPI specification and is available through the `@umbraco-cms/backoffice/http-client` package. | ||
|
|
||
| ```javascript | ||
| import { umbHttpClient } from '@umbraco-cms/backoffice/http-client'; | ||
|
|
||
| const { data } = await umbHttpClient.get({ | ||
| url: '/umbraco/management/api/v1/server/status' | ||
| }); | ||
|
|
||
| if (data) { | ||
| console.log('Server status:', data); | ||
| } | ||
| ``` | ||
|
|
||
| The above example shows how to use the HTTP client to make a GET request to the Management API. The `umbHttpClient` object provides methods for making requests, including `get`, `post`, `put`, and `delete`. Each method takes an options object that specifies the URL, headers, and body of the request. | ||
|
|
||
| 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. | ||
|
|
||
| ## Executing the request | ||
|
|
||
| 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. | ||
|
|
||
| ```javascript | ||
| import { umbHttpClient } from '@umbraco-cms/backoffice/http-client'; | ||
| import { tryExecute } from '@umbraco-cms/backoffice/resources'; | ||
|
|
||
| const { data, error } = await tryExecute(this, umbHttpClient.get({ | ||
| url: '/umbraco/management/api/v1/server/status' | ||
| })); | ||
|
|
||
| if (error) { | ||
| console.error('There was a problem with the fetch operation:', error); | ||
| } else { | ||
| console.log(data); // Do something with the data | ||
| } | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| 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: | ||
|
|
||
| ```javascript | ||
| tryExecute(this, request, { | ||
| disableNotifications: true, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Cancelling requests | ||
|
|
||
| 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: | ||
|
|
||
| ```javascript | ||
| const abortController = new AbortController(); | ||
|
|
||
| // Cancel the request before starting it for illustration purposes | ||
| abortController.abort(); | ||
|
|
||
| tryExecute(this, request, { | ||
| disableNotifications: true, | ||
| abortSignal: abortController.signal, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Custom Generated Client | ||
|
|
||
| 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. | ||
|
|
||
| If you want to generate your own client, you can use the following command: | ||
|
|
||
| ```bash | ||
| npm install @hey-api/openapi-ts | ||
| ``` | ||
|
|
||
| Then, you can use the `openapi-ts` command to generate a client from your OpenAPI specification: | ||
|
|
||
| ```bash | ||
| npx openapi-ts generate --url https://example.com/openapi.json --output ./my-client | ||
| ``` | ||
|
|
||
| This will generate a TypeScript client in the `./my-client` folder. You can then import the client into your project and use it to make requests to the Management API. | ||
|
|
||
| ### Connecting to the Management API | ||
|
|
||
| You will need to set up a few configuration options in order to connect to the Management API. The following options are required: | ||
|
|
||
| - `auth`: The authentication method to use. This is typically `Bearer` for the Management API. | ||
| - `baseUrl`: The base URL of the Management API. This is typically `https://example.com/umbraco/api/management/v1`. | ||
| - `credentials`: The credentials to use for the request. This is typically `same-origin` for the Management API. | ||
|
|
||
| You can set these options either directly with the `openapi-ts` command or in a central place in your code. For example, you can create a [BackofficeEntryPoint](../../extending-overview/extension-types/backoffice-entry-point.md) that sets up the configuration options for the HTTP client: | ||
|
|
||
| ```javascript | ||
| import { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth'; | ||
| import { client } from './my-client/client.gen'; | ||
|
|
||
| export const onInit = (host) => { | ||
| host.consumeContext(UMB_AUTH_CONTEXT, (authContext) => { | ||
| // Get the token info from Umbraco | ||
| const config = authContext?.getOpenApiConfiguration(); | ||
|
|
||
| client.setConfig({ | ||
| auth: config?.token ?? undefined, | ||
| baseUrl: config?.base ?? "", | ||
| credentials: config?.credentials ?? "same-origin", | ||
| }); | ||
| }; | ||
| ``` | ||
|
|
||
| This will set up the HTTP client to use the Management API base URL and authentication method. You can then use the client to make requests to the Management API. | ||
|
|
||
| {% hint style="info" %} | ||
| You can see the above example in action by looking at the [Umbraco Extension Template](../../development-flow/umbraco-extension-template.md). | ||
| {% endhint %} | ||
|
|
||
| **Fetch API** | ||
|
|
||
| The approach with a Backoffice Entry Point is good if you have a lot of requests to make. However, if you only have a few requests to make, you can use the `fetch` function directly. Read more about that here: | ||
|
|
||
| {% content-ref url="fetch-api.md" %} | ||
| [fetch-api.md](fetch-api.md) | ||
| {% endcontent-ref %} | ||
|
|
||
| **Setting the client directly** | ||
| You can also set the client directly in your code. This is useful if you only have a few requests to make and don't want to set up a Backoffice Entry Point. | ||
|
|
||
| ```javascript | ||
| import { getMyControllerAction } from './my-client'; | ||
| import { tryExecute } from '@umbraco-cms/backoffice/resources'; | ||
| import { umbHttpClient } from '@umbraco-cms/backoffice/http-client'; | ||
|
|
||
| const { data } = await tryExecute(this, getMyControllerAction({ | ||
| client: umbHttpClient, // Use Umbraco's HTTP client | ||
| })); | ||
|
|
||
| if (data) { | ||
| console.log('Server status:', data); | ||
| } | ||
| ``` | ||
|
|
||
| The above example shows how to use the `getMyControllerAction` function, which is generated through `openapi-ts`. The `client` parameter is the HTTP client that you want to use. You can use any HTTP client that implements the underlying interface from **@hey-api/openapi-ts**, which the Umbraco HTTP Client does. The `getMyControllerAction` function will then use the built-in HTTP client over its own to make the request to the Management API. | ||
|
|
||
| ## Further reading | ||
|
|
||
| - [@hey-api/openapi-ts](https://heyapi.dev/openapi-ts/get-started) | ||
eshanrnh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - [@umbraco-cms/backoffice/http-client](https://apidocs.umbraco.com/v16/ui-api/modules/packages_core_http-client.html) | ||
eshanrnh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - [Using the Fetch API](fetch-api.md) | ||
| - [Working with Data](../working-with-data/README.md) | ||
| - [Creating a Backoffice API](../../../tutorials/creating-a-backoffice-api/README.md) | ||
| - [Creating a Backoffice Entry Point](../../extending-overview/extension-types/backoffice-entry-point.md) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.