Skip to content

Instantly share code, notes, and snippets.

@jonathanws
Last active July 8, 2020 17:18
Show Gist options
  • Save jonathanws/37c07a34d6372ef7d10a49774b666ad7 to your computer and use it in GitHub Desktop.
Save jonathanws/37c07a34d6372ef7d10a49774b666ad7 to your computer and use it in GitHub Desktop.
/**
* Document module
*
* This module serves as a basic CRUD implementation of 'document' objects.
* It is meant to be consumed by a frontend webapp and used to talk to an API.
*/
// Import the Axios instance with our backend information
import { api } from '@/constants/globals.constant'
/**
* These are all the routes to our API. They are NOT exported from this module
* because they are meant to be used by helper functions where data validation
* and other checks can be done.
*
* TODO: How is the readability with the comments between each line?
*/
const backend = {
// POST /documents
createDocument: ({ name }) => api.post('/documents', { name }),
// DELETE /documents/{documentId}
deleteDocument: (documentId) => api.delete(`/documents/${documentId}`),
// GET /documents/{documentId}
getDocument: (documentId) => api.get(`/documents/${documentId}`),
// GET /documents
getDocuments: () => api.get('/documents'),
// POST /documents
updateDocumnent: (document, updates) => api.post('/documents', { ...document, ...updates })
}
/**
* Below are all of the functions exported from this module. These functions
* should implement data validation, catch clauses, and should implement the
* 'backend' routes above.
*/
/**
* Create a document
*
* @param {Object} obj - document data
* @param {String} obj.name - name of the document
*/
const createDocument = (obj) =>
!obj.name
? Promise.reject('Name is required for documents')
: backend
.createDocument(obj)
.then((data) => data)
.catch((err) => console.error(err))
/**
* Deletes a document
*
* @param {String} documentId - id of the document to delete
*/
const deleteDocument = (documentId) =>
!documentId //
? Promise.reject(new Error('Document ID required'))
: backend
.deleteDocument(documentId)
.then((data) => data)
.catch((err) => console.error(err))
/**
* Gets a single document
*
* @param {String} documentId - ID of the document to get
*/
const getDocument = (documentId) =>
!documentId
? Promise.reject(new Error('Document ID required'))
: backend
.getDocument(documentId)
.then(({ data }) => otherAction(data))
.catch((err) => console.error(err))
/**
* Gets a list of documents
*/
const getDocuments = () =>
backend
.getDocuments()
.then(({ data }) =>
data
.map((r) => anotherAction(r))
.sort((a, b) => (a.name > b.name ? 1 : -1))
)
.catch((err) => console.error(err))
/**
* Updates a document
*
* @param {Object} obj - Document to be updated
* @param {Object} obj.updates - Updates to the document
*/
const updateDocument = (document, updates) =>
!document.id
? Promise.reject('Document ID required')
: backend
.updateDocument(document, updates)
.then((data) => data)
.catch((err) => console.error(err))
// Only functions that call the backend routes are exported
export { createDocument, deleteDocument, getDocument, getDocuments, updateDocument }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment