@canva/cli 0.0.1-beta.28 → 0.0.1-beta.29

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 (50) hide show
  1. package/cli.js +257 -257
  2. package/package.json +1 -1
  3. package/templates/data_connector/README.md +84 -0
  4. package/templates/data_connector/declarations/declarations.d.ts +29 -0
  5. package/templates/data_connector/eslint.config.mjs +14 -0
  6. package/templates/data_connector/package.json +91 -0
  7. package/templates/data_connector/scripts/copy_env.ts +10 -0
  8. package/templates/data_connector/scripts/ssl/ssl.ts +131 -0
  9. package/templates/data_connector/scripts/start/app_runner.ts +201 -0
  10. package/templates/data_connector/scripts/start/context.ts +171 -0
  11. package/templates/data_connector/scripts/start/start.ts +46 -0
  12. package/templates/data_connector/scripts/start/tests/start.tests.ts +61 -0
  13. package/templates/data_connector/src/api/connect_client.ts +6 -0
  14. package/templates/data_connector/src/api/data_source.ts +96 -0
  15. package/templates/data_connector/src/api/data_sources/designs.tsx +253 -0
  16. package/templates/data_connector/src/api/data_sources/index.ts +4 -0
  17. package/templates/data_connector/src/api/data_sources/templates.tsx +287 -0
  18. package/templates/data_connector/src/api/fetch_data_table.ts +51 -0
  19. package/templates/data_connector/src/api/index.ts +4 -0
  20. package/templates/data_connector/src/api/oauth.ts +8 -0
  21. package/templates/data_connector/src/api/tests/data_source.test.tsx +99 -0
  22. package/templates/data_connector/src/app.tsx +24 -0
  23. package/templates/data_connector/src/components/app_error.tsx +15 -0
  24. package/templates/data_connector/src/components/footer.tsx +26 -0
  25. package/templates/data_connector/src/components/header.tsx +40 -0
  26. package/templates/data_connector/src/components/index.ts +3 -0
  27. package/templates/data_connector/src/components/inputs/messages.tsx +80 -0
  28. package/templates/data_connector/src/components/inputs/select_field.tsx +26 -0
  29. package/templates/data_connector/src/context/app_context.tsx +124 -0
  30. package/templates/data_connector/src/context/index.ts +2 -0
  31. package/templates/data_connector/src/context/use_app_context.ts +17 -0
  32. package/templates/data_connector/src/entrypoint.tsx +73 -0
  33. package/templates/data_connector/src/home.tsx +21 -0
  34. package/templates/data_connector/src/index.tsx +69 -0
  35. package/templates/data_connector/src/pages/data_source_config.tsx +9 -0
  36. package/templates/data_connector/src/pages/error.tsx +37 -0
  37. package/templates/data_connector/src/pages/index.ts +4 -0
  38. package/templates/data_connector/src/pages/login.tsx +145 -0
  39. package/templates/data_connector/src/pages/select_source.tsx +24 -0
  40. package/templates/data_connector/src/routes/index.ts +2 -0
  41. package/templates/data_connector/src/routes/protected_route.tsx +25 -0
  42. package/templates/data_connector/src/routes/routes.tsx +46 -0
  43. package/templates/data_connector/src/utils/data_params.ts +17 -0
  44. package/templates/data_connector/src/utils/data_table.ts +100 -0
  45. package/templates/data_connector/src/utils/fetch_result.ts +36 -0
  46. package/templates/data_connector/src/utils/index.ts +2 -0
  47. package/templates/data_connector/src/utils/tests/data_table.test.ts +133 -0
  48. package/templates/data_connector/styles/components.css +38 -0
  49. package/templates/data_connector/tsconfig.json +54 -0
  50. package/templates/data_connector/webpack.config.ts +270 -0
