datocms-plugin-alt-text-ai 0.5.0 → 0.5.2

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/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "datocms-plugin-alt-text-ai",
3
- "version": "0.5.0",
4
- "private": false,
5
- "homepage": "https://github.com/marcelofinamorvieira/datocms-plugin-alt-text-ai",
3
+ "homepage": "https://github.com/datocms/plugins/blob/addMarceloPlugins/alt-text-ai/README.md",
4
+ "version": "0.5.2",
5
+ "author": "DatoCMS <support@datocms.com>",
6
6
  "description": "Generate alt texts for your assets in a single click",
7
7
  "keywords": [
8
8
  "datocms",
@@ -1,31 +1,36 @@
1
- import { Client, buildClient } from '@datocms/cma-client-browser';
2
- import { RenderFieldExtensionCtx } from 'datocms-plugin-sdk';
3
- import { Button, Canvas, Spinner } from 'datocms-react-ui';
4
- import { isArray } from 'lodash';
5
- import get from 'lodash/get';
6
- import { useState } from 'react';
1
+ import { Client, buildClient } from "@datocms/cma-client-browser";
2
+ import {
3
+ type FileFieldValue,
4
+ RenderFieldExtensionCtx,
5
+ } from "datocms-plugin-sdk";
6
+ import { Button, Canvas, Spinner } from "datocms-react-ui";
7
+ import { isArray } from "lodash";
8
+ import get from "lodash/get";
9
+ import { useState } from "react";
7
10
 
8
11
  type PropTypes = {
9
12
  ctx: RenderFieldExtensionCtx;
10
13
  };
11
14
 
12
- async function fetchAlt(
13
- apiKey: string,
14
- client: Client,
15
- asset: Record<string, string>
16
- ) {
15
+ async function fetchAlt(apiKey: string, client: Client, asset: FileFieldValue) {
17
16
  const { url } = await client.uploads.find(asset.upload_id);
18
17
 
18
+ // Instead of sending over files that are possibly too big or the wrong format, etc.,
19
+ // we'll use Imgix to pre-transform them into a valid format and smaller size
20
+ let transformedUrl = new URL(url);
21
+ transformedUrl.searchParams.set("fm", "jpeg");
22
+ transformedUrl.searchParams.set("w", "1024");
23
+
19
24
  const result = await (
20
- await fetch('https://alttext.ai/api/v1/images', {
21
- method: 'POST',
25
+ await fetch("https://alttext.ai/api/v1/images", {
26
+ method: "POST",
22
27
  headers: {
23
- 'Content-Type': 'application/json',
24
- 'X-API-Key': apiKey ?? '',
28
+ "Content-Type": "application/json",
29
+ "X-API-Key": apiKey ?? "",
25
30
  },
26
31
  body: JSON.stringify({
27
32
  image: {
28
- url,
33
+ url: transformedUrl.toString(),
29
34
  },
30
35
  }),
31
36
  })
@@ -37,42 +42,64 @@ async function fetchAlt(
37
42
  async function generateAlts(
38
43
  currentFieldValue: unknown,
39
44
  ctx: RenderFieldExtensionCtx,
40
- setIsLoading: React.Dispatch<React.SetStateAction<boolean>>
45
+ setIsLoading: React.Dispatch<React.SetStateAction<boolean>>,
41
46
  ) {
47
+ if (!ctx.currentUserAccessToken) {
48
+ await ctx.alert(
49
+ "This plugin needs the currentUserAccessToken to function. Please give it that permission and try again.",
50
+ );
51
+ return;
52
+ }
53
+
42
54
  setIsLoading(true);
43
55
  try {
44
- const client = buildClient({ apiToken: ctx.currentUserAccessToken || '' });
56
+ const client = buildClient({ apiToken: ctx.currentUserAccessToken || "" });
45
57
  const isGallery = isArray(currentFieldValue);
46
58
  if (isGallery) {
59
+ const existingAssets = currentFieldValue as FileFieldValue[];
47
60
  const newAssets = [];
48
- for (const asset of currentFieldValue) {
61
+ for (const asset of existingAssets) {
62
+ console.log("asset", asset);
49
63
  const result = await fetchAlt(
50
64
  ctx.plugin.attributes.parameters.apiKey as string,
51
65
  client,
52
- asset
66
+ asset,
53
67
  );
54
68
 
69
+ if (result.error_code) {
70
+ ctx.alert(
71
+ `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>`,
72
+ );
73
+ }
74
+
55
75
  asset.alt = result.alt_text;
56
76
  newAssets.push(asset);
57
77
  }
58
78
  ctx.setFieldValue(ctx.fieldPath, newAssets);
59
79
  } else {
60
- const assetValue = currentFieldValue as Record<string, string>;
80
+ const assetValue = currentFieldValue as FileFieldValue;
61
81
 
62
82
  const result = await fetchAlt(
63
83
  ctx.plugin.attributes.parameters.apiKey as string,
64
84
  client,
65
- assetValue
85
+ assetValue,
66
86
  );
67
87
 
88
+ if (result.error_code) {
89
+ await ctx.alert(
90
+ `Error fetching alt text: <code>${result.error_code}: ${result.errors?.base?.[0] ?? ""}</code>`,
91
+ );
92
+ return;
93
+ }
94
+
68
95
  assetValue.alt = result.alt_text;
69
96
  ctx.setFieldValue(ctx.fieldPath, assetValue);
70
97
  }
71
98
  } catch (error) {
72
99
  console.log(error);
100
+ } finally {
101
+ setIsLoading(false);
73
102
  }
74
-
75
- setIsLoading(false);
76
103
  }
77
104
 
78
105
  const AltTextAIButton = ({ ctx }: PropTypes) => {