holosphere 1.1.4 → 1.1.6

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": "holosphere",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Holonic Geospatial Communication Infrastructure",
5
5
  "main": "holosphere.js",
6
6
  "types": "holosphere.d.ts",
@@ -14,12 +14,14 @@
14
14
  "license": "GPL-3.0-or-later",
15
15
  "dependencies": {
16
16
  "ajv": "^8.12.0",
17
+ "axios": "^1.6.7",
17
18
  "dotenv": "^16.4.7",
18
19
  "gun": "^0.2020.1240",
19
20
  "h3-js": "^4.1.0",
20
21
  "openai": "^4.85.1"
21
22
  },
22
23
  "devDependencies": {
24
+ "@babel/preset-env": "^7.24.0",
23
25
  "jest": "^29.7.0",
24
26
  "jest-environment-node": "^29.7.0"
25
27
  },
@@ -0,0 +1,253 @@
1
+ import axios from 'axios';
2
+ import dotenv from 'dotenv';
3
+
4
+ dotenv.config();
5
+
6
+ const logError = (service, error) => {
7
+ const status = error.response?.status;
8
+ const message = error.response?.data?.message || error.message;
9
+ console.error(`${service} API Error:`, { status, message });
10
+ };
11
+
12
+ /**
13
+ * Fetch Carbon Sequestration Data (Using NASA POWER API)
14
+ */
15
+ export async function getCarbonSequestration(lat = 41.9, lon = 12.5) {
16
+ try {
17
+ const response = await axios.default.get(
18
+ `https://power.larc.nasa.gov/api/temporal/daily/point?parameters=T2M,PRECTOT,PS,WS2M&community=RE&longitude=${lon}&latitude=${lat}&start=20230101&end=20231231&format=JSON`
19
+ );
20
+ return response.data;
21
+ } catch (error) {
22
+ logError('Carbon Sequestration', error);
23
+ return null;
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Fetch Soil Carbon Data (Using ISRIC World Soil Information)
29
+ */
30
+ export async function getSoilCarbon(lat = 41.9, lon = 12.5) {
31
+ try {
32
+ const response = await axios.default.get(
33
+ `https://rest.isric.org/soilgrids/v2.0/properties/query?lat=${lat}&lon=${lon}&property=soc&depth=0-30cm&value=mean`
34
+ );
35
+ return response.data;
36
+ } catch (error) {
37
+ logError('Soil Carbon', error);
38
+ return null;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Fetch Biodiversity Data (GBIF API)
44
+ */
45
+ export async function getBiodiversityData(countryCode = "ITA") {
46
+ try {
47
+ const response = await axios.default.get(`https://api.gbif.org/v1/occurrence/search?country=${countryCode}&limit=100`);
48
+ return response.data;
49
+ } catch (error) {
50
+ logError('Biodiversity', error);
51
+ return null;
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Fetch Vegetation Cover Data (Using NASA MODIS Vegetation Index)
57
+ */
58
+ export async function getVegetationCover(lat = 41.9, lon = 12.5) {
59
+ try {
60
+ const response = await axios.default.get(
61
+ `https://modis.ornl.gov/rst/api/v1/MOD13Q1/subset?latitude=${lat}&longitude=${lon}&band=NDVI&startDate=2023-01-01&endDate=2023-12-31`,
62
+ {
63
+ headers: {
64
+ 'Accept': 'application/json'
65
+ }
66
+ }
67
+ );
68
+ return response.data;
69
+ } catch (error) {
70
+ logError('Vegetation Cover', error);
71
+ return null;
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Fetch Air Quality Data (OpenWeather API)
77
+ */
78
+ export async function getAirQuality(lat = 41.9, lon = 12.5) {
79
+ try {
80
+ const response = await axios.default.get(
81
+ `https://api.openweathermap.org/data/2.5/air_pollution?lat=${lat}&lon=${lon}&appid=${process.env.OPENWEATHER_API_KEY}`
82
+ );
83
+ return response.data;
84
+ } catch (error) {
85
+ logError('Air Quality', error);
86
+ return null;
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Fetch Water Retention Data (Using USGS Water Services)
92
+ */
93
+ export async function getWaterRetention(lat = 41.9, lon = 12.5) {
94
+ try {
95
+ const response = await axios.default.get(
96
+ `https://waterservices.usgs.gov/nwis/iv/?format=json&sites=11447650&siteStatus=active`,
97
+ {
98
+ headers: {
99
+ 'Accept': 'application/json'
100
+ }
101
+ }
102
+ );
103
+ return response.data;
104
+ } catch (error) {
105
+ logError('Water Retention', error);
106
+ return null;
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Fetch Deforestation Data (Using World Bank Forest Data)
112
+ */
113
+ export async function getDeforestationData(countryCode = "ITA") {
114
+ try {
115
+ const response = await axios.default.get(
116
+ `https://api.worldbank.org/v2/country/${countryCode}/indicator/AG.LND.FRST.ZS?format=json`
117
+ );
118
+ return response.data;
119
+ } catch (error) {
120
+ logError('Deforestation', error);
121
+ return null;
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Fetch Flood Risk Data (Using USGS Water Services)
127
+ */
128
+ export async function getFloodRisk(lat = 41.9, lon = 12.5) {
129
+ try {
130
+ const response = await axios.default.get(
131
+ `https://waterservices.usgs.gov/nwis/iv/?format=json&stateCd=CA&parameterCd=00065&siteStatus=active`,
132
+ {
133
+ headers: {
134
+ 'Accept': 'application/json'
135
+ }
136
+ }
137
+ );
138
+ return response.data;
139
+ } catch (error) {
140
+ logError('Flood Risk', error);
141
+ return null;
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Fetch Food Security Data (World Bank API)
147
+ */
148
+ export async function getFoodSecurity(countryCode = "ITA") {
149
+ try {
150
+ const response = await axios.default.get(
151
+ `https://api.worldbank.org/v2/country/${countryCode}/indicator/SN.ITK.DEFC.ZS?format=json`
152
+ );
153
+ return response.data;
154
+ } catch (error) {
155
+ logError('Food Security', error);
156
+ return null;
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Fetch Local Employment Rate (World Bank API)
162
+ */
163
+ export async function getEmploymentRate(countryCode = "ITA") {
164
+ try {
165
+ const response = await axios.default.get(
166
+ `https://api.worldbank.org/v2/country/${countryCode}/indicator/SL.EMP.TOTL.SP.ZS?format=json`
167
+ );
168
+ return response.data;
169
+ } catch (error) {
170
+ logError('Employment Rate', error);
171
+ return null;
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Fetch Governance Score (World Bank API)
177
+ */
178
+ export async function getTransparencyScore(countryCode = "ITA") {
179
+ try {
180
+ const response = await axios.default.get(
181
+ `https://api.worldbank.org/v2/country/${countryCode}/indicator/GE.EST?format=json`
182
+ );
183
+ return response.data;
184
+ } catch (error) {
185
+ logError('Transparency Score', error);
186
+ return null;
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Fetch Blockchain Transactions (Etherscan API)
192
+ */
193
+ export async function getBlockchainTransactions(address = "0x0000000000000000000000000000000000000000") {
194
+ try {
195
+ const response = await axios.default.get(
196
+ `https://api.etherscan.io/api?module=account&action=txlist&address=${address}&startblock=0&endblock=99999999&sort=desc&apikey=${process.env.ETHERSCAN_API_KEY}`
197
+ );
198
+ return response.data;
199
+ } catch (error) {
200
+ logError('Blockchain Transactions', error);
201
+ return null;
202
+ }
203
+ }
204
+
205
+ /**
206
+ * Fetch Circular Economy Data (Using World Bank Development Indicators)
207
+ */
208
+ export async function getCircularEconomyData(countryCode = "ITA") {
209
+ try {
210
+ const response = await axios.default.get(
211
+ `https://api.worldbank.org/v2/country/${countryCode}/indicator/EN.ATM.GHGT.KT.CE?format=json`
212
+ );
213
+ return response.data;
214
+ } catch (error) {
215
+ logError('Circular Economy', error);
216
+ return null;
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Fetch Renewable Energy Data (World Bank API)
222
+ */
223
+ export async function getRenewableEnergyData(countryCode = "ITA") {
224
+ try {
225
+ const response = await axios.default.get(
226
+ `https://api.worldbank.org/v2/country/${countryCode}/indicator/EG.FEC.RNEW.ZS?format=json`
227
+ );
228
+ return response.data;
229
+ } catch (error) {
230
+ logError('Renewable Energy', error);
231
+ return null;
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Fetch Climate Change Data (NOAA Climate API)
237
+ */
238
+ export async function getClimateChangeData(lat = 41.9, lon = 12.5) {
239
+ try {
240
+ const response = await axios.default.get(
241
+ `https://www.ncdc.noaa.gov/cdo-web/api/v2/data?datasetid=GHCND&latitude=${lat}&longitude=${lon}&startdate=2023-01-01&enddate=2023-12-31&limit=1000`,
242
+ {
243
+ headers: {
244
+ 'token': process.env.NOAA_API_KEY
245
+ }
246
+ }
247
+ );
248
+ return response.data;
249
+ } catch (error) {
250
+ logError('Climate Change', error);
251
+ return null;
252
+ }
253
+ }
@@ -0,0 +1,164 @@
1
+ import { jest } from '@jest/globals';
2
+
3
+ // Mock axios before importing the modules that use it
4
+ jest.mock('axios', () => ({
5
+ default: {
6
+ get: jest.fn()
7
+ }
8
+ }));
9
+
10
+ // Import axios after mocking
11
+ import axios from 'axios';
12
+
13
+ // Import the API functions
14
+ import {
15
+ getCarbonSequestration,
16
+ getSoilCarbon,
17
+ getBiodiversityData,
18
+ getVegetationCover,
19
+ getAirQuality,
20
+ getWaterRetention,
21
+ getDeforestationData,
22
+ getFloodRisk,
23
+ getFoodSecurity,
24
+ getEmploymentRate,
25
+ getTransparencyScore,
26
+ getBlockchainTransactions,
27
+ getCircularEconomyData,
28
+ getRenewableEnergyData,
29
+ getClimateChangeData
30
+ } from './environmentalApi.js';
31
+
32
+ describe('Environmental API Tests', () => {
33
+ // Increase timeout for real API calls
34
+ jest.setTimeout(60000);
35
+
36
+ const ROME_LAT = 41.9;
37
+ const ROME_LON = 12.5;
38
+ const COUNTRY_CODE = 'ITA';
39
+ const ETH_ADDRESS = '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe';
40
+
41
+ test('getCarbonSequestration returns NASA POWER data', async () => {
42
+ const result = await getCarbonSequestration(ROME_LAT, ROME_LON);
43
+ expect(result).not.toBeNull();
44
+ expect(result.messages).toBeDefined();
45
+ expect(result.parameters).toBeDefined();
46
+ expect(result.parameters).toHaveProperty('T2M');
47
+ expect(result.parameters).toHaveProperty('PRECTOT');
48
+ });
49
+
50
+ test('getSoilCarbon returns ISRIC soil data', async () => {
51
+ const result = await getSoilCarbon(ROME_LAT, ROME_LON);
52
+ expect(result).not.toBeNull();
53
+ expect(result.properties).toBeDefined();
54
+ expect(result.properties.soc).toBeDefined();
55
+ });
56
+
57
+ test('getBiodiversityData returns GBIF data', async () => {
58
+ const result = await getBiodiversityData(COUNTRY_CODE);
59
+ expect(result).not.toBeNull();
60
+ expect(result.count).toBeDefined();
61
+ expect(result.results).toBeDefined();
62
+ expect(Array.isArray(result.results)).toBeTruthy();
63
+ });
64
+
65
+ test('getVegetationCover returns MODIS NDVI data', async () => {
66
+ const result = await getVegetationCover(ROME_LAT, ROME_LON);
67
+ expect(result).not.toBeNull();
68
+ expect(result.subset || result.data).toBeDefined();
69
+ });
70
+
71
+ test('getAirQuality returns OpenWeather data', async () => {
72
+ const result = await getAirQuality(ROME_LAT, ROME_LON);
73
+ expect(result).not.toBeNull();
74
+ expect(result.list).toBeDefined();
75
+ expect(result.list[0].main).toBeDefined();
76
+ expect(result.list[0].components).toBeDefined();
77
+ });
78
+
79
+ test('getWaterRetention returns USGS data', async () => {
80
+ const result = await getWaterRetention(ROME_LAT, ROME_LON);
81
+ expect(result).not.toBeNull();
82
+ expect(result.value || result.timeSeries).toBeDefined();
83
+ });
84
+
85
+ test('getDeforestationData returns World Bank forest data', async () => {
86
+ const result = await getDeforestationData(COUNTRY_CODE);
87
+ expect(result).not.toBeNull();
88
+ expect(Array.isArray(result)).toBeTruthy();
89
+ if (result.length > 1) {
90
+ expect(result[1][0]).toHaveProperty('indicator');
91
+ expect(result[1][0].indicator.id).toBe('AG.LND.FRST.ZS');
92
+ }
93
+ });
94
+
95
+ test('getFloodRisk returns USGS gauge data', async () => {
96
+ const result = await getFloodRisk(ROME_LAT, ROME_LON);
97
+ expect(result).not.toBeNull();
98
+ expect(result.value || result.timeSeries).toBeDefined();
99
+ });
100
+
101
+ test('getFoodSecurity returns World Bank data', async () => {
102
+ const result = await getFoodSecurity(COUNTRY_CODE);
103
+ expect(result).not.toBeNull();
104
+ expect(Array.isArray(result)).toBeTruthy();
105
+ if (result.length > 1) {
106
+ expect(result[1][0]).toHaveProperty('indicator');
107
+ expect(result[1][0].indicator.id).toBe('SN.ITK.DEFC.ZS');
108
+ }
109
+ });
110
+
111
+ test('getEmploymentRate returns World Bank data', async () => {
112
+ const result = await getEmploymentRate(COUNTRY_CODE);
113
+ expect(result).not.toBeNull();
114
+ expect(Array.isArray(result)).toBeTruthy();
115
+ if (result.length > 1) {
116
+ expect(result[1][0]).toHaveProperty('indicator');
117
+ expect(result[1][0].indicator.id).toBe('SL.EMP.TOTL.SP.ZS');
118
+ }
119
+ });
120
+
121
+ test('getTransparencyScore returns World Bank governance data', async () => {
122
+ const result = await getTransparencyScore(COUNTRY_CODE);
123
+ expect(result).not.toBeNull();
124
+ expect(Array.isArray(result)).toBeTruthy();
125
+ if (result.length > 1) {
126
+ expect(result[1][0]).toHaveProperty('indicator');
127
+ expect(result[1][0].indicator.id).toBe('GE.EST');
128
+ }
129
+ });
130
+
131
+ test('getBlockchainTransactions returns Etherscan data', async () => {
132
+ const result = await getBlockchainTransactions(ETH_ADDRESS);
133
+ expect(result).not.toBeNull();
134
+ expect(result.status).toBeDefined();
135
+ expect(result.result).toBeDefined();
136
+ expect(Array.isArray(result.result)).toBeTruthy();
137
+ });
138
+
139
+ test('getCircularEconomyData returns World Bank emissions data', async () => {
140
+ const result = await getCircularEconomyData(COUNTRY_CODE);
141
+ expect(result).not.toBeNull();
142
+ expect(Array.isArray(result)).toBeTruthy();
143
+ if (result.length > 1) {
144
+ expect(result[1][0]).toHaveProperty('indicator');
145
+ expect(result[1][0].indicator.id).toBe('EN.ATM.GHGT.KT.CE');
146
+ }
147
+ });
148
+
149
+ test('getRenewableEnergyData returns World Bank energy data', async () => {
150
+ const result = await getRenewableEnergyData(COUNTRY_CODE);
151
+ expect(result).not.toBeNull();
152
+ expect(Array.isArray(result)).toBeTruthy();
153
+ if (result.length > 1) {
154
+ expect(result[1][0]).toHaveProperty('indicator');
155
+ expect(result[1][0].indicator.id).toBe('EG.FEC.RNEW.ZS');
156
+ }
157
+ });
158
+
159
+ test('getClimateChangeData returns NOAA data', async () => {
160
+ const result = await getClimateChangeData(ROME_LAT, ROME_LON);
161
+ expect(result).not.toBeNull();
162
+ expect(result.metadata || result.results).toBeDefined();
163
+ });
164
+ });