@canva/cli 0.0.1-beta.9 → 1.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.
Files changed (149) hide show
  1. package/README.md +131 -31
  2. package/cli.js +841 -437
  3. package/lib/cjs/index.cjs +11 -0
  4. package/lib/esm/index.mjs +11 -0
  5. package/package.json +19 -4
  6. package/templates/base/backend/routers/auth.ts +4 -4
  7. package/templates/base/backend/routers/oauth.ts +3 -3
  8. package/templates/base/eslint.config.mjs +2 -25
  9. package/templates/base/package.json +26 -23
  10. package/templates/base/scripts/ssl/ssl.ts +1 -1
  11. package/templates/base/scripts/start/app_runner.ts +42 -5
  12. package/templates/base/scripts/start/context.ts +12 -6
  13. package/templates/base/scripts/start/start.ts +12 -1
  14. package/templates/base/scripts/start/tests/start.tests.ts +61 -0
  15. package/templates/base/utils/backend/base_backend/create.ts +3 -3
  16. package/templates/base/utils/backend/bearer_middleware/bearer_middleware.ts +2 -2
  17. package/templates/base/utils/backend/jwt_middleware/jwt_middleware.ts +4 -4
  18. package/templates/base/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +1 -1
  19. package/templates/base/utils/use_add_element.ts +1 -1
  20. package/templates/base/utils/use_feature_support.ts +1 -1
  21. package/templates/{gen_ai/webpack.config.cjs → base/webpack.config.ts} +50 -68
  22. package/templates/common/.env.template +3 -3
  23. package/templates/common/README.md +16 -17
  24. package/templates/common/jest.config.mjs +1 -1
  25. package/templates/common/jest.setup.ts +1 -0
  26. package/templates/common/utils/backend/base_backend/create.ts +3 -3
  27. package/templates/common/utils/backend/jwt_middleware/jwt_middleware.ts +4 -4
  28. package/templates/common/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +1 -1
  29. package/templates/common/utils/tests/table_wrapper.tests.ts +1 -1
  30. package/templates/common/utils/use_add_element.ts +1 -1
  31. package/templates/common/utils/use_feature_support.ts +1 -1
  32. package/templates/dam/backend/routers/dam.ts +24 -20
  33. package/templates/dam/backend/server.ts +1 -1
  34. package/templates/dam/canva-app.json +16 -0
  35. package/templates/dam/eslint.config.mjs +2 -28
  36. package/templates/dam/package.json +38 -41
  37. package/templates/dam/scripts/ssl/ssl.ts +1 -1
  38. package/templates/dam/scripts/start/app_runner.ts +42 -5
  39. package/templates/dam/scripts/start/context.ts +12 -6
  40. package/templates/dam/scripts/start/start.ts +12 -1
  41. package/templates/dam/scripts/start/tests/start.tests.ts +61 -0
  42. package/templates/dam/src/app.tsx +24 -3
  43. package/templates/dam/src/config.ts +212 -87
  44. package/templates/dam/src/index.tsx +1 -1
  45. package/templates/dam/utils/backend/base_backend/create.ts +3 -3
  46. package/templates/dam/utils/backend/jwt_middleware/jwt_middleware.ts +4 -4
  47. package/templates/dam/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +1 -1
  48. package/templates/{base/webpack.config.cjs → dam/webpack.config.ts} +50 -68
  49. package/templates/data_connector/README.md +84 -0
  50. package/templates/data_connector/canva-app.json +21 -0
  51. package/templates/data_connector/declarations/declarations.d.ts +29 -0
  52. package/templates/data_connector/eslint.config.mjs +14 -0
  53. package/templates/data_connector/package.json +91 -0
  54. package/templates/data_connector/scripts/ssl/ssl.ts +131 -0
  55. package/templates/data_connector/scripts/start/app_runner.ts +201 -0
  56. package/templates/data_connector/scripts/start/context.ts +171 -0
  57. package/templates/data_connector/scripts/start/start.ts +46 -0
  58. package/templates/data_connector/scripts/start/tests/start.tests.ts +61 -0
  59. package/templates/data_connector/src/api/connect_client.ts +6 -0
  60. package/templates/data_connector/src/api/data_source.ts +96 -0
  61. package/templates/data_connector/src/api/data_sources/designs.tsx +296 -0
  62. package/templates/data_connector/src/api/data_sources/index.ts +4 -0
  63. package/templates/data_connector/src/api/data_sources/templates.tsx +329 -0
  64. package/templates/data_connector/src/api/fetch_data_table.ts +55 -0
  65. package/templates/data_connector/src/api/index.ts +4 -0
  66. package/templates/data_connector/src/api/oauth.ts +8 -0
  67. package/templates/data_connector/src/api/tests/data_source.test.tsx +99 -0
  68. package/templates/data_connector/src/app.tsx +20 -0
  69. package/templates/data_connector/src/components/app_error.tsx +15 -0
  70. package/templates/data_connector/src/components/footer.tsx +26 -0
  71. package/templates/data_connector/src/components/header.tsx +40 -0
  72. package/templates/data_connector/src/components/index.ts +3 -0
  73. package/templates/data_connector/src/components/inputs/messages.tsx +99 -0
  74. package/templates/data_connector/src/components/inputs/search_filter.tsx +108 -0
  75. package/templates/data_connector/src/components/inputs/select_field.tsx +26 -0
  76. package/templates/data_connector/src/context/app_context.tsx +124 -0
  77. package/templates/data_connector/src/context/index.ts +2 -0
  78. package/templates/data_connector/src/context/use_app_context.ts +17 -0
  79. package/templates/data_connector/src/entrypoint.tsx +70 -0
  80. package/templates/data_connector/src/home.tsx +21 -0
  81. package/templates/data_connector/src/index.tsx +68 -0
  82. package/templates/data_connector/src/pages/data_source_config.tsx +9 -0
  83. package/templates/data_connector/src/pages/error.tsx +37 -0
  84. package/templates/data_connector/src/pages/index.ts +4 -0
  85. package/templates/data_connector/src/pages/login.tsx +145 -0
  86. package/templates/data_connector/src/pages/select_source.tsx +24 -0
  87. package/templates/data_connector/src/routes/index.ts +2 -0
  88. package/templates/data_connector/src/routes/protected_route.tsx +25 -0
  89. package/templates/data_connector/src/routes/routes.tsx +46 -0
  90. package/templates/data_connector/src/utils/data_params.ts +17 -0
  91. package/templates/data_connector/src/utils/data_table.ts +115 -0
  92. package/templates/data_connector/src/utils/fetch_result.ts +36 -0
  93. package/templates/data_connector/src/utils/index.ts +2 -0
  94. package/templates/data_connector/src/utils/tests/data_table.test.ts +133 -0
  95. package/templates/data_connector/styles/components.css +38 -0
  96. package/templates/data_connector/tsconfig.json +54 -0
  97. package/templates/{dam/webpack.config.cjs → data_connector/webpack.config.ts} +50 -68
  98. package/templates/gen_ai/backend/routers/image.ts +8 -8
  99. package/templates/gen_ai/backend/server.ts +1 -1
  100. package/templates/gen_ai/canva-app.json +20 -0
  101. package/templates/gen_ai/eslint.config.mjs +2 -25
  102. package/templates/gen_ai/package.json +42 -43
  103. package/templates/gen_ai/scripts/ssl/ssl.ts +1 -1
  104. package/templates/gen_ai/scripts/start/app_runner.ts +42 -5
  105. package/templates/gen_ai/scripts/start/context.ts +12 -6
  106. package/templates/gen_ai/scripts/start/start.ts +12 -1
  107. package/templates/gen_ai/scripts/start/tests/start.tests.ts +61 -0
  108. package/templates/gen_ai/src/app.tsx +3 -3
  109. package/templates/gen_ai/src/components/footer.tsx +4 -4
  110. package/templates/gen_ai/src/components/image_grid.tsx +1 -1
  111. package/templates/gen_ai/src/components/loading_results.tsx +8 -8
  112. package/templates/gen_ai/src/components/prompt_input.tsx +3 -3
  113. package/templates/gen_ai/src/components/remaining_credits.tsx +1 -1
  114. package/templates/gen_ai/src/components/report_box.tsx +1 -1
  115. package/templates/gen_ai/src/components/tests/remaining_credit.tests.tsx +2 -2
  116. package/templates/gen_ai/src/home.tsx +2 -2
  117. package/templates/gen_ai/src/pages/error.tsx +2 -2
  118. package/templates/gen_ai/src/pages/results.tsx +1 -1
  119. package/templates/gen_ai/utils/backend/base_backend/create.ts +3 -3
  120. package/templates/gen_ai/utils/backend/bearer_middleware/bearer_middleware.ts +2 -2
  121. package/templates/{hello_world/webpack.config.cjs → gen_ai/webpack.config.ts} +50 -68
  122. package/templates/hello_world/canva-app.json +16 -0
  123. package/templates/hello_world/eslint.config.mjs +2 -25
  124. package/templates/hello_world/package.json +35 -38
  125. package/templates/hello_world/scripts/copy_env.ts +10 -0
  126. package/templates/hello_world/scripts/ssl/ssl.ts +1 -1
  127. package/templates/hello_world/scripts/start/app_runner.ts +42 -5
  128. package/templates/hello_world/scripts/start/context.ts +12 -6
  129. package/templates/hello_world/scripts/start/start.ts +12 -1
  130. package/templates/hello_world/scripts/start/tests/start.tests.ts +61 -0
  131. package/templates/hello_world/src/app.tsx +1 -1
  132. package/templates/hello_world/src/index.tsx +1 -1
  133. package/templates/hello_world/src/tests/__snapshots__/app.tests.tsx.snap +5 -5
  134. package/templates/hello_world/src/tests/app.tests.tsx +3 -3
  135. package/templates/hello_world/utils/use_add_element.ts +1 -1
  136. package/templates/hello_world/utils/use_feature_support.ts +1 -1
  137. package/templates/hello_world/webpack.config.ts +254 -0
  138. package/templates/optional/.cursor/mcp.json +8 -0
  139. package/templates/optional/.vscode/mcp.json +9 -0
  140. package/templates/optional/AGENTS.md +76 -0
  141. package/templates/optional/CLAUDE.md +76 -0
  142. package/templates/common/conf/eslint-general.mjs +0 -303
  143. package/templates/common/conf/eslint-i18n.mjs +0 -41
  144. package/templates/common/conf/eslint-local-i18n-rules/index.mjs +0 -181
  145. /package/templates/base/scripts/{copy-env.ts → copy_env.ts} +0 -0
  146. /package/templates/dam/scripts/{copy-env.ts → copy_env.ts} +0 -0
  147. /package/templates/{gen_ai/scripts/copy-env.ts → data_connector/scripts/copy_env.ts} +0 -0
  148. /package/templates/{hello_world/scripts/copy-env.ts → gen_ai/scripts/copy_env.ts} +0 -0
  149. /package/templates/{common → optional}/.vscode/extensions.json +0 -0
@@ -1,95 +1,220 @@
1
1
  import type { Config } from "@canva/app-components";
2
+ import { useIntl } from "react-intl";
2
3
 
3
4
  type ContainerTypes = "folder";
4
- export const config: Config<ContainerTypes> = {
5
- serviceName: "Example App",
6
- search: {
7
- enabled: true,
8
- filterFormConfig: {
9
- containerTypes: ["folder"],
10
- filters: [
11
- {
12
- filterType: "CHECKBOX",
13
- label: "File Type",
14
- key: "fileType",
15
- options: [
16
- { value: "mp4", label: "MP4" },
17
- { value: "png", label: "PNG" },
18
- { value: "jpeg", label: "JPEG" },
19
- ],
20
- allowCustomValue: true,
21
- },
22
- {
23
- filterType: "RADIO",
24
- label: "Size",
25
- key: "size",
26
- options: [
27
- {
28
- label: "Large (800+ px)",
29
- value: "large",
30
- },
31
- {
32
- label: "Medium (200-799px)",
33
- value: "medium",
34
- },
35
- ],
36
- allowCustomValue: true,
37
- customValueInputType: "SIZE_RANGE",
38
- },
39
- {
40
- filterType: "RADIO",
41
- label: "Update Date",
42
- key: "updateDate",
43
- options: [
44
- { value: ">now-30m", label: "Last 30 Minutes" },
45
- { value: ">now-7d", label: "Last 7 days" },
46
- ],
47
- allowCustomValue: true,
48
- customValueInputType: "DATE_RANGE",
49
- },
50
- {
51
- filterType: "RADIO",
52
- label: "Design Status",
53
- key: "designStatus",
54
- options: [
55
- { value: "approved", label: "Approved" },
56
- { value: "rejected", label: "Rejected" },
57
- { value: "draft", label: "Draft" },
58
- ],
59
- allowCustomValue: true,
60
- customValueInputType: "PLAIN_TEXT",
61
- },
62
- ],
5
+ export const useConfig = (): Config<ContainerTypes> => {
6
+ const intl = useIntl();
7
+ return {
8
+ serviceName: intl.formatMessage({
9
+ defaultMessage: "Example App",
10
+ description:
11
+ "Name of the service where the app will pull digital assets from",
12
+ }),
13
+ search: {
14
+ enabled: true,
15
+ filterFormConfig: {
16
+ containerTypes: ["folder"],
17
+ filters: [
18
+ {
19
+ filterType: "CHECKBOX",
20
+ label: intl.formatMessage({
21
+ defaultMessage: "File Type",
22
+ description: "Label of filters for file type",
23
+ }),
24
+ key: "fileType",
25
+ // These options do not need to be translated as they are universal technical terms
26
+ /* eslint-disable formatjs/no-literal-string-in-object */
27
+ options: [
28
+ { value: "mp4", label: "MP4" },
29
+ { value: "png", label: "PNG" },
30
+ { value: "jpeg", label: "JPEG" },
31
+ ],
32
+ /* eslint-enable formatjs/no-literal-string-in-object */
33
+ allowCustomValue: true,
34
+ },
35
+ {
36
+ filterType: "RADIO",
37
+ label: intl.formatMessage({
38
+ defaultMessage: "Size",
39
+ description: "Label of filters for asset size",
40
+ }),
41
+ key: "size",
42
+ options: [
43
+ {
44
+ label: intl.formatMessage({
45
+ defaultMessage: "Large (800+ px)",
46
+ description: "One of the filter options for asset size",
47
+ }),
48
+ value: "large",
49
+ },
50
+ {
51
+ label: intl.formatMessage({
52
+ defaultMessage: "Medium (200-799px)",
53
+ description: "One of the filter options for asset size",
54
+ }),
55
+ value: "medium",
56
+ },
57
+ ],
58
+ allowCustomValue: true,
59
+ customValueInputType: "SIZE_RANGE",
60
+ },
61
+ {
62
+ filterType: "RADIO",
63
+ label: intl.formatMessage({
64
+ defaultMessage: "Update Date",
65
+ description: "Label of the filters for asset's update date",
66
+ }),
67
+ key: "updateDate",
68
+ options: [
69
+ {
70
+ value: ">now-30m",
71
+ label: intl.formatMessage({
72
+ defaultMessage: "Last 30 Minutes",
73
+ description:
74
+ "One of the filter options for asset update date",
75
+ }),
76
+ },
77
+ {
78
+ value: ">now-7d",
79
+ label: intl.formatMessage({
80
+ defaultMessage: "Last 7 days",
81
+ description:
82
+ "One of the filter options for asset update date",
83
+ }),
84
+ },
85
+ ],
86
+ allowCustomValue: true,
87
+ customValueInputType: "DATE_RANGE",
88
+ },
89
+ {
90
+ filterType: "RADIO",
91
+ label: intl.formatMessage({
92
+ defaultMessage: "Design Status",
93
+ description: "Label of the filters for asset's design status",
94
+ }),
95
+ key: "designStatus",
96
+ options: [
97
+ {
98
+ value: "approved",
99
+ label: intl.formatMessage({
100
+ defaultMessage: "Approved",
101
+ description:
102
+ "One of the filter options for asset design status",
103
+ }),
104
+ },
105
+ {
106
+ value: "rejected",
107
+ label: intl.formatMessage({
108
+ defaultMessage: "Rejected",
109
+ description:
110
+ "One of the filter options for asset design status",
111
+ }),
112
+ },
113
+ {
114
+ value: "draft",
115
+ label: intl.formatMessage({
116
+ defaultMessage: "Draft",
117
+ description:
118
+ "One of the filter options for asset design status",
119
+ }),
120
+ },
121
+ ],
122
+ allowCustomValue: true,
123
+ customValueInputType: "PLAIN_TEXT",
124
+ },
125
+ ],
126
+ },
63
127
  },
64
- },
65
- containerTypes: [
66
- {
67
- value: "folder",
68
- label: "Folders",
69
- listingSurfaces: [
70
- { surface: "HOMEPAGE" },
71
- {
72
- surface: "CONTAINER",
73
- parentContainerTypes: ["folder"],
128
+ containerTypes: [
129
+ {
130
+ value: "folder",
131
+ label: intl.formatMessage({
132
+ defaultMessage: "Folders",
133
+ description: "Name of the asset container type",
134
+ }),
135
+ listingSurfaces: [
136
+ { surface: "HOMEPAGE" },
137
+ {
138
+ surface: "CONTAINER",
139
+ parentContainerTypes: ["folder"],
140
+ },
141
+ { surface: "SEARCH" },
142
+ ],
143
+ searchInsideContainer: {
144
+ enabled: true,
145
+ placeholder: intl.formatMessage({
146
+ defaultMessage: "Search for media inside this folder",
147
+ description: "Placeholder of a search input box",
148
+ }),
74
149
  },
75
- { surface: "SEARCH" },
76
- ],
77
- searchInsideContainer: {
78
- enabled: true,
79
- placeholder: "Search for resources inside this folder",
80
150
  },
151
+ ],
152
+ sortOptions: [
153
+ {
154
+ value: "created_at DESC",
155
+ label: intl.formatMessage({
156
+ defaultMessage: "Creation date (newest)",
157
+ description: "One of the sort options",
158
+ }),
159
+ },
160
+ {
161
+ value: "created_at ASC",
162
+ label: intl.formatMessage({
163
+ defaultMessage: "Creation date (oldest)",
164
+ description: "One of the sort options",
165
+ }),
166
+ },
167
+ {
168
+ value: "updated_at DESC",
169
+ label: intl.formatMessage({
170
+ defaultMessage: "Updated (newest)",
171
+ description: "One of the sort options",
172
+ }),
173
+ },
174
+ {
175
+ value: "updated_at ASC",
176
+ label: intl.formatMessage({
177
+ defaultMessage: "Updated (oldest)",
178
+ description: "One of the sort options",
179
+ }),
180
+ },
181
+ {
182
+ value: "name ASC",
183
+ label: intl.formatMessage({
184
+ defaultMessage: "Name (A-Z)",
185
+ description: "One of the sort options",
186
+ }),
187
+ },
188
+ {
189
+ value: "name DESC",
190
+ label: intl.formatMessage({
191
+ defaultMessage: "Name (Z-A)",
192
+ description: "One of the sort options",
193
+ }),
194
+ },
195
+ ],
196
+ layouts: ["MASONRY", "LIST"],
197
+ resourceTypes: ["IMAGE", "VIDEO", "EMBED"],
198
+ moreInfoMessage: intl.formatMessage({
199
+ defaultMessage:
200
+ "At the moment, we only support images and videos. Corrupted and unsupported files will not appear.",
201
+ description: "Helper text to explain why some assets are not visible",
202
+ }),
203
+ // TODO remove `export` if your app does not support exporting the Canva design into an external platform
204
+ export: {
205
+ enabled: true,
206
+ // TODO provide a container type that user can choose to save into, or remove this field if user doesn't need to choose a container
207
+ containerTypes: ["folder"],
208
+ // TODO remove file types that are not supported by your platform
209
+ acceptedFileTypes: [
210
+ "png",
211
+ "pdf_standard",
212
+ "jpg",
213
+ "gif",
214
+ "svg",
215
+ "video",
216
+ "pptx",
217
+ ],
81
218
  },
82
- ],
83
- sortOptions: [
84
- { value: "created_at DESC", label: "Creation date (newest)" },
85
- { value: "created_at ASC", label: "Creation date (oldest)" },
86
- { value: "updated_at DESC", label: "Updated (newest)" },
87
- { value: "updated_at ASC", label: "Updated (oldest)" },
88
- { value: "name ASC", label: "Name (A-Z)" },
89
- { value: "name DESC", label: "Name (Z-A)" },
90
- ],
91
- layouts: ["MASONRY", "LIST"],
92
- resourceTypes: ["IMAGE", "VIDEO", "EMBED"],
93
- moreInfoMessage:
94
- "At the moment, we only support images and videos. Corrupted and unsupported files will not appear.",
219
+ };
95
220
  };
@@ -1,8 +1,8 @@
1
+ import { AppI18nProvider } from "@canva/app-i18n-kit";
1
2
  import { AppUiProvider } from "@canva/app-ui-kit";
2
3
  import { createRoot } from "react-dom/client";
3
4
  import { App } from "./app";
4
5
  import "@canva/app-ui-kit/styles.css";
5
- import { AppI18nProvider } from "@canva/app-i18n-kit";
6
6
 
7
7
  const root = createRoot(document.getElementById("root") as Element);
8
8
  function render() {
@@ -1,10 +1,10 @@
1
1
  /* eslint-disable no-console */
2
+ import debug from "debug";
2
3
  import * as express from "express";
4
+ import type { NextFunction, Request, Response } from "express";
5
+ import * as fs from "fs";
3
6
  import * as http from "http";
4
7
  import * as https from "https";
5
- import * as fs from "fs";
6
- import type { Request, Response, NextFunction } from "express";
7
- import debug from "debug";
8
8
 
9
9
  const serverDebug = debug("server");
10
10
 
@@ -1,11 +1,11 @@
1
1
  /* eslint-disable no-console */
2
2
  import * as chalk from "chalk";
3
3
  import * as debug from "debug";
4
- import type { Request, Response, NextFunction } from "express";
5
- import * as jwt from "jsonwebtoken";
6
- import { JwksClient, SigningKeyNotFoundError } from "jwks-rsa";
4
+ import type { NextFunction, Request, Response } from "express";
7
5
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
8
6
  import Express from "express-serve-static-core";
7
+ import * as jwt from "jsonwebtoken";
8
+ import { JwksClient, SigningKeyNotFoundError } from "jwks-rsa";
9
9
 
10
10
  /**
11
11
  * Prefix your start command with `DEBUG=express:middleware:jwt` to enable debug logging
@@ -110,7 +110,7 @@ export function createJwtMiddleware(
110
110
  return sendUnauthorizedResponse(res);
111
111
  }
112
112
 
113
- req.canva = {
113
+ req["canva"] = {
114
114
  appId: payload.aud,
115
115
  brandId: payload.brandId,
116
116
  userId: payload.userId,
@@ -446,7 +446,7 @@ describe("createJwtMiddleware", () => {
446
446
  expect(res.status).not.toHaveBeenCalled();
447
447
  expect(res.json).not.toHaveBeenCalled();
448
448
 
449
- expect(req.canva).toEqual({
449
+ expect(req["canva"]).toEqual({
450
450
  userId: FAKE_USER_ID,
451
451
  brandId: FAKE_BRAND_ID,
452
452
  appId: FAKE_APP_ID,
@@ -1,31 +1,36 @@
1
- require("dotenv").config();
2
- const path = require("path");
3
- const TerserPlugin = require("terser-webpack-plugin");
4
- const { DefinePlugin, optimize } = require("webpack");
5
- const chalk = require("chalk");
6
- const { transform } = require("@formatjs/ts-transformer");
7
- const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
1
+ import type { Configuration } from "webpack";
2
+ import { DefinePlugin, optimize } from "webpack";
3
+ import * as path from "path";
4
+ import * as TerserPlugin from "terser-webpack-plugin";
5
+ import { transform } from "@formatjs/ts-transformer";
6
+ import * as chalk from "chalk";
7
+ import { config } from "dotenv";
8
+ import { Configuration as DevServerConfiguration } from "webpack-dev-server";
8
9
 
9
- /**
10
- *
11
- * @param {Object} [options]
12
- * @param {string} [options.appEntry=./src/index.tsx]
13
- * @param {string} [options.backendHost]
14
- * @param {Object} [options.devConfig]
15
- * @param {number} [options.devConfig.port]
16
- * @param {boolean} [options.devConfig.enableHmr]
17
- * @param {boolean} [options.devConfig.enableHttps]
18
- * @param {string} [options.devConfig.appOrigin]
19
- * @param {string} [options.devConfig.appId] - Deprecated in favour of appOrigin
20
- * @param {string} [options.devConfig.certFile]
21
- * @param {string} [options.devConfig.keyFile]
22
- * @returns {Object}
23
- */
24
- function buildConfig({
10
+ config();
11
+
12
+ type DevConfig = {
13
+ port: number;
14
+ enableHmr: boolean;
15
+ enableHttps: boolean;
16
+ appOrigin?: string;
17
+ appId?: string; // Deprecated in favour of appOrigin
18
+ certFile?: string;
19
+ keyFile?: string;
20
+ };
21
+
22
+ export function buildConfig({
25
23
  devConfig,
26
24
  appEntry = path.join(process.cwd(), "src", "index.tsx"),
27
25
  backendHost = process.env.CANVA_BACKEND_HOST,
28
- } = {}) {
26
+ // For IN_HARNESS, refer to the following docs for more information: https://www.canva.dev/docs/apps/test-harness/
27
+ inHarness = process.env.IN_HARNESS?.toLowerCase() === "true",
28
+ }: {
29
+ devConfig?: DevConfig;
30
+ appEntry?: string;
31
+ backendHost?: string;
32
+ inHarness?: boolean;
33
+ } = {}): Configuration & DevServerConfiguration {
29
34
  const mode = devConfig ? "development" : "production";
30
35
 
31
36
  if (!backendHost) {
@@ -46,9 +51,14 @@ function buildConfig({
46
51
  return {
47
52
  mode,
48
53
  context: path.resolve(process.cwd(), "./"),
49
- entry: {
50
- app: appEntry,
51
- },
54
+ entry: inHarness
55
+ ? {
56
+ harness: path.join(process.cwd(), "harness", "harness.tsx"),
57
+ init: path.join(process.cwd(), "harness", "init.ts"),
58
+ }
59
+ : {
60
+ app: appEntry,
61
+ },
52
62
  target: "web",
53
63
  resolve: {
54
64
  alias: {
@@ -60,7 +70,7 @@ function buildConfig({
60
70
  extensions: [".ts", ".tsx", ".js", ".css", ".svg", ".woff", ".woff2"],
61
71
  },
62
72
  infrastructureLogging: {
63
- level: "none",
73
+ level: inHarness ? "info" : "none",
64
74
  },
65
75
  module: {
66
76
  rules: [
@@ -172,35 +182,26 @@ function buildConfig({
172
182
  new DefinePlugin({
173
183
  BACKEND_HOST: JSON.stringify(backendHost),
174
184
  }),
175
- // Apps can only submit a single JS file via the developer portal
185
+ // Apps can only submit a single JS file via the Developer Portal
176
186
  new optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
177
- mode === "development" && new ReactRefreshWebpackPlugin(),
178
187
  ].filter(Boolean),
179
188
  ...buildDevConfig(devConfig),
180
189
  };
181
190
  }
182
191
 
183
- /**
184
- *
185
- * @param {Object} [options]
186
- * @param {number} [options.port]
187
- * @param {boolean} [options.enableHmr]
188
- * @param {boolean} [options.enableHttps]
189
- * @param {string} [options.appOrigin]
190
- * @param {string} [options.appId] - Deprecated in favour of appOrigin
191
- * @param {string} [options.certFile]
192
- * @param {string} [options.keyFile]
193
- * @returns {Object|null}
194
- */
195
- function buildDevConfig(options) {
192
+ function buildDevConfig(options?: DevConfig): {
193
+ devtool?: string;
194
+ devServer?: DevServerConfiguration;
195
+ } {
196
196
  if (!options) {
197
- return null;
197
+ return {};
198
198
  }
199
199
 
200
200
  const { port, enableHmr, appOrigin, appId, enableHttps, certFile, keyFile } =
201
201
  options;
202
+ const host = "localhost";
202
203
 
203
- let devServer = {
204
+ let devServer: DevServerConfiguration = {
204
205
  server: enableHttps
205
206
  ? {
206
207
  type: "https",
@@ -210,7 +211,8 @@ function buildDevConfig(options) {
210
211
  },
211
212
  }
212
213
  : "http",
213
- host: "localhost",
214
+ host,
215
+ allowedHosts: [host],
214
216
  historyApiFallback: {
215
217
  rewrites: [{ from: /^\/$/, to: "/app.js" }],
216
218
  },
@@ -227,31 +229,13 @@ function buildDevConfig(options) {
227
229
  if (enableHmr && appOrigin) {
228
230
  devServer = {
229
231
  ...devServer,
230
- allowedHosts: new URL(appOrigin).hostname,
232
+ allowedHosts: [host, new URL(appOrigin).hostname],
231
233
  headers: {
232
234
  "Access-Control-Allow-Origin": appOrigin,
233
235
  "Access-Control-Allow-Credentials": "true",
234
236
  "Access-Control-Allow-Private-Network": "true",
235
237
  },
236
238
  };
237
- } else if (enableHmr && appId) {
238
- // Deprecated - App ID should not be used to configure HMR in the future and can be safely removed
239
- // after a few months.
240
-
241
- console.warn(
242
- "Enabling Hot Module Replacement (HMR) with an App ID is deprecated, please see the README.md on how to update.",
243
- );
244
-
245
- const appDomain = `app-${appId.toLowerCase().trim()}.canva-apps.com`;
246
- devServer = {
247
- ...devServer,
248
- allowedHosts: appDomain,
249
- headers: {
250
- "Access-Control-Allow-Origin": `https://${appDomain}`,
251
- "Access-Control-Allow-Credentials": "true",
252
- "Access-Control-Allow-Private-Network": "true",
253
- },
254
- };
255
239
  } else {
256
240
  if (enableHmr && !appOrigin) {
257
241
  console.warn(
@@ -267,6 +251,4 @@ function buildDevConfig(options) {
267
251
  };
268
252
  }
269
253
 
270
- module.exports = () => buildConfig();
271
-
272
- module.exports.buildConfig = buildConfig;
254
+ export default buildConfig;
@@ -0,0 +1,84 @@
1
+ # Data Connector Template
2
+
3
+ ## Overview
4
+
5
+ This template provides a working example of a data connector app which uses the Canva Connect API as a data source. It demonstrates how to log in with OAuth and then fetch data from an external source, using Canva's API for listing Designs and Brand Templates. There are instructions below for how to configure your authentication settings to try it out.
6
+
7
+ The template provides a list / detail interface for choosing a data source. The user would first select the data source they want to import from a list. Next, they configure the data source to select the desired query by applying search filters or sort criteria. The app turns their configured data source into an API request and then returns the data in a table format for use in a Canva Design.
8
+
9
+ ## Best Practices
10
+
11
+ This template captures best practices for improving user experience in your application.
12
+
13
+ ### State Management
14
+
15
+ In this template, we've set up state management using `React Context`. It's just one way to do it, not a strict rule. If your app gets more complicated, you might want to check out other options like `Redux` or `MobX`.
16
+
17
+ ### Routing
18
+
19
+ As your application evolves, you may find the need for routing to manage multiple views or pages. In this template, we've integrated React Router to illustrate how routing can facilitate seamless navigation between various components.
20
+
21
+ ### App UI Kit
22
+
23
+ The App UI Kit is a React-based component library designed for creating apps that emulate Canva's look and feel. We strongly recommend using the App UI Kit if you're planning to release an app to the public, because this makes it easier to comply with our [design guidelines](https://www.canva.dev/docs/apps/design-guidelines/).
24
+
25
+ ### Data Sources
26
+
27
+ A data source is a type of data that this connector can retrieve. It has:
28
+
29
+ - properties that define the configuration for this source - e.g. search criteria, ordering conditions, selection filters
30
+ - UI for how to edit this configuration
31
+ - columns to display for each data item
32
+ - logic for how to fetch the data from an external API
33
+
34
+ The initial data sources for the Canva Connect API (Designs and Brand Templates) in this template are located in `src/api/data_sources/`.
35
+
36
+ This template provides a `DataSourceHandler` class in `src/api/data_source.ts` as a suggested starting point for defining a reusable concept for a data source and convert API responses into data table outputs.
37
+
38
+ ## Setup - Authentication
39
+
40
+ This template is a working app that reads the [Canva Connect API](https://www.canva.dev/docs/connect/).
41
+
42
+ To run it and authenticate with the Canva Connect API via OAuth you must first complete some authentication setup steps.
43
+
44
+ ### 0. Set up an App
45
+
46
+ - If not already handled by the Canva CLI, you need to create an app.
47
+ Go to [Your Apps](https://www.canva.com/developers/apps) and create an app.
48
+ - On the **Configuration** page, enable the `Data Connector` intent.
49
+
50
+ ### 1. Set up a Connect API Integration
51
+
52
+ - Go to [Your Integrations](https://www.canva.com/developers/integrations/connect-api) and create a new integration
53
+ - On the **Configuration** page, generate a client secret - you need the client ID and client secret in the app setup.
54
+ - On the **Scopes** page, add the `designs:meta` and `brandtemplate:meta` read scopes
55
+ - On the **Authentication** page, set `https://www.canva.com/apps/oauth/authorized` as an Authorized redirect
56
+
57
+ ### 2. Set up OAuth for the app
58
+
59
+ - Go to [Your Apps](https://www.canva.com/developers/apps) and open your data connector app.
60
+ - On the **Authentication** page, add an OAuth 2.0 provider with the following settings:
61
+ > Provider: `CanvaConnect`
62
+ >
63
+ > Client ID: `(generated in step 1)`
64
+ >
65
+ > Client secret: `(generated in step 1)`
66
+ >
67
+ > Credential transfer mode: `Headers (default)`
68
+ >
69
+ > Authorization server URL: `https://www.canva.com/api/oauth/authorize`
70
+ >
71
+ > Token exchange URL: `https://api.canva.com/rest/v1/oauth/token`
72
+ >
73
+ > Proof Key for Code Exchange (PKCE): `Enabled`
74
+
75
+ ### 3. Run your app
76
+
77
+ - Run `npm start` to run the app.
78
+ - You should be able to log in and then choose from the available data sources.
79
+
80
+ ### To use another OAuth source
81
+
82
+ - `src/api/oauth.ts` has a `scope` property that will be used in the oauth flow.
83
+ - This template starts with the scope for the Canva Connect API login set to `["design:meta:read", "brandtemplate:meta:read"]`
84
+ - It should match the scopes set in Step 1.
@@ -0,0 +1,21 @@
1
+ {
2
+ "$schema": "https://www.canva.dev/schemas/app/v1/manifest-schema.json",
3
+ "manifest_schema_version": 1,
4
+ "runtime": {
5
+ "permissions": [
6
+ {
7
+ "name": "canva:design:content:read",
8
+ "type": "mandatory"
9
+ },
10
+ {
11
+ "name": "canva:design:content:write",
12
+ "type": "mandatory"
13
+ }
14
+ ]
15
+ },
16
+ "intent": {
17
+ "data_connector": {
18
+ "enrolled": true
19
+ }
20
+ }
21
+ }