@canva/platform 1.1.0 → 2.1.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/index.d.ts CHANGED
@@ -1,57 +1,62 @@
1
1
  /**
2
2
  * @public
3
- * An API for interacting with the App Process.
3
+ * Provides methods for interacting with an app process.
4
4
  */
5
5
  export declare interface AppProcess {
6
+ /**
7
+ * The current app process.
8
+ */
6
9
  readonly current: CurrentAppProcess;
7
10
  /**
8
11
  * @public
9
- * Request the termination of an app process.
10
- * @param target - The id of the app process to close.
11
- * @param params - A parameters object passed to all callback functions registered via registerOnBeforeClose API for the provided AppProcessId.
12
- * In addition to the required 'reason' field, app can choose to pass any structured data via params.
13
- * {@link https://html.spec.whatwg.org/multipage/structured-data.html#safe-passing-of-structured-data | safe passing of structured data}
14
- * @remarks
15
- * A successful invocation indicates the platform has begun shutdown following procedure for the target AppProcessId, which:
16
- * 1. Transitions process state to `CLOSING`, triggering all registered state change callbacks.
17
- * 2. Invokes all registered onBeforeClose callbacks.
18
- * 3. Waits for the process to close.
19
- * 4. Transitions process state to `CLOSED`, triggering all registered state change callbacks.
12
+ * Requests the termination of the specified app process.
13
+ *
14
+ * @param target - The ID of an app process.
15
+ * @param params - Parameters to pass to the `setOnDispose` callback. Any kind of structured data can be passed via this property.
16
+ *
20
17
  * @remarks
21
- * Once initiated, the closing process is irreversible and the process will NOT receive any events or messages from other processes.
18
+ * Once called, this method:
19
+ *
20
+ * 1. Transitions the state of the process to `"closing"`.
21
+ * 2. Invokes all registered `setOnDispose` callbacks.
22
+ * 3. Waits for the process to finish closing.
23
+ * 4. Transitions the state of the process to `"closed"`.
24
+ *
25
+ * Each time the state changes, all of the `registerOnStateChange` callbacks are called..
22
26
  */
23
27
  requestClose<T extends CloseParams>(
24
28
  target: AppProcessId,
25
- params: T
29
+ params: T,
26
30
  ): Promise<void>;
27
31
  /**
28
32
  * @public
29
- * Registers a callback to be executed when the state of the specified app process changes.
30
- * @param target - The id of the app process for which to register the callback.
31
- * @param callback - Callback function triggered on state change
32
- * @returns a disposer function to unsubscribe the callback.
33
+ * Registers a callback that runs when the state of the specified app process changes.
34
+ *
35
+ * @param target - The ID of an app process.
36
+ * @param callback - The callback to run when the state of the process changes.
37
+ *
38
+ * @returns
39
+ * A disposer function that cleans up the registered callback.
33
40
  */
34
41
  registerOnStateChange(
35
42
  target: AppProcessId,
36
- callback: (opts: { state: ProcessState }) => void
43
+ callback: OnStateChangeCallback,
37
44
  ): () => Promise<void>;
38
45
  /**
39
- * Registers a handler to receive messages sent from other app processes.
40
- * @param callback - Handler function to receive incoming messages.
41
- * @returns a disposer function to unsubscribe the handler.
46
+ * @public
47
+ * Registers a callback that listens for broadcasted messages.
48
+ *
49
+ * @param callback - The callback that listens for broadcasted messages.
50
+ *
51
+ * @returns
52
+ * A disposer function that cleans up the registered callback.
42
53
  */
43
- registerOnMessage(
44
- callback: (
45
- sender: {
46
- appProcessId: AppProcessId;
47
- surface: AppSurface;
48
- },
49
- message: any
50
- ) => void
51
- ): () => Promise<void>;
54
+ registerOnMessage(callback: OnMessageCallback): () => Promise<void>;
52
55
  /**
53
- * Broadcasts a message to all other running app processes which share the caller's AppId.
54
- * @param message - app-defined message to be broadcast. message needs to be structured data.
56
+ * @public
57
+ * Broadcasts a message to all of the app's active processes, not including the current process.
58
+ *
59
+ * @param message - The message to be broadcasted. This can be any kind of structured data.
55
60
  */
56
61
  broadcastMessage(message: any): void;
57
62
  }
@@ -64,7 +69,7 @@ export declare const appProcess: AppProcess;
64
69
 
65
70
  /**
66
71
  * @public
67
- * The unique identifier for an app process.
72
+ * The unique identifier of an app process.
68
73
  */
69
74
  export declare type AppProcessId = string & {
70
75
  __appProcessId: never;
@@ -76,7 +81,7 @@ export declare type AppProcessId = string & {
76
81
  */
77
82
  export declare type AppProcessInfo<T> = {
78
83
  /**
79
- * The surface on which the app is running.
84
+ * The surface on which the app process is running.
80
85
  */
81
86
  surface: AppSurface;
82
87
  /**
@@ -84,144 +89,213 @@ export declare type AppProcessInfo<T> = {
84
89
  */
85
90
  processId: AppProcessId;
86
91
  /**
87
- * The parameters provided to the app at the time of its process launch.
92
+ * Parameters passed to the app process when it was opened.
88
93
  */
89
94
  launchParams?: T;
90
95
  };
91
96
 
92
97
  /**
93
98
  * @public
94
- * The types of surfaces that can run an app process.
99
+ * The type of surface on which an app process can run.
100
+ *
101
+ * @remarks
102
+ * The possible surfaces include:
103
+ *
104
+ * - `"headless"` - A surface for when there is no visible user interface.
105
+ * - `"object_panel"` - A surface that renders a user interface in the side panel of the Canva editor.
106
+ * - `"selected_image_overlay"` - A surface that can be opened on top of a selected image.
95
107
  */
96
108
  export declare type AppSurface =
97
- /**
98
- * The object panel on the left side of the editing UI.
99
- */
100
109
  | "object_panel"
101
- /**
102
- * The overlay surface on top of a selected image in the design.
103
- */
104
- | "selected_image_overlay";
110
+ | "selected_image_overlay"
111
+ | "headless";
105
112
 
106
113
  /**
107
114
  * @public
108
- * The parameters specified when closing an app process.
109
- * @remarks
110
- * CloseParams are passed on to the callback function registered for handling the termination of an app process,
111
- * and provide additional context for ending the process.
112
- * For example, Apps should persist unsaved changes when the close reason is 'completed'.
115
+ * Parameters passed to the `setOnDispose` callback when a process is about to close.
113
116
  */
114
117
  export declare type CloseParams = {
118
+ /**
119
+ * The reason the app process is closing.
120
+ */
115
121
  reason: CloseReason;
116
122
  };
117
123
 
118
124
  /**
119
125
  * @public
120
- * The reasons why an app process is closed.
126
+ * The reason why an app process is closing.
127
+ *
128
+ * @remarks
129
+ * The possible reasons include:
130
+ *
131
+ * - `"completed"` - Indicates that a workflow has been completed and unsaved changes should be saved.
132
+ * - `"aborted"` - Indicates that a workflow has been abandoned and unsaved changes should be discarded.
121
133
  */
122
- export declare type CloseReason =
123
- /**
124
- * Indicates the workflow is completed, for example, when user clicks a 'Save and Close' in the App.
125
- * Any unsaved changes should be saved before closing.
126
- */
127
- | "completed"
128
- /**
129
- * Indicates user has aborted the workflow, for example, when user clicked the 'cancel' button in the App.
130
- * App should be closed without saving any current changes.
131
- */
132
- | "aborted";
134
+ export declare type CloseReason = "completed" | "aborted";
133
135
 
134
136
  /**
135
137
  * @public
136
- * Represents and exposes functionality specific to the currently running app process.
138
+ * Provides methods for interacting with the current app process.
137
139
  */
138
140
  export declare type CurrentAppProcess = {
139
141
  /**
140
142
  * @public
141
- * Retrieves information about the current app process.
143
+ * Returns information about the current app process.
142
144
  */
143
145
  getInfo<T extends any>(): AppProcessInfo<T>;
144
146
  /**
145
147
  * @public
146
- * Requests the current app process be closed.
147
- * @param params - A parameters object passed to all callback functions registered via registerOnBeforeClose API for the current process.
148
- * In addition to the required 'reason' field, app can choose to pass any structured data via params.
149
- * @remarks
150
- * A successful invocation indicates the platform has begun shutdown following procedure for the current process, which:
151
- * 1. Transitions process state to `CLOSING`, triggering all registered state change callbacks.
152
- * 2. Invokes all registered onBeforeClose callbacks.
153
- * 3. Waits for the process to close.
154
- * 4. Transitions process state to `CLOSED`, triggering all registered state change callbacks.
148
+ * Requests the termination of the current process.
149
+ *
150
+ * @param params - Parameters to pass to the `setOnDispose` callback. Any structured data can be passed via this property.
151
+ *
155
152
  * @remarks
156
- * Once initiated, the closing process is irreversible and the process will NOT receive any events or messages from other processes.
153
+ * Once called, this method:
154
+ *
155
+ * 1. Transitions the state of the process to `"closing"`.
156
+ * 2. Invokes all registered `setOnDispose` callbacks.
157
+ * 3. Waits for the process to finish closing.
158
+ * 4. Transitions the state of the process to `"closed"`.
159
+ *
160
+ * Each time the state changes, all of the `registerOnStateChange` callbacks are called.
157
161
  */
158
162
  requestClose<T extends CloseParams>(params: T): Promise<void>;
159
163
  /**
160
164
  * @public
161
- * Registers a callback to be executed when the current app process is about to close.
162
- * @remarks Only allow registering one callback.
163
- * Subsequential invokes of the API will override the existing callback.
164
- * The process remains open until the callback resolve or a timeout occurs.
165
- * @param callback - The callback function to be invoked before closure.
166
- * @returns a disposer function to unsubscribe the callback.
167
- * @remarks Complete execution of the callback is not guaranteed. Certain user actions (e.g. closing tabs, top level navigation) may terminate app processes prematurely. Consider these callbacks **best effort only.**
165
+ * Registers a callback that runs when the current app process is about to close.
166
+ *
167
+ * @param callback - The callback to run when the current app process is about to close.
168
+ *
169
+ * @returns
170
+ * A disposer function that cleans up the registered callback.
171
+ *
172
+ * @remarks
173
+ * - Apps can't register multiple callbacks.
174
+ * - If an app attempts to register multiple callbacks, only the last callback will be registered.
175
+ * - The app process will remain open until the callback resolves or a timeout error occurs.
176
+ * - The complete execution of the callback is not guaranteed as some user actions (e.g. closing tabs) may close the process prematurely.
168
177
  */
169
178
  setOnDispose<T extends CloseParams>(
170
- callback: OnDisposeCallback<T>
179
+ callback: OnDisposeCallback<T>,
171
180
  ): () => Promise<void>;
172
181
  };
173
182
 
183
+ /**
184
+ * @public
185
+ * Disposes an event listener.
186
+ */
187
+ declare type Disposer = () => void;
188
+
189
+ /**
190
+ * @public
191
+ * An SDK method that can be inspected for feature support.
192
+ */
193
+ export declare type Feature = (...args: any[]) => unknown;
194
+
195
+ /**
196
+ * An alias for the FeatureSupport interface for interacting with feature controls.
197
+ * @public
198
+ */
199
+ export declare const features: FeatureSupport;
200
+
201
+ /**
202
+ * @public
203
+ * Client interface for checking if a given SDK method is supported the currently presented context.
204
+ */
205
+ export declare interface FeatureSupport {
206
+ /**
207
+ * @public
208
+ * Checks whether a given SDK method is supported under the current context.
209
+ * @param features - Canva SDK methods to be checked for support.
210
+ */
211
+ isSupported(...features: Feature[]): boolean;
212
+ /**
213
+ * @public
214
+ * Registers an event listener for changes in what SDK methods are currently supported.
215
+ * @param onSupportChange - A callback that gets fired upon any change to the feature support context.
216
+ */
217
+ registerOnSupportChange(onSupportChange: () => void): Disposer;
218
+ }
219
+
174
220
  /**
175
221
  * @public
176
222
  * Returns information about the platform on which the app is running.
177
223
  */
178
- export declare function getPlatformInfo(): PlatformInfo;
224
+ export declare const getPlatformInfo: () => PlatformInfo;
179
225
 
180
226
  /**
181
227
  * @public
182
- * The type of a callback that is invoked when an app process is being closed.
183
- * @returns a promise.
228
+ * A callback that runs when an app process is about to close.
229
+ * @param opts - Parameters passed to the `setOnDispose` callback when a process is about to close.
184
230
  */
185
231
  export declare type OnDisposeCallback<T extends CloseParams> = (
186
- opts: T
232
+ opts: T,
187
233
  ) => Promise<void>;
188
234
 
189
235
  /**
190
236
  * @public
191
- * The result of opening an external URL when a user chooses to not navigate away from Canva.
237
+ * A callback that runs when an app process receives a broadcasted message.
238
+ *
239
+ * @param sender - Information about the process that sent the message.
240
+ * - sender.appProcessId - The ID of the process that sent the message.
241
+ * - sender.surface - The surface of the process that sent the message.
242
+ * @param message - The broadcasted message.
243
+ */
244
+ export declare type OnMessageCallback = (
245
+ sender: {
246
+ appProcessId: AppProcessId;
247
+ surface: AppSurface;
248
+ },
249
+ message: any,
250
+ ) => Promise<void>;
251
+
252
+ /**
253
+ * @public
254
+ * A callback that runs when the state of a process changes.
255
+ *
256
+ * @param opts - Information about the state change.
257
+ * - opts.state - The state of the process.
258
+ */
259
+ export declare type OnStateChangeCallback = (opts: {
260
+ state: ProcessState;
261
+ }) => void;
262
+
263
+ /**
264
+ * @public
265
+ * The result when a user doesn't agree to navigate to an external URL.
192
266
  */
193
267
  export declare type OpenExternalUrlAborted = {
194
268
  /**
195
269
  * The status of the request.
196
270
  */
197
- status: "ABORTED";
271
+ status: "aborted";
198
272
  };
199
273
 
200
274
  /**
201
275
  * @public
202
- * The result of opening an external URL when a user agrees to navigate away from Canva.
276
+ * The result when a user agrees to navigate to an external URL.
203
277
  */
204
278
  export declare type OpenExternalUrlCompleted = {
205
279
  /**
206
280
  * The status of the request.
207
281
  */
208
- status: "COMPLETED";
282
+ status: "completed";
209
283
  };
210
284
 
211
285
  /**
212
286
  * @public
213
- * The options for opening an external URL.
287
+ * Options for prompting the user to open an external URL.
214
288
  */
215
289
  export declare type OpenExternalUrlRequest = {
216
290
  /**
217
- * The URL to open in a new window
291
+ * The URL to open.
218
292
  */
219
293
  url: string;
220
294
  };
221
295
 
222
296
  /**
223
297
  * @public
224
- * The result of opening an external URL.
298
+ * The result of prompting the user to open an external URL.
225
299
  */
226
300
  export declare type OpenExternalUrlResponse =
227
301
  | OpenExternalUrlCompleted
@@ -242,40 +316,35 @@ export declare type PlatformInfo = {
242
316
  * to render payment-related call-to-actions while running on those platforms.
243
317
  *
244
318
  * @example
319
+ * ```ts
245
320
  * const info = getPlatformInfo();
246
321
  *
247
322
  * if (info.canAcceptPayments) {
248
323
  * // Display payment links and upgrade flows
249
324
  * } else {
250
325
  * // Hide payment links and upgrade flows
251
- * // Optionally show an appropriate message
326
+ * // Optionally, show an appropriate message
252
327
  * }
328
+ * ```
253
329
  */
254
330
  canAcceptPayments: boolean;
255
331
  };
256
332
 
257
333
  /**
258
334
  * @public
259
- * The states are an app process.
335
+ * The state of an app process.
336
+ *
337
+ * @remarks
338
+ * The possible states include:
339
+ *
340
+ * - `"opening"` - The app process is opening.
341
+ * - `"open"` - The app process is open, active, and visible on the designated surface.
342
+ * - `"closing"` - The app process is closing.
343
+ * - `"closed"` - The app process has been closed and is no longer active.
344
+ *
345
+ * While a process is closing, it won't receive any events or messages from other processes.
260
346
  */
261
- export declare type ProcessState =
262
- /**
263
- * The app process is in the process of opening.
264
- */
265
- | "opening"
266
- /**
267
- * The app process is active and visible to the user on its designated surface.
268
- */
269
- | "open"
270
- /**
271
- * The app process is in the process of closing.
272
- * At this state, the process becomes non-interactive and won't receive any events and messages from other processes.
273
- */
274
- | "closing"
275
- /**
276
- * The app process has been terminated and is no longer running.
277
- */
278
- | "closed";
347
+ export declare type ProcessState = "opening" | "open" | "closing" | "closed";
279
348
 
280
349
  /**
281
350
  * @public
@@ -286,8 +355,8 @@ export declare type ProcessState =
286
355
  *
287
356
  * In some browsers, the user must enable popup permissions before any URL can be opened.
288
357
  */
289
- export declare function requestOpenExternalUrl(
290
- request: OpenExternalUrlRequest
291
- ): Promise<OpenExternalUrlResponse>;
358
+ export declare const requestOpenExternalUrl: (
359
+ request: OpenExternalUrlRequest,
360
+ ) => Promise<OpenExternalUrlResponse>;
292
361
 
293
362
  export {};
@@ -0,0 +1,29 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "createFakePlatformClients", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return createFakePlatformClients;
9
+ }
10
+ });
11
+ const _fake_app_process_client = require("./fake_app_process_client");
12
+ const _fake_feature_support_client = require("./fake_feature_support_client");
13
+ const _fake_platform_client = require("./fake_platform_client");
14
+ function createFakePlatformClients() {
15
+ const appProcess = new _fake_app_process_client.FakeAppProcessClient();
16
+ const platform = new _fake_platform_client.FakePlatformClient();
17
+ const features = new _fake_feature_support_client.FakeFeatureSupportClient();
18
+ const i18n = undefined;
19
+ return {
20
+ platform: {
21
+ v2: {
22
+ platform,
23
+ appProcess,
24
+ i18n,
25
+ features
26
+ }
27
+ }
28
+ };
29
+ }
@@ -0,0 +1,41 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "FakeAppProcessClient", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return FakeAppProcessClient;
9
+ }
10
+ });
11
+ class FakeAppProcessClient {
12
+ async requestClose(target, params) {
13
+ await Promise.resolve();
14
+ }
15
+ registerOnStateChange(target, callback) {
16
+ return async ()=>{
17
+ await Promise.resolve();
18
+ };
19
+ }
20
+ registerOnMessage(callback) {
21
+ return async ()=>{
22
+ await Promise.resolve();
23
+ };
24
+ }
25
+ broadcastMessage(message) {}
26
+ constructor(){
27
+ this.current = {
28
+ getInfo: ()=>({
29
+ processId: 'test-process-id',
30
+ surface: 'object_panel'
31
+ }),
32
+ requestClose: async (params)=>{
33
+ await Promise.resolve();
34
+ },
35
+ setOnDispose: (callback)=>async ()=>{
36
+ await Promise.resolve();
37
+ },
38
+ registerOnStateChangeInternal: (callback)=>()=>Promise.resolve()
39
+ };
40
+ }
41
+ }
@@ -0,0 +1,19 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "FakeFeatureSupportClient", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return FakeFeatureSupportClient;
9
+ }
10
+ });
11
+ class FakeFeatureSupportClient {
12
+ isSupported(...features) {
13
+ return true;
14
+ }
15
+ registerOnSupportChange(onSupportChange) {
16
+ onSupportChange();
17
+ return ()=>{};
18
+ }
19
+ }
@@ -0,0 +1,23 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "FakePlatformClient", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return FakePlatformClient;
9
+ }
10
+ });
11
+ class FakePlatformClient {
12
+ async requestOpenExternalUrl(options) {
13
+ await Promise.resolve();
14
+ return {
15
+ status: 'completed'
16
+ };
17
+ }
18
+ getPlatformInfo() {
19
+ return {
20
+ canAcceptPayments: true
21
+ };
22
+ }
23
+ }
@@ -1,5 +1,4 @@
1
- // Copyright 2023 Canva Inc. All Rights Reserved.
2
- "use strict";
1
+ "use strict"
3
2
  Object.defineProperty(exports, "__esModule", {
4
3
  value: true
5
4
  });
@@ -17,3 +16,5 @@ function _export_star(from, to) {
17
16
  });
18
17
  return from;
19
18
  }
19
+ var _window___canva___sdkRegistration, _window___canva__;
20
+ (_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : (_window___canva___sdkRegistration = _window___canva__.sdkRegistration) === null || _window___canva___sdkRegistration === void 0 ? void 0 : _window___canva___sdkRegistration.registerPackageVersion('platform', '2.1.0', 'ga');
@@ -1,5 +1,4 @@
1
- // Copyright 2023 Canva Inc. All Rights Reserved.
2
- "use strict";
1
+ "use strict"
3
2
  Object.defineProperty(exports, "__esModule", {
4
3
  value: true
5
4
  });
@@ -13,6 +12,9 @@ _export(exports, {
13
12
  appProcess: function() {
14
13
  return appProcess;
15
14
  },
15
+ features: function() {
16
+ return features;
17
+ },
16
18
  requestOpenExternalUrl: function() {
17
19
  return requestOpenExternalUrl;
18
20
  },
@@ -20,11 +22,8 @@ _export(exports, {
20
22
  return getPlatformInfo;
21
23
  }
22
24
  });
23
- const { canva } = window;
24
- const appProcess = canva.appProcess;
25
- function requestOpenExternalUrl(request) {
26
- return canva.platform.requestOpenExternalUrl(request);
27
- }
28
- function getPlatformInfo() {
29
- return canva.platform.getPlatformInfo();
30
- }
25
+ const { canva_sdk } = window;
26
+ const appProcess = canva_sdk.platform.v2.appProcess;
27
+ const features = canva_sdk.platform.v2.features;
28
+ const requestOpenExternalUrl = canva_sdk.platform.v2.platform.requestOpenExternalUrl;
29
+ const getPlatformInfo = canva_sdk.platform.v2.platform.getPlatformInfo;
@@ -0,0 +1,16 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "initTestEnvironment", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return initTestEnvironment;
9
+ }
10
+ });
11
+ const _create = require('../fake/create');
12
+ const _canva_sdk = require('../../utils/canva_sdk');
13
+ function initTestEnvironment() {
14
+ (0, _canva_sdk.assertIsTestCanvaSdk)();
15
+ (0, _canva_sdk.injectFakeAPIClients)((0, _create.createFakePlatformClients)());
16
+ }
@@ -0,0 +1,41 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ getCanvaSdk: function() {
13
+ return getCanvaSdk;
14
+ },
15
+ assertIsTestCanvaSdk: function() {
16
+ return assertIsTestCanvaSdk;
17
+ },
18
+ injectFakeAPIClients: function() {
19
+ return injectFakeAPIClients;
20
+ }
21
+ });
22
+ function getCanvaSdk() {
23
+ return window.canva_sdk;
24
+ }
25
+ function assertIsTestCanvaSdk() {
26
+ var _window___canva__;
27
+ if ((_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : _window___canva__.uiKit) {
28
+ var _getCanvaSdk_error, _getCanvaSdk;
29
+ const CanvaError = (_getCanvaSdk = getCanvaSdk()) === null || _getCanvaSdk === void 0 ? void 0 : (_getCanvaSdk_error = _getCanvaSdk.error) === null || _getCanvaSdk_error === void 0 ? void 0 : _getCanvaSdk_error.v2.CanvaError;
30
+ throw new CanvaError({
31
+ code: 'failed_precondition',
32
+ message: "Canva App SDK: You're attempting to call `initTestEnvironment` in a non-test environment, such as in production. This method should be called in test environments, once and only once. For more info refer to https://canva.dev/docs/apps/testing/"
33
+ });
34
+ }
35
+ }
36
+ function injectFakeAPIClients(clients) {
37
+ window.canva_sdk = {
38
+ ...getCanvaSdk(),
39
+ ...clients
40
+ };
41
+ }
@@ -0,0 +1,19 @@
1
+ import { FakeAppProcessClient } from './fake_app_process_client';
2
+ import { FakeFeatureSupportClient } from './fake_feature_support_client';
3
+ import { FakePlatformClient } from './fake_platform_client';
4
+ export function createFakePlatformClients() {
5
+ const appProcess = new FakeAppProcessClient();
6
+ const platform = new FakePlatformClient();
7
+ const features = new FakeFeatureSupportClient();
8
+ const i18n = undefined;
9
+ return {
10
+ platform: {
11
+ v2: {
12
+ platform,
13
+ appProcess,
14
+ i18n,
15
+ features
16
+ }
17
+ }
18
+ };
19
+ }
@@ -0,0 +1,31 @@
1
+ export class FakeAppProcessClient {
2
+ async requestClose(target, params) {
3
+ await Promise.resolve();
4
+ }
5
+ registerOnStateChange(target, callback) {
6
+ return async ()=>{
7
+ await Promise.resolve();
8
+ };
9
+ }
10
+ registerOnMessage(callback) {
11
+ return async ()=>{
12
+ await Promise.resolve();
13
+ };
14
+ }
15
+ broadcastMessage(message) {}
16
+ constructor(){
17
+ this.current = {
18
+ getInfo: ()=>({
19
+ processId: 'test-process-id',
20
+ surface: 'object_panel'
21
+ }),
22
+ requestClose: async (params)=>{
23
+ await Promise.resolve();
24
+ },
25
+ setOnDispose: (callback)=>async ()=>{
26
+ await Promise.resolve();
27
+ },
28
+ registerOnStateChangeInternal: (callback)=>()=>Promise.resolve()
29
+ };
30
+ }
31
+ }
@@ -0,0 +1,9 @@
1
+ export class FakeFeatureSupportClient {
2
+ isSupported(...features) {
3
+ return true;
4
+ }
5
+ registerOnSupportChange(onSupportChange) {
6
+ onSupportChange();
7
+ return ()=>{};
8
+ }
9
+ }
@@ -0,0 +1,13 @@
1
+ export class FakePlatformClient {
2
+ async requestOpenExternalUrl(options) {
3
+ await Promise.resolve();
4
+ return {
5
+ status: 'completed'
6
+ };
7
+ }
8
+ getPlatformInfo() {
9
+ return {
10
+ canAcceptPayments: true
11
+ };
12
+ }
13
+ }
@@ -1,2 +1,3 @@
1
- // Copyright 2023 Canva Inc. All Rights Reserved.
1
+ var _window___canva___sdkRegistration, _window___canva__;
2
2
  export * from './public';
3
+ (_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : (_window___canva___sdkRegistration = _window___canva__.sdkRegistration) === null || _window___canva___sdkRegistration === void 0 ? void 0 : _window___canva___sdkRegistration.registerPackageVersion('platform', '2.1.0', 'ga');
@@ -1,23 +1,5 @@
1
- // Copyright 2023 Canva Inc. All Rights Reserved.
2
- const { canva } = window;
3
- /**
4
- * An alias for the AppProcess interface for interacting with the App Process.
5
- * @public
6
- */ export const appProcess = canva.appProcess;
7
- /**
8
- * @public
9
- * Opens an external URL.
10
- *
11
- * @remarks
12
- * The URL is opened natively, such as in a new browser tab on desktop or in a browser sheet on mobile.
13
- *
14
- * In some browsers, the user must enable popup permissions before any URL can be opened.
15
- */ export function requestOpenExternalUrl(request) {
16
- return canva.platform.requestOpenExternalUrl(request);
17
- }
18
- /**
19
- * @public
20
- * Returns information about the platform on which the app is running.
21
- */ export function getPlatformInfo() {
22
- return canva.platform.getPlatformInfo();
23
- }
1
+ const { canva_sdk } = window;
2
+ export const appProcess = canva_sdk.platform.v2.appProcess;
3
+ export const features = canva_sdk.platform.v2.features;
4
+ export const requestOpenExternalUrl = canva_sdk.platform.v2.platform.requestOpenExternalUrl;
5
+ export const getPlatformInfo = canva_sdk.platform.v2.platform.getPlatformInfo;
@@ -0,0 +1,6 @@
1
+ import { createFakePlatformClients } from '../fake/create';
2
+ import { assertIsTestCanvaSdk, injectFakeAPIClients } from '../../utils/canva_sdk';
3
+ export function initTestEnvironment() {
4
+ assertIsTestCanvaSdk();
5
+ injectFakeAPIClients(createFakePlatformClients());
6
+ }
@@ -0,0 +1,20 @@
1
+ export function getCanvaSdk() {
2
+ return window.canva_sdk;
3
+ }
4
+ export function assertIsTestCanvaSdk() {
5
+ var _window___canva__;
6
+ if ((_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : _window___canva__.uiKit) {
7
+ var _getCanvaSdk_error, _getCanvaSdk;
8
+ const CanvaError = (_getCanvaSdk = getCanvaSdk()) === null || _getCanvaSdk === void 0 ? void 0 : (_getCanvaSdk_error = _getCanvaSdk.error) === null || _getCanvaSdk_error === void 0 ? void 0 : _getCanvaSdk_error.v2.CanvaError;
9
+ throw new CanvaError({
10
+ code: 'failed_precondition',
11
+ message: "Canva App SDK: You're attempting to call `initTestEnvironment` in a non-test environment, such as in production. This method should be called in test environments, once and only once. For more info refer to https://canva.dev/docs/apps/testing/"
12
+ });
13
+ }
14
+ }
15
+ export function injectFakeAPIClients(clients) {
16
+ window.canva_sdk = {
17
+ ...getCanvaSdk(),
18
+ ...clients
19
+ };
20
+ }
package/package.json CHANGED
@@ -1,18 +1,24 @@
1
1
  {
2
2
  "name": "@canva/platform",
3
- "version": "1.1.0",
3
+ "version": "2.1.0",
4
4
  "description": "The Canva Apps SDK app platform library",
5
5
  "author": "Canva Pty Ltd.",
6
6
  "license": "SEE LICENSE IN LICENSE.md FILE",
7
7
  "peerDependencies": {
8
- "@canva/error": "^1.0.0"
8
+ "@canva/error": "^2.0.0"
9
9
  },
10
- "main": "lib/cjs/sdk/platform/index.js",
11
- "module": "lib/esm/sdk/platform/index.js",
10
+ "main": "./lib/cjs/sdk/platform/index.js",
11
+ "module": "./lib/esm/sdk/platform/index.js",
12
12
  "exports": {
13
13
  ".": {
14
+ "types": "./index.d.ts",
14
15
  "require": "./lib/cjs/sdk/platform/index.js",
15
16
  "import": "./lib/esm/sdk/platform/index.js"
17
+ },
18
+ "./test": {
19
+ "types": "./test/index.d.ts",
20
+ "require": "./lib/cjs/sdk/platform/test/index.js",
21
+ "import": "./lib/esm/sdk/platform/test/index.js"
16
22
  }
17
23
  },
18
24
  "typings": "./index.d.ts"
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @public
3
+ * Initializes a test environment for the `@canva/platform` package, enabling unit tests to mock Canva's APIs.
4
+ * @remarks
5
+ * This method should only be called once in a test environment, such as in a Jest setup file.
6
+ * @see
7
+ * https://www.canva.dev/docs/apps/testing/
8
+ */
9
+ export declare function initTestEnvironment(): void;
10
+
11
+ export {};