@@ -0,0 +1,100 @@
1
+ import type {
2
+ BooleanDataTableCell,
3
+ DataTable,
4
+ DataTableCell,
5
+ DateDataTableCell,
6
+ NumberDataTableCell,
7
+ StringDataTableCell,
8
+ } from "@canva/intents/data";
9
+ import type { APIResponseItem } from "src/api";
10
+
11
+ export interface DataTableColumn<T extends APIResponseItem> {
12
+ label: string;
13
+ getValue: keyof T | ((result: T) => boolean | string | number | Date);
14
+ toCell: (value) => DataTableCell;
15
+ }
16
+
17
+ export function toDataTable<T extends APIResponseItem>(
18
+ apiData: T[],
19
+ columns: DataTableColumn<T>[],
20
+ rowLimit: number,
21
+ ): DataTable {
22
+ const items = apiData.slice(0, rowLimit);
23
+ const headerRow = columns.map((column) => stringCell(column.label));
24
+ const dataTable: DataTable = {
25
+ rows: [{ cells: headerRow }],
26
+ };
27
+ items.forEach((item) => {
28
+ const cells = columns.map((column) => {
29
+ const value =
30
+ typeof column.getValue === "function"
31
+ ? column.getValue(item)
32
+ : item[column.getValue];
33
+ return column.toCell(value);
34
+ });
35
+ dataTable.rows.push({ cells });
36
+ });
37
+ return dataTable;
38
+ }
39
+
40
+ /**
41
+ * Creates a string cell for the data table.
42
+ * @param value String containing up to 10,000 characters
43
+ */
44
+ export function stringCell(value: string): StringDataTableCell {
45
+ return {
46
+ type: "string",
47
+ value,
48
+ };
49
+ }
50
+
51
+ /**
52
+ * Creates a number cell for the data table.
53
+ * @param value Number within range `Number.MIN_SAFE_INTEGER` and `Number.MAX_SAFE_INTEGER`
54
+ * @param formatting Formatting using ISO/IEC 29500-1:2016 Office Open XML Format
55
+ */
56
+ export function numberCell(
57
+ value: number,
58
+ formatting?: string,
59
+ ): NumberDataTableCell {
60
+ return {
61
+ type: "number",
62
+ value,
63
+ metadata: {
64
+ formatting,
65
+ },
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Creates a boolean cell for the data table.
71
+ * @param value Boolean value
72
+ */
73
+ export function booleanCell(value: boolean): BooleanDataTableCell {
74
+ return {
75
+ type: "boolean",
76
+ value,
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Creates a date cell for the data table.
82
+ * @param value Number, Date or String
83
+ * @description If value is a string, it will be parsed as a date. If value is a number, it will be treated as a timestamp in seconds.
84
+ */
85
+ export function dateCell(value: number | Date | string): DateDataTableCell {
86
+ // if string, parse as date
87
+ if (typeof value === "string") {
88
+ value = new Date(value);
89
+ }
90
+
91
+ // if is date, convert to timestamp in seconds
92
+ if (value instanceof Date) {
93
+ value = value.valueOf() / 1000;
94
+ }
95
+
96
+ return {
97
+ type: "date",
98
+ value,
99
+ };
100
+ }
@@ -0,0 +1,36 @@
1
+ import type {
2
+ DataTable,
3
+ DataTableMetadata,
4
+ FetchDataTableError,
5
+ FetchDataTableCompleted,
6
+ } from "@canva/intents/data";
7
+
8
+ export const completeDataTable = (
9
+ dataTable: DataTable,
10
+ metadata?: DataTableMetadata,
11
+ ): FetchDataTableCompleted => {
12
+ return {
13
+ status: "completed",
14
+ dataTable,
15
+ metadata,
16
+ };
17
+ };
18
+
19
+ export const appError = (message?: string): FetchDataTableError => {
20
+ return {
21
+ status: "app_error",
22
+ message,
23
+ };
24
+ };
25
+
26
+ export const outdatedSourceRef = (): FetchDataTableError => {
27
+ return {
28
+ status: "outdated_source_ref",
29
+ };
30
+ };
31
+
32
+ export const remoteRequestFailed = (): FetchDataTableError => {
33
+ return {
34
+ status: "remote_request_failed",
35
+ };
36
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./data_table";
2
+ export * from "./data_params";
@@ -0,0 +1,133 @@
1
+ /* eslint-disable formatjs/no-literal-string-in-object */
2
+ import {
3
+ toDataTable,
4
+ stringCell,
5
+ numberCell,
6
+ dateCell,
7
+ booleanCell,
8
+ } from "../data_table";
9
+ import type { DataTableColumn } from "../data_table";
10
+ import type { APIResponseItem } from "src/api";
11
+
12
+ describe("data table utils", () => {
13
+ interface TestItem extends APIResponseItem {
14
+ id: string;
15
+ name: string;
16
+ count: number;
17
+ active: boolean;
18
+ createdAt: string;
19
+ }
20
+
21
+ const testItems: TestItem[] = [
22
+ {
23
+ id: "item1",
24
+ name: "Test Item 1",
25
+ count: 42,
26
+ active: true,
27
+ createdAt: "2023-01-01T00:00:00Z",
28
+ },
29
+ {
30
+ id: "item2",
31
+ name: "Test Item 2",
32
+ count: 0,
33
+ active: false,
34
+ createdAt: "2023-02-15T12:30:45Z",
35
+ },
36
+ ];
37
+
38
+ const columns: DataTableColumn<TestItem>[] = [
39
+ {
40
+ label: "ID",
41
+ getValue: "id",
42
+ toCell: stringCell,
43
+ },
44
+ {
45
+ label: "Name",
46
+ getValue: "name",
47
+ toCell: stringCell,
48
+ },
49
+ {
50
+ label: "Count",
51
+ getValue: "count",
52
+ toCell: numberCell,
53
+ },
54
+ {
55
+ label: "Status",
56
+ getValue: "active",
57
+ toCell: booleanCell,
58
+ },
59
+ {
60
+ label: "Created",
61
+ getValue: "createdAt",
62
+ toCell: dateCell,
63
+ },
64
+ {
65
+ label: "Custom",
66
+ getValue: (item) => `${item.name} (${item.count})`,
67
+ toCell: stringCell,
68
+ },
69
+ ];
70
+
71
+ test("toDataTable creates correct structure with header row", () => {
72
+ const result = toDataTable(testItems, columns, 10);
73
+
74
+ expect(result.rows.length).toBe(3); // header + 2 data rows
75
+ expect(result.rows[0].cells.map((cell) => cell.value)).toEqual([
76
+ "ID",
77
+ "Name",
78
+ "Count",
79
+ "Status",
80
+ "Created",
81
+ "Custom",
82
+ ]);
83
+ });
84
+
85
+ test("toDataTable respects row limit", () => {
86
+ const result = toDataTable(testItems, columns, 1);
87
+
88
+ expect(result.rows.length).toBe(2); // header + 1 data row (limited)
89
+ });
90
+
91
+ test("toDataTable handles function-based getValue", () => {
92
+ const result = toDataTable(testItems, columns, 10);
93
+
94
+ // Check the custom column values
95
+ expect(result.rows[1].cells[5].value).toBe("Test Item 1 (42)");
96
+ expect(result.rows[2].cells[5].value).toBe("Test Item 2 (0)");
97
+ });
98
+
99
+ test("cell formatter functions create correct cell structures", () => {
100
+ expect(stringCell("test")).toEqual({ type: "string", value: "test" });
101
+
102
+ expect(numberCell(42)).toEqual({
103
+ type: "number",
104
+ value: 42,
105
+ metadata: { formatting: undefined },
106
+ });
107
+
108
+ expect(numberCell(42, "#,##0")).toEqual({
109
+ type: "number",
110
+ value: 42,
111
+ metadata: { formatting: "#,##0" },
112
+ });
113
+
114
+ expect(booleanCell(true)).toEqual({ type: "boolean", value: true });
115
+
116
+ // Test date with string input
117
+ const dateResult1 = dateCell("2023-01-01T00:00:00Z");
118
+ expect(dateResult1.type).toBe("date");
119
+ expect(typeof dateResult1.value).toBe("number");
120
+
121
+ // Test date with Date object input
122
+ const dateObj = new Date("2023-01-01T00:00:00Z");
123
+ const dateResult2 = dateCell(dateObj);
124
+ expect(dateResult2.type).toBe("date");
125
+ expect(typeof dateResult2.value).toBe("number");
126
+
127
+ // Test date with timestamp input
128
+ const timestamp = dateObj.valueOf() / 1000; // seconds
129
+ const dateResult3 = dateCell(timestamp);
130
+ expect(dateResult3.type).toBe("date");
131
+ expect(dateResult3.value).toBe(timestamp);
132
+ });
133
+ });
@@ -0,0 +1,38 @@
1
+ /* Scroll container */
2
+ .scrollContainer {
3
+ box-sizing: border-box;
4
+ overflow-y: scroll;
5
+ height: 100%;
6
+ padding-top: var(--ui-kit-space-2);
7
+ padding-right: var(--ui-kit-space-2);
8
+ padding-bottom: var(--ui-kit-space-2);
9
+
10
+ /* for firefox */
11
+ scrollbar-width: thin;
12
+ scrollbar-color: var(--ui-kit-color-typography-quaternary) transparent;
13
+ }
14
+
15
+ .scrollContainer::-webkit-scrollbar {
16
+ position: absolute;
17
+ width: var(--ui-kit-base-unit);
18
+ height: 0;
19
+ }
20
+
21
+ .scrollContainer::-webkit-scrollbar-track {
22
+ background: transparent;
23
+ width: var(--ui-kit-base-unit);
24
+ margin-top: var(--ui-kit-space-1);
25
+ margin-bottom: var(--ui-kit-space-1);
26
+ }
27
+
28
+ .scrollContainer::-webkit-scrollbar-thumb {
29
+ border-radius: var(--ui-kit-border-radius);
30
+ background: var(--ui-kit-color-typography-quaternary);
31
+ visibility: hidden;
32
+ }
33
+
34
+ .scrollContainer:hover::-webkit-scrollbar-thumb,
35
+ .scrollContainer:focus::-webkit-scrollbar-thumb,
36
+ .scrollContainer:focus-within::-webkit-scrollbar-thumb {
37
+ visibility: visible;
38
+ }
@@ -0,0 +1,54 @@
1
+ {
2
+ "compilerOptions": {
3
+ "jsx": "react-jsx",
4
+ "lib": [
5
+ "dom",
6
+ "dom.iterable",
7
+ "es2018",
8
+ "es2019.array",
9
+ "es2019.object",
10
+ "es2019.string",
11
+ "es2020.promise",
12
+ "es2020.string"
13
+ ],
14
+ "types": ["node", "webpack-env", "jest"],
15
+ "composite": false,
16
+ "declaration": false,
17
+ "declarationMap": false,
18
+ "experimentalDecorators": true,
19
+ "importHelpers": true,
20
+ "noImplicitOverride": true,
21
+ "moduleResolution": "node",
22
+ "rootDir": ".",
23
+ "outDir": "dist",
24
+ "strict": true,
25
+ "skipLibCheck": true,
26
+ "target": "ES2019",
27
+ "sourceMap": true,
28
+ "inlineSources": true,
29
+ "module": "ESNext",
30
+ "noImplicitAny": false,
31
+ "removeComments": true,
32
+ "preserveConstEnums": true,
33
+ "allowSyntheticDefaultImports": true,
34
+ "baseUrl": "./",
35
+ "paths": {
36
+ "assets": ["./assets"],
37
+ "styles": ["./styles"]
38
+ }
39
+ },
40
+ "include": [
41
+ "./src/**/*",
42
+ "./backend/**/*",
43
+ "./utils/**/*",
44
+ "./scripts/**/*",
45
+ "./declarations/declarations.d.ts",
46
+ "./styles/**/*",
47
+ "./node_modules/@types/**/*"
48
+ ],
49
+ "ts-node": {
50
+ "compilerOptions": {
51
+ "module": "commonjs"
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,270 @@
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";
9
+
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({
23
+ devConfig,
24
+ appEntry = path.join(process.cwd(), "src", "index.tsx"),
25
+ backendHost = process.env.CANVA_BACKEND_HOST,
26
+ // For IN_HARNESS, refer to the following docs for more information: https://www.canva.dev/docs/apps/mcp-server/harness-setup/
27
+ inHarness = process.env.IN_HARNESS === "true",
28
+ }: {
29
+ devConfig?: DevConfig;
30
+ appEntry?: string;
31
+ backendHost?: string;
32
+ inHarness?: boolean;
33
+ } = {}): Configuration & DevServerConfiguration {
34
+ const mode = devConfig ? "development" : "production";
35
+
36
+ if (!backendHost) {
37
+ console.error(
38
+ chalk.redBright.bold("BACKEND_HOST is undefined."),
39
+ `Refer to "Customizing the backend host" in the README.md for more information.`,
40
+ );
41
+ process.exit(-1);
42
+ } else if (backendHost.includes("localhost") && mode === "production") {
43
+ console.error(
44
+ chalk.redBright.bold(
45
+ "BACKEND_HOST should not be set to localhost for production builds!",
46
+ ),
47
+ `Refer to "Customizing the backend host" in the README.md for more information.`,
48
+ );
49
+ }
50
+
51
+ return {
52
+ mode,
53
+ context: path.resolve(process.cwd(), "./"),
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
+ },
62
+ target: "web",
63
+ resolve: {
64
+ alias: {
65
+ assets: path.resolve(process.cwd(), "assets"),
66
+ utils: path.resolve(process.cwd(), "utils"),
67
+ styles: path.resolve(process.cwd(), "styles"),
68
+ src: path.resolve(process.cwd(), "src"),
69
+ },
70
+ extensions: [".ts", ".tsx", ".js", ".css", ".svg", ".woff", ".woff2"],
71
+ },
72
+ infrastructureLogging: {
73
+ level: inHarness ? "info" : "none",
74
+ },
75
+ module: {
76
+ rules: [
77
+ {
78
+ test: /\.tsx?$/,
79
+ exclude: /node_modules/,
80
+ use: [
81
+ {
82
+ loader: "ts-loader",
83
+ options: {
84
+ transpileOnly: true,
85
+ getCustomTransformers() {
86
+ return {
87
+ before: [
88
+ transform({
89
+ overrideIdFn: "[sha512:contenthash:base64:6]",
90
+ }),
91
+ ],
92
+ };
93
+ },
94
+ },
95
+ },
96
+ ],
97
+ },
98
+ {
99
+ test: /\.css$/,
100
+ exclude: /node_modules/,
101
+ use: [
102
+ "style-loader",
103
+ {
104
+ loader: "css-loader",
105
+ options: {
106
+ modules: true,
107
+ },
108
+ },
109
+ {
110
+ loader: "postcss-loader",
111
+ options: {
112
+ postcssOptions: {
113
+ plugins: [require("cssnano")({ preset: "default" })],
114
+ },
115
+ },
116
+ },
117
+ ],
118
+ },
119
+ {
120
+ test: /\.(png|jpg|jpeg)$/i,
121
+ type: "asset/inline",
122
+ },
123
+ {
124
+ test: /\.(woff|woff2)$/,
125
+ type: "asset/inline",
126
+ },
127
+ {
128
+ test: /\.svg$/,
129
+ oneOf: [
130
+ {
131
+ issuer: /\.[jt]sx?$/,
132
+ resourceQuery: /react/, // *.svg?react
133
+ use: ["@svgr/webpack", "url-loader"],
134
+ },
135
+ {
136
+ type: "asset/resource",
137
+ parser: {
138
+ dataUrlCondition: {
139
+ maxSize: 200,
140
+ },
141
+ },
142
+ },
143
+ ],
144
+ },
145
+ {
146
+ test: /\.css$/,
147
+ include: /node_modules/,
148
+ use: [
149
+ "style-loader",
150
+ "css-loader",
151
+ {
152
+ loader: "postcss-loader",
153
+ options: {
154
+ postcssOptions: {
155
+ plugins: [require("cssnano")({ preset: "default" })],
156
+ },
157
+ },
158
+ },
159
+ ],
160
+ },
161
+ ],
162
+ },
163
+ optimization: {
164
+ minimizer: [
165
+ new TerserPlugin({
166
+ terserOptions: {
167
+ format: {
168
+ // Turned on because emoji and regex is not minified properly using default
169
+ // https://github.com/facebook/create-react-app/issues/2488
170
+ ascii_only: true,
171
+ },
172
+ },
173
+ }),
174
+ ],
175
+ },
176
+ output: {
177
+ filename: `[name].js`,
178
+ path: path.resolve(process.cwd(), "dist"),
179
+ clean: true,
180
+ },
181
+ plugins: [
182
+ new DefinePlugin({
183
+ BACKEND_HOST: JSON.stringify(backendHost),
184
+ }),
185
+ // Apps can only submit a single JS file via the developer portal
186
+ new optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
187
+ ].filter(Boolean),
188
+ ...buildDevConfig(devConfig),
189
+ };
190
+ }
191
+
192
+ function buildDevConfig(options?: DevConfig): {
193
+ devtool?: string;
194
+ devServer?: DevServerConfiguration;
195
+ } {
196
+ if (!options) {
197
+ return {};
198
+ }
199
+
200
+ const { port, enableHmr, appOrigin, appId, enableHttps, certFile, keyFile } =
201
+ options;
202
+
203
+ let devServer: DevServerConfiguration = {
204
+ server: enableHttps
205
+ ? {
206
+ type: "https",
207
+ options: {
208
+ cert: certFile,
209
+ key: keyFile,
210
+ },
211
+ }
212
+ : "http",
213
+ host: "localhost",
214
+ historyApiFallback: {
215
+ rewrites: [{ from: /^\/$/, to: "/app.js" }],
216
+ },
217
+ port,
218
+ client: {
219
+ logging: "verbose",
220
+ },
221
+ static: {
222
+ directory: path.resolve(process.cwd(), "assets"),
223
+ publicPath: "/assets",
224
+ },
225
+ };
226
+
227
+ if (enableHmr && appOrigin) {
228
+ devServer = {
229
+ ...devServer,
230
+ allowedHosts: new URL(appOrigin).hostname,
231
+ headers: {
232
+ "Access-Control-Allow-Origin": appOrigin,
233
+ "Access-Control-Allow-Credentials": "true",
234
+ "Access-Control-Allow-Private-Network": "true",
235
+ },
236
+ };
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
+ } else {
256
+ if (enableHmr && !appOrigin) {
257
+ console.warn(
258
+ "Attempted to enable Hot Module Replacement (HMR) without configuring App Origin... Disabling HMR.",
259
+ );
260
+ }
261
+ devServer.webSocketServer = false;
262
+ }
263
+
264
+ return {
265
+ devtool: "source-map",
266
+ devServer,
267
+ };
268
+ }
269
+
270
+ export default buildConfig;