Connect your modules
Web App Api Service (WAAS) - lightweight JS/Angular 6+ library that organizes your APIs.
Install WAAS library by running
npm i -S @ciklum-digital/waas
Then add WaasModule
to imports of your module and register services
For the main application:
import { NgModule } from '@angular/core';
import { HttpTransportAdapterService, IWaasConfiguration, WaasModule } from '@ciklum/ng-waas';
const waasConfig: IWaasConfiguration = {
basePath: 'https://reqres.in/api'
};
@NgModule({
imports: [
WaasModule.forRoot( waasConfig, [HttpClient], HttpTransportAdapterService]),
]
})
export class MyModule {
}
For the module of application:
import { NgModule } from '@angular/core';
import { IServiceRegistration, WaasModule } from '@ciklum/ng-waas';
export const searchServices: IServiceRegistration[] = [
{
path: '/test',
alias: 'search'
}
];
@NgModule({
imports: [WaasModule.forFeature(searchServices)]
})
export class MyModule { }
WaasService
class allows you to move the most of your payload outside your main code and care only
about business logic.
...
constructor(private readonly waas: NgWaasService) {
this.userService = waas.service('users');
}
...
or
@Waas('test') testService: IWaasChildService;
Adapter tells Api Service how to work with some API.
Adapter should implement ITransportAdapter
interface.
The default adapter provided by class HttpTransportAdapterService
, which uses native angular HttpClient
under the hood.
NgWaasMockService
add a possibility to returns a mocked response if it was provided or makes http request if wasn't.
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
WaasModule.forRoot( waasConfig, [HttpClient], HttpTransportAdapterService, [NgWaasMockService]),
WaasMockModule.forRoot([MockItem])
],
providers: [],
bootstrap: [AppComponent]
})
....
const ITEMS_MOCK = [
{title: 'Item 1', description: 'item 1 description'},
{title: 'Item 2', description: 'item 2 description'},
];
@mockApiClass
export class MockItem {
@mockApiData({path: '/test', method: 'get'})
getData(data: IMockRequest): any {
return ITEMS_MOCK;
}
}
@mockApiClass
- mark current class as mock class@mockApiData({path: ..., method: ...})
- use this method as mock data for defined path and method
export interface IWaasConfiguration {
basePath: string; // Base path
hooks?: IWaasHooks; //Hooks for service
}
export interface IWaasHooks {
before?: ((config: IRequestConfigExecute) => IRequestConfigExecute)[]
| ((data: IRequestConfigExecute) => IRequestConfigExecute); // Before request send
after?: ((config: any) => any)[] | ((config: any) => any); //After recieved response
}
WAAS provides custom events:
export enum RequestEventEnum {
request = 'request', // before send request
success = 'success', // response status is 2xx
error = 'error', // response status is 4xx
failure = 'failure', // all other response status
cancel = 'cancel' // request is canceled
}