@ridder/request v1.0.1
Request service
1. Description
RequestService
is an extension of the HttpClient service which is implementing the
error handling method on top of get()
and post()
methods
2. Installation
Install the module into your application and save it as a dev
dependency in your package.json
file
npm install @ridder/request --save-dev
3. Usage
In order to use the RequestService
you have to:
1. Include the RequestModule
in the app's imports list:
import { RequestModule } from '@ridder/request';
//...
imports: [
//...
RequestModule
//...
]
- inject the
RequestService
via dependency injection into your services/components and use it:import { RequestService } from '@ridder/request'; //... export class AppComponent implements OnInit{ constructor(private request: RequestService) { } ngOnInit() { this.request.get('/assets/test.json').subscribe((response)=>{ console.log('Response:', response); }) } }
4. Methods
get(url: string, errorParams?: ResponseError, httpOptions: any = {}): Observable
Performs an http get request.
Parameters:
url - url to be requested.
errorParams - optional
default error parameters and data which should be returned back in
case of an error.
Structure of the object:
{
serviceName?: string;
operation?: string;
result?: T;
}
where T
is the type of the data which should be returned back.
httpOptions - optional
additional http options (headers etc.) used with the request.
Return:
Method returns an Observable<T>
which contains the response data coming from the server,
where T
is the type of the data which should be returned back.
post<D, R>(url: string, data: D, errorParams?: ResponseError, httpOptions: any = {}): Observable
Performs an http get request.
Parameters:
url - url to be requested.
data - data to be send to the server via post request. D
is the type of data to be sent.
errorParams - optional
default error parameters and data which should be returned back in
case of an error.
Structure of the object:
{
serviceName?: string;
operation?: string;
result?: R;
}
where R
is the type of the data which should be returned back.
httpOptions - optional
additional http options (headers etc.) used with the request.
Return:
Method returns an Observable<R>
which contains the response data coming from the server,
where R
is the type of the data which should be returned back.
private handleError(serviceName = '', operation = '', result = {} as T)
Handle the error and throws and error observable with the expected result.
Parameters:
serviceName - optional
name of the service to be logged within the error message.
operation - optional
operation to be logged within the error message.
result - optional
expected result which should be returned back as an error observable.
Return:
Method returns an error Observable<T>
which contains the result of type T
.
5. Git repository
https://github.com/dtxprs/request
6. Run tests
To run the unit tests, use this command:
ng test --code-coverage --project=request
Current test coverage is 100%!
7. Build
To build the final package run this command:
ng build request
The build process will generate the packed sources into the dist
folder.
8. Publish to npm
To publish the new version to npm
, go into the dist
folder:
cd ./dist/request
and publish it to npm:
npm publish --access public
9. Version
1.0.1