Last active
July 17, 2016 07:02
-
-
Save hectorlorenzo/ca59dbf0a88bab82095274fe65abb5de to your computer and use it in GitHub Desktop.
API Wrapper for the Dashboard project
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
let ApiCalls = { | |
businessId: '', | |
moduleId: '', | |
formId: '', | |
type: '', | |
parentId: '', | |
baseUrl: '//admin:[email protected]/', | |
apiUrl: '', | |
/** | |
* Makes sure (again) that all components are defined and creates a base URL | |
* to resolve all the calls to. If the URL is already defined, we return it | |
* | |
* @return {String} Final URL to resolve the API calls to | |
*/ | |
getBaseUrl () { | |
if (this.apiUrl !== '') { | |
return this.apiUrl | |
} | |
if (this.moduleId === '' || this.formId === '') { | |
return new Error('Module ID and Form ID can not be empty') | |
} | |
if (this.type === 'client') { | |
if (this.businessId === '') { | |
return new Error('Business ID should not be empty if we are defining client-side endpoint') | |
} | |
return this.baseUrl + 'client/' + this.businessId + '/modules/' + this.moduleId + '/forms/' + this.formId + '/items' | |
} else if (this.type === 'admin') { | |
return this.baseUrl + 'admin/modules/' + this.moduleId + '/forms/' + this.formId + '/items' | |
} | |
}, | |
/** | |
* Makes a GET call to the API and returns a Promise we can work with | |
* | |
* @return {Promise} Promise containing the results of the GET call | |
*/ | |
get () { | |
// Do your get and return a Promise... | |
}, | |
/** | |
* Makes a POST call to the API to create a new object and returns the newly | |
* created object information | |
* | |
* @param {Object} itemData Object containing the information of the object to | |
* be created | |
* @return {Promise} Promise containing the results of the POST call | |
*/ | |
create (itemData) { | |
// Do your post and return a Promise... | |
}, | |
/** | |
* Makes a PUT call to the API to update an existing object and returns the | |
* updated object information | |
* | |
* @param {Number} itemId ID of the object to be created | |
* @param {Object} itemData Object containing the information of the object to updated | |
* @return {Promise} Promise containing the results of the PUT call | |
*/ | |
update (itemId, itemData) { | |
// Do your put and return a Promise... | |
}, | |
/** | |
* Makes a DELETE call to the API to delete an existing object | |
* | |
* @param {Number} itemId ID of the object to be deleted | |
* @return {Promise} Promise containing the results of the DELETE call | |
*/ | |
delete (itemId) { | |
// Do your delete and return a Promise... | |
} | |
} | |
/** | |
* Wrapper to get information from the API. It will require the moduleId, | |
* formId and parentId to construct a valid URL to hit to. At least, it has to | |
* do CRUD | |
* | |
* @param {String} moduleId ID of the module we want to communicate with | |
* @param {String} formId ID of the form inside the module | |
* @param {String} type Is it an 'admin' endpoint or a 'client' endpoint? | |
* @param {String} parentId ID of the parent item (optional) | |
*/ | |
export default function (connectionData) { | |
let defaults = { | |
type: 'client' | |
} | |
connectionData = Object.assign({}, defaults, connectionData) | |
if (!connectionData.hasOwnProperty('moduleId') || connectionData.moduleId === '') { | |
console.warn('ApiWrapper::Module ID is not defined') | |
return false | |
} | |
if (!connectionData.hasOwnProperty('formId') || connectionData.formId === '') { | |
console.warn('ApiWrapper::Form ID is not defined') | |
return false | |
} | |
if (connectionData.type === 'client' && (!connectionData.hasOwnProperty('businessId') || connectionData.businessId === '')) { | |
console.warn('ApiWrapper::If type equals "client", a business ID should be defined') | |
return false | |
} | |
return Object.assign(Object.create(ApiCalls), connectionData) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment