Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on May 11, 2021. It is now read-only.

The world-famous HTTP client Request now RxJS compliant, wrote in full Typescript | ES6 for client and server side.

License

NotificationsYou must be signed in to change notification settings

akanass/rx-http-request

Repository files navigation

buildcoverallsdependenciesdevDependencies
Typescript logoReactiveX logo

The world-famous HTTP clientRequest nowRxJS compliant, wrote in fullTypescript |ES6 for client and server side.

Table of contents

Installation

$ npm install --save @akanass/rx-http-request rxjsor$ yarn add @akanass/rx-http-request rxjs

Super simple to use

Rx-Http-Request is designed to be the simplest way possible to make http calls.

It's fullyTypescript |ES6 wrotten so you can import it :

import{RxHR}from"@akanass/rx-http-request";

or useCommonJS:

constRxHR=require('@akanass/rx-http-request').RxHR;

Now, it's easy to perform aHTTP request:

RxHR.get('http://www.google.fr').subscribe((data)=>{if(data.response.statusCode===200){console.log(data.body);// Show the HTML for the Google homepage.}},(err)=>console.error(err)// Show error in console);

Browser compatibility

Rx-Http-Request can be used in your favorite browser to have all features in your own front application.

Just importbrowser/index.js script and enjoy:

<scriptsrc="node_modules/@akanass/rx-http-request/browser/index.js"type="application/javascript"></script><scripttype="application/javascript">constRxHR=rhr.RxHR;RxHR.get('http://www.google.fr').subscribe(function(data){if(data.response.statusCode===200){console.log(data.body);// Show the HTML for the Google homepage.}},function(err){console.error(err)// Show error in console});</script>

Browser version is astandalone version so you just need tocopy/paste file fromnode_modules/@akanass/rx-http-request/browser/index.js when you want to create your bundle and change path to it.

Build your project with Webpack

If you want to include this library inside a project builds withwebpack for aclient application, you must add this configuration inside yourwebpack configuration:

{target:"web",node:{fs:"empty",net:"empty",tls:"empty"}}

For aserver application,target will benode,node block in configurationdoesn't exist anduglify plugin must bedisabled.

API in Detail

Rx-Http-Request usesRequestAPI to perform calls and returnsRxJS.Observable.

Alloptions to pass toAPImethods can be foundhere.

Allmethods to execute onresponse object can be foundhere.


.request

Returns the originalRequestAPI to perform calls withoutRxJS.Observable response but with acallback method.

import{RxHR}from'@akanass/rx-http-request';RxHR.request({uri:'http://www.google.fr'},(error,response,body)=>{if(!error&&response.statusCode==200){console.log(body);// Show the HTML for the Google homepage.}});

Back to top

.defaults(options)

This methodreturns a wrapper around the normalRx-Http-Request API that defaults to whatever options you pass to it.

Parameters:

options(required): OriginalRequestoptions object with default values foreach next requests

Response:

newRxHttpRequest instance

Note:RxHR.defaults()does not modify the global API; instead, it returns a wrapper that has your default settings applied to it.

Note: You can call.defaults() on the wrapper that is returned fromRxHR.defaults() to add/override defaults that were previously defaulted.

For example:

// requests using baseRequest will set the 'x-token' headerconstbaseRequest=RxHR.defaults({headers:{'x-token':'my-token'}});// requests using specialRequest will include the 'x-token' header set in// baseRequest and will also include the 'special' headerconstspecialRequest=baseRequest.defaults({headers:{special:'special value'}});

Back to top

.get(uri[, options])

Performs a request withget http method.

Parameters:

  • uri(required): Theuri where request will be performed
  • options(optional): OriginalRequestoptions object

Response:

RxJS.Observable instance

Crawl a webpage

import{RxHR}from'@akanass/rx-http-request';RxHR.get('http://www.google.fr').subscribe((data)=>{if(data.response.statusCode===200){console.log(data.body);// Show the HTML for the Google homepage.}},(err)=>console.error(err)// Show error in console);

GET something from a JSON REST API

import{RxHR}from'@akanass/rx-http-request';constoptions={qs:{access_token:'xxxxx xxxxx'// -> uri + '?access_token=xxxxx%20xxxxx'},headers:{'User-Agent':'Rx-Http-Request'},json:true// Automatically parses the JSON string in the response};RxHR.get('https://api.github.com/user/repos',options).subscribe((data)=>{if(data.response.statusCode===200){console.log(data.body);// Show the JSON response object.}},(err)=>console.error(err)// Show error in console);

Back to top

.getBuffer(uri[, options])

Performs a request withget http method and returns abuffer in response body. Very useful to crawl data from astream.

Parameters:

  • uri(required): Theuri where request will be performed
  • options(optional): OriginalRequestoptions object

Response:

RxJS.Observable instance

GET a buffer image

import{RxHR}from'@akanass/rx-http-request';RxHR.getBuffer('https://portalstoragewuprod2.azureedge.net/vision/Analysis/1-1.jpg').subscribe((data)=>{if(data.response.statusCode===200){console.log(data.response.headers['content-type']);// Show image content-type.console.log(data.body);// Show image buffer array.}},(err)=>console.error(err)// Show error in console);

Back to top

.post(uri[, options])

Performs a request withpost http method.

Parameters:

  • uri(required): Theuri where request will be performed
  • options(optional): OriginalRequestoptions object

Response:

RxJS.Observable instance

POST data to a JSON REST API

import{RxHR}from'@akanass/rx-http-request';constoptions={body:{some:'payload'},json:true// Automatically stringifies the body to JSON};RxHR.post('http://posttestserver.com/posts',options).subscribe((data)=>{if(data.response.statusCode===201){console.log(data.body);// Show the JSON response object.}},(err)=>console.error(err)// Show error in console);

POST like HTML forms do

import{RxHR}from'@akanass/rx-http-request';constoptions={form:{some:'payload'// Will be urlencoded},headers:{/* 'content-type': 'application/x-www-form-urlencoded' */// Set automatically}};RxHR.post('http://posttestserver.com/posts',options).subscribe((data)=>{if(data.response.statusCode===201){console.log(data.body);// POST succeeded...}},(err)=>console.error(err)// Show error in console);

Back to top

.put(uri[, options])

Performs a request withput http method.

Parameters:

  • uri(required): Theuri where request will be performed
  • options(optional): OriginalRequestoptions object

Response:

RxJS.Observable instance

import{RxHR}from'@akanass/rx-http-request';RxHR.put(uri).subscribe(...);

Back to top

.patch(uri[, options])

Performs a request withpatch http method.

Parameters:

  • uri(required): Theuri where request will be performed
  • options(optional): OriginalRequestoptions object

Response:

RxJS.Observable instance

import{RxHR}from'@akanass/rx-http-request';RxHR.patch(uri).subscribe(...);

Back to top

.delete(uri[, options])

Performs a request withdelete http method.

Parameters:

  • uri(required): Theuri where request will be performed
  • options(optional): OriginalRequestoptions object

Response:

RxJS.Observable instance

import{RxHR}from'@akanass/rx-http-request';RxHR.delete(uri).subscribe(...);

Back to top

.head(uri[, options])

Parameters:

  • uri(required): Theuri where request will be performed
  • options(optional): OriginalRequestoptions object

Response:

RxJS.Observable instance

Performs a request withhead http method.

import{RxHR}from'@akanass/rx-http-request';RxHR.head(uri).subscribe(...);

Back to top

.options(uri[, options])

Parameters:

  • uri(required): Theuri where request will be performed
  • options(optional): OriginalRequestoptions object

Response:

RxJS.Observable instance

Performs a request withoptions http method.

import{RxHR}from'@akanass/rx-http-request';RxHR.options(uri).subscribe(...);

Back to top

.jar()

Creates a newRxCookieJar instance

Response:

RxJS.Observable instance

import{RxHR}from'@akanass/rx-http-request';RxHR.jar().subscribe(...);

Back to top

.cookie(str)

Creates a new cookie

Parameters:

  • str(required): Thestring representation of the cookie

Response:

RxJS.Observable instance

import{RxHR}from'@akanass/rx-http-request';RxHR.cookie('key1=value1').subscribe(...);

Back to top

Contributing

To set up your development environment:

  1. clone the repo to your workspace,
  2. in the shellcd to the main folder,
  3. hitnpm or yarn install,
  4. runnpm or yarn run test.
    • It will lint the code and execute all tests.
    • The test coverage report can be viewed from./coverage/lcov-report/index.html.

Back to top

Change History

  • v3.3.0 (2019-07-15)
    • MergePR #30
    • Upgrade all packages' versions
    • Documentation
  • v3.2.0 (2019-07-08)
    • Upgrade all packages' versions
    • Fix tests
    • Documentation
  • v3.1.0 (2018-10-16)
    • Upgrade all packages' versions
    • Migrate tests tojest andts-jest
    • Refactor files' names
    • Documentation
  • v3.0.0 (2018-05-28)
    • Upgrade torxjs v6+ (#29)
    • Addgenerics in response (#28)
    • Upgrade all packages' versions
    • Documentation
  • v2.7.1 (2018-01-24)
    • Upgrade all packages' versions
    • Fix problem with response object with retry process
    • Documentation
  • v2.7.0 (2017-11-20)
    • Upgrade all packages' versions
    • FixIssue 12
    • FixIssue 13
    • FixIssue 15
    • Lettable version ofrxjs operators
    • Update tests
    • Documentation
  • v2.6.0 (2017-09-14)
    • Upgrade all packages' versions
    • Add config forunused packages error in compilation
    • Update code to userxjs operators instead of manual creation
    • Update tests
  • v2.5.0 (2017-07-18)
    • Upgrade all packages' versions
    • FixIssue 11
    • rxjs inpeerDependencies and need to be installed manually
  • v2.4.0 (2017-07-10)
    • Upgrade all packages' versions
    • Changeno-shadowed-variable value intslint config
  • v2.3.0 (2017-05-15)
    • Upgrade all packages' versions
    • Extendedtsconfig files
    • Check file exists in packaging process
  • v2.2.1 (2017-05-02)
    • Upgrade all packages' versions
    • Fix error handler ingetBuffer method if nouri provided
  • v2.2.0 (2017-04-14)
    • Upgrade all packages' versions
    • Fix tests
    • Add new method to get data with buffer response
    • Export all initial elements from request to have them in library
  • v2.1.1 (2017-03-23)
  • v2.1.0 (2017-03-09)
    • UpgradeRequest version tov2.80.0
  • v2.0.0 (2017-02-28)
    • New package version forRxJS andRequest
    • Don't import all ofRxJS, onlyObservable
    • Rewritten alllibrary and test files inTypescript
    • Addtypings support
    • Addscope to library and move to@akanass/rx-http-request
  • v1.2.0 (2016-09-29)
  • v1.1.0 (2016-03-28)
    • Browserify to have browser compatibility
  • v1.0.0 (2016-03-27)
    • Carefully rewritten from scratch to make Rx-Http-Request a drop-in replacement for Request

Back to top

License

Copyright (c) 2019Nicolas Jessel Licensed under theMIT license.

Back to top

About

The world-famous HTTP client Request now RxJS compliant, wrote in full Typescript | ES6 for client and server side.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp