@canva/platform 2.0.0 → 2.1.1-beta.1

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.
@@ -0,0 +1,28 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "notification", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return notification;
9
+ }
10
+ });
11
+ _export_star(require("./public"), exports);
12
+ function _export_star(from, to) {
13
+ Object.keys(from).forEach(function(k) {
14
+ if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
15
+ Object.defineProperty(to, k, {
16
+ enumerable: true,
17
+ get: function() {
18
+ return from[k];
19
+ }
20
+ });
21
+ }
22
+ });
23
+ return from;
24
+ }
25
+ var _window___canva___sdkRegistration, _window___canva__;
26
+ (_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.1', 'beta');
27
+ const { canva_sdk } = window;
28
+ const notification = canva_sdk.platform.v2.notification;
@@ -0,0 +1,32 @@
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_notification_client = require("./fake_notification_client");
14
+ const _fake_platform_client = require("./fake_platform_client");
15
+ function createFakePlatformClients() {
16
+ const appProcess = new _fake_app_process_client.FakeAppProcessClient();
17
+ const platform = new _fake_platform_client.FakePlatformClient();
18
+ const features = new _fake_feature_support_client.FakeFeatureSupportClient();
19
+ const notification = new _fake_notification_client.FakeNotificationClient();
20
+ const i18n = undefined;
21
+ return {
22
+ platform: {
23
+ v2: {
24
+ platform,
25
+ appProcess,
26
+ i18n,
27
+ features,
28
+ notification
29
+ }
30
+ }
31
+ };
32
+ }
@@ -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,17 @@
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "FakeNotificationClient", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return FakeNotificationClient;
9
+ }
10
+ });
11
+ class FakeNotificationClient {
12
+ async addToast(options) {
13
+ return {
14
+ status: 'completed'
15
+ };
16
+ }
17
+ }
@@ -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
+ }
@@ -15,19 +15,15 @@ _export(exports, {
15
15
  features: function() {
16
16
  return features;
17
17
  },
18
- requestOpenExternalUrl: function() {
19
- return requestOpenExternalUrl;
20
- },
21
18
  getPlatformInfo: function() {
22
19
  return getPlatformInfo;
20
+ },
21
+ requestOpenExternalUrl: function() {
22
+ return requestOpenExternalUrl;
23
23
  }
24
24
  });
25
25
  const { canva_sdk } = window;
26
26
  const appProcess = canva_sdk.platform.v2.appProcess;
27
27
  const features = canva_sdk.platform.v2.features;
28
- function requestOpenExternalUrl(request) {
29
- return canva_sdk.platform.v2.platform.requestOpenExternalUrl(request);
30
- }
31
- function getPlatformInfo() {
32
- return canva_sdk.platform.v2.platform.getPlatformInfo();
33
- }
28
+ const requestOpenExternalUrl = canva_sdk.platform.v2.platform.requestOpenExternalUrl;
29
+ const getPlatformInfo = canva_sdk.platform.v2.platform.getPlatformInfo;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
- _export_star(require("./public"), exports);
5
+ _export_star(require("./index"), exports);
6
6
  function _export_star(from, to) {
7
7
  Object.keys(from).forEach(function(k) {
8
8
  if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
@@ -16,6 +16,3 @@ function _export_star(from, to) {
16
16
  });
17
17
  return from;
18
18
  }
19
- var _window___canva___sdkRegistration, _window___canva__;
20
- (_window___canva__ =
21
- 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.0.0', 'ga');
@@ -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,65 @@
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
+ assertIsTestCanvaSdk: function() {
13
+ return assertIsTestCanvaSdk;
14
+ },
15
+ bindMethodsToClients: function() {
16
+ return bindMethodsToClients;
17
+ },
18
+ getCanvaSdk: function() {
19
+ return getCanvaSdk;
20
+ },
21
+ injectFakeAPIClients: function() {
22
+ return injectFakeAPIClients;
23
+ }
24
+ });
25
+ function getCanvaSdk() {
26
+ return window.canva_sdk;
27
+ }
28
+ function assertIsTestCanvaSdk() {
29
+ var _window___canva__;
30
+ if ((_window___canva__ = window.__canva__) === null || _window___canva__ === void 0 ? void 0 : _window___canva__.uiKit) {
31
+ var _getCanvaSdk_error, _getCanvaSdk;
32
+ 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;
33
+ throw new CanvaError({
34
+ code: 'failed_precondition',
35
+ 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/"
36
+ });
37
+ }
38
+ }
39
+ function injectFakeAPIClients(clients) {
40
+ bindMethodsToClients(clients);
41
+ window.canva_sdk = {
42
+ ...getCanvaSdk(),
43
+ ...clients
44
+ };
45
+ }
46
+ function bindMethodsToClients(objectToBind) {
47
+ if (typeof objectToBind !== 'object' || objectToBind == null)
48
+ return;
49
+ const classMethods = new Set();
50
+ let currentPrototype = Object.getPrototypeOf(objectToBind);
51
+ while(currentPrototype && currentPrototype !== Object.prototype){
52
+ Object.getOwnPropertyNames(currentPrototype).forEach((method)=>classMethods.add(method));
53
+ currentPrototype = Object.getPrototypeOf(currentPrototype);
54
+ }
55
+ classMethods.delete('constructor');
56
+ for (const method of classMethods) {
57
+ const originalFn = objectToBind[method];
58
+ if (typeof originalFn === 'function') Object.defineProperty(objectToBind, method, {
59
+ value: (...args)=>{
60
+ return originalFn.call(objectToBind, ...args);
61
+ }
62
+ });
63
+ }
64
+ Object.values(objectToBind).forEach(bindMethodsToClients);
65
+ }
@@ -0,0 +1,5 @@
1
+ var _window___canva___sdkRegistration, _window___canva__;
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.1', 'beta');
4
+ const { canva_sdk } = window;
5
+ export const notification = canva_sdk.platform.v2.notification;
@@ -0,0 +1,22 @@
1
+ import { FakeAppProcessClient } from './fake_app_process_client';
2
+ import { FakeFeatureSupportClient } from './fake_feature_support_client';
3
+ import { FakeNotificationClient } from './fake_notification_client';
4
+ import { FakePlatformClient } from './fake_platform_client';
5
+ export function createFakePlatformClients() {
6
+ const appProcess = new FakeAppProcessClient();
7
+ const platform = new FakePlatformClient();
8
+ const features = new FakeFeatureSupportClient();
9
+ const notification = new FakeNotificationClient();
10
+ const i18n = undefined;
11
+ return {
12
+ platform: {
13
+ v2: {
14
+ platform,
15
+ appProcess,
16
+ i18n,
17
+ features,
18
+ notification
19
+ }
20
+ }
21
+ };
22
+ }
@@ -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,7 @@
1
+ export class FakeNotificationClient {
2
+ async addToast(options) {
3
+ return {
4
+ status: 'completed'
5
+ };
6
+ }
7
+ }
@@ -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,9 +1,5 @@
1
1
  const { canva_sdk } = window;
2
2
  export const appProcess = canva_sdk.platform.v2.appProcess;
3
3
  export const features = canva_sdk.platform.v2.features;
4
- export function requestOpenExternalUrl(request) {
5
- return canva_sdk.platform.v2.platform.requestOpenExternalUrl(request);
6
- }
7
- export function getPlatformInfo() {
8
- return canva_sdk.platform.v2.platform.getPlatformInfo();
9
- }
4
+ export const requestOpenExternalUrl = canva_sdk.platform.v2.platform.requestOpenExternalUrl;
5
+ export const getPlatformInfo = canva_sdk.platform.v2.platform.getPlatformInfo;
@@ -0,0 +1 @@
1
+ export * from './index';
@@ -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,41 @@
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
+ bindMethodsToClients(clients);
17
+ window.canva_sdk = {
18
+ ...getCanvaSdk(),
19
+ ...clients
20
+ };
21
+ }
22
+ export function bindMethodsToClients(objectToBind) {
23
+ if (typeof objectToBind !== 'object' || objectToBind == null)
24
+ return;
25
+ const classMethods = new Set();
26
+ let currentPrototype = Object.getPrototypeOf(objectToBind);
27
+ while(currentPrototype && currentPrototype !== Object.prototype){
28
+ Object.getOwnPropertyNames(currentPrototype).forEach((method)=>classMethods.add(method));
29
+ currentPrototype = Object.getPrototypeOf(currentPrototype);
30
+ }
31
+ classMethods.delete('constructor');
32
+ for (const method of classMethods) {
33
+ const originalFn = objectToBind[method];
34
+ if (typeof originalFn === 'function') Object.defineProperty(objectToBind, method, {
35
+ value: (...args)=>{
36
+ return originalFn.call(objectToBind, ...args);
37
+ }
38
+ });
39
+ }
40
+ Object.values(objectToBind).forEach(bindMethodsToClients);
41
+ }
package/package.json CHANGED
@@ -1,25 +1,25 @@
1
1
  {
2
2
  "name": "@canva/platform",
3
- "version": "2.0.0",
3
+ "version": "2.1.1-beta.1",
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
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/beta.js",
11
+ "module": "./lib/esm/sdk/platform/beta.js",
12
12
  "exports": {
13
13
  ".": {
14
- "require": {
15
- "types": "./index.d.ts",
16
- "default": "./lib/cjs/sdk/platform/index.js"
17
- },
18
- "import": {
19
- "types": "./index.d.ts",
20
- "default": "./lib/esm/sdk/platform/index.js"
21
- }
14
+ "types": "./beta.d.ts",
15
+ "require": "./lib/cjs/sdk/platform/beta.js",
16
+ "import": "./lib/esm/sdk/platform/beta.js"
17
+ },
18
+ "./test": {
19
+ "types": "./test/beta.d.ts",
20
+ "require": "./lib/cjs/sdk/platform/test/beta.js",
21
+ "import": "./lib/esm/sdk/platform/test/beta.js"
22
22
  }
23
23
  },
24
- "typings": "./index.d.ts"
24
+ "typings": "./beta.d.ts"
25
25
  }
package/test/beta.d.ts ADDED
@@ -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 {};