/
module.ts
73 lines (64 loc) · 1.92 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Injectable, NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { RestangularHandler, RestangularInterceptingHandler } from './handler';
import { Restangular } from './restangular';
import { DefaultRestangularConfig, RestangularConfig } from './config';
import { RestangularClient } from './client';
import { RestangularBuilder } from './builder';
@Injectable()
export class InitialRestangular {
constructor(private handler: RestangularHandler) {
}
one(routeOrId, id?) {
let route = routeOrId;
if (typeof id === 'undefined') {
id = routeOrId;
route = undefined;
}
const builder = new RestangularBuilder({id, route, isCollection: false});
return new RestangularClient(builder, this.handler);
}
all(route) {
const builder = new RestangularBuilder({route, isCollection: true});
return new RestangularClient(builder, this.handler);
}
extendConfig(options: any) {
const handler = this.handler.extendConfig(options);
return new InitialRestangular(handler);
}
withConfig(options: any) {
const handler = this.handler.withConfig(options);
return new InitialRestangular(handler);
}
}
@NgModule({
imports: [
HttpClientModule
],
providers: [
{provide: RestangularConfig, useValue: DefaultRestangularConfig},
{provide: Restangular, useClass: InitialRestangular},
{provide: RestangularHandler, useClass: RestangularInterceptingHandler},
],
})
export class RestangularModule {
static config(config: any) {
let configProvider;
switch (typeof config) {
case 'object': {
configProvider = {provide: RestangularConfig, useValue: config};
break;
}
case 'function': {
configProvider = {provide: RestangularConfig, useClass: config};
break;
}
}
return {
ngModule: RestangularModule,
providers: [
configProvider,
]
};
}
}