Skip to content

Commit

Permalink
switch library to use commonjs
Browse files Browse the repository at this point in the history
  • Loading branch information
niyiomotoso committed Sep 13, 2023
1 parent 3c20608 commit 2dd4840
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 49 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "waqi-js-client",
"version": "1.0.0",
"type": "module",
"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.",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 6 additions & 5 deletions src/API.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import MapStation from './Entity/MapStation';
import CityFeed from "./Entity/CityFeed";
import Search from "./Entity/Search";
import IPFeed from "./Entity/IPFeed";
import GeoFeed from "./Entity/GeoField";
const MapStation = require('./Entity/MapStation');
const CityFeed= require('./Entity/CityFeed');
const Search= require('./Entity/Search');
const IPFeed= require('./Entity/IPFeed');
const GeoFeed= require('./Entity/GeoField');


class API {
constructor(apiKey) {
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/CityFeed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WaqiAPIEntity from './WaqiAPIEntity'
const WaqiAPIEntity = require('./WaqiAPIEntity')

class CityFeed extends WaqiAPIEntity {
setCity(city) {
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/GeoField.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WaqiAPIEntity from './WaqiAPIEntity'
const WaqiAPIEntity = require('./WaqiAPIEntity')

class GeoFeed extends WaqiAPIEntity {
setCoordinates(latitude, longitude) {
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/IPFeed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WaqiAPIEntity from './WaqiAPIEntity'
const WaqiAPIEntity = require('./WaqiAPIEntity')

class IPFeed extends WaqiAPIEntity {
setIP(ipAddress) {
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/MapStation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WaqiAPIEntity from './WaqiAPIEntity'
const WaqiAPIEntity = require('./WaqiAPIEntity')

class MapStation extends WaqiAPIEntity {
setMapBounds(latitudeNorth, longitudeWest, latitudeSouth, longitudeEast) {
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WaqiAPIEntity from './WaqiAPIEntity'
const WaqiAPIEntity = require('./WaqiAPIEntity')

class Search extends WaqiAPIEntity {
setKeyword(keyword) {
Expand Down
23 changes: 15 additions & 8 deletions src/Entity/WaqiAPIEntity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
const axios = require('axios')

class WaqiAPIEntity {
constructor(apiKey) {
Expand All @@ -20,17 +20,24 @@ class WaqiAPIEntity {
return '&' + queryParams.toString();
}

async fetchItems(asArray = false) {
async fetchItems() {
const url = this.url();
const tokenParam = `token=${this.apiKey}`;
const queryParams = this.buildQueryParams();

try {
const response = await axios.get(`${url}?${tokenParam}${queryParams}`);
return asArray ? response.data : JSON.parse(response.data);
} catch (error) {
throw new Error(`Error fetching data: ${error.message}`);
}
const options = {
method: 'GET',
url: `${url}?${tokenParam}${queryParams}`,
headers: {
'content-type': 'application/json',
},
};

return axios.request(options).then(response => {
return response.data
}).catch( error => {
throw error;
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/example/example.js → src/example.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import API from '../API';
const API = require('./API')

const apiKey = 'demo';
const waqiAPI = new API(apiKey);

const cityFeedEntity = waqiAPI.cityFeed();
cityFeedEntity.setCity("Munich");
cityFeedEntity.fetchItems(true).then(response => {
cityFeedEntity.setCity("Shanghai");
cityFeedEntity.fetchItems().then(response => {
console.log(response);
}).catch(error => {
console.error(error);
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import API from './API'
const API = require('./API')

module.exports = API
52 changes: 26 additions & 26 deletions src/tests/APITest.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {describe, test, expect, beforeAll} from '@jest/globals'
import API from '../API';
const apiKey = 'demo'; // Replace with your actual API key
const apiKey = 'demo'; // Replace with your actual API key

describe('API Tests', () => {
let waqiAPI;
Expand All @@ -11,31 +11,31 @@ describe('API Tests', () => {

test('CityFeed API', async () => {
const cityFeedEntity = waqiAPI.cityFeed();
const response = await cityFeedEntity.setCity('Lagos').fetchItems();
const response = await cityFeedEntity.setCity('Shanghai').fetchItems();
expect(typeof response).toBe('object');
}, 10000);
//
// test('Search API Returns Array', async () => {
// const searchEntity = waqiAPI.search();
// const response = await searchEntity.setKeyword('keyword').fetch(true);
// expect(Array.isArray(response)).toBe(true);
// });
//
// test('GeoFeed API', async () => {
// const geoFeedEntity = waqiAPI.geoFeed();
// const response = await geoFeedEntity.setCoordinates(37.7749, -122.4194).fetch();
// expect(typeof response).toBe('object');
// });
//
// test('IPFeed API', async () => {
// const ipFeedEntity = waqiAPI.ipFeed();
// const response = await ipFeedEntity.setIP('8.8.8.8').fetch();
// expect(typeof response).toBe('object');
// });
//
// test('MapStations API', async () => {
// const mapStationEntity = waqiAPI.mapStation();
// const response = await mapStationEntity.setMapBounds(40.712, -74.006, 34.052, -118.243).fetch();
// expect(typeof response).toBe('object');
// });

test('Search API Returns Array', async () => {
const searchEntity = waqiAPI.search();
const response = await searchEntity.setKeyword('keyword').fetchItems();
expect(Array.isArray(response?.data)).toBe(true);
});

test('GeoFeed API', async () => {
const geoFeedEntity = waqiAPI.geoFeed();
const response = await geoFeedEntity.setCoordinates(37.7749, -122.4194).fetchItems();
expect(typeof response).toBe('object');
});

test('IPFeed API', async () => {
const ipFeedEntity = waqiAPI.ipFeed();
const response = await ipFeedEntity.setIP('8.8.8.8').fetchItems();
expect(typeof response).toBe('object');
});

test('MapStations API', async () => {
const mapStationEntity = waqiAPI.mapStation();
const response = await mapStationEntity.setMapBounds(40.712, -74.006, 34.052, -118.243).fetchItems();
expect(typeof response).toBe('object');
});
});

0 comments on commit 2dd4840

Please sign in to comment.