@canva/cli 0.0.1-beta.1 → 0.0.1-beta.10

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 (79) hide show
  1. package/README.md +184 -108
  2. package/cli.js +446 -359
  3. package/lib/cjs/index.cjs +392 -0
  4. package/lib/esm/index.esm +392 -0
  5. package/package.json +11 -2
  6. package/templates/base/backend/routers/oauth.ts +393 -0
  7. package/templates/base/eslint.config.mjs +5 -277
  8. package/templates/base/package.json +26 -23
  9. package/templates/base/scripts/copy-env.ts +10 -0
  10. package/templates/base/utils/backend/bearer_middleware/bearer_middleware.ts +101 -0
  11. package/templates/base/utils/backend/bearer_middleware/index.ts +1 -0
  12. package/templates/base/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +192 -0
  13. package/templates/base/utils/use_add_element.ts +48 -0
  14. package/templates/base/utils/use_feature_support.ts +28 -0
  15. package/templates/base/webpack.config.cjs +3 -1
  16. package/templates/common/.gitignore.template +5 -6
  17. package/templates/common/.nvmrc +1 -0
  18. package/templates/common/.prettierrc +21 -0
  19. package/templates/common/.vscode/extensions.json +6 -0
  20. package/templates/common/README.md +4 -74
  21. package/templates/common/conf/eslint-general.mjs +303 -0
  22. package/templates/common/conf/eslint-i18n.mjs +41 -0
  23. package/templates/common/conf/eslint-local-i18n-rules/index.mjs +181 -0
  24. package/templates/common/jest.config.mjs +29 -2
  25. package/templates/common/jest.setup.ts +19 -0
  26. package/templates/common/utils/backend/base_backend/create.ts +104 -0
  27. package/templates/common/utils/table_wrapper.ts +477 -0
  28. package/templates/common/utils/tests/table_wrapper.tests.ts +252 -0
  29. package/templates/common/utils/use_add_element.ts +48 -0
  30. package/templates/common/utils/use_feature_support.ts +28 -0
  31. package/templates/common/utils/use_overlay_hook.ts +74 -0
  32. package/templates/common/utils/use_selection_hook.ts +37 -0
  33. package/templates/dam/backend/server.ts +0 -7
  34. package/templates/dam/eslint.config.mjs +6 -275
  35. package/templates/dam/package.json +43 -35
  36. package/templates/dam/scripts/copy-env.ts +10 -0
  37. package/templates/dam/src/app.tsx +2 -135
  38. package/templates/dam/webpack.config.cjs +3 -1
  39. package/templates/gen_ai/README.md +1 -1
  40. package/templates/gen_ai/backend/routers/image.ts +3 -3
  41. package/templates/gen_ai/backend/server.ts +0 -7
  42. package/templates/gen_ai/eslint.config.mjs +5 -277
  43. package/templates/gen_ai/package.json +46 -38
  44. package/templates/gen_ai/scripts/copy-env.ts +10 -0
  45. package/templates/gen_ai/src/api/api.ts +1 -39
  46. package/templates/gen_ai/src/app.tsx +16 -10
  47. package/templates/gen_ai/src/components/footer.messages.ts +0 -5
  48. package/templates/gen_ai/src/components/footer.tsx +2 -16
  49. package/templates/gen_ai/src/components/image_grid.tsx +9 -7
  50. package/templates/gen_ai/src/components/index.ts +0 -1
  51. package/templates/gen_ai/src/components/prompt_input.tsx +2 -0
  52. package/templates/gen_ai/src/components/tests/remaining_credit.tests.tsx +43 -0
  53. package/templates/gen_ai/src/context/app_context.tsx +4 -26
  54. package/templates/gen_ai/src/context/context.messages.ts +1 -12
  55. package/templates/gen_ai/src/home.tsx +13 -0
  56. package/templates/gen_ai/src/index.tsx +2 -18
  57. package/templates/gen_ai/src/routes/routes.tsx +2 -2
  58. package/templates/gen_ai/utils/backend/bearer_middleware/bearer_middleware.ts +101 -0
  59. package/templates/gen_ai/utils/backend/bearer_middleware/index.ts +1 -0
  60. package/templates/gen_ai/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +192 -0
  61. package/templates/gen_ai/webpack.config.cjs +3 -1
  62. package/templates/hello_world/eslint.config.mjs +5 -277
  63. package/templates/hello_world/package.json +46 -28
  64. package/templates/hello_world/scripts/copy-env.ts +10 -0
  65. package/templates/hello_world/src/app.tsx +25 -3
  66. package/templates/hello_world/src/tests/__snapshots__/app.tests.tsx.snap +45 -0
  67. package/templates/hello_world/src/tests/app.tests.tsx +86 -0
  68. package/templates/hello_world/utils/use_add_element.ts +48 -0
  69. package/templates/hello_world/utils/use_feature_support.ts +28 -0
  70. package/templates/hello_world/webpack.config.cjs +3 -1
  71. package/templates/dam/backend/database/database.ts +0 -42
  72. package/templates/dam/backend/routers/auth.ts +0 -285
  73. package/templates/gen_ai/backend/routers/auth.ts +0 -285
  74. package/templates/gen_ai/src/components/logged_in_status.tsx +0 -44
  75. package/templates/gen_ai/src/services/auth.tsx +0 -31
  76. package/templates/gen_ai/src/services/index.ts +0 -1
  77. /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/index.ts +0 -0
  78. /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/jwt_middleware.ts +0 -0
  79. /package/templates/{gen_ai → common}/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +0 -0
@@ -1,11 +1,9 @@
1
- import typescriptEslint from "@typescript-eslint/eslint-plugin";
2
- import jest from "eslint-plugin-jest";
3
- import react from "eslint-plugin-react";
4
- import globals from "globals";
5
1
  import path from "node:path";
6
2
  import { fileURLToPath } from "node:url";
7
3
  import js from "@eslint/js";
8
4
  import { FlatCompat } from "@eslint/eslintrc";
5
+ import general from "./conf/eslint-general.mjs";
6
+ import i18n from "./conf/eslint-i18n.mjs";
9
7
 
10
8
  const __filename = fileURLToPath(import.meta.url);
11
9
  const __dirname = path.dirname(__filename);
@@ -22,8 +20,6 @@ export default [
22
20
  "**/dist",
23
21
  "**/*.d.ts",
24
22
  "**/*.d.tsx",
25
- "**/sdk",
26
- "**/internal",
27
23
  "**/*.config.*",
28
24
  ],
29
25
  },
@@ -34,276 +30,8 @@ export default [
34
30
  "plugin:@typescript-eslint/strict",
35
31
  "plugin:@typescript-eslint/stylistic",
36
32
  "plugin:react/recommended",
37
- "plugin:jest/recommended"
33
+ "plugin:jest/recommended",
38
34
  ),
