Giter Club home page Giter Club logo

http-client's Introduction

http request promise (based on axios http client)

Install

npm i --save @pubcore/http-client

Example

import request from '@pubcore/http-client'

//a get request
request({uri:'https://example.com', query:{id: 42}}).then(
	result => {
		//do something with result ...
	},
	err => {
		//do someging with error ...
	}
)

//post some json, with basic auth
request({uri:'https://example.com', method:'post', username:'foo', password:'...', data:{name:'T-Bone'}, query:{id:42}}).then(
	res => {
		//show feedback
	},
	err => {
		//do someging with error ...
	}
)

Features (test descriptions)

✓ defaults to method get and accept application/json
✓ rejects to {code:"REQUEST_ERROR", ...}, e.g. on timeout
✓ rejects 4xx or 5xx to {code:"HTTP_ERROR", ...} (this is expected, not an exception)
✓ serializes query params in "qs" package default format
✓ has "authorization" argument to forward authentication
✓ exports function to create basic-auth value for "authorization" agrument
✓ has usernaame, password arguments for basic authentication
✓ has userAgent argument to define own one
✓ has "data" argument, treated as json (default) for post and put
✓ has "contentType" argument, if set to "urlEncoded" data is converted accordingly
✓ supports optional headers
✓ "__Host-Csrf-Token" cookie is forwarded via HTTP header "X-Csrf-Token" for POST, PUT and DELETE requests

rejected error object

Example if request failed (no response exists)

{
	code:'REQUEST_ERROR',
	details: {
		message: 'timeout',
		uri: 'https://example.com/foo?id=12',
		method: 'get'
	}
}

Example if request succeeded, but there is a http error (response exists)

{
	code:'HTTP_ERROR',
	details:{
		body:{status:{code:'ERROR'}},
		headers:{'content-type': 'application/json'},
		message:'Request failed with status code 500',
		status:500,
		method:'get',
		uri:'https://www.example.com/500'
	}
}

If there is a response, the promise is resolved to following result object

Example for "json" response:

{
	status: 200,
	headers: {'Content-Type': 'application/json' //,...}
	body: {foo: bar}
}

References

CQRS protection "__Host-" cookie prefix

http-client's People

Contributors

fidorw avatar jacklrs avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.