Last active
March 12, 2025 09:45
-
-
Save etoxin/4fcfb5e8e25e22f21b9334096a908fb0 to your computer and use it in GitHub Desktop.
Sample ES6 Service Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Geocoding API | |
* | |
* The Google Maps Geocoding API is a service that provides geocoding and reverse geocoding of addresses. | |
* | |
* @see https://developers.google.com/maps/documentation/geocoding/start | |
* @class Geocoding | |
*/ | |
export default class Geocoding { | |
/** | |
* Geocoding Class | |
*/ | |
constructor() { | |
this.url = 'https://maps.googleapis.com/maps/api/geocode/json?'; | |
this.data = { | |
// API key | |
key: '' | |
}; | |
} | |
/** | |
* Post service method | |
* @param {object} formData data to pass to the api. | |
* @returns {Promise<any>} | |
* @constructor | |
*/ | |
Get(formData) { | |
return new Promise((resolve, reject) => { | |
// No address, reject | |
'address' in formData || reject('[Error] Geocoding Service requires `address` field.'); | |
let payload = Object.assign(this.data, formData); | |
let esc = encodeURIComponent; | |
let query = Object.keys(payload) | |
.map(k => esc(k) + '=' + esc(payload[k])) | |
.join('&'); | |
try { | |
resolve(async () => await (await fetch(this.url + query)).json()); | |
} catch(error) { | |
reject(error); | |
} | |
}); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import forEach from 'lodash/forEach'; | |
/** | |
* Sample Web Service | |
* This is a sample service class that uses fetch and promises. | |
* | |
* I am using the http://httpbin.org endpoint to ensure this example is working properly. | |
* | |
* @class sampleWebService | |
*/ | |
class sampleWebService { | |
constructor() { } | |
/** | |
* Sample Get Fetch using HTTP Bin | |
* | |
* @example | |
var service = new sampleWebService(); | |
service.Get().then((success => { | |
console.log(success); | |
})) | |
* @memberof sampleWebService | |
* @returns {promise} returns a Promise | |
*/ | |
Get() { | |
return new Promise((resolve, reject) => { | |
// We fetch the API endpoint | |
fetch('https://httpbin.org/get').then((response) => { | |
if (response.status !== 200) { | |
// Not success | |
resolve(response.text()); | |
} else { | |
// success | |
resolve(response.text()); | |
} | |
}).catch(err => { | |
// Service Error | |
reject(err); | |
}); | |
}); | |
} | |
/** | |
* Sample Post Fetch using HTTP Bin | |
* @example | |
var service = new sampleWebService(); | |
service.Post({ | |
custname: 'John Doe', | |
custemail: '[email protected]' | |
}).then(success => { | |
console.log(success); | |
}) | |
* @param {Object} object This is the form data. | |
* @memberof sampleWebService | |
* @returns {Promise} return a promise | |
*/ | |
Post(object) { | |
return new Promise((resolve, reject) => { | |
// We create a new form | |
var formData = new FormData(); | |
// we add all object items to the new form | |
forEach(object, (value, key) => { | |
formData.append(key, value); | |
}); | |
// We fetch Post the API | |
fetch('https://httpbin.org/post', { | |
method: 'post', | |
body: formData | |
}).then((response) => { | |
if (response.status !== 200) { | |
// Not success | |
resolve(response.text()); | |
} else { | |
// Success | |
resolve(response.text()); | |
} | |
}).catch(err => { | |
// Service Error | |
reject(err); | |
}); | |
}); | |
} | |
} | |
export { sampleWebService }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment