@canva/cli 0.0.1-beta.1 → 0.0.1-beta.3
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/cli.js +134 -134
- package/package.json +1 -1
- package/templates/base/backend/routers/oauth.ts +393 -0
- package/templates/base/eslint.config.mjs +5 -275
- package/templates/base/package.json +1 -1
- package/templates/base/utils/backend/bearer_middleware/bearer_middleware.ts +101 -0
- package/templates/base/utils/backend/bearer_middleware/index.ts +1 -0
- package/templates/base/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +192 -0
- package/templates/base/utils/use_add_element.ts +48 -0
- package/templates/base/utils/use_feature_support.ts +28 -0
- package/templates/common/README.md +0 -67
- package/templates/common/conf/eslint-general.mjs +277 -0
- package/templates/common/conf/eslint-i18n.mjs +23 -0
- package/templates/dam/backend/server.ts +0 -7
- package/templates/dam/eslint.config.mjs +6 -275
- package/templates/dam/package.json +8 -7
- package/templates/dam/src/app.tsx +2 -135
- package/templates/gen_ai/README.md +40 -1
- package/templates/gen_ai/backend/routers/oauth.ts +393 -0
- package/templates/gen_ai/backend/server.ts +1 -1
- package/templates/gen_ai/eslint.config.mjs +5 -275
- package/templates/gen_ai/package.json +7 -6
- package/templates/gen_ai/src/api/api.ts +44 -27
- package/templates/gen_ai/src/components/footer.tsx +9 -5
- package/templates/gen_ai/src/components/image_grid.tsx +9 -7
- package/templates/gen_ai/src/components/loading_results.tsx +8 -4
- package/templates/gen_ai/src/components/prompt_input.tsx +2 -0
- package/templates/gen_ai/src/context/app_context.tsx +8 -2
- package/templates/gen_ai/src/services/auth.tsx +5 -10
- package/templates/gen_ai/utils/backend/bearer_middleware/bearer_middleware.ts +101 -0
- package/templates/gen_ai/utils/backend/bearer_middleware/index.ts +1 -0
- package/templates/gen_ai/utils/backend/bearer_middleware/tests/bearer_middleware.tests.ts +192 -0
- package/templates/hello_world/eslint.config.mjs +5 -275
- package/templates/hello_world/package.json +7 -5
- package/templates/hello_world/src/app.tsx +5 -3
- package/templates/hello_world/utils/use_add_element.ts +48 -0
- package/templates/hello_world/utils/use_feature_support.ts +28 -0
- package/templates/dam/backend/database/database.ts +0 -42
- package/templates/dam/backend/routers/auth.ts +0 -285
- package/templates/gen_ai/backend/routers/auth.ts +0 -285
- package/templates/gen_ai/utils/backend/jwt_middleware/index.ts +0 -1
- package/templates/gen_ai/utils/backend/jwt_middleware/jwt_middleware.ts +0 -229
- package/templates/gen_ai/utils/backend/jwt_middleware/tests/jwt_middleware.tests.ts +0 -630
|
@@ -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);
|
|
@@ -34,276 +32,8 @@ export default [
|
|
|
34
32
|
"plugin:@typescript-eslint/strict",
|
|
35
33
|
"plugin:@typescript-eslint/stylistic",
|
|
36
34
|
"plugin:react/recommended",
|
|
37
|
-
"plugin:jest/recommended"
|
|
35
|
+
"plugin:jest/recommended",
|
|
38
36
|
),
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
},
|
|
37
|
+
...general,
|
|
38
|
+
...i18n,
|
|
309
39
|
];
|
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@canva/app-i18n-kit": "^0.0.1-beta.5",
|
|
20
|
-
"@canva/app-ui-kit": "^
|
|
21
|
-
"@canva/asset": "^
|
|
22
|
-
"@canva/design": "^1.
|
|
23
|
-
"@canva/platform": "^
|
|
24
|
-
"@canva/user": "^
|
|
20
|
+
"@canva/app-ui-kit": "^4.0.0",
|
|
21
|
+
"@canva/asset": "^2.0.0",
|
|
22
|
+
"@canva/design": "^2.1.0",
|
|
23
|
+
"@canva/platform": "^2.0.0",
|
|
24
|
+
"@canva/user": "^2.0.0",
|
|
25
25
|
"cookie-parser": "1.4.6",
|
|
26
26
|
"cors": "2.8.5",
|
|
27
27
|
"html-react-parser": "5.1.12",
|
|
@@ -61,7 +61,8 @@
|
|
|
61
61
|
"cssnano": "7.0.5",
|
|
62
62
|
"debug": "4.3.6",
|
|
63
63
|
"dotenv": "16.4.5",
|
|
64
|
-
"eslint": "
|
|
64
|
+
"eslint": "8.57.1",
|
|
65
|
+
"eslint-plugin-formatjs": "4.13.3",
|
|
65
66
|
"eslint-plugin-jest": "28.8.0",
|
|
66
67
|
"eslint-plugin-react": "7.35.0",
|
|
67
68
|
"exponential-backoff": "3.1.1",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Oauth } from "@canva/user";
|
|
2
2
|
import { POLLING_INTERVAL_IN_SECONDS } from "src/config";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -66,18 +66,21 @@ const endpoints = {
|
|
|
66
66
|
* @param {number} options.numberOfImages - The number of images to generate.
|
|
67
67
|
* @returns {Promise<QueueImageGenerationResponse>} A promise that resolves to the created job ID.
|
|
68
68
|
*/
|
|
69
|
-
export const queueImageGeneration = async (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
69
|
+
export const queueImageGeneration = async (
|
|
70
|
+
{
|
|
71
|
+
prompt,
|
|
72
|
+
numberOfImages,
|
|
73
|
+
}: {
|
|
74
|
+
prompt: string;
|
|
75
|
+
numberOfImages: number;
|
|
76
|
+
},
|
|
77
|
+
oauth: Oauth,
|
|
78
|
+
): Promise<QueueImageGenerationResponse> => {
|
|
76
79
|
const url = new URL(endpoints.queueImageGeneration, BACKEND_HOST);
|
|
77
80
|
url.searchParams.append("count", numberOfImages.toString());
|
|
78
81
|
url.searchParams.append("prompt", prompt);
|
|
79
82
|
|
|
80
|
-
const result: QueueImageGenerationResponse = await sendRequest(url);
|
|
83
|
+
const result: QueueImageGenerationResponse = await sendRequest(url, oauth);
|
|
81
84
|
|
|
82
85
|
return result;
|
|
83
86
|
};
|
|
@@ -87,11 +90,14 @@ export const queueImageGeneration = async ({
|
|
|
87
90
|
* @param {string} jobId - The ID of the job to retrieve status for.
|
|
88
91
|
* @returns {Promise<ImageGenerationResult>} A promise that resolves to the image generation result.
|
|
89
92
|
*/
|
|
90
|
-
export const getImageGenerationJobStatus = async (
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
export const getImageGenerationJobStatus = async (
|
|
94
|
+
{
|
|
95
|
+
jobId,
|
|
96
|
+
}: {
|
|
97
|
+
jobId: string;
|
|
98
|
+
},
|
|
99
|
+
oauth: Oauth,
|
|
100
|
+
): Promise<ImageGenerationResult> => {
|
|
95
101
|
const url = new URL(endpoints.getImageGenerationJobStatus, BACKEND_HOST);
|
|
96
102
|
url.searchParams.append("jobId", jobId);
|
|
97
103
|
|
|
@@ -103,6 +109,7 @@ export const getImageGenerationJobStatus = async ({
|
|
|
103
109
|
try {
|
|
104
110
|
const response = (await sendRequest(
|
|
105
111
|
url,
|
|
112
|
+
oauth,
|
|
106
113
|
)) as ImageGenerationJobStatusResponse;
|
|
107
114
|
if (response.status === "completed") {
|
|
108
115
|
return { images: response.images, credits: response.credits };
|
|
@@ -131,12 +138,13 @@ export const getImageGenerationJobStatus = async ({
|
|
|
131
138
|
*/
|
|
132
139
|
export const cancelImageGenerationJob = async (
|
|
133
140
|
jobId: string,
|
|
141
|
+
oauth: Oauth,
|
|
134
142
|
): Promise<void> => {
|
|
135
143
|
const url = new URL(endpoints.cancelImageGenerationJob, BACKEND_HOST);
|
|
136
144
|
url.searchParams.append("jobId", jobId);
|
|
137
145
|
|
|
138
146
|
try {
|
|
139
|
-
await sendRequest(url, {
|
|
147
|
+
await sendRequest(url, oauth, {
|
|
140
148
|
method: "POST",
|
|
141
149
|
});
|
|
142
150
|
} catch {
|
|
@@ -148,23 +156,26 @@ export const cancelImageGenerationJob = async (
|
|
|
148
156
|
* Retrieves the remaining credits.
|
|
149
157
|
* @returns {Promise<RemainingCreditsResult>} - A promise that resolves to the remaining credits.
|
|
150
158
|
*/
|
|
151
|
-
export const getRemainingCredits =
|
|
152
|
-
|
|
153
|
-
|
|
159
|
+
export const getRemainingCredits = async (
|
|
160
|
+
oauth: Oauth,
|
|
161
|
+
): Promise<RemainingCreditsResult> => {
|
|
162
|
+
const url = new URL(endpoints.getRemainingCredits, BACKEND_HOST);
|
|
154
163
|
|
|
155
|
-
|
|
164
|
+
const result: RemainingCreditsResult = await sendRequest(url, oauth);
|
|
156
165
|
|
|
157
|
-
|
|
158
|
-
|
|
166
|
+
return result;
|
|
167
|
+
};
|
|
159
168
|
|
|
160
169
|
/**
|
|
161
170
|
* Purchases credits for the user.
|
|
162
171
|
* @returns {Promise<RemainingCreditsResult>} - A promise that resolves to the remaining credits after purchasing.
|
|
163
172
|
*/
|
|
164
|
-
export const purchaseCredits = async (
|
|
173
|
+
export const purchaseCredits = async (
|
|
174
|
+
oauth: Oauth,
|
|
175
|
+
): Promise<RemainingCreditsResult> => {
|
|
165
176
|
const url = new URL(endpoints.purchaseCredits, BACKEND_HOST);
|
|
166
177
|
|
|
167
|
-
const result: RemainingCreditsResult = await sendRequest(url, {
|
|
178
|
+
const result: RemainingCreditsResult = await sendRequest(url, oauth, {
|
|
168
179
|
method: "POST",
|
|
169
180
|
});
|
|
170
181
|
|
|
@@ -177,10 +188,12 @@ export const purchaseCredits = async (): Promise<RemainingCreditsResult> => {
|
|
|
177
188
|
*
|
|
178
189
|
* Note: You must register the provided endpoint via the Developer Portal.
|
|
179
190
|
*/
|
|
180
|
-
export const checkAuthenticationStatus = async (
|
|
191
|
+
export const checkAuthenticationStatus = async (
|
|
192
|
+
oauth: Oauth,
|
|
193
|
+
): Promise<LoggedInState> => {
|
|
181
194
|
try {
|
|
182
195
|
const url = new URL(endpoints.getLoggedInStatus, BACKEND_HOST);
|
|
183
|
-
const result: { isAuthenticated: string } = await sendRequest(url, {
|
|
196
|
+
const result: { isAuthenticated: string } = await sendRequest(url, oauth, {
|
|
184
197
|
method: "POST",
|
|
185
198
|
});
|
|
186
199
|
|
|
@@ -202,8 +215,12 @@ export const checkAuthenticationStatus = async (): Promise<LoggedInState> => {
|
|
|
202
215
|
* @param {RequestInit} [options] - Optional fetch options to be passed to the fetch function.
|
|
203
216
|
* @returns {Promise<Object>} - A promise that resolves to the response body.
|
|
204
217
|
*/
|
|
205
|
-
const sendRequest = async <T>(
|
|
206
|
-
|
|
218
|
+
const sendRequest = async <T>(
|
|
219
|
+
url: URL,
|
|
220
|
+
oauth: Oauth,
|
|
221
|
+
options?: RequestInit,
|
|
222
|
+
): Promise<T> => {
|
|
223
|
+
const token = (await oauth.getAccessToken())?.token;
|
|
207
224
|
const res = await fetch(url, {
|
|
208
225
|
headers: {
|
|
209
226
|
...options?.headers,
|
|
@@ -27,6 +27,7 @@ export const Footer = () => {
|
|
|
27
27
|
setIsLoadingImages,
|
|
28
28
|
remainingCredits,
|
|
29
29
|
setRemainingCredits,
|
|
30
|
+
oauth,
|
|
30
31
|
} = useAppContext();
|
|
31
32
|
const intl = useIntl();
|
|
32
33
|
|
|
@@ -74,10 +75,13 @@ export const Footer = () => {
|
|
|
74
75
|
|
|
75
76
|
setIsLoadingImages(true);
|
|
76
77
|
try {
|
|
77
|
-
const { jobId } = await queueImageGeneration(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
const { jobId } = await queueImageGeneration(
|
|
79
|
+
{
|
|
80
|
+
prompt: promptInput,
|
|
81
|
+
numberOfImages: NUMBER_OF_IMAGES_TO_GENERATE,
|
|
82
|
+
},
|
|
83
|
+
oauth,
|
|
84
|
+
);
|
|
81
85
|
|
|
82
86
|
setJobId(jobId);
|
|
83
87
|
} catch {
|
|
@@ -91,7 +95,7 @@ export const Footer = () => {
|
|
|
91
95
|
};
|
|
92
96
|
|
|
93
97
|
const onPurchaseMoreCredits = async () => {
|
|
94
|
-
const { credits } = await purchaseCredits();
|
|
98
|
+
const { credits } = await purchaseCredits(oauth);
|
|
95
99
|
|
|
96
100
|
setRemainingCredits(credits);
|
|
97
101
|
};
|
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import { Grid, ImageCard, Rows, Text } from "@canva/app-ui-kit";
|
|
2
2
|
import type { QueuedImage } from "@canva/asset";
|
|
3
3
|
import { upload } from "@canva/asset";
|
|
4
|
-
import {
|
|
4
|
+
import { addElementAtPoint, ui } from "@canva/design";
|
|
5
5
|
import type { ImageType } from "src/api";
|
|
6
6
|
import { useAppContext } from "src/context";
|
|
7
7
|
import * as styles from "styles/utils.css";
|
|
8
|
-
import { FormattedMessage } from "react-intl";
|
|
8
|
+
import { FormattedMessage, useIntl } from "react-intl";
|
|
9
9
|
|
|
10
10
|
const THUMBNAIL_HEIGHT = 150;
|
|
11
11
|
|
|
12
12
|
const uploadImage = async (image: ImageType): Promise<QueuedImage> => {
|
|
13
13
|
// Upload the image using @canva/asset.
|
|
14
14
|
const queuedImage = await upload({
|
|
15
|
-
type: "
|
|
15
|
+
type: "image",
|
|
16
16
|
mimeType: "image/jpeg",
|
|
17
17
|
thumbnailUrl: image.thumbnail.url,
|
|
18
18
|
url: image.fullsize.url,
|
|
19
19
|
width: image.fullsize.width,
|
|
20
20
|
height: image.fullsize.height,
|
|
21
|
+
aiDisclosure: "app_generated",
|
|
21
22
|
});
|
|
22
23
|
|
|
23
24
|
return queuedImage;
|
|
@@ -34,8 +35,8 @@ export const ImageGrid = () => {
|
|
|
34
35
|
try {
|
|
35
36
|
parentNode?.classList.add(styles.hidden);
|
|
36
37
|
|
|
37
|
-
await ui.
|
|
38
|
-
type: "
|
|
38
|
+
await ui.startDragToPoint(event, {
|
|
39
|
+
type: "image",
|
|
39
40
|
resolveImageRef: () => uploadImage(image),
|
|
40
41
|
previewUrl: image.thumbnail.url,
|
|
41
42
|
previewSize: {
|
|
@@ -55,8 +56,9 @@ export const ImageGrid = () => {
|
|
|
55
56
|
const onImageClick = async (image: ImageType) => {
|
|
56
57
|
const queuedImage = await uploadImage(image);
|
|
57
58
|
|
|
58
|
-
await
|
|
59
|
-
type: "
|
|
59
|
+
await addElementAtPoint({
|
|
60
|
+
type: "image",
|
|
61
|
+
altText: { text: image.label, decorative: false },
|
|
60
62
|
ref: queuedImage.ref,
|
|
61
63
|
});
|
|
62
64
|
};
|
|
@@ -70,6 +70,7 @@ export const LoadingResults = ({
|
|
|
70
70
|
promptInput,
|
|
71
71
|
setGeneratedImages,
|
|
72
72
|
setRemainingCredits,
|
|
73
|
+
oauth,
|
|
73
74
|
} = useAppContext();
|
|
74
75
|
|
|
75
76
|
useEffect(() => {
|
|
@@ -82,9 +83,12 @@ export const LoadingResults = ({
|
|
|
82
83
|
const pollJobStatus = async () => {
|
|
83
84
|
if (jobId) {
|
|
84
85
|
try {
|
|
85
|
-
const { images, credits } = await getImageGenerationJobStatus(
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
const { images, credits } = await getImageGenerationJobStatus(
|
|
87
|
+
{
|
|
88
|
+
jobId,
|
|
89
|
+
},
|
|
90
|
+
oauth,
|
|
91
|
+
);
|
|
88
92
|
setGeneratedImages(images);
|
|
89
93
|
setRemainingCredits(credits);
|
|
90
94
|
// Clear the jobId after fetching images
|
|
@@ -117,7 +121,7 @@ export const LoadingResults = ({
|
|
|
117
121
|
]);
|
|
118
122
|
|
|
119
123
|
const onCancelClick = async () => {
|
|
120
|
-
await cancelImageGenerationJob(jobId);
|
|
124
|
+
await cancelImageGenerationJob(jobId, oauth);
|
|
121
125
|
setIsLoadingImages(false);
|
|
122
126
|
navigate(Paths.HOME);
|
|
123
127
|
};
|
|
@@ -19,6 +19,8 @@ const MIN_INPUT_ROWS = 3;
|
|
|
19
19
|
/**
|
|
20
20
|
* Array of example prompts that could be used to generate interesting pictures with an AI.
|
|
21
21
|
* Consider fetching these prompts from a server or API call for dynamic and varied content.
|
|
22
|
+
* These would need to be localised, but that is left out here as the method would depend on
|
|
23
|
+
* the specific implementation or API used.
|
|
22
24
|
*/
|
|
23
25
|
const examplePrompts: string[] = [
|
|
24
26
|
"Cats ruling a parallel universe",
|
|
@@ -3,6 +3,8 @@ import { useIntl } from "react-intl";
|
|
|
3
3
|
import type { ImageType, LoggedInState } from "src/api";
|
|
4
4
|
import { checkAuthenticationStatus, getRemainingCredits } from "src/api";
|
|
5
5
|
import { ContextMessages as Messages } from "./context.messages";
|
|
6
|
+
import type { Oauth } from "@canva/user";
|
|
7
|
+
import { auth } from "@canva/user";
|
|
6
8
|
|
|
7
9
|
export interface AppContextType {
|
|
8
10
|
loggedInState: LoggedInState;
|
|
@@ -25,6 +27,7 @@ export interface AppContextType {
|
|
|
25
27
|
setPromptInputError: (value: string) => void;
|
|
26
28
|
generatedImages: ImageType[];
|
|
27
29
|
setGeneratedImages: (value: ImageType[]) => void;
|
|
30
|
+
oauth: Oauth;
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
export const AppContext = createContext<AppContextType>({
|
|
@@ -48,6 +51,7 @@ export const AppContext = createContext<AppContextType>({
|
|
|
48
51
|
setPromptInputError: () => {},
|
|
49
52
|
generatedImages: [] as ImageType[],
|
|
50
53
|
setGeneratedImages: () => {},
|
|
54
|
+
oauth: {} as Oauth,
|
|
51
55
|
});
|
|
52
56
|
|
|
53
57
|
/**
|
|
@@ -77,6 +81,7 @@ export const ContextProvider = ({
|
|
|
77
81
|
const [generatedImages, setGeneratedImages] = useState<ImageType[]>([]);
|
|
78
82
|
const [creditsError, setCreditsError] = useState<string>("");
|
|
79
83
|
const intl = useIntl();
|
|
84
|
+
const oauth = auth.initOauth();
|
|
80
85
|
|
|
81
86
|
// Fetches initial data on component mount
|
|
82
87
|
useEffect(() => {
|
|
@@ -86,7 +91,7 @@ export const ContextProvider = ({
|
|
|
86
91
|
|
|
87
92
|
// Fetch remaining credits
|
|
88
93
|
try {
|
|
89
|
-
const { credits } = await getRemainingCredits();
|
|
94
|
+
const { credits } = await getRemainingCredits(oauth);
|
|
90
95
|
setRemainingCredits(credits);
|
|
91
96
|
} catch (error) {
|
|
92
97
|
setAppError(
|
|
@@ -98,7 +103,7 @@ export const ContextProvider = ({
|
|
|
98
103
|
|
|
99
104
|
// Fetch login status
|
|
100
105
|
try {
|
|
101
|
-
checkAuthenticationStatus();
|
|
106
|
+
checkAuthenticationStatus(oauth);
|
|
102
107
|
} catch (error) {
|
|
103
108
|
setAppError(
|
|
104
109
|
intl.formatMessage(Messages.appErrorGetLoggedInStatusFailed),
|
|
@@ -168,6 +173,7 @@ export const ContextProvider = ({
|
|
|
168
173
|
setPromptInputError,
|
|
169
174
|
generatedImages,
|
|
170
175
|
setGeneratedImages,
|
|
176
|
+
oauth,
|
|
171
177
|
};
|
|
172
178
|
|
|
173
179
|
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
|
|
@@ -1,25 +1,20 @@
|
|
|
1
|
-
import { auth } from "@canva/user";
|
|
2
1
|
import { useAppContext } from "src/context";
|
|
3
2
|
|
|
4
3
|
export const useAuth = () => {
|
|
5
|
-
const { setLoggedInState } = useAppContext();
|
|
4
|
+
const { setLoggedInState, oauth } = useAppContext();
|
|
6
5
|
|
|
7
6
|
const requestAuthentication = async () => {
|
|
8
7
|
try {
|
|
9
|
-
const response = await
|
|
8
|
+
const response = await oauth.requestAuthorization();
|
|
10
9
|
switch (response.status) {
|
|
11
|
-
case "
|
|
12
|
-
setLoggedInState("authenticated");
|
|
13
|
-
break;
|
|
14
|
-
case "ABORTED":
|
|
10
|
+
case "aborted":
|
|
15
11
|
setLoggedInState("not_authenticated");
|
|
16
12
|
break;
|
|
17
|
-
case "
|
|
18
|
-
setLoggedInState("
|
|
13
|
+
case "completed":
|
|
14
|
+
setLoggedInState("authenticated");
|
|
19
15
|
break;
|
|
20
16
|
default:
|
|
21
17
|
setLoggedInState("not_authenticated");
|
|
22
|
-
break;
|
|
23
18
|
}
|
|
24
19
|
} catch (e) {
|
|
25
20
|
// eslint-disable-next-line no-console
|