@iconify/tools 5.0.0-beta.4 → 5.0.0-beta.5
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/lib/download/api/index.d.ts +2 -2
- package/lib/download/api/index.js +23 -13
- package/lib/download/api/types.d.ts +14 -1
- package/lib/download/github/hash.js +3 -3
- package/lib/download/gitlab/hash.js +3 -3
- package/lib/import/figma/error.d.ts +5 -0
- package/lib/import/figma/error.js +14 -0
- package/lib/import/figma/query.js +14 -13
- package/package.json +5 -1
|
@@ -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<
|
|
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
|
|
12
|
+
if (cached) return {
|
|
13
|
+
success: true,
|
|
14
|
+
content: cached
|
|
15
|
+
};
|
|
13
16
|
}
|
|
14
|
-
const
|
|
15
|
-
if (cache &&
|
|
16
|
-
await storeAPICache(cache, cacheKey,
|
|
17
|
-
} catch
|
|
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
|
|
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(
|
|
31
|
-
fetchCallbacks.onError?.(url, query,
|
|
32
|
-
return
|
|
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
|
|
45
|
-
|
|
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
|
-
|
|
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
|
|
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 (
|
|
15
|
-
const hash = JSON.parse(
|
|
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
|
|
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 (
|
|
13
|
-
const content = JSON.parse(
|
|
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;
|
|
@@ -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 };
|
|
@@ -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
|
|
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
|
|
48
|
+
const response$1 = await sendAPIQuery(versionCheckParams);
|
|
48
49
|
try {
|
|
49
|
-
if (
|
|
50
|
-
if (identicalDates(JSON.parse(
|
|
50
|
+
if (response$1.success) {
|
|
51
|
+
if (identicalDates(JSON.parse(response$1.content).lastModified, ifModifiedSince)) return false;
|
|
51
52
|
}
|
|
52
|
-
} catch
|
|
53
|
+
} catch {}
|
|
53
54
|
await clearAPICache(cache.dir);
|
|
54
55
|
return true;
|
|
55
56
|
};
|
|
56
57
|
if (!await isModified()) return "not_modified";
|
|
57
|
-
const
|
|
58
|
-
if (
|
|
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(
|
|
62
|
-
} catch
|
|
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((
|
|
96
|
-
if (
|
|
97
|
-
reject(
|
|
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(
|
|
103
|
+
parsedData = JSON.parse(response.content);
|
|
103
104
|
} catch {
|
|
104
105
|
reject("Bad API response");
|
|
105
106
|
return;
|
package/package.json
CHANGED
|
@@ -3,7 +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.
|
|
6
|
+
"version": "5.0.0-beta.5",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"tag": "next"
|
|
9
9
|
},
|
|
@@ -285,6 +285,10 @@
|
|
|
285
285
|
"types": "./lib/import/figma/index.d.ts",
|
|
286
286
|
"import": "./lib/import/figma/index.js"
|
|
287
287
|
},
|
|
288
|
+
"./lib/import/figma/error": {
|
|
289
|
+
"types": "./lib/import/figma/error.d.ts",
|
|
290
|
+
"import": "./lib/import/figma/error.js"
|
|
291
|
+
},
|
|
288
292
|
"./lib/import/figma/index": {
|
|
289
293
|
"types": "./lib/import/figma/index.d.ts",
|
|
290
294
|
"import": "./lib/import/figma/index.js"
|