@iconify/tools 5.0.0-beta.4 → 5.0.0

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.
@@ -44,7 +44,7 @@ async function getAPICache(dir, key) {
44
44
  try {
45
45
  const content = JSON.parse(await promises.readFile(item.filename, "utf8"));
46
46
  return content.version === cacheVersion && content.expires > time ? content.data : null;
47
- } catch (err) {
47
+ } catch {
48
48
  return null;
49
49
  }
50
50
  }
@@ -63,7 +63,7 @@ async function getStoredFiles(dir, clear = false) {
63
63
  storedFiles[dir] = storage;
64
64
  try {
65
65
  await promises.mkdir(dir, { recursive: true });
66
- } catch (err) {}
66
+ } catch {}
67
67
  await scanDirectory(dir, async (ext, file, subdir, path) => {
68
68
  if (ext !== ".json") return false;
69
69
  const filename = path + subdir + file + ext;
@@ -1,6 +1,6 @@
1
- import { APICacheOptions, APIQueryParams } from "./types.js";
1
+ import { APICacheOptions, APIQueryParams, APIQueryResult } from "./types.js";
2
2
  /**
3
3
  * Send API query
4
4
  */
5
- declare function sendAPIQuery(query: APIQueryParams, cache?: APICacheOptions): Promise<number | string>;
5
+ declare function sendAPIQuery(query: APIQueryParams, cache?: APICacheOptions): Promise<APIQueryResult>;
6
6
  export { sendAPIQuery };
@@ -9,15 +9,18 @@ async function sendAPIQuery(query, cache) {
9
9
  const cacheKey = cache ? apiCacheKey(query) : "";
10
10
  if (cache) {
11
11
  const cached = await getAPICache(cache.dir, cacheKey);
12
- if (cached) return cached;
12
+ if (cached) return {
13
+ success: true,
14
+ content: cached
15
+ };
13
16
  }
14
- const result = await sendQuery(query);
15
- if (cache && typeof result !== "number") try {
16
- await storeAPICache(cache, cacheKey, result);
17
- } catch (err) {
17
+ const response = await sendQuery(query);
18
+ if (cache && response.success) try {
19
+ await storeAPICache(cache, cacheKey, response.content);
20
+ } catch {
18
21
  console.error("Error writing API cache");
19
22
  }
20
- return result;
23
+ return response;
21
24
  }
22
25
  /**
23
26
  * Send query
@@ -27,9 +30,13 @@ async function sendQuery(query) {
27
30
  const url = query.uri + (params ? "?" + params : "");
28
31
  const headers = query.headers;
29
32
  fetchCallbacks.onStart?.(url, query);
30
- function fail(value) {
31
- fetchCallbacks.onError?.(url, query, value);
32
- return value ?? 404;
33
+ function fail(response, status) {
34
+ fetchCallbacks.onError?.(url, query, status);
35
+ return {
36
+ success: false,
37
+ response,
38
+ error: status ?? 404
39
+ };
33
40
  }
34
41
  const fetch = getFetch();
35
42
  try {
@@ -37,12 +44,15 @@ async function sendQuery(query) {
37
44
  ...axiosConfig,
38
45
  headers
39
46
  });
40
- if (response.status !== 200) return fail(response.status);
47
+ if (response.status !== 200) return fail(response, response.status);
41
48
  const data = await response.text();
42
- if (typeof data !== "string") return fail();
49
+ if (typeof data !== "string") return fail(response);
43
50
  fetchCallbacks.onSuccess?.(url, query);
44
- return data;
45
- } catch (err) {
51
+ return {
52
+ success: true,
53
+ content: data
54
+ };
55
+ } catch {
46
56
  return fail();
47
57
  }
48
58
  }
@@ -13,4 +13,17 @@ interface APIQueryParams {
13
13
  params?: URLSearchParams;
14
14
  headers?: Record<string, string>;
15
15
  }
16
- export { APICacheOptions, APIQueryParams };
16
+ /**
17
+ * Query result
18
+ */
19
+ interface APIQuerySuccess {
20
+ success: true;
21
+ content: string;
22
+ }
23
+ interface APIQueryFailure {
24
+ success: false;
25
+ response?: Response;
26
+ error: number;
27
+ }
28
+ type APIQueryResult = APIQuerySuccess | APIQueryFailure;
29
+ export { APICacheOptions, APIQueryParams, APIQueryResult };
@@ -4,15 +4,15 @@ import { sendAPIQuery } from "../api/index.js";
4
4
  * Get latest hash from GitHub using API
5
5
  */
6
6
  async function getGitHubRepoHash(options) {
7
- const data = await sendAPIQuery({
7
+ const response = await sendAPIQuery({
8
8
  uri: `https://api.github.com/repos/${options.user}/${options.repo}/branches/${options.branch}`,
9
9
  headers: {
10
10
  Accept: "application/vnd.github.v3+json",
11
11
  Authorization: "token " + options.token
12
12
  }
13
13
  });
14
- if (typeof data !== "string") throw new Error(`Error downloading data from GitHub API: ${data}`);
15
- const hash = JSON.parse(data)?.commit?.sha;
14
+ if (!response.success) throw new Error(`Error downloading data from GitHub API: ${response.error}`);
15
+ const hash = JSON.parse(response.content)?.commit?.sha;
16
16
  if (typeof hash !== "string") throw new Error("Error parsing GitHub API response");
17
17
  return hash;
18
18
  }
@@ -5,12 +5,12 @@ import { defaultGitLabBaseURI } from "./types.js";
5
5
  * Get latest hash from GitHub using API
6
6
  */
7
7
  async function getGitLabRepoHash(options) {
8
- const data = await sendAPIQuery({
8
+ const response = await sendAPIQuery({
9
9
  uri: `${options.uri || defaultGitLabBaseURI}/${options.project}/repository/branches/${options.branch}/`,
10
10
  headers: { Authorization: "token " + options.token }
11
11
  });
12
- if (typeof data !== "string") throw new Error(`Error downloading data from GitLab API: ${data}`);
13
- const content = JSON.parse(data);
12
+ if (!response.success) throw new Error(`Error downloading data from GitLab API: ${response.error}`);
13
+ const content = JSON.parse(response.content);
14
14
  const item = (content instanceof Array ? content : [content]).find((item$1) => item$1.name === options.branch && typeof item$1.commit.id === "string");
15
15
  if (!item) throw new Error("Error parsing GitLab API response");
16
16
  return item.commit.id;
@@ -16,7 +16,7 @@ async function downloadNPMPackage(options) {
16
16
  else expectedVersion = null;
17
17
  else expectedVersion = ifModifiedSince === true ? await getPackageVersion(contentsDir) : ifModifiedSince;
18
18
  if (version === expectedVersion) return "not_modified";
19
- } catch (err) {
19
+ } catch {
20
20
  options.cleanup = true;
21
21
  }
22
22
  const archiveURL = versionInfo.file;
@@ -26,7 +26,7 @@ async function downloadNPMPackage(options) {
26
26
  let archiveExists = false;
27
27
  try {
28
28
  archiveExists = (await promises.stat(archiveTarget)).isFile();
29
- } catch (err) {}
29
+ } catch {}
30
30
  if (!archiveExists) {
31
31
  if (options.log) console.log(`Downloading ${archiveURL}`);
32
32
  await downloadFile({
@@ -11,7 +11,7 @@ async function exportCustomFiles(dir, options, result) {
11
11
  if (content === null) {
12
12
  try {
13
13
  await promises.unlink(dir + "/" + filename);
14
- } catch (err) {}
14
+ } catch {}
15
15
  continue;
16
16
  }
17
17
  if (typeof content === "string") await promises.writeFile(dir + "/" + filename, content, "utf8");
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Get error message from Figma API response
3
+ */
4
+ declare function getFigmaErrorMessage(status: number, response?: Response): string;
5
+ export { getFigmaErrorMessage };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Get error message from Figma API response
3
+ */
4
+ function getFigmaErrorMessage(status, response) {
5
+ if (response) switch (status) {
6
+ case 429: {
7
+ const retryAfter = response.headers.get("Retry-After");
8
+ return `Error retrieving document from API: rate limit exceeded.${retryAfter ? ` Try again after ${retryAfter} seconds.` : ""}`;
9
+ }
10
+ }
11
+ return `Error retrieving document from API: ${status}`;
12
+ }
13
+
14
+ export { getFigmaErrorMessage };
@@ -37,7 +37,7 @@ async function importFromFigma(options) {
37
37
  const svg = new SVG(item.content);
38
38
  cleanupSVG(svg);
39
39
  iconSet.fromSVG(item.keyword, svg);
40
- } catch (err) {
40
+ } catch {
41
41
  missing.push(item);
42
42
  continue;
43
43
  }
@@ -1,6 +1,7 @@
1
1
  import { apiCacheKey, clearAPICache, getAPICache } from "../../download/api/cache.js";
2
2
  import { sendAPIQuery } from "../../download/api/index.js";
3
3
  import { runConcurrentQueries } from "../../download/api/queue.js";
4
+ import { getFigmaErrorMessage } from "./error.js";
4
5
 
5
6
  /**
6
7
  * Compare last modified dates
@@ -34,7 +35,7 @@ async function figmaFilesQuery(options, cache) {
34
35
  return true;
35
36
  }
36
37
  ifModifiedSince = parsedData$1.lastModified;
37
- } catch (err) {
38
+ } catch {
38
39
  await clearAPICache(cache.dir);
39
40
  return true;
40
41
  }
@@ -44,22 +45,22 @@ async function figmaFilesQuery(options, cache) {
44
45
  params: new URLSearchParams(params)
45
46
  };
46
47
  versionCheckParams.params.set("depth", "1");
47
- const data$1 = await sendAPIQuery(versionCheckParams);
48
+ const response$1 = await sendAPIQuery(versionCheckParams);
48
49
  try {
49
- if (typeof data$1 === "string") {
50
- if (identicalDates(JSON.parse(data$1).lastModified, ifModifiedSince)) return false;
50
+ if (response$1.success) {
51
+ if (identicalDates(JSON.parse(response$1.content).lastModified, ifModifiedSince)) return false;
51
52
  }
52
- } catch (err) {}
53
+ } catch {}
53
54
  await clearAPICache(cache.dir);
54
55
  return true;
55
56
  };
56
57
  if (!await isModified()) return "not_modified";
57
- const data = await sendAPIQuery(queryParams, cache);
58
- if (typeof data === "number") throw new Error(`Error retrieving document from API: ${data}`);
58
+ const response = await sendAPIQuery(queryParams, cache);
59
+ if (!response.success) throw new Error(getFigmaErrorMessage(response.error, response.response));
59
60
  let parsedData;
60
61
  try {
61
- parsedData = JSON.parse(data);
62
- } catch (err) {
62
+ parsedData = JSON.parse(response.content);
63
+ } catch {
63
64
  throw new Error(`Error retrieving document from API: invalid data`);
64
65
  }
65
66
  if (typeof parsedData.status === "number") {
@@ -92,14 +93,14 @@ async function figmaImagesQuery(options, nodes, cache) {
92
93
  uri,
93
94
  params,
94
95
  headers: { "X-FIGMA-TOKEN": options.token }
95
- }, cache).then((data) => {
96
- if (typeof data === "number") {
97
- reject(data);
96
+ }, cache).then((response) => {
97
+ if (!response.success) {
98
+ reject(getFigmaErrorMessage(response.error, response.response));
98
99
  return;
99
100
  }
100
101
  let parsedData;
101
102
  try {
102
- parsedData = JSON.parse(data);
103
+ parsedData = JSON.parse(response.content);
103
104
  } catch {
104
105
  reject("Bad API response");
105
106
  return;
package/package.json CHANGED
@@ -3,10 +3,7 @@
3
3
  "type": "module",
4
4
  "description": "Collection of functions for cleaning up and parsing SVG for Iconify project",
5
5
  "author": "Vjacheslav Trushkin",
6
- "version": "5.0.0-beta.4",
7
- "publishConfig": {
8
- "tag": "next"
9
- },
6
+ "version": "5.0.0",
10
7
  "license": "MIT",
11
8
  "bugs": "https://github.com/iconify/tools/issues",
12
9
  "homepage": "https://github.com/iconify/tools",
@@ -285,6 +282,10 @@
285
282
  "types": "./lib/import/figma/index.d.ts",
286
283
  "import": "./lib/import/figma/index.js"
287
284
  },
285
+ "./lib/import/figma/error": {
286
+ "types": "./lib/import/figma/error.d.ts",
287
+ "import": "./lib/import/figma/error.js"
288
+ },
288
289
  "./lib/import/figma/index": {
289
290
  "types": "./lib/import/figma/index.d.ts",
290
291
  "import": "./lib/import/figma/index.js"