@fababracx/ngx-mock-api
v0.0.3
Published
Mock api library for Angular
Downloads
27
Maintainers
Readme
NgxMockApi
Mock api library for Angular
Usage
In AppModule add in import :
HttpClientModuleandNgxMockApiModule.withConfig(activeMockApi, mockApiUrls, delayRequest)- with params:
const activeMockApi: true; - and params:
const delayRequest: 0; - and
- with params:
const mockApiUrlsExemple: INgxMockApiUrlsConfig[] = [
{
method: HttpMethod.GET, // Default value HttpMethod.GET
url: 'http:localhost:4200/users',
data: [
{
id: 1,
name: 'User 1'
},
{
id: 2,
name: 'User 2'
}
],
compareType: CompareType.EQUALS, // Default value CompareType.EQUALS
status: 200 // Default value 200,
errorMessage: undefined, // Displayed message if error
//WIP: callback: undefined // callback after first request
},
...NgxMockApiUrlsConfig.generateNgxMockApiUrlConfig('http://localhost:4200', ['generate'], ['generate']), // generateNgxMockApiUrlConfig(apiUrl?: string, endpoints?: string[], datas?: any[], methods?: HttpMethod[], compareType?: CompareType, statuses?: number[], errorMessages?: string[])
];Allowed compare types
CompareType.EQUALSor'Equals'This compare type check if mock Url match regex
/((https?:\/\/).*(\/.*)?)(\:.*)?$/gm;
CompareType.STARTS_WITHor'StartsWith'CompareType.ENDS_WITHor'EndsWith'
Allowed http method
HttpMethod.GETor'GET'HttpMethod.POSTor'POST'HttpMethod.PUTor'PUT'HttpMethod.PATCHor'PATCH'HttpMethod.DELETEor'DELETE'HttpMethod.OPTIONSor'OPTIONS'
Just call your api with your service
get users$(): Observable<any> {
return this.http.get('http://localhost:4200/users');
}
get generate$(): Observable<any> {
return this.http.get('http://localhost:4200/generate');
}