Skip to content

thondery/http-services

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http-services

A set of tools for the http service ...

NPM Version NPM Downloads Build Status Codecov Status dependencies Status Gratipay

Install

npm install --save http-services

SETTING

const { httpServices } = require('http-services')
const HttpServices = new httpServices()

// set domain
HttpServices.domain = 'http://localhost:4000'

// set apiPath
HttpServices.apiPath = '/api/v1'

// set statusMessage
HttpServices.statusMessage = 'Request Success!'

GET / POST

// Promise/A+

HttpServices.GET('notes/list', { tag: 'javascript' })
  .then( ret => {
    console.log(ret)
  })
  .catch( err => {
    console.log(err)
  })

HttpServices.POST('sign-in', { username: 'admin', password: 'password' })
  .then( ret => {
    console.log(ret)
  })
  .catch( err => {
    console.log(err)
  })

// Async/Await

async () => {
  try {
    let ret = await HttpServices.GET('notes/list', { tag: 'javascript' })
    console.log(ret)
  }
  catch (error) {
    console.log(error)
  }
}

async () => {
  try {
    let ret = await HttpServices.POST('sign-in', { username: 'admin', password: 'password' })
    console.log(ret)
  }
  catch (error) {
    console.log(error)
  }
}

Other Method

const { createAction, createReducer, statusToError, getStatusError } = require('http-services')

// createAction

const FETCH_SIGNIN_SUCCESS = 'FETCH_SIGNIN_SUCCESS'
const FETCH_SIGNIN_FAILURE = 'FETCH_SIGNIN_FAILURE'
const Response = {
  data: {
    id: 1,
    username: 'admin'
  },
  status: {
    code: 0,
    message: 'Request Success!'
  }
}
createAction(FETCH_SIGNIN_SUCCESS, Response)
/*
{
  type: 'FETCH_SIGNIN_SUCCESS',
  payload: {
    data: {
      id: 1,
      username: 'admin'
    },
    status: {
      code: 0,
      message: 'Request Success!'
    }
  },
  error: null
}
*/
createAction(FETCH_SIGNIN_FAILURE, Response)
/*
{
  type: 'FETCH_SIGNIN_FAILURE',
  payload: null,
  error: Error
}
*/

// createReducer

const ROOT_INITIAL_SUCCESS = 'ROOT_INITIAL_SUCCESS'
const initialState = {
  initial: false
}
const action = createAction(ROOT_INITIAL_SUCCESS, null)
const handlers = {
  [ROOT_INITIAL_SUCCESS]: function (state, action) {
    return Object.assign(state, { initial: true })
  }
}
createReducer(state, action, handlers)
/*
{
  initial: true
}
*/

// statusToError

const Response = {
  data: null,
  status: {
    code: 1024,
    message: 'Wraning Message!'
  }
}
statusToError(Response, 'loginError')
/*
{
  loginError: {
    code: 1024,
    message: 'Wraning Message!'
  }
}
*/
statusToError(Response, 'loginError', 'loginMessage')
/*
{
  loginError: 1024,
  loginMessage: 'Wraning Message!'
}
*/

// getStatusError

const error = new Error()
getStatusError(error)
/*
{
  code: 1000,
  message: 'Abnormal error'
}
*/
const error = new Error('Unexpected token < in JSON at position 0')
getStatusError(error)
/*
{
  code: 1000,
  message: 'Unexpected token < in JSON at position 0'
}
*/
const error = new Error()
error.response = { status: 404, statusText: 'Not Found' }
getStatusError(error)
/*
{
  code: 404,
  message: 'Not Found'
}
*/

License

this repo is released under the MIT License.

About

A set of tools for the http service ...

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published