spoko-design-system 0.2.53 → 0.2.54

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,6 +1,6 @@
1
1
  {
2
2
  "name": "spoko-design-system",
3
- "version": "0.2.53",
3
+ "version": "0.2.54",
4
4
  "private": false,
5
5
  "main": "./index.ts",
6
6
  "module": "./index.ts",
@@ -1,45 +1,64 @@
1
1
  import fs from 'node:fs';
2
2
  import fetch from 'node-fetch';
3
3
  import dotenv from 'dotenv';
4
+ import path from 'node:path'; // dodajemy do bezpiecznej obsługi ścieżek
4
5
 
5
6
  dotenv.config();
6
7
 
7
8
  const API_URL = process.env.API_URL || '';
8
9
  const CACHE_DIR = process.env.CACHE_DIR || './.cache';
9
- const version = new Date().toISOString().split('T')[0]; // 'YYYY-MM-DD' daily version to refresh cache from Cloudflare
10
+ const version = new Date().toISOString().split('T')[0]; // 'YYYY-MM-DD' daily version
10
11
 
11
- export const getData = async (type: string): Promise<any | null> => {
12
+ // Dodajemy interfejs dla odpowiedzi API
13
+ interface ApiResponse<T> {
14
+ success: boolean;
15
+ data: T;
16
+ }
17
+
18
+ export const getData = async <T>(type: string): Promise<T | null> => {
12
19
  if (!API_URL) {
13
20
  console.error("API_URL is not defined in .env");
14
21
  return null;
15
22
  }
16
23
 
17
- const cacheFilePath = `${CACHE_DIR}/${type}.json`;
24
+ // Używamy path.join dla lepszej kompatybilności między systemami
25
+ const cacheFilePath = path.join(CACHE_DIR, `${type}.json`);
18
26
  const apiUrl = `${API_URL}${type}?version=${version}`;
19
27
 
20
28
  // Ensure cache directory exists
21
- if (!fs.existsSync(CACHE_DIR)) {
22
- fs.mkdirSync(CACHE_DIR, { recursive: true });
29
+ try {
30
+ await fs.promises.mkdir(CACHE_DIR, { recursive: true });
31
+ } catch (error) {
32
+ console.error("Failed to create cache directory:", error);
33
+ return null;
23
34
  }
24
35
 
25
36
  // Check if cached file exists
26
- if (fs.existsSync(cacheFilePath)) {
27
- try {
28
- const rawData = fs.readFileSync(cacheFilePath, 'utf-8');
37
+ try {
38
+ if (fs.existsSync(cacheFilePath)) {
39
+ const rawData = await fs.promises.readFile(cacheFilePath, 'utf-8');
29
40
  return JSON.parse(rawData);
30
- } catch (error) {
31
- console.error("Failed to read or parse cached data:", error);
32
- return null;
33
41
  }
42
+ } catch (error) {
43
+ console.error("Failed to read or parse cached data:", error);
44
+ // Kontynuujemy wykonanie, aby spróbować pobrać dane z API
34
45
  }
35
46
 
36
47
  // Fetch data from API and cache it
37
48
  try {
38
49
  const response = await fetch(apiUrl);
39
- const jsonObject = await response.json();
50
+
51
+ if (!response.ok) {
52
+ throw new Error(`HTTP error! status: ${response.status}`);
53
+ }
54
+
55
+ const jsonObject: ApiResponse<T> = await response.json();
40
56
 
41
57
  if (jsonObject.success) {
42
- fs.writeFileSync(cacheFilePath, JSON.stringify(jsonObject.data, null, 2));
58
+ await fs.promises.writeFile(
59
+ cacheFilePath,
60
+ JSON.stringify(jsonObject.data, null, 2)
61
+ );
43
62
  return jsonObject.data;
44
63
  } else {
45
64
  console.warn(`API response unsuccessful for type: ${type}`);
@@ -51,5 +70,4 @@ export const getData = async (type: string): Promise<any | null> => {
51
70
  }
52
71
  };
53
72
 
54
-
55
73
  export default getData;