NgxMockApi
Mock api library for Angular
Usage
-
In AppModule add in import :
HttpClientModule
andNgxMockApiModule.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.EQUALS
or'Equals'
-
This compare type check if mock Url match regex
/((https?:\/\/).*(\/.*)?)(\:.*)?$/gm;
-
-
CompareType.STARTS_WITH
or'StartsWith'
-
CompareType.ENDS_WITH
or'EndsWith'
-
-
Allowed http method
-
HttpMethod.GET
or'GET'
-
HttpMethod.POST
or'POST'
-
HttpMethod.PUT
or'PUT'
-
HttpMethod.PATCH
or'PATCH'
-
HttpMethod.DELETE
or'DELETE'
-
HttpMethod.OPTIONS
or'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');
}