39
- {
40
- plugins: {
41
- "@typescript-eslint": typescriptEslint,
42
- jest,
43
- react,
44
- },
45
- languageOptions: {
46
- globals: {
47
- ...globals.serviceworker,
48
- ...globals.browser,
49
- },
50
- },
51
- settings: {
52
- react: {
53
- version: "detect",
54
- },
55
- },
56
- rules: {
57
- "@typescript-eslint/no-non-null-assertion": "warn",
58
- "@typescript-eslint/no-empty-function": "off",
59
- "@typescript-eslint/consistent-type-imports": "error",
60
- "@typescript-eslint/no-explicit-any": "warn",
61
- "@typescript-eslint/no-empty-interface": "warn",
62
- "@typescript-eslint/consistent-type-definitions": "off",
63
- "@typescript-eslint/explicit-member-accessibility": [
64
- "error",
65
- {
66
- accessibility: "no-public",
67
- overrides: {
68
- parameterProperties: "off",
69
- },
70
- },
71
- ],
72
- "@typescript-eslint/naming-convention": [
73
- "error",
74
- {
75
- selector: "typeLike",
76
- format: ["PascalCase"],
77
- leadingUnderscore: "allow",
78
- },
79
- ],
80
- "no-invalid-this": "off",
81
- "@typescript-eslint/no-invalid-this": "error",
82
- "@typescript-eslint/no-unused-expressions": [
83
- "error",
84
- {
85
- allowShortCircuit: true,
86
- allowTernary: true,
87
- },
88
- ],
89
- "no-unused-vars": "off",
90
- "@typescript-eslint/no-unused-vars": [
91
- "error",
92
- {
93
- vars: "all",
94
- varsIgnorePattern: "^_",
95
- args: "none",
96
- ignoreRestSiblings: true,
97
- },
98
- ],
99
- "@typescript-eslint/no-require-imports": "error",
100
- "jest/no-restricted-matchers": [
101
- "error",
102
- {
103
- toContainElement:
104
- "toContainElement is not recommended as it encourages testing the internals of the components",
105
- toContainHTML:
106
- "toContainHTML is not recommended as it encourages testing the internals of the components",
107
- toHaveAttribute:
108
- "toHaveAttribute is not recommended as it encourages testing the internals of the components",
109
- toHaveClass:
110
- "toHaveClass is not recommended as it encourages testing the internals of the components",
111
- toHaveStyle:
112
- "toHaveStyle is not recommended as it encourages testing the internals of the components",
113
- },
114
- ],
115
- "react/jsx-curly-brace-presence": [
116
- "error",
117
- {
118
- props: "never",
119
- children: "never",
120
- },
121
- ],
122
- "react/jsx-tag-spacing": [
123
- "error",
124
- {
125
- closingSlash: "never",
126
- beforeSelfClosing: "allow",
127
- afterOpening: "never",
128
- beforeClosing: "allow",
129
- },
130
- ],
131
- "react/self-closing-comp": "error",
132
- "react/no-unescaped-entities": "off",
133
- "react/jsx-uses-react": "off",
134
- "react/react-in-jsx-scope": "off",
135
- "default-case": "error",
136
- eqeqeq: [
137
- "error",
138
- "always",
139
- {
140
- null: "never",
141
- },
142
- ],
143
- "no-caller": "error",
144
- "no-console": "error",
145
- "no-eval": "error",
146
- "no-inner-declarations": "error",
147
- "no-new-wrappers": "error",
148
- "no-restricted-globals": [
149
- "error",
150
- {
151
- name: "fit",
152
- message: "Don't focus tests",
153
- },
154
- {
155
- name: "fdescribe",
156
- message: "Don't focus tests",
157
- },
158
- {
159
- name: "length",
160
- message:
161
- "Undefined length - Did you mean to use window.length instead?",
162
- },
163
- {
164
- name: "name",
165
- message: "Undefined name - Did you mean to use window.name instead?",
166
- },
167
- {
168
- name: "status",
169
- message:
170
- "Undefined status - Did you mean to use window.status instead?",
171
- },
172
- {
173
- name: "spyOn",
174
- message: "Don't use spyOn directly, use jest.spyOn",
175
- },
176
- ],
177
- "no-restricted-properties": [
178
- "error",
179
- {
180
- property: "bind",
181
- message: "Don't o.f.bind(o, ...), use () => o.f(...)",
182
- },
183
- {
184
- object: "ReactDOM",
185
- property: "findDOMNode",
186
- message: "Don't use ReactDOM.findDOMNode() as it is deprecated",
187
- },
188
- ],
189
- "no-restricted-syntax": [
190
- "error",
191
- {
192
- selector: "AccessorProperty, TSAbstractAccessorProperty",
193
- message:
194
- "Accessor property syntax is not allowed, use getter and setters.",
195
- },
196
- {
197
- selector: "PrivateIdentifier",
198
- message:
199
- "Private identifiers are not allowed, use TypeScript private fields.",
200
- },
201
- {
202
- selector:
203
- "JSXOpeningElement[name.name = /^[A-Z]/] > JSXAttribute[name.name = /-/]",
204
- message:
205
- "Passing hyphenated props to custom components is not type-safe. Prefer a camelCased equivalent if available. (See https://github.com/microsoft/TypeScript/issues/55182)",
206
- },
207
- {
208
- selector:
209
- "CallExpression[callee.object.name='window'][callee.property.name='open']",
210
- message:
211
- "Apps are currently not allowed to open popups, or new tabs via browser APIs. Please use `requestOpenExternalUrl` from `@canva/platform` to link to external URLs. To learn more, see https://www.canva.dev/docs/apps/api/platform-request-open-external-url/",
212
- },
213
- ],
214
- "no-return-await": "error",
215
- "no-throw-literal": "error",
216
- "no-undef-init": "error",
217
- "no-var": "error",
218
- "object-shorthand": "error",
219
- "prefer-const": [
220
- "error",
221
- {
222
- destructuring: "all",
223
- },
224
- ],
225
- "prefer-object-spread": "error",
226
- "prefer-rest-params": "error",
227
- "prefer-spread": "error",
228
- radix: "error",
229
- },
230
- },
231
- {
232
- files: ["**/*.tsx"],
233
- rules: {
234
- "react/no-deprecated": "error",
235
- "react/forbid-elements": [
236
- "error",
237
- {
238
- forbid: [
239
- {
240
- element: "video",
241
- message:
242
- "Don't use HTML video directly. Instead, use the App UI Kit <VideoCard /> as this respects users' auto-playing preferences",
243
- },
244
- {
245
- element: "em",
246
- message:
247
- "Don't use <em> to italicize text. Canva's UI fonts don't support italic font style.",
248
- },
249
- {
250
- element: "i",
251
- message:
252
- "Don't use <i> to italicize text. Canva's UI fonts don't support italic font style.",
253
- },
254
- {
255
- element: "iframe",
256
- message:
257
- "Canva Apps aren't allowed to contain iframes. You should either recreate the UI you want to show in the iframe in the app directly, or link to your page via a `<Link>` tag. For more info see https://www.canva.dev/docs/apps/content-security-policy/#what-is-and-isnt-allowed",
258
- },
259
- {
260
- element: "script",
261
- message:
262
- "Script tags are not allowed in Canva SDK Apps. You should import JavaScript modules instead. For more info see https://www.canva.dev/docs/apps/content-security-policy/#what-is-and-isnt-allowed",
263
- },
264
- {
265
- element: "a",
266
- message:
267
- "Don't use <a> tags. Instead, use the <Link> component from the App UI Kit, and remember to open the url via the requestOpenExternalUrl method from @canva/platform.",
268
- },
269
- {
270
- element: "img",
271
- message:
272
- "Have you considered using <ImageCard /> from the App UI Kit instead?",
273
- },
274
- {
275
- element: "embed",
276
- message:
277
- "Have you considered using <EmbedCard /> from the App UI Kit instead?",
278
- },
279
- {
280
- element: "audio",
281
- message:
282
- "Have you considered using <AudioCard /> from the App UI Kit instead?",
283
- },
284
- {
285
- element: "button",
286
- message:
287
- "Rather than using the native HTML <button> element, use the <Button> component from the App UI Kit for consistency and accessibility.",
288
- },
289
- {
290
- element: "input",
291
- message:
292
- "Wherever possible, prefer using the form inputs from the App UI Kit for consistency and accessibility (TextInput, Checkbox, FileInput, etc).",
293
- },
294
- {
295
- element: "base",
296
- message:
297
- "The <base> tag is not supported in Canva Apps. We recommend using hash-based routing. For more on what is and isn't allowed in Canva Apps see https://www.canva.dev/docs/apps/content-security-policy/#what-is-and-isnt-allowed",
298
- },
299
- {
300
- element: "link",
301
- message:
302
- "If you're trying to include a css stylesheet, we recommend importing css using React, or using embedded stylesheets. For more on what is and isn't allowed in Canva Apps see https://www.canva.dev/docs/apps/content-security-policy/#what-is-and-isnt-allowed",
303
- },
304
- ],
305
- },
306
- ],
307
- },
308
- },
35
+ ...general,
36
+ ...i18n,
309
37
  ];
@@ -6,13 +6,13 @@
6
6
  "license": "SEE LICENSE IN LICENSE.md",
7
7
  "author": "Canva Pty Ltd.",
8
8
  "dependencies": {
9
- "@canva/app-ui-kit": "^3.8.0",
10
- "@canva/asset": "^1.7.1",
11
- "@canva/design": "^1.10.0",
12
- "@canva/error": "^1.1.0",
13
- "@canva/platform": "^1.1.0",
14
- "@canva/user": "^1.0.0",
15
- "cookie-parser": "1.4.6",
9
+ "@canva/app-ui-kit": "^4.4.0",
10
+ "@canva/asset": "^2.1.0",
11
+ "@canva/design": "^2.3.0",
12
+ "@canva/error": "^2.1.0",
13
+ "@canva/platform": "^2.1.0",
14
+ "@canva/user": "^2.1.0",
15
+ "cookie-parser": "1.4.7",
16
16
  "react": "18.3.1",
17
17
  "react-dom": "18.3.1"
18
18
  },
@@ -21,47 +21,47 @@
21
21
  "@svgr/webpack": "8.1.0",
22
22
  "@types/debug": "4.1.12",
23
23
  "@types/express": "4.17.21",
24
- "@types/express-serve-static-core": "4.19.5",
25
- "@types/jest": "29.5.12",
26
- "@types/jsonwebtoken": "9.0.6",
24
+ "@types/express-serve-static-core": "4.19.6",
25
+ "@types/jest": "29.5.14",
26
+ "@types/jsonwebtoken": "9.0.7",
27
27
  "@types/node": "20.10.0",
28
- "@types/node-fetch": "2.6.11",
28
+ "@types/node-fetch": "2.6.12",
29
29
  "@types/node-forge": "1.3.11",
30
30
  "@types/nodemon": "1.19.6",
31
31
  "@types/prompts": "2.4.9",
32
- "@types/react": "18.3.4",
33
- "@types/react-dom": "18.3.0",
32
+ "@types/react": "18.3.12",
33
+ "@types/react-dom": "18.3.1",
34
34
  "@types/webpack-env": "1.18.5",
35
35
  "chalk": "4.1.2",
36
36
  "cli-table3": "0.6.5",
37
37
  "css-loader": "7.1.2",
38
38
  "css-modules-typescript-loader": "4.0.1",
39
- "cssnano": "7.0.5",
40
- "debug": "4.3.6",
41
- "dotenv": "16.4.5",
39
+ "cssnano": "7.0.6",
40
+ "debug": "4.4.0",
41
+ "dotenv": "16.4.7",
42
42
  "exponential-backoff": "3.1.1",
43
- "express": "4.19.2",
43
+ "express": "4.21.2",
44
44
  "express-basic-auth": "1.2.1",
45
45
  "jest": "29.7.0",
46
46
  "jsonwebtoken": "9.0.2",
47
47
  "jwks-rsa": "3.1.0",
48
- "mini-css-extract-plugin": "2.9.1",
48
+ "mini-css-extract-plugin": "2.9.2",
49
49
  "node-fetch": "3.3.2",
50
50
  "node-forge": "1.3.1",
51
51
  "nodemon": "3.0.1",
52
52
  "postcss-loader": "8.1.1",
53
- "prettier": "3.3.3",
53
+ "prettier": "3.4.2",
54
54
  "prompts": "2.4.2",
55
55
  "style-loader": "4.0.0",
56
56
  "terser-webpack-plugin": "5.3.10",
57
- "ts-jest": "29.2.4",
57
+ "ts-jest": "29.2.5",
58
58
  "ts-loader": "9.5.1",
59
59
  "ts-node": "10.9.2",
60
60
  "typescript": "5.5.4",
61
61
  "url-loader": "4.1.1",
62
- "webpack": "5.94.0",
62
+ "webpack": "5.97.1",
63
63
  "webpack-cli": "5.1.4",
64
- "webpack-dev-server": "5.0.4",
64
+ "webpack-dev-server": "5.1.0",
65
65
  "yargs": "17.7.2"
66
66
  },
67
67
  "keywords": [
@@ -72,7 +72,10 @@
72
72
  "npm": "^9 || ^10"
73
73
  },
74
74
  "canvaCliMetadata": {
75
- "version": "Will be read from the root NPM package",
75
+ "metaFields": [
76
+ "name",
77
+ "version"
78
+ ],
76
79
  "inheritable": [
77
80
  "author",
78
81
  "license",
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env node
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+
5
+ const envPath = path.resolve(__dirname, "..", ".env");
6
+ const templatePath = path.resolve(__dirname, "..", ".env.template");
7
+
8
+ if (!fs.existsSync(envPath)) {
9
+ fs.copyFileSync(templatePath, envPath);
10
+ }
@@ -0,0 +1,101 @@
1
+ /* eslint-disable no-console */
2
+ import * as debug from "debug";
3
+ import type { Request, Response, NextFunction } from "express";
4
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
5
+ import Express from "express-serve-static-core";
6
+
7
+ /**
8
+ * Prefix your start command with `DEBUG=express:middleware:bearer` to enable debug logging
9
+ * for this middleware
10
+ */
11
+ const debugLogger = debug("express:middleware:bearer");
12
+
13
+ /**
14
+ * Augment the Express request context to include the appId/userId/brandId fields decoded
15
+ * from the JWT.
16
+ */
17
+ declare module "express-serve-static-core" {
18
+ export interface Request {
19
+ user_id: string;
20
+ }
21
+ }
22
+
23
+ const sendUnauthorizedResponse = (res: Response, message?: string) =>
24
+ res.status(401).json({ error: "unauthorized", message });
25
+
26
+ /**
27
+ * An Express.js middleware verifying a Bearer token.
28
+ * This middleware extracts the token from the `Authorization` header.
29
+ *
30
+ * @param getTokenFromRequest - A function that extracts a token from the request. If a token isn't found, throw a `JWTAuthorizationError`.
31
+ * @returns An Express.js middleware for verifying and decoding JWTs.
32
+ */
33
+ export function createBearerMiddleware(
34
+ tokenToUser: (access_token: string) => Promise<string | undefined>,
35
+ getTokenFromRequest: GetTokenFromRequest = getTokenFromHttpHeader,
36
+ ): (req: Request, res: Response, next: NextFunction) => void {
37
+ return async (req, res, next) => {
38
+ try {
39
+ debugLogger(`processing token for '${req.url}'`);
40
+
41
+ const token = await getTokenFromRequest(req);
42
+ const user = await tokenToUser(token);
43
+
44
+ if (!user) {
45
+ throw new AuthorizationError("Token is invalid");
46
+ }
47
+
48
+ req.user_id = user;
49
+
50
+ next();
51
+ } catch (e) {
52
+ if (e instanceof AuthorizationError) {
53
+ return sendUnauthorizedResponse(res, e.message);
54
+ }
55
+
56
+ next(e);
57
+ }
58
+ };
59
+ }
60
+
61
+ export type GetTokenFromRequest = (req: Request) => Promise<string> | string;
62
+
63
+ export const getTokenFromHttpHeader: GetTokenFromRequest = (
64
+ req: Request,
65
+ ): string => {
66
+ // The names of a HTTP header bearing the JWT, and a scheme
67
+ const headerName = "Authorization";
68
+ const schemeName = "Bearer";
69
+
70
+ const header = req.header(headerName);
71
+ if (!header) {
72
+ throw new AuthorizationError(`Missing the "${headerName}" header`);
73
+ }
74
+
75
+ if (!header.match(new RegExp(`^${schemeName}\\s+[^\\s]+$`, "i"))) {
76
+ console.trace(
77
+ `jwtMiddleware: failed to match token in "${headerName}" header`,
78
+ );
79
+ throw new AuthorizationError(
80
+ `Missing a "${schemeName}" token in the "${headerName}" header`,
81
+ );
82
+ }
83
+
84
+ const token = header.replace(new RegExp(`^${schemeName}\\s+`, "i"), "");
85
+
86
+ return token;
87
+ };
88
+
89
+ /**
90
+ * A class representing JWT validation errors in the JWT middleware.
91
+ * The error message provided to the constructor will be forwarded to the
92
+ * API consumer trying to access a JWT-protected endpoint.
93
+ * @private
94
+ */
95
+ export class AuthorizationError extends Error {
96
+ constructor(message: string) {
97
+ super(message);
98
+
99
+ Object.setPrototypeOf(this, AuthorizationError.prototype);
100
+ }
101
+ }
@@ -0,0 +1 @@
1
+ export { createBearerMiddleware } from "./bearer_middleware";