ak-mock-decorator
v1.0.0
Published
Mock decorator to simulate API responses.
Downloads
3
Maintainers
Readme
MockDecorator
Mock decorator to simulate API responses.
Getting started
Import MockDecoratorModule
import { MockDecoratorModule } from 'ak-mock-decorator';
imports: [
MockDecoratorModule.forRoot()
]
// Or with config
imports: [
MockDecoratorModule.forRoot({
disable: false,
endpoint: "http://my-fake-endpoint"
})
]
Add MockMapping
method decorator
import { HttpClient } from '@angular/common/http';
import { MockMapping } from 'ak-mock-decorator';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
export const PRODUCTS_MOCK = [
{ id: 1, title: 'Product 1' },
{ id: 2, title: 'Product 2' },
{ id: 3, title: 'Product 3' },
{ id: 4, title: 'Product 4' },
];
@Injectable({
providedIn: 'root',
})
export class ApiService {
constructor(private http: HttpClient) {}
@MockMapping([{ disable: false, delay: 600, body: PRODUCTS_MOCK }])
getProduct(): Observable<any> {
return this.http.get('http://api-endpoint/products');
}
@MockMapping([
{ body: { name: 'User 0' } },
{ uri: '/users/1', body: { name: 'User 1' } },
{ uri: '/users/2', body: { name: 'User 2' } },
])
getUser(): Observable<any> {
return this.http.get('http://api-endpoint/users/1');
}
}