/
in-memory-web-api-module.ts
58 lines (54 loc) 路 2.19 KB
/
in-memory-web-api-module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {XhrFactory} from '@angular/common';
import {HttpBackend} from '@angular/common/http';
import {PRIMARY_HTTP_BACKEND} from '@angular/common/http/src/interceptor';
import {ModuleWithProviders, NgModule, Type} from '@angular/core';
import {HttpClientBackendService} from './http-client-backend-service';
import {InMemoryBackendConfig, InMemoryBackendConfigArgs, InMemoryDbService} from './interfaces';
@NgModule()
export class InMemoryWebApiModule {
/**
* Redirect BOTH Angular `Http` and `HttpClient` XHR calls
* to in-memory data store that implements `InMemoryDbService`.
* with class that implements InMemoryDbService and creates an in-memory database.
*
* Usually imported in the root application module.
* Can import in a lazy feature module too, which will shadow modules loaded earlier
*
* @param dbCreator - Class that creates seed data for in-memory database. Must implement
* InMemoryDbService.
* @param [options]
*
* @example
* InMemoryWebApiModule.forRoot(dbCreator);
* InMemoryWebApiModule.forRoot(dbCreator, {useValue: {delay:600}});
*/
static forRoot(dbCreator: Type<InMemoryDbService>, options?: InMemoryBackendConfigArgs):
ModuleWithProviders<InMemoryWebApiModule> {
return {
ngModule: InMemoryWebApiModule,
providers: [
{provide: InMemoryDbService, useClass: dbCreator},
{provide: InMemoryBackendConfig, useValue: options},
{provide: HttpBackend, useClass: HttpClientBackendService},
{provide: PRIMARY_HTTP_BACKEND, useExisting: HttpClientBackendService}
]
};
}
/**
*
* Enable and configure the in-memory web api in a lazy-loaded feature module.
* Same as `forRoot`.
* This is a feel-good method so you can follow the Angular style guide for lazy-loaded modules.
*/
static forFeature(dbCreator: Type<InMemoryDbService>, options?: InMemoryBackendConfigArgs):
ModuleWithProviders<InMemoryWebApiModule> {
return InMemoryWebApiModule.forRoot(dbCreator, options);
}
}