npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backend-service

v1.3.2

Published

Generate multiple ajaxs function with name!!

Downloads

31

Readme

#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 )