|
2 | 2 |
|
3 | 3 | [](https://travis-ci.com/d3byte/rxjs-http)
|
4 | 4 |
|
| 5 | +HTTP client based on observables. |
| 6 | + |
| 7 | +Compatible with both Javascript and Typescript. |
| 8 | + |
| 9 | +### Table of Content |
| 10 | + |
| 11 | +* [Example](#example) |
| 12 | +* [Available methods](#available-methods) |
| 13 | +* [Configuration](#configuration) |
| 14 | +* [Interfaces](#interfaces) |
| 15 | + * [ConfigurationInterface](#configurationinterface) |
| 16 | + * [ObjectInterface](#objectinterface) |
| 17 | + |
| 18 | +### Example |
| 19 | + |
| 20 | +```javascript |
| 21 | +import { http } from '@d3byte/rxjs-http'; |
| 22 | + |
| 23 | +const configuration = { |
| 24 | + baseUrl: 'http://localhost:8080', |
| 25 | +}; |
| 26 | + |
| 27 | +const httpClient = http(configuration); |
| 28 | + |
| 29 | +httpClient |
| 30 | + .post( |
| 31 | + 'login', |
| 32 | + { username: 'user', password: 'pass' }, |
| 33 | + { MyCustomHeader: 'Value' }, |
| 34 | + ) |
| 35 | + .subscribe(user => { |
| 36 | + // Do something with data |
| 37 | + }); |
| 38 | +``` |
| 39 | + |
| 40 | +### Available methods |
| 41 | + |
| 42 | +All methods you would probably need are available in the package. You can use all of those: |
| 43 | +* GET |
| 44 | +* POST |
| 45 | +* PUT |
| 46 | +* PATCH |
| 47 | +* DELETE |
| 48 | + |
| 49 | +### Configuration |
| 50 | + |
| 51 | +The package supports configuring clients and keeping multiple instances of clients at the same time. |
| 52 | + |
| 53 | +To configure a client, you have to pass a [configuration object](#configuration-interface) to `http` function. |
| 54 | + |
| 55 | +### Interfaces |
| 56 | + |
| 57 | +#### ConfigurationInterface |
| 58 | +This interface represents objects for configuring HTTP Client. |
| 59 | + |
| 60 | +```javascript |
| 61 | +interface ConfigurationInterface { |
| 62 | + jwt?: string; |
| 63 | + baseUrl?: string; |
| 64 | + defaultHeaders?: ObjectInterface; |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Properties: |
| 69 | +* <b>jwt</b> <i>[optional]</i> – property where you can store jwt token for communication with api. Client will automatically insert it into `Authorization` header with `Bearer`. |
| 70 | +* <b>baseUrl</b> <i>[optional]</i> – URL to API. For example, `http://localhost:8080`. Pay attention that there should not be a a slash (`/`) at the end of the url. If no baseUrl passed, you will have to manually set the entire url in method calls. |
| 71 | +* <b>defaultHeaders</b> <i>[optional]</i> – an object with pairs of keys and strings. Here you can pass any default headers you want. Client will insert them into requests. |
| 72 | + |
| 73 | +#### ObjectInterface |
| 74 | + |
| 75 | +A simple representation of object with dynamic keys and values. It suits for any object you would ever pass. |
| 76 | + |
| 77 | +### Examples of all available methods |
| 78 | + |
| 79 | +#### GET |
| 80 | + |
| 81 | +```javascript |
| 82 | +httpClient |
| 83 | + .get( |
| 84 | + 'user', |
| 85 | + { MyCustomHeader: 'Value' }, |
| 86 | + ) |
| 87 | + .subscribe(user => { |
| 88 | + // Do something with data |
| 89 | + }); |
| 90 | +``` |
| 91 | + |
| 92 | +#### POST |
| 93 | + |
| 94 | +```javascript |
| 95 | +httpClient |
| 96 | + .post( |
| 97 | + 'login', |
| 98 | + { username: 'user', password: 'pass' }, |
| 99 | + { MyCustomHeader: 'Value' }, |
| 100 | + ) |
| 101 | + .subscribe(user => { |
| 102 | + // Do something with data |
| 103 | + }); |
| 104 | +``` |
| 105 | + |
| 106 | +#### PUT |
| 107 | + |
| 108 | +```javascript |
| 109 | +httpClient |
| 110 | + .put( |
| 111 | + 'books', |
| 112 | + { title: 'Fav Book' }, |
| 113 | + { MyCustomHeader: 'Value' }, |
| 114 | + ) |
| 115 | + .subscribe(data => { |
| 116 | + // Do something with data |
| 117 | + }); |
| 118 | +``` |
| 119 | + |
| 120 | +#### PATCH |
| 121 | + |
| 122 | +```javascript |
| 123 | +httpClient |
| 124 | + .patch( |
| 125 | + 'books', |
| 126 | + { title: 'Fav Book' }, |
| 127 | + { MyCustomHeader: 'Value' }, |
| 128 | + ) |
| 129 | + .subscribe(data => { |
| 130 | + // Do something with data |
| 131 | + }); |
| 132 | +``` |
| 133 | + |
| 134 | +#### DELETE |
| 135 | + |
| 136 | +```javascript |
| 137 | +httpClient |
| 138 | + .delete( |
| 139 | + 'books', |
| 140 | + { title: 'Fav Book' }, |
| 141 | + { MyCustomHeader: 'Value' }, |
| 142 | + ) |
| 143 | + .subscribe(data => { |
| 144 | + // Do something with data |
| 145 | + }); |
| 146 | +``` |
| 147 | + |
0 commit comments