@kontent-ai/custom-app-sdk 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +167 -24
- package/dist/contexts.d.ts +14 -0
- package/dist/contexts.js +36 -0
- package/dist/contexts.js.map +1 -0
- package/dist/customAppSdk.d.ts +27 -17
- package/dist/customAppSdk.js +133 -18
- package/dist/customAppSdk.js.map +1 -1
- package/dist/iframeMessenger.d.ts +5 -2
- package/dist/iframeMessenger.js +25 -4
- package/dist/iframeMessenger.js.map +1 -1
- package/dist/iframeSchema.d.ts +312 -121
- package/dist/iframeSchema.js +190 -20
- package/dist/iframeSchema.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/utilityTypes.d.ts +8 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -18,23 +18,30 @@ It facilitates the communication between the Kontent.ai app and the custom app p
|
|
|
18
18
|
### Installation
|
|
19
19
|
|
|
20
20
|
```sh
|
|
21
|
-
npm install @kontent-ai/custom-app-sdk
|
|
21
|
+
npm install @kontent-ai/custom-app-sdk
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
> [!IMPORTANT]
|
|
25
|
-
> The SDK attaches event listeners to communicate with the Kontent.ai app. Make sure to
|
|
25
|
+
> The SDK attaches event listeners to communicate with the Kontent.ai app. Make sure to include the SDK only in the browser environment.
|
|
26
26
|
|
|
27
27
|
## Usage example
|
|
28
28
|
|
|
29
29
|
```typescript
|
|
30
|
-
import { getCustomAppContext, CustomAppContext } from "@kontent-ai/custom-app-sdk
|
|
30
|
+
import { getCustomAppContext, CustomAppContext } from "@kontent-ai/custom-app-sdk";
|
|
31
31
|
|
|
32
|
-
const response
|
|
32
|
+
const response = await getCustomAppContext();
|
|
33
33
|
|
|
34
34
|
if (response.isError) {
|
|
35
|
-
console.error({ errorCode: response.code, description: response.description});
|
|
35
|
+
console.error({ errorCode: response.code, description: response.description });
|
|
36
36
|
} else {
|
|
37
|
-
|
|
37
|
+
// TypeScript will narrow the type based on currentPage
|
|
38
|
+
if (response.context.currentPage === "itemEditor") {
|
|
39
|
+
console.log({
|
|
40
|
+
contentItemId: response.context.contentItemId,
|
|
41
|
+
languageId: response.context.languageId,
|
|
42
|
+
validationErrors: response.context.validationErrors
|
|
43
|
+
});
|
|
44
|
+
}
|
|
38
45
|
}
|
|
39
46
|
```
|
|
40
47
|
|
|
@@ -42,38 +49,174 @@ if (response.isError) {
|
|
|
42
49
|
|
|
43
50
|
### getCustomAppContext
|
|
44
51
|
|
|
45
|
-
|
|
52
|
+
Retrieves the context of the custom app. The function takes no arguments and automatically detects the current page type, returning the appropriate context with all relevant properties for that page.
|
|
46
53
|
|
|
47
|
-
####
|
|
54
|
+
#### Return Type
|
|
55
|
+
|
|
56
|
+
Returns a promise that resolves to a discriminated union type with two possible states:
|
|
57
|
+
|
|
58
|
+
##### Success Response (`isError: false`)
|
|
59
|
+
|
|
60
|
+
| Property | Type | Description |
|
|
61
|
+
|---------------|------------------------|------------------------------------------------------------------------------|
|
|
62
|
+
| `isError` | `false` | Indicates the request was successful |
|
|
63
|
+
| `context` | `CustomAppContext` | A discriminated union of page-specific context objects |
|
|
64
|
+
|
|
65
|
+
##### Error Response (`isError: true`)
|
|
48
66
|
|
|
49
67
|
| Property | Type | Description |
|
|
50
68
|
|---------------|------------------------|------------------------------------------------------------------------------|
|
|
51
|
-
| `isError` |
|
|
52
|
-
| `code` | ErrorCode enum
|
|
53
|
-
| `description` | string
|
|
54
|
-
|
|
55
|
-
|
|
69
|
+
| `isError` | `true` | Indicates an error occurred |
|
|
70
|
+
| `code` | ErrorCode enum | The code of the error message |
|
|
71
|
+
| `description` | string | The description of the error message |
|
|
72
|
+
|
|
73
|
+
#### CustomAppContext
|
|
74
|
+
|
|
75
|
+
`CustomAppContext` is a discriminated union type based on the `currentPage` property. Each page type includes only the relevant properties for that specific page:
|
|
76
|
+
|
|
77
|
+
##### Item Editor Page Context
|
|
78
|
+
|
|
79
|
+
When `currentPage` is `"itemEditor"`, the context includes:
|
|
56
80
|
|
|
57
|
-
|
|
58
|
-
|
|
81
|
+
| Property | Type | Description |
|
|
82
|
+
|-------------------------|---------------------------------------|----------------------------------------------------------------------|
|
|
83
|
+
| `currentPage` | `"itemEditor"` | Identifies this as an item editor page |
|
|
84
|
+
| `environmentId` | UUID | The environment's ID |
|
|
85
|
+
| `userId` | string | The current user's ID |
|
|
86
|
+
| `userEmail` | string | The current user's email |
|
|
87
|
+
| `userRoles` | Array of UserRole | An array containing all the roles of the current user in the environment |
|
|
88
|
+
| `path` | string | The current path within the Kontent.ai application |
|
|
89
|
+
| `pageTitle` | string | The title of the current page |
|
|
90
|
+
| `appConfig` | unknown \| undefined | JSON object specified in the custom app configuration |
|
|
91
|
+
| `contentItemId` | UUID | The ID of the content item being edited |
|
|
92
|
+
| `languageId` | UUID | The ID of the current language |
|
|
93
|
+
| `validationErrors` | Record<string, string[]> | A record of validation errors for content item fields |
|
|
59
94
|
|
|
60
|
-
|
|
61
|
-
The `context` object contains data provided by the Kontent.ai application that you can leverage in your custom app.
|
|
95
|
+
##### Other Page Context
|
|
62
96
|
|
|
63
|
-
|
|
64
|
-
|-----------------|-------------------|--------------------------------------------------------------------------|
|
|
65
|
-
| `environmentId` | UUID | The environment's ID |
|
|
66
|
-
| `userId` | string | The current user's ID |
|
|
67
|
-
| `userEmail` | string | The current user's email |
|
|
68
|
-
| `userRoles` | Array of UserRole | An array containing all the roles of the current user in the environment |
|
|
97
|
+
When `currentPage` is `"other"`, the context includes:
|
|
69
98
|
|
|
70
|
-
|
|
99
|
+
| Property | Type | Description |
|
|
100
|
+
|-------------------------|---------------------------------------|----------------------------------------------------------------------|
|
|
101
|
+
| `currentPage` | `"other"` | Identifies this as any other page type |
|
|
102
|
+
| `environmentId` | UUID | The environment's ID |
|
|
103
|
+
| `userId` | string | The current user's ID |
|
|
104
|
+
| `userEmail` | string | The current user's email |
|
|
105
|
+
| `userRoles` | Array of UserRole | An array containing all the roles of the current user in the environment |
|
|
106
|
+
| `path` | string | The current path within the Kontent.ai application |
|
|
107
|
+
| `pageTitle` | string | The title of the current page |
|
|
108
|
+
| `appConfig` | unknown \| undefined | JSON object specified in the custom app configuration |
|
|
109
|
+
|
|
110
|
+
#### UserRole
|
|
71
111
|
|
|
72
112
|
| Property | Type | Description |
|
|
73
113
|
|------------|--------|----------------------------------------------------------------------|
|
|
74
114
|
| `id` | UUID | The role's ID |
|
|
75
115
|
| `codename` | string | The role's codename - applicable only for the _Project manager_ role |
|
|
76
116
|
|
|
117
|
+
### observeCustomAppContext
|
|
118
|
+
|
|
119
|
+
Subscribes to context changes and receives notifications when the context is updated. The function takes a callback that will be invoked whenever the context changes.
|
|
120
|
+
|
|
121
|
+
#### Parameters
|
|
122
|
+
|
|
123
|
+
| Parameter | Type | Description |
|
|
124
|
+
|------------|-----------------------------|-------------------------------------------------------|
|
|
125
|
+
| `callback` | `(context: CustomAppContext) => void` | Function to be called when the context changes |
|
|
126
|
+
|
|
127
|
+
#### Return Type
|
|
128
|
+
|
|
129
|
+
Returns a promise that resolves to a discriminated union type with two possible states:
|
|
130
|
+
|
|
131
|
+
##### Success Response (`isError: false`)
|
|
132
|
+
|
|
133
|
+
| Property | Type | Description |
|
|
134
|
+
|---------------|------------------------|------------------------------------------------------------------------------|
|
|
135
|
+
| `isError` | `false` | Indicates the request was successful |
|
|
136
|
+
| `context` | `CustomAppContext` | The initial context value |
|
|
137
|
+
| `unsubscribe` | `() => Promise<void>` | Function to call to stop receiving context updates |
|
|
138
|
+
|
|
139
|
+
##### Error Response (`isError: true`)
|
|
140
|
+
|
|
141
|
+
| Property | Type | Description |
|
|
142
|
+
|---------------|------------------------|------------------------------------------------------------------------------|
|
|
143
|
+
| `isError` | `true` | Indicates an error occurred |
|
|
144
|
+
| `code` | ErrorCode enum | The code of the error message |
|
|
145
|
+
| `description` | string | The description of the error message |
|
|
146
|
+
|
|
147
|
+
#### Usage Example
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { observeCustomAppContext } from "@kontent-ai/custom-app-sdk";
|
|
151
|
+
|
|
152
|
+
const response = await observeCustomAppContext((context) => {
|
|
153
|
+
console.log("Context updated:", context);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
if (response.isError) {
|
|
157
|
+
console.error({ errorCode: response.code, description: response.description });
|
|
158
|
+
} else {
|
|
159
|
+
console.log("Initial context:", response.context);
|
|
160
|
+
|
|
161
|
+
// Later, when you want to stop observing
|
|
162
|
+
await response.unsubscribe();
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### setPopupSize
|
|
167
|
+
|
|
168
|
+
Sets the size of the popup window when the custom app is displayed in a popup.
|
|
169
|
+
|
|
170
|
+
#### Parameters
|
|
171
|
+
|
|
172
|
+
| Parameter | Type | Description |
|
|
173
|
+
|-----------|----------------------|--------------------------------------------------|
|
|
174
|
+
| `width` | `PopupSizeDimension` | The desired width of the popup |
|
|
175
|
+
| `height` | `PopupSizeDimension` | The desired height of the popup |
|
|
176
|
+
|
|
177
|
+
#### PopupSizeDimension
|
|
178
|
+
|
|
179
|
+
A discriminated union type for specifying dimensions in either pixels or percentages:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
type PopupSizeDimension =
|
|
183
|
+
| { unit: "px"; value: number }
|
|
184
|
+
| { unit: "%"; value: number };
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
#### Return Type
|
|
188
|
+
|
|
189
|
+
Returns a promise that resolves to a discriminated union type with two possible states:
|
|
190
|
+
|
|
191
|
+
##### Success Response (`isError: false`)
|
|
192
|
+
|
|
193
|
+
| Property | Type | Description |
|
|
194
|
+
|---------------|------------------------|------------------------------------------------------------------------------|
|
|
195
|
+
| `isError` | `false` | Indicates the request was successful |
|
|
196
|
+
|
|
197
|
+
##### Error Response (`isError: true`)
|
|
198
|
+
|
|
199
|
+
| Property | Type | Description |
|
|
200
|
+
|---------------|------------------------|------------------------------------------------------------------------------|
|
|
201
|
+
| `isError` | `true` | Indicates an error occurred |
|
|
202
|
+
| `code` | ErrorCode enum | The code of the error message |
|
|
203
|
+
| `description` | string | The description of the error message |
|
|
204
|
+
|
|
205
|
+
#### Usage Example
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import { setPopupSize } from "@kontent-ai/custom-app-sdk";
|
|
209
|
+
|
|
210
|
+
const response = await setPopupSize(
|
|
211
|
+
{ unit: "px", value: 800 },
|
|
212
|
+
{ unit: "%", value: 90 }
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
if (response.isError) {
|
|
216
|
+
console.error({ errorCode: response.code, description: response.description });
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
77
220
|
## Contributing
|
|
78
221
|
|
|
79
222
|
For Contributing please see <a href="./CONTRIBUTING.md">`CONTRIBUTING.md`</a> for more information.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CustomAppContextProperties } from "./iframeSchema";
|
|
2
|
+
export declare const itemEditorContextProperties: readonly ["path", "pageTitle", "environmentId", "userId", "userEmail", "userRoles", "appConfig", "contentItemId", "languageId", "validationErrors", "currentPage"];
|
|
3
|
+
type RawItemEditorContext = Pick<CustomAppContextProperties, (typeof itemEditorContextProperties)[number]>;
|
|
4
|
+
export type ItemEditorContext = Required<WithSpecificPage<CustomAppContextProperties, "itemEditor">>;
|
|
5
|
+
export declare const isItemEditorContext: (context: RawItemEditorContext) => context is ItemEditorContext;
|
|
6
|
+
export declare const otherContextProperties: readonly ["path", "pageTitle", "environmentId", "userId", "userEmail", "userRoles", "appConfig", "currentPage"];
|
|
7
|
+
type RawOtherContext = Pick<CustomAppContextProperties, (typeof otherContextProperties)[number]>;
|
|
8
|
+
export type OtherContext = Required<WithSpecificPage<CustomAppContextProperties, "other">>;
|
|
9
|
+
export declare const isOtherContext: (context: RawOtherContext) => context is OtherContext;
|
|
10
|
+
export type Context = ItemEditorContext | OtherContext;
|
|
11
|
+
type WithSpecificPage<T extends CustomAppContextProperties, Page extends Context["currentPage"]> = T & {
|
|
12
|
+
readonly currentPage: Page;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
package/dist/contexts.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const sharedContextProperties = [
|
|
2
|
+
"path",
|
|
3
|
+
"pageTitle",
|
|
4
|
+
"environmentId",
|
|
5
|
+
"userId",
|
|
6
|
+
"userEmail",
|
|
7
|
+
"userRoles",
|
|
8
|
+
"appConfig",
|
|
9
|
+
];
|
|
10
|
+
export const itemEditorContextProperties = [
|
|
11
|
+
...sharedContextProperties,
|
|
12
|
+
"contentItemId",
|
|
13
|
+
"languageId",
|
|
14
|
+
"validationErrors",
|
|
15
|
+
"currentPage",
|
|
16
|
+
];
|
|
17
|
+
const optionalProperties = [
|
|
18
|
+
"appConfig",
|
|
19
|
+
];
|
|
20
|
+
export const isItemEditorContext = (context) => {
|
|
21
|
+
return (context.currentPage === "itemEditor" &&
|
|
22
|
+
itemEditorContextProperties
|
|
23
|
+
.filter((property) => !optionalProperties.includes(property))
|
|
24
|
+
.every((property) => property in context && context[property] !== undefined));
|
|
25
|
+
};
|
|
26
|
+
export const otherContextProperties = [
|
|
27
|
+
...sharedContextProperties,
|
|
28
|
+
"currentPage",
|
|
29
|
+
];
|
|
30
|
+
export const isOtherContext = (context) => {
|
|
31
|
+
return (context.currentPage === "other" &&
|
|
32
|
+
otherContextProperties
|
|
33
|
+
.filter((property) => !optionalProperties.includes(property))
|
|
34
|
+
.every((property) => property in context && context[property] !== undefined));
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=contexts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contexts.js","sourceRoot":"","sources":["../src/contexts.ts"],"names":[],"mappings":"AAEA,MAAM,uBAAuB,GAAG;IAC9B,MAAM;IACN,WAAW;IACX,eAAe;IACf,QAAQ;IACR,WAAW;IACX,WAAW;IACX,WAAW;CACuD,CAAC;AAErE,MAAM,CAAC,MAAM,2BAA2B,GAAG;IACzC,GAAG,uBAAuB;IAC1B,eAAe;IACf,YAAY;IACZ,kBAAkB;IAClB,aAAa;CACqD,CAAC;AAErE,MAAM,kBAAkB,GAAoD;IAC1E,WAAW;CACuD,CAAC;AAWrE,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,OAA6B,EACC,EAAE;IAChC,OAAO,CACL,OAAO,CAAC,WAAW,KAAK,YAAY;QACpC,2BAA2B;aACxB,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,kBAAkB,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;aAC5D,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,IAAI,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,SAAS,CAAC,CAC/E,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,GAAG,uBAAuB;IAC1B,aAAa;CACqD,CAAC;AAMrE,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,OAAwB,EAA2B,EAAE;IAClF,OAAO,CACL,OAAO,CAAC,WAAW,KAAK,OAAO;QAC/B,sBAAsB;aACnB,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,kBAAkB,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;aAC5D,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,IAAI,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,SAAS,CAAC,CAC/E,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/customAppSdk.d.ts
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
|
+
import { type Context } from "./contexts";
|
|
1
2
|
export declare enum ErrorCode {
|
|
2
|
-
UnknownMessage = "unknown-message"
|
|
3
|
+
UnknownMessage = "unknown-message",
|
|
4
|
+
NotSupported = "not-supported",
|
|
5
|
+
OutdatedContext = "outdated-context"
|
|
3
6
|
}
|
|
4
|
-
export
|
|
5
|
-
readonly
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
readonly userRoles: ReadonlyArray<{
|
|
11
|
-
readonly id: string;
|
|
12
|
-
readonly codename: string | null;
|
|
13
|
-
}>;
|
|
14
|
-
};
|
|
15
|
-
readonly config?: unknown;
|
|
7
|
+
export declare const getCustomAppContext: () => Promise<Result<{
|
|
8
|
+
readonly context: Context;
|
|
9
|
+
}>>;
|
|
10
|
+
export type PopupSizeDimension = {
|
|
11
|
+
readonly unit: "px";
|
|
12
|
+
readonly value: number;
|
|
16
13
|
} | {
|
|
17
|
-
readonly
|
|
18
|
-
readonly
|
|
19
|
-
|
|
14
|
+
readonly unit: "%";
|
|
15
|
+
readonly value: number;
|
|
16
|
+
};
|
|
17
|
+
export declare const setPopupSize: (width: PopupSizeDimension, height: PopupSizeDimension) => Promise<Result<{
|
|
18
|
+
readonly isError: false;
|
|
19
|
+
}>>;
|
|
20
|
+
export declare const observeCustomAppContext: (callback: (context: Context) => void) => Promise<Result<{
|
|
21
|
+
readonly context: Context;
|
|
22
|
+
readonly unsubscribe: () => Promise<void>;
|
|
23
|
+
}>>;
|
|
24
|
+
type Result<T> = ({
|
|
25
|
+
isError: false;
|
|
26
|
+
} & T) | {
|
|
27
|
+
isError: true;
|
|
28
|
+
code: ErrorCode;
|
|
29
|
+
description: string;
|
|
20
30
|
};
|
|
21
|
-
export
|
|
31
|
+
export {};
|
package/dist/customAppSdk.js
CHANGED
|
@@ -1,27 +1,142 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isItemEditorContext, isOtherContext, itemEditorContextProperties, otherContextProperties, } from "./contexts";
|
|
2
|
+
import { addNotificationCallback, removeNotificationCallback, sendMessage, } from "./iframeMessenger";
|
|
3
|
+
import { ErrorMessage, } from "./iframeSchema";
|
|
3
4
|
import { matchesSchema } from "./matchesSchema";
|
|
4
5
|
export var ErrorCode;
|
|
5
6
|
(function (ErrorCode) {
|
|
6
7
|
ErrorCode["UnknownMessage"] = "unknown-message";
|
|
8
|
+
ErrorCode["NotSupported"] = "not-supported";
|
|
9
|
+
ErrorCode["OutdatedContext"] = "outdated-context";
|
|
7
10
|
})(ErrorCode || (ErrorCode = {}));
|
|
8
|
-
export const getCustomAppContext =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
export const getCustomAppContext = async () => {
|
|
12
|
+
const currentPageResponse = await sendMessage({
|
|
13
|
+
type: "get-context-request",
|
|
14
|
+
version: "2.0.0",
|
|
15
|
+
payload: {
|
|
16
|
+
properties: ["currentPage"],
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
if (matchesSchema(ErrorMessage, currentPageResponse)) {
|
|
20
|
+
return {
|
|
21
|
+
isError: true,
|
|
22
|
+
code: currentPageResponse.code,
|
|
23
|
+
description: currentPageResponse.description,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const currentPage = currentPageResponse.payload.properties.currentPage;
|
|
27
|
+
const propertiesToFetch = currentPage === "itemEditor" ? itemEditorContextProperties : otherContextProperties;
|
|
28
|
+
const response = await sendMessage({
|
|
29
|
+
type: "get-context-request",
|
|
30
|
+
version: "2.0.0",
|
|
31
|
+
payload: {
|
|
32
|
+
properties: propertiesToFetch,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
if (matchesSchema(ErrorMessage, response)) {
|
|
36
|
+
return { isError: true, code: response.code, description: response.description };
|
|
37
|
+
}
|
|
38
|
+
return getContextFromProperties(response.payload.properties);
|
|
39
|
+
};
|
|
40
|
+
export const setPopupSize = async (width, height) => {
|
|
41
|
+
const response = await sendMessage({
|
|
42
|
+
type: "set-popup-size-request",
|
|
43
|
+
version: "1.0.0",
|
|
44
|
+
payload: {
|
|
45
|
+
width,
|
|
46
|
+
height,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
if (matchesSchema(ErrorMessage, response)) {
|
|
50
|
+
return { isError: true, code: response.code, description: response.description };
|
|
51
|
+
}
|
|
52
|
+
return { isError: false };
|
|
53
|
+
};
|
|
54
|
+
const outdatedContextError = {
|
|
55
|
+
isError: true,
|
|
56
|
+
code: ErrorCode.OutdatedContext,
|
|
57
|
+
description: "The context we received is outdated, please try to get the context again.",
|
|
58
|
+
};
|
|
59
|
+
export const observeCustomAppContext = async (callback) => {
|
|
60
|
+
const currentPageResponse = await sendMessage({
|
|
61
|
+
type: "get-context-request",
|
|
62
|
+
version: "2.0.0",
|
|
63
|
+
payload: {
|
|
64
|
+
properties: ["currentPage"],
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
if (matchesSchema(ErrorMessage, currentPageResponse)) {
|
|
68
|
+
return {
|
|
69
|
+
isError: true,
|
|
70
|
+
code: currentPageResponse.code,
|
|
71
|
+
description: currentPageResponse.description,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const currentPage = currentPageResponse.payload.properties.currentPage;
|
|
75
|
+
const propertiesToObserve = currentPage === "itemEditor" ? itemEditorContextProperties : otherContextProperties;
|
|
76
|
+
const observeResponse = await sendMessage({
|
|
77
|
+
type: "observe-context-request",
|
|
78
|
+
version: "1.0.0",
|
|
79
|
+
payload: {
|
|
80
|
+
properties: propertiesToObserve,
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
if (matchesSchema(ErrorMessage, observeResponse)) {
|
|
84
|
+
return { isError: true, code: observeResponse.code, description: observeResponse.description };
|
|
85
|
+
}
|
|
86
|
+
const initialContextResponse = await sendMessage({
|
|
87
|
+
type: "get-context-request",
|
|
88
|
+
version: "2.0.0",
|
|
89
|
+
payload: {
|
|
90
|
+
properties: propertiesToObserve,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
if (matchesSchema(ErrorMessage, initialContextResponse)) {
|
|
94
|
+
return {
|
|
95
|
+
isError: true,
|
|
96
|
+
code: initialContextResponse.code,
|
|
97
|
+
description: initialContextResponse.description,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const initialContext = getContextFromProperties(initialContextResponse.payload.properties);
|
|
101
|
+
if (initialContext.isError) {
|
|
102
|
+
return initialContext;
|
|
103
|
+
}
|
|
104
|
+
const notificationHandler = (notification) => {
|
|
105
|
+
const contextResult = getContextFromProperties(notification.payload.properties);
|
|
106
|
+
if (!contextResult.isError) {
|
|
107
|
+
callback(contextResult.context);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
addNotificationCallback(observeResponse.payload.subscriptionId, notificationHandler);
|
|
111
|
+
const unsubscribe = async () => {
|
|
112
|
+
removeNotificationCallback(observeResponse.payload.subscriptionId);
|
|
113
|
+
await sendMessage({
|
|
114
|
+
type: "unsubscribe-context-request",
|
|
12
115
|
version: "1.0.0",
|
|
13
|
-
payload:
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
resolve({ isError: true, code: response.code, description: response.description });
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
resolve({ ...response.payload, isError: false });
|
|
20
|
-
}
|
|
116
|
+
payload: {
|
|
117
|
+
subscriptionId: observeResponse.payload.subscriptionId,
|
|
118
|
+
},
|
|
21
119
|
});
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
isError: false,
|
|
123
|
+
context: initialContext.context,
|
|
124
|
+
unsubscribe,
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
const getContextFromProperties = (properties) => {
|
|
128
|
+
const currentPage = properties.currentPage;
|
|
129
|
+
switch (currentPage) {
|
|
130
|
+
case "itemEditor":
|
|
131
|
+
return isItemEditorContext(properties)
|
|
132
|
+
? { isError: false, context: properties }
|
|
133
|
+
: outdatedContextError;
|
|
134
|
+
case "other":
|
|
135
|
+
return isOtherContext(properties)
|
|
136
|
+
? { isError: false, context: properties }
|
|
137
|
+
: outdatedContextError;
|
|
138
|
+
default:
|
|
139
|
+
return outdatedContextError;
|
|
22
140
|
}
|
|
23
|
-
|
|
24
|
-
reject(error);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
141
|
+
};
|
|
27
142
|
//# sourceMappingURL=customAppSdk.js.map
|
package/dist/customAppSdk.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"customAppSdk.js","sourceRoot":"","sources":["../src/customAppSdk.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"customAppSdk.js","sourceRoot":"","sources":["../src/customAppSdk.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,mBAAmB,EACnB,cAAc,EACd,2BAA2B,EAC3B,sBAAsB,GACvB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,uBAAuB,EACvB,0BAA0B,EAC1B,WAAW,GACZ,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAGL,YAAY,GACb,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,MAAM,CAAN,IAAY,SAIX;AAJD,WAAY,SAAS;IACnB,+CAAkC,CAAA;IAClC,2CAA8B,CAAA;IAC9B,iDAAoC,CAAA;AACtC,CAAC,EAJW,SAAS,KAAT,SAAS,QAIpB;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,IAAoD,EAAE;IAC5F,MAAM,mBAAmB,GAAG,MAAM,WAAW,CAAsB;QACjE,IAAI,EAAE,qBAAqB;QAC3B,OAAO,EAAE,OAAO;QAChB,OAAO,EAAE;YACP,UAAU,EAAE,CAAC,aAAa,CAAC;SAC5B;KACF,CAAC,CAAC;IAEH,IAAI,aAAa,CAAC,YAAY,EAAE,mBAAmB,CAAC,EAAE,CAAC;QACrD,OAAO;YACL,OAAO,EAAE,IAAI;YACb,IAAI,EAAE,mBAAmB,CAAC,IAAI;YAC9B,WAAW,EAAE,mBAAmB,CAAC,WAAW;SAC7C,CAAC;IACJ,CAAC;IAED,MAAM,WAAW,GAAG,mBAAmB,CAAC,OAAO,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EAAE,UAAU,EAAE;gBACzC,CAAC,CAAC,oBAAoB,CAAC;QAC3B,KAAK,OAAO;YACV,OAAO,cAAc,CAAC,UAAU,CAAC;gBAC/B,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE;gBACzC,CAAC,CAAC,oBAAoB,CAAC;QAC3B;YACE,OAAO,oBAAoB,CAAC;IAChC,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { ClientContextChangedV1Notification, Schema } from "./iframeSchema";
|
|
3
|
+
export declare const sendMessage: <TMessageType extends keyof Schema["client"]>(message: Omit<Schema["client"][TMessageType]["request"], "requestId">) => Promise<Schema["client"][TMessageType]["response"]>;
|
|
4
|
+
export declare const addNotificationCallback: (subscriptionId: string, callback: (notification: z.infer<typeof ClientContextChangedV1Notification>) => void) => void;
|
|
5
|
+
export declare const removeNotificationCallback: (subscriptionId: string) => void;
|
package/dist/iframeMessenger.js
CHANGED
|
@@ -1,12 +1,33 @@
|
|
|
1
1
|
import { createUuid } from "./createUuid";
|
|
2
2
|
let callbacks = {};
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
let notificationCallbacks = {};
|
|
4
|
+
export const sendMessage = (message) => {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
try {
|
|
7
|
+
const requestId = createUuid();
|
|
8
|
+
callbacks = { ...callbacks, [requestId]: resolve };
|
|
9
|
+
window.parent.postMessage({ ...message, requestId }, "*");
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
reject(error);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
export const addNotificationCallback = (subscriptionId, callback) => {
|
|
17
|
+
notificationCallbacks = {
|
|
18
|
+
...notificationCallbacks,
|
|
19
|
+
[subscriptionId]: callback,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export const removeNotificationCallback = (subscriptionId) => {
|
|
23
|
+
notificationCallbacks = Object.fromEntries(Object.entries(notificationCallbacks).filter(([subId]) => subId !== subscriptionId));
|
|
7
24
|
};
|
|
8
25
|
const processMessage = (event) => {
|
|
9
26
|
const message = event.data;
|
|
27
|
+
if ("subscriptionId" in message) {
|
|
28
|
+
notificationCallbacks[message.subscriptionId]?.(message);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
10
31
|
const callback = callbacks[message.requestId];
|
|
11
32
|
callbacks = Object.fromEntries(Object.entries(callbacks).filter(([requestId]) => requestId !== message.requestId));
|
|
12
33
|
callback?.(message);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"iframeMessenger.js","sourceRoot":"","sources":["../src/iframeMessenger.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"iframeMessenger.js","sourceRoot":"","sources":["../src/iframeMessenger.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAI1C,IAAI,SAAS,GAAsD,EAAE,CAAC;AACtE,IAAI,qBAAqB,GAAsD,EAAE,CAAC;AAElF,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,OAAqE,EAChB,EAAE;IACvD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,IAAI,CAAC;YACH,MAAM,SAAS,GAAG,UAAU,EAAE,CAAC;YAC/B,SAAS,GAAG,EAAE,GAAG,SAAS,EAAE,CAAC,SAAS,CAAC,EAAE,OAAO,EAAsB,CAAC;YACvE,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,GAAG,OAAO,EAAE,SAAS,EAAE,EAAE,GAAG,CAAC,CAAC;QAC5D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,CAAC;QAChB,CAAC;IACH,CAAC,CAAC,CA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