Skip to content

Commit 2dd4840

Browse files
committed
switch library to use commonjs
1 parent 3c20608 commit 2dd4840

File tree

11 files changed

+56
-49
lines changed

11 files changed

+56
-49
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "waqi-js-client",
33
"version": "1.0.0",
4-
"type": "module",
54
"description": "JS client library for the World Air Quality Index (WAQI) APIs. All available API modules are supported - City feed, Geolocalized feed, Search, and Map Queries.",
65
"main": "index.js",
76
"scripts": {

src/API.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import MapStation from './Entity/MapStation';
2-
import CityFeed from "./Entity/CityFeed";
3-
import Search from "./Entity/Search";
4-
import IPFeed from "./Entity/IPFeed";
5-
import GeoFeed from "./Entity/GeoField";
1+
const MapStation = require('./Entity/MapStation');
2+
const CityFeed= require('./Entity/CityFeed');
3+
const Search= require('./Entity/Search');
4+
const IPFeed= require('./Entity/IPFeed');
5+
const GeoFeed= require('./Entity/GeoField');
6+
67

78
class API {
89
constructor(apiKey) {

src/Entity/CityFeed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WaqiAPIEntity from './WaqiAPIEntity'
1+
const WaqiAPIEntity = require('./WaqiAPIEntity')
22

33
class CityFeed extends WaqiAPIEntity {
44
setCity(city) {

src/Entity/GeoField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WaqiAPIEntity from './WaqiAPIEntity'
1+
const WaqiAPIEntity = require('./WaqiAPIEntity')
22

33
class GeoFeed extends WaqiAPIEntity {
44
setCoordinates(latitude, longitude) {

src/Entity/IPFeed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WaqiAPIEntity from './WaqiAPIEntity'
1+
const WaqiAPIEntity = require('./WaqiAPIEntity')
22

33
class IPFeed extends WaqiAPIEntity {
44
setIP(ipAddress) {

src/Entity/MapStation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WaqiAPIEntity from './WaqiAPIEntity'
1+
const WaqiAPIEntity = require('./WaqiAPIEntity')
22

33
class MapStation extends WaqiAPIEntity {
44
setMapBounds(latitudeNorth, longitudeWest, latitudeSouth, longitudeEast) {

src/Entity/Search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WaqiAPIEntity from './WaqiAPIEntity'
1+
const WaqiAPIEntity = require('./WaqiAPIEntity')
22

33
class Search extends WaqiAPIEntity {
44
setKeyword(keyword) {

src/Entity/WaqiAPIEntity.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios from 'axios';
1+
const axios = require('axios')
22

33
class WaqiAPIEntity {
44
constructor(apiKey) {
@@ -20,17 +20,24 @@ class WaqiAPIEntity {
2020
return '&' + queryParams.toString();
2121
}
2222

23-
async fetchItems(asArray = false) {
23+
async fetchItems() {
2424
const url = this.url();
2525
const tokenParam = `token=${this.apiKey}`;
2626
const queryParams = this.buildQueryParams();
2727

28-
try {
29-
const response = await axios.get(`${url}?${tokenParam}${queryParams}`);
30-
return asArray ? response.data : JSON.parse(response.data);
31-
} catch (error) {
32-
throw new Error(`Error fetching data: ${error.message}`);
33-
}
28+
const options = {
29+
method: 'GET',
30+
url: `${url}?${tokenParam}${queryParams}`,
31+
headers: {
32+
'content-type': 'application/json',
33+
},
34+
};
35+
36+
return axios.request(options).then(response => {
37+
return response.data
38+
}).catch( error => {
39+
throw error;
40+
})
3441
}
3542
}
3643

src/example/example.js renamed to src/example.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import API from '../API';
1+
const API = require('./API')
22

33
const apiKey = 'demo';
44
const waqiAPI = new API(apiKey);
55

66
const cityFeedEntity = waqiAPI.cityFeed();
7-
cityFeedEntity.setCity("Munich");
8-
cityFeedEntity.fetchItems(true).then(response => {
7+
cityFeedEntity.setCity("Shanghai");
8+
cityFeedEntity.fetchItems().then(response => {
99
console.log(response);
1010
}).catch(error => {
1111
console.error(error);

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import API from './API'
1+
const API = require('./API')
22

33
module.exports = API

src/tests/APITest.test.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {describe, test, expect, beforeAll} from '@jest/globals'
22
import API from '../API';
3-
const apiKey = 'demo'; // Replace with your actual API key
3+
const apiKey = 'demo'; // Replace with your actual API key
44

55
describe('API Tests', () => {
66
let waqiAPI;
@@ -11,31 +11,31 @@ describe('API Tests', () => {
1111

1212
test('CityFeed API', async () => {
1313
const cityFeedEntity = waqiAPI.cityFeed();
14-
const response = await cityFeedEntity.setCity('Lagos').fetchItems();
14+
const response = await cityFeedEntity.setCity('Shanghai').fetchItems();
1515
expect(typeof response).toBe('object');
1616
}, 10000);
17-
//
18-
// test('Search API Returns Array', async () => {
19-
// const searchEntity = waqiAPI.search();
20-
// const response = await searchEntity.setKeyword('keyword').fetch(true);
21-
// expect(Array.isArray(response)).toBe(true);
22-
// });
23-
//
24-
// test('GeoFeed API', async () => {
25-
// const geoFeedEntity = waqiAPI.geoFeed();
26-
// const response = await geoFeedEntity.setCoordinates(37.7749, -122.4194).fetch();
27-
// expect(typeof response).toBe('object');
28-
// });
29-
//
30-
// test('IPFeed API', async () => {
31-
// const ipFeedEntity = waqiAPI.ipFeed();
32-
// const response = await ipFeedEntity.setIP('8.8.8.8').fetch();
33-
// expect(typeof response).toBe('object');
34-
// });
35-
//
36-
// test('MapStations API', async () => {
37-
// const mapStationEntity = waqiAPI.mapStation();
38-
// const response = await mapStationEntity.setMapBounds(40.712, -74.006, 34.052, -118.243).fetch();
39-
// expect(typeof response).toBe('object');
40-
// });
17+
18+
test('Search API Returns Array', async () => {
19+
const searchEntity = waqiAPI.search();
20+
const response = await searchEntity.setKeyword('keyword').fetchItems();
21+
expect(Array.isArray(response?.data)).toBe(true);
22+
});
23+
24+
test('GeoFeed API', async () => {
25+
const geoFeedEntity = waqiAPI.geoFeed();
26+
const response = await geoFeedEntity.setCoordinates(37.7749, -122.4194).fetchItems();
27+
expect(typeof response).toBe('object');
28+
});
29+
30+
test('IPFeed API', async () => {
31+
const ipFeedEntity = waqiAPI.ipFeed();
32+
const response = await ipFeedEntity.setIP('8.8.8.8').fetchItems();
33+
expect(typeof response).toBe('object');
34+
});
35+
36+
test('MapStations API', async () => {
37+
const mapStationEntity = waqiAPI.mapStation();
38+
const response = await mapStationEntity.setMapBounds(40.712, -74.006, 34.052, -118.243).fetchItems();
39+
expect(typeof response).toBe('object');
40+
});
4141
});

0 commit comments

Comments
 (0)