@zodic/shared 0.0.134 → 0.0.136
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/app/api/astrology/index.ts +80 -23
- package/package.json +1 -1
|
@@ -1,36 +1,93 @@
|
|
|
1
1
|
import { BackendBindings } from '../../../types';
|
|
2
2
|
|
|
3
|
-
export async function makeAstrologyApiCall(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
) {
|
|
9
|
-
|
|
3
|
+
// export async function makeAstrologyApiCall(
|
|
4
|
+
// path: string,
|
|
5
|
+
// payload: any,
|
|
6
|
+
// env: BackendBindings,
|
|
7
|
+
// language: string
|
|
8
|
+
// ) {
|
|
9
|
+
// const shortLanguage = language.slice(0, 2); // Extracts only "en" or "pt"
|
|
10
|
+
|
|
11
|
+
// const endpoint = `https://astrologyapi.com/api/${path}`;
|
|
12
|
+
// const headers = {
|
|
13
|
+
// Authorization: `Bearer ${env.ASTROLOGY_WESTERN_API_KEY}`,
|
|
14
|
+
// 'Content-Type': 'application/json',
|
|
15
|
+
// 'Accept-Language': shortLanguage, // Sends "en" or "pt"
|
|
16
|
+
// };
|
|
10
17
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
// try {
|
|
19
|
+
// const response = await fetch(endpoint, {
|
|
20
|
+
// method: 'POST',
|
|
21
|
+
// headers,
|
|
22
|
+
// body: JSON.stringify(payload),
|
|
23
|
+
// });
|
|
17
24
|
|
|
25
|
+
// if (!response.ok) {
|
|
26
|
+
// const error = await response.json();
|
|
27
|
+
// console.error('Astrology API Error:', error);
|
|
28
|
+
// throw new Error(`Astrology API Error: ${response.status}`);
|
|
29
|
+
// }
|
|
30
|
+
|
|
31
|
+
// return await response.json();
|
|
32
|
+
// } catch (err: any) {
|
|
33
|
+
// console.error('Error calling Astrology API:', err.message);
|
|
34
|
+
// throw err;
|
|
35
|
+
// }
|
|
36
|
+
// }
|
|
37
|
+
|
|
38
|
+
export const makeAstrologyApiCall = async (
|
|
39
|
+
path: string,
|
|
40
|
+
payload: Record<string, any>,
|
|
41
|
+
env: BackendBindings,
|
|
42
|
+
language: 'en-us' | 'pt-br'
|
|
43
|
+
) => {
|
|
44
|
+
const shortLanguage = language.slice(0, 2);
|
|
18
45
|
try {
|
|
19
|
-
const
|
|
46
|
+
const url = `https://json.astrologyapi.com/v1/${path}`;
|
|
47
|
+
console.log(`🌍 Making Astrology API Call: ${url}`);
|
|
48
|
+
|
|
49
|
+
const response = await fetch(url, {
|
|
20
50
|
method: 'POST',
|
|
21
|
-
headers
|
|
51
|
+
headers: {
|
|
52
|
+
'Content-Type': 'application/json',
|
|
53
|
+
'Accept-Language': shortLanguage,
|
|
54
|
+
Authorization: `Bearer ${env.ASTROLOGY_WESTERN_API_KEY}`,
|
|
55
|
+
},
|
|
22
56
|
body: JSON.stringify(payload),
|
|
23
57
|
});
|
|
24
58
|
|
|
59
|
+
// ✅ Log response status
|
|
60
|
+
console.log(`🔄 Response Status (${path}):`, response.status);
|
|
61
|
+
|
|
62
|
+
// ✅ Handle non-OK responses gracefully
|
|
25
63
|
if (!response.ok) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
64
|
+
console.error(
|
|
65
|
+
`❌ API Error (${path}):`,
|
|
66
|
+
response.status,
|
|
67
|
+
await response.text()
|
|
68
|
+
);
|
|
69
|
+
throw new Error(`Failed to fetch astrology data from ${path}`);
|
|
29
70
|
}
|
|
30
71
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
72
|
+
// ✅ Check if response body is empty
|
|
73
|
+
const text = await response.text();
|
|
74
|
+
if (!text) {
|
|
75
|
+
console.error(`❌ API Response Empty (${path})`);
|
|
76
|
+
throw new Error(`Empty response from ${path}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ✅ Try parsing JSON
|
|
80
|
+
try {
|
|
81
|
+
return JSON.parse(text);
|
|
82
|
+
} catch (jsonError) {
|
|
83
|
+
console.error(`❌ JSON Parse Error (${path}):`, jsonError);
|
|
84
|
+
throw new Error(`Invalid JSON response from ${path}`);
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error(
|
|
88
|
+
`❌ Astrology API Call Failed (${path}):`,
|
|
89
|
+
(error as Error).message
|
|
90
|
+
);
|
|
91
|
+
throw error;
|
|
35
92
|
}
|
|
36
|
-
}
|
|
93
|
+
};
|