Skip to content

Commit b48590c

Browse files
authored
Add update pages context param type (#19)
* add new mutation param for pages.context mutation * add new mutation param for pages.context mutation * improve mutation and query differentiation * fix changeset
1 parent c069b62 commit b48590c

File tree

8 files changed

+65
-7
lines changed

8 files changed

+65
-7
lines changed

.changeset/new-cooks-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sitecore-marketplace-sdk/core': patch
3+
---
4+
5+
Added a new shared type for the mutation parameters to support update Pages Context

.changeset/stale-nails-pick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sitecore-marketplace-sdk/client': patch
3+
---
4+
5+
Added new mutation key to support update Pages context

packages/client/src/__tests__/client.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ describe('ClientSDK', () => {
354354
});
355355

356356
// Verify the request was made with correct endpoint and parameters
357-
expect(CoreSDK.prototype.request).toHaveBeenCalledWith('pages.reloadCanvas', undefined);
357+
expect(CoreSDK.prototype.request).toHaveBeenCalledWith('pages.reloadCanvas:mutation', undefined);
358358
expect(logger.info).toHaveBeenCalledWith(`Mutation (${mutationKey}) success:`, undefined);
359359
expect(onSuccessMock).toHaveBeenCalledWith(undefined);
360360
expect(onErrorMock).not.toHaveBeenCalled();
@@ -680,4 +680,20 @@ describe('ClientSDK', () => {
680680
await client.closeApp();
681681
expect(mockRequest).toHaveBeenCalledWith('pages.closeApp', {});
682682
});
683+
684+
it('should call coreSdk.request with pages.context in mutate()', async () => {
685+
client = await ClientSDK.init(config);
686+
const mockRequest = vi.spyOn(client['coreSdk'], 'request').mockResolvedValue(undefined);
687+
const params = { itemId: '123', language: 'en', itemVersion: '1' };
688+
await client.mutate('pages.context', { params });
689+
expect(mockRequest).toHaveBeenCalledWith('pages.context:mutation', params);
690+
});
691+
692+
it('should call coreSdk.request with pages.setValue in mutate()', async () => {
693+
client = await ClientSDK.init(config);
694+
const mockRequest = vi.spyOn(client['coreSdk'], 'request').mockResolvedValue(undefined);
695+
const params = { value: 'test', canvasReload: false };
696+
await client.mutate('pages.setValue', { params });
697+
expect(mockRequest).toHaveBeenCalledWith('pages.setValue:mutation', params);
698+
});
683699
});

packages/client/src/__tests__/sdk-types.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ describe('sdk-types', () => {
112112
expect(mutationMap).toBeDefined();
113113
});
114114

115+
it('should validate pages.context MutationMap', () => {
116+
const mutationMap: MutationMap = {
117+
'pages.reloadCanvas': {
118+
params: undefined,
119+
response: undefined,
120+
subscribe: false,
121+
},
122+
'pages.context': {
123+
params: { itemId: 'item1', language: 'en', itemVersion: '2' },
124+
response: undefined,
125+
subscribe: false,
126+
},
127+
};
128+
expect(mutationMap['pages.context'].params).toEqual({ itemId: 'item1', language: 'en', itemVersion: '2' });
129+
expect(mutationMap['pages.context'].response).toBeUndefined();
130+
});
131+
115132
it('should validate QueryKey', () => {
116133
const queryKey: QueryKey = 'host.user';
117134
expect(queryKey).toBe('host.user');

packages/client/src/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ export class ClientSDK {
145145
return Array.from(this.modules.keys());
146146
}
147147

148-
private resolveOperation(keyOrOperationKey: string): { request: Function; operation: string } {
148+
private resolveOperation(keyOrOperationKey: string, type: 'query' | 'mutation'): { request: Function; operation: string } {
149149
let request = this.coreSdk.request.bind(this.coreSdk); // Bind to preserve context
150-
let operation = keyOrOperationKey;
150+
let operation = keyOrOperationKey + ':' + type; // Default operation name
151151

152152
if (keyOrOperationKey.includes('.')) {
153153
const firstDotIndex = keyOrOperationKey.indexOf('.');
@@ -216,7 +216,7 @@ export class ClientSDK {
216216
*/
217217

218218
async query<K extends QueryKey>(key: K, queryOptions?: QueryOptions<K>): Promise<QueryResult<K>> {
219-
const { request, operation } = this.resolveOperation(key as string);
219+
const { request, operation } = this.resolveOperation(key as string, 'query');
220220

221221
const { subscribe, onSuccess, onError, params, timeoutMs } = queryOptions || {};
222222
const hashedKey = subscribe ? key : await this.generateKeyWithHash(key, queryOptions);
@@ -339,7 +339,7 @@ export class ClientSDK {
339339
key: K,
340340
mutationOptions?: MutationOptions<K>,
341341
): Promise<MutationMap[K]['response']> {
342-
const { request, operation } = this.resolveOperation(key as string);
342+
const { request, operation } = this.resolveOperation(key as string, 'mutation');
343343
const { onSuccess, onError, params, timeoutMs } = mutationOptions || {};
344344
logger.debug(`Mutation (${key}) initiated with params:`, params, `timeoutMs: ${timeoutMs}`);
345345

packages/client/src/sdk-types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import shared types (e.g. User and ApplicationMetadata)
2-
import { ApplicationContext, UserInfo } from '@sitecore-marketplace-sdk/core';
2+
import { ApplicationContext, UserInfo, PagesContextParams } from '@sitecore-marketplace-sdk/core';
33

44
// --- Host state types ---
55

@@ -224,6 +224,11 @@ export interface MutationMap {
224224
response: void;
225225
subscribe: false;
226226
};
227+
'pages.context': {
228+
params: PagesContextParams;
229+
response: void;
230+
subscribe: false;
231+
};
227232
}
228233

229234
// Utility types for keys

packages/core/src/post-message.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,9 @@ export class PostMessageBridge {
431431
message,
432432
);
433433

434-
const handler = this.requestHandlers.get(message.action);
434+
const fullKey = message.action.split(':');
435+
const actionWithoutPostfix = fullKey[0];
436+
const handler = this.requestHandlers.get(message.action) ?? this.requestHandlers.get(actionWithoutPostfix);
435437
const catchAllHandler = this.requestHandlers.get('*');
436438
if (!handler && !catchAllHandler) {
437439
// No handler registered for this action, send an error response.

packages/core/src/shared-types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,11 @@ export interface ExternalUrlPayload {
137137
newTab?: boolean;
138138
}
139139

140+
/**
141+
* Parameters for Pages Context mutation.
142+
*/
143+
export interface PagesContextParams {
144+
itemId?: string;
145+
language?: string;
146+
itemVersion?: string;
147+
}

0 commit comments

Comments
 (0)