datocms-plugin-alt-text-ai 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/cover.png CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datocms-plugin-alt-text-ai",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "private": false,
5
5
  "homepage": "https://github.com/marcelofinamorvieira/datocms-plugin-alt-text-ai",
6
6
  "description": "Generate alt texts for your assets in a single click",
@@ -1,5 +1,8 @@
1
1
  import { Client, buildClient } from '@datocms/cma-client-browser';
2
- import { RenderFieldExtensionCtx } from 'datocms-plugin-sdk';
2
+ import {
3
+ type FileFieldValue,
4
+ RenderFieldExtensionCtx,
5
+ } from "datocms-plugin-sdk";
3
6
  import { Button, Canvas, Spinner } from 'datocms-react-ui';
4
7
  import { isArray } from 'lodash';
5
8
  import get from 'lodash/get';
@@ -12,10 +15,16 @@ type PropTypes = {
12
15
  async function fetchAlt(
13
16
  apiKey: string,
14
17
  client: Client,
15
- asset: Record<string, string>
18
+ asset: FileFieldValue
16
19
  ) {
17
20
  const { url } = await client.uploads.find(asset.upload_id);
18
21
 
22
+ // Instead of sending over files that are possibly too big or the wrong format, etc.,
23
+ // we'll use Imgix to pre-transform them into a valid format and smaller size
24
+ let transformedUrl = new URL(url);
25
+ transformedUrl.searchParams.set("fm", "jpeg");
26
+ transformedUrl.searchParams.set("w", "1024");
27
+
19
28
  const result = await (
20
29
  await fetch('https://alttext.ai/api/v1/images', {
21
30
  method: 'POST',
@@ -25,7 +34,7 @@ async function fetchAlt(
25
34
  },
26
35
  body: JSON.stringify({
27
36
  image: {
28
- url,
37
+ url: transformedUrl.toString(),
29
38
  },
30
39
  }),
31
40
  })
@@ -39,25 +48,39 @@ async function generateAlts(
39
48
  ctx: RenderFieldExtensionCtx,
40
49
  setIsLoading: React.Dispatch<React.SetStateAction<boolean>>
41
50
  ) {
51
+ if (!ctx.currentUserAccessToken) {
52
+ await ctx.alert('This plugin needs the currentUserAccessToken to function. Please give it that permission and try again.');
53
+ return
54
+ }
55
+
42
56
  setIsLoading(true);
43
57
  try {
44
58
  const client = buildClient({ apiToken: ctx.currentUserAccessToken || '' });
45
59
  const isGallery = isArray(currentFieldValue);
46
60
  if (isGallery) {
61
+ const existingAssets = currentFieldValue as FileFieldValue[]
47
62
  const newAssets = [];
48
- for (const asset of currentFieldValue) {
63
+ for (const asset of existingAssets) {
64
+ console.log('asset', asset);
49
65
  const result = await fetchAlt(
50
66
  ctx.plugin.attributes.parameters.apiKey as string,
51
67
  client,
52
68
  asset
53
69
  );
54
70
 
71
+ if (result.error_code) {
72
+ ctx.alert(
73
+ `Alt text error for <a href="/media/assets/${asset.upload_id}" target="_blank">image ${asset.upload_id}</a>: <code>${result.error_code}: ${result.errors?.base?.[0] ?? ""}</code>`,
74
+ );
75
+ }
76
+
77
+
55
78
  asset.alt = result.alt_text;
56
79
  newAssets.push(asset);
57
80
  }
58
81
  ctx.setFieldValue(ctx.fieldPath, newAssets);
59
82
  } else {
60
- const assetValue = currentFieldValue as Record<string, string>;
83
+ const assetValue = currentFieldValue as FileFieldValue;
61
84
 
62
85
  const result = await fetchAlt(
63
86
  ctx.plugin.attributes.parameters.apiKey as string,
@@ -65,14 +88,22 @@ async function generateAlts(
65
88
  assetValue
66
89
  );
67
90
 
91
+ if (result.error_code) {
92
+ await ctx.alert(
93
+ `Error fetching alt text: <code>${result.error_code}: ${result.errors?.base?.[0] ?? ""}</code>`,
94
+ );
95
+ return;
96
+ }
97
+
98
+
68
99
  assetValue.alt = result.alt_text;
69
100
  ctx.setFieldValue(ctx.fieldPath, assetValue);
70
101
  }
71
102
  } catch (error) {
72
103
  console.log(error);
104
+ } finally {
105
+ setIsLoading(false);
73
106
  }
74
-
75
- setIsLoading(false);
76
107
  }
77
108
 
78
109
  const AltTextAIButton = ({ ctx }: PropTypes) => {