![quality gate - coverage](https://sonarcloud.io/api/project_badges/measure?project=imcotton_ngx-endpoint&metric=coverage)
ngx-endpoint
Simplify Angular HTTP request API management.
Installing
npm install ngx-endpoint --save
Configuring
(1) create one EndpointService
which extends
the BaseEndpointService
import { Injectable } from '@angular/core';
import { BaseEndpointService } from 'ngx-endpoint';
@Injectable({
providedIn: 'root',
})
export class EndpointService extends BaseEndpointService {
readonly api = this.booking('/api');
readonly gitLab = this.booking('https://gitlab.com/api/v4');
protected onInit () {
this.api.headers.add({
'X-Powered-By': 'NgxEndpoint',
});
}
}
(2) wire up everything together
import { HttpClientModule } from '@angular/common/http';
import { NgxEndpointModule, BaseEndpointService } from 'ngx-endpoint';
import { EndpointService } from 'app/endpoint-service';
@NgModule({
imports: [
HttpClientModule,
NgxEndpointModule,
],
providers: [
{ provide: BaseEndpointService, useExisting: EndpointService },
],
bootstrap: [ AppComponent ],
})
export class AppModule { }
(3) let's ues this api
in real service
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { EndpointService } from 'app/endpoint-service';
@Injectable()
export class MyService {
constructor (
private readonly http: HttpClient,
private readonly endpoint: EndpointService,
) {
}
private readonly api = this.endpoint.api;
getInfo () {
return this.http.get(this.api.to('/info'));
}
setOAuth (token: string) {
this.api.auth.OAuth2(token);
}
setJWT (token: string) {
this.api.auth.JWT(token);
}
setBasic (user: string, pass: string) {
this.api.auth.basic(user, pass);
}
}
that's it.
License
the MIT