1.3.2 • Published 9 years ago

backend-service v1.3.2

Weekly downloads
3
License
ISC
Repository
github
Last release
9 years ago

#BackendService.js Generate multiple ajaxs function with name!!

##Initialize

import MyApi from 'backend-service' // or var MyApi = require('backend-service')
var routes = [
  {"name":"users", "method":"GET", "path":"/users"},
  {"name":"user",  "method":"GET", "path":"/users/:id"},
  {"name":"post",  "method":"GET", "path":"/posts/:slug"}
]

MyApi.init({
  routes: routes
})

##Ajax functions generated Backend service generate function using your path, method and name defined in the routes.json

MyApi.users( (users) => {console.log(users)} )                      //GET /users
MyApi.user( {id:1}, (user) => {console.log(user)} )                 //GET /users/1
MyApi.users( {location:"madrid"}, (users) => {console.log(users)} ) //GET /users?location=madrid
MyApi.post( {slug:"post-title"}, (post) => {console.log(post)} )    //GET /post/post-title

Backendservice use superagent.js The generate function i'ts the same with this

request(method, path)
  .set('Accept', 'application/json')
  .query(data)
  .end( (err, res) => {} )

##Path functions generated BackendService generate path functions like this

"/users/"  == MyApi.users_path()
"/users/3" == MyApi.user_path({id:3})
"/post/post-title" == MyApi.post_path({slug:"post-title-slug"})

##Mock in your Unit test Use the method responseWith to override the ajax function and response immediately

it("when click the heart will be red; in this test the ajax call it's a mock", function(){
  MyApi.reset()
  MyApi.add_to_favorite.responseWith({ok:true, err:false})
  const wrapper = mount(<FavoriteBtn active={false} book={book} />)
  wrapper.find('a').simulate('click');
  expect( wrapper.props().active ).to.equal(true);
  expect( wrapper.find('a').hasClass('heart-red') ).to.equal(true);
})

You don't need to change your production code, BackendService call the same callbackSucess but call immediately and the response it's a mock.

##Default functions _get, _post, _delete, _put

  MyApi._get(
    {path:"/users", location:"madrid"},
    (users) => console.log(users)
  )
  //GET /users?location=madrid

##Unauthorized function BackendService recive a function and called when the response status it's 401

MyApi.init({
  routes: routes,
  unauthorizedFnc: function(response_body){
    if ( response_body.error == "You need to sign in or sign up before continuing.")
      openLoginModal()
  }
})

##Arguments Recive

  • MyApi.users( query_data, callbackSucess, callBackError )
  • MyApi.create_users( send_data, callbackSuccess, callBackError ) Post|PUT

##Arguments contain

  • callBackSuccess( response.body, response.status, response )
1.3.2

9 years ago

1.3.1

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago