You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/client/README.md
+66-56Lines changed: 66 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,17 @@
3
3
The `client` package provides secure, bidirectional communication between a Marketplace application (the client) and Sitecore (the host). Sitecore loads the Marketplace app inside a sandboxed [iframe](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe). The iframe and its parent window securely communicate using the web browser's [PostMessage API](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage).
4
4
5
5
This package lets you:
6
+
6
7
- Make queries. Queries support one-off data requests and subscriptions for live updates. The `client` package lets you query the host's state and environment, and the [context](#query-the-application-context) of your Marketplace app.
7
8
- Make mutations. Mutations trigger state changes or HTTP requests in Sitecore.
8
-
- Interact with Sitecore APIs to perform actions on behalf of the resources it was granted access to during installation.
9
+
- Interact with Sitecore APIs to perform actions on behalf of the resource access it was granted during installation.
9
10
> [!TIP]
10
11
> Inspired by GraphQL and React Query, the query/mutation API manages internal state, loading status, and error handling.
11
12
12
13
The `client` package is required for all Marketplace apps.
13
14
14
15
## Prerequisites
16
+
15
17
- Node.js 16 or later. Check your installed version by using the `node --version` command.
16
18
- npm 10 or later. Check your installed version by using the `npm --version` command.
17
19
- Access to the Sitecore Cloud Portal.
@@ -31,8 +33,8 @@ Before you use queries or mutations, you must initialize the Client SDK.
errorinstanceofError?error:newError('Failed to initialize MarketplaceClient'),
134
+
isLoading: false,
135
+
isInitialized: false,
136
+
});
137
+
} finally {
138
+
isInitializingRef.current=false;
127
139
}
128
-
129
-
setState({
130
-
client: null,
131
-
error: errorinstanceofError?error:newError('Failed to initialize MarketplaceClient'),
132
-
isLoading: false,
133
-
isInitialized: false,
134
-
});
135
-
} finally {
136
-
isInitializingRef.current=false;
137
-
}
138
-
}, [opts.retryAttempts, opts.retryDelay]); // Removed state dependencies
140
+
},
141
+
[opts.retryAttempts, opts.retryDelay],
142
+
); // Removed state dependencies
139
143
140
144
useEffect(() => {
141
145
if (opts.autoInit) {
@@ -154,14 +158,18 @@ export function useMarketplaceClient(options: UseMarketplaceClientOptions = {})
154
158
}, [opts.autoInit, initializeClient]);
155
159
156
160
// Memoize the return value to prevent object recreation on every render
157
-
returnuseMemo(() => ({
158
-
...state,
159
-
initialize: initializeClient,
160
-
}), [state, initializeClient]);
161
+
returnuseMemo(
162
+
() => ({
163
+
...state,
164
+
initialize: initializeClient,
165
+
}),
166
+
[state, initializeClient],
167
+
);
161
168
}
162
169
```
163
170
164
171
2. Initialize the SDK:
172
+
165
173
```typescript
166
174
// src/pages/index.tsx
167
175
@@ -201,7 +209,6 @@ function App() {
201
209
exportdefaultApp;
202
210
```
203
211
204
-
205
212
## Usage
206
213
207
214
### Make a query
@@ -211,17 +218,18 @@ Use the `query` method to make one-off data requests and live subscriptions. Pas
211
218
For example, pass `'application.context'` to retrieve details about the Marketplace app and the Sitecore host, including, for example, your Context IDs:
The application context provides information about your Marketplace app, such as its ID, URL, name, type, icon URL, installation ID, and associated resources:
232
+
The application context provides information about your Marketplace app, such as its ID, URL, name, type, icon URL, installation ID, and associated resource access:
225
233
226
234
```javascript
227
235
{
@@ -231,13 +239,13 @@ The application context provides information about your Marketplace app, such as
231
239
url:'https://my-app.com/app',
232
240
iconUrl:'https://my-app.com/assets/icon.png',
233
241
installationId:'abc1234567890',
234
-
resources: [
242
+
resourceAccess: [
235
243
{
236
244
resourceId:'resource-1',
237
245
tenantId:'tenant-1',
238
246
tenantName:'Example Tenant',
239
247
context: {
240
-
live:'1234567890',
248
+
live:'1234567890',
241
249
preview:'0987654321'
242
250
}
243
251
}
@@ -249,13 +257,13 @@ For an overview of all the possible values, refer to the [`QueryMap` interface](
249
257
250
258
### Make a mutation
251
259
252
-
Use the `mutate` method to trigger changes in Sitecore (the host). Pass a value to the method depending on the change you want to make.
260
+
Use the `mutate` method to trigger changes in Sitecore (the host). Pass a value to the method depending on the change you want to make.
253
261
254
262
For example, pass `'pages.reloadCanvas'` to reload the XM Cloud page builder canvas:
255
263
256
264
```typescript
257
265
const reloadCanvas = () => {
258
-
client?.mutate("pages.reloadCanvas");
266
+
client?.mutate('pages.reloadCanvas');
259
267
};
260
268
```
261
269
@@ -268,8 +276,10 @@ For an overview of all the possible values, refer to the [`MutationMap` interfac
268
276
269
277
For more information, refer to the reference documentation in the `/docs` folder, and the official [Marketplace developer documentation](https://doc.sitecore.com/mp/en/developers/marketplace/introduction-to-sitecore-marketplace.html) and [Marketplace SDK documentation](https://doc.sitecore.com/mp/en/developers/sdk/latest/sitecore-marketplace-sdk/sitecore-marketplace-sdk-for-javascript.html).
270
278
271
-
## License
279
+
## License
280
+
272
281
This package is part of the Sitecore Marketplace SDK, licensed under the Apache 2.0 License. Refer to the [LICENSE](../../LICENSE.md) file in the repository root.
273
282
274
283
## Status
284
+
275
285
The `client` package is actively maintained as part of the Sitecore Marketplace SDK.
0 commit comments