spoko-design-system 0.2.58 → 0.2.59
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 +2 -2
- package/src/utils/getData.ts +28 -61
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spoko-design-system",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.59",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "./index.ts",
|
|
6
6
|
"module": "./index.ts",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
"astro-pagefind": "^1.6.0",
|
|
84
84
|
"astro-remote": "^0.3.3",
|
|
85
85
|
"dotenv": "^16.4.5",
|
|
86
|
-
"i18next": "^23.16.
|
|
86
|
+
"i18next": "^23.16.5",
|
|
87
87
|
"i18next-browser-languagedetector": "^8.0.0",
|
|
88
88
|
"i18next-fs-backend": "^2.3.2",
|
|
89
89
|
"i18next-http-backend": "^2.6.2",
|
package/src/utils/getData.ts
CHANGED
|
@@ -1,68 +1,35 @@
|
|
|
1
|
+
//
|
|
1
2
|
|
|
3
|
+
const API_URL = import.meta.env.API_URL;
|
|
4
|
+
const version = (Math.random() + 1).toString(36).substring(7); // random version to refresh cache from Cloudflare
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
// Dodajemy interfejs dla odpowiedzi API
|
|
8
|
-
interface ApiResponse<T> {
|
|
9
|
-
success: boolean;
|
|
10
|
-
data: T;
|
|
6
|
+
interface ApiResponse {
|
|
7
|
+
success: boolean;
|
|
8
|
+
data: any; // Zamień `any` na właściwy typ, jeśli struktura danych jest znana
|
|
11
9
|
}
|
|
12
10
|
|
|
13
|
-
export const getData = async
|
|
14
|
-
|
|
15
|
-
console.error("API_URL is not defined in .env");
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Używamy path.join dla lepszej kompatybilności między systemami
|
|
20
|
-
const cacheFilePath = path.join(CACHE_DIR, `${type}.json`);
|
|
21
|
-
const apiUrl = `${API_URL}${type}?version=${version}`;
|
|
22
|
-
|
|
23
|
-
// Ensure cache directory exists
|
|
24
|
-
try {
|
|
25
|
-
await fs.promises.mkdir(CACHE_DIR, { recursive: true });
|
|
26
|
-
} catch (error) {
|
|
27
|
-
console.error("Failed to create cache directory:", error);
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Check if cached file exists
|
|
32
|
-
try {
|
|
33
|
-
if (fs.existsSync(cacheFilePath)) {
|
|
34
|
-
const rawData = await fs.promises.readFile(cacheFilePath, 'utf-8');
|
|
35
|
-
return JSON.parse(rawData);
|
|
36
|
-
}
|
|
37
|
-
} catch (error) {
|
|
38
|
-
console.error("Failed to read or parse cached data:", error);
|
|
39
|
-
// Kontynuujemy wykonanie, aby spróbować pobrać dane z API
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Fetch data from API and cache it
|
|
43
|
-
try {
|
|
44
|
-
const response = await fetch(apiUrl);
|
|
11
|
+
export const getData = async (type: string): Promise<any | null> => {
|
|
12
|
+
const API = `${API_URL}${type}?version=${version}`;
|
|
45
13
|
|
|
46
|
-
|
|
47
|
-
|
|
14
|
+
try {
|
|
15
|
+
const response = await fetch(API);
|
|
16
|
+
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
console.error(`Request failed with status ${response.status}: ${response.statusText}`);
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const jsonObject: ApiResponse = await response.json();
|
|
23
|
+
|
|
24
|
+
// Return data if API call is successful
|
|
25
|
+
if (jsonObject.success) {
|
|
26
|
+
return jsonObject.data;
|
|
27
|
+
} else {
|
|
28
|
+
console.error('API returned success: false');
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error('Fetch error:', error);
|
|
33
|
+
return null;
|
|
48
34
|
}
|
|
49
|
-
|
|
50
|
-
const jsonObject: ApiResponse<T> = await response.json();
|
|
51
|
-
|
|
52
|
-
if (jsonObject.success) {
|
|
53
|
-
await fs.promises.writeFile(
|
|
54
|
-
cacheFilePath,
|
|
55
|
-
JSON.stringify(jsonObject.data, null, 2)
|
|
56
|
-
);
|
|
57
|
-
return jsonObject.data;
|
|
58
|
-
} else {
|
|
59
|
-
console.warn(`API response unsuccessful for type: ${type}`);
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
} catch (error) {
|
|
63
|
-
console.error("Error fetching data from API:", error);
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
35
|
};
|
|
67
|
-
|
|
68
|
-
export default getData;
|