README
Basic API Connection
This module and module 'hiep294-route' are a couple. This module is to use for client side.
Example
my todo list
, a todo
has the following fields: _id
(created automactically by mongodb), title
, isCompleted
Suppose that in app Create this todoAPIConnection.js
in the app
import ApiConnection from 'hiep294-apiconnection'
const todoAPIConnection = ApiConnection({
url: '/api/todos',
actions: ['index', 'create', 'update', 'delete']
})
export default todoAPIConnection
todoAPIConnection
may include the following format in overview:
Above file will exports to an object {
index: () => {}, /* if actions include 'index' */
create: () => {}, /* if actions include 'create' */
update: () => {}, /* if actions include 'update' */
delete: () => {}, /* if actions include 'delete' */
}
todoAPIConnection
In detail of - Note:
handleSuccess
,handleInvalid
,handleFailedConnection
which are below are callbacks which are defined by own developer while developing front-end service, and not be defined by this module.
1. todoAPIConnection.index = (handleSuccess, handleFailedConnection) => {/* content code defined in 'hiep294-apiconnection' module */}
handleSuccess(responseItems)
Next common process: - when this method is called, if success, it will run
handleSuccess(responseTodos)
, withresponseTodos
array is filtered from API response. The client side can use them to print out in the screen
2. todoAPIConnection.create: (todoItem, handleSuccess, handleInvalid, handleFailedConnection) => {/* content code defined in 'hiep294-apiconnection' module */},
todoItem
is passed from client side
Input: handleSuccess(responseItem)
or handleInvalid(responseErrors)
Next common processes: - if creating
todoItem
successfully, it will run the methodhandleSuccess(responseItem)
, withresponseItem
is filtered from API response, so the developer can use it to add in client. - if creating
todoItem
un-successfully and because of invalid input, it will run the methodhandleInvalid(responseErrors)
, withresponseErrors
array is filter from API response
3. todoAPIConnection.update = (todoItem, handleSuccess, handleInvalid, handleFailedConnection) => {/* content code defined in 'hiep294-apiconnection' module */},
todoItem
is passed from client side, and should include key _id
Input: handleSuccess()
or handleInvalid(responseErrors)
=> Next common processes: - if updating
todoItem
successfully, it will run the methodhandleSuccess()
. - if updating
todoItem
un-successfully and because of invalid input, it will run the moethodhandleInvalid(responseErrors)
, withresponseErrors
array is filter from API response
4. todoAPIConnection.delete = (_id, handleSuccess, handleInvalid, handleFailedConnection) => {/* content code defined in 'hiep294-apiconnection' module */}
_id
of item in need of deleting
Input: handleSuccess()
or handleInvalid(responseErrors)
Next common processes: Change log
- 1.0.2: more info about example