Skip to content

Commit

Permalink
Merge pull request #330 from christophercr/feature/injection-tokens
Browse files Browse the repository at this point in the history
chore(core): define InjectionTokens for every service and a forRoot() method for every module
  • Loading branch information
SuperITMan authored Apr 24, 2018
2 parents 9306c93 + 4086151 commit 69e1f71
Show file tree
Hide file tree
Showing 31 changed files with 261 additions and 150 deletions.
2 changes: 1 addition & 1 deletion packages/stark-build/config/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"use-pipe-transform-interface": true,
"no-output-named-after-standard-event": true,
"max-inline-declarations": true,
// "no-life-cycle-call": true, // FIXME: throws an exception (Codelyzer v4.3.0). Enable this rule when solved
// "no-life-cycle-call": true, // FIXME: throws an exception (Codelyzer v4.3.0). Enable this rule when solved
"no-conflicting-life-cycle-hooks": true,
"enforce-component-selector": true,
"component-class-suffix": [true, "Component"],
Expand Down
4 changes: 2 additions & 2 deletions packages/stark-core/src/common/routes/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Location } from "@angular/common";
import { StarkRoutingService, starkRoutingServiceName, StarkStateConfigWithParams } from "../../routing/services";
import { StarkRoutingService, STARK_ROUTING_SERVICE, StarkStateConfigWithParams } from "../../routing/services";
import { StatesModule } from "@uirouter/angular";

export const starkAppInitStateName: string = "starkAppInit";
Expand All @@ -26,7 +26,7 @@ export const starkCoreRouteConfig: StatesModule = <any>{
resolve: {
targetRoute: [
"$location",
starkRoutingServiceName,
STARK_ROUTING_SERVICE,
($location: Location, routingService: StarkRoutingService) => {
// get the path of the current URL in the browser's navigation bar
const targetUrlPath: string = $location.path();
Expand Down
2 changes: 1 addition & 1 deletion packages/stark-core/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./configuration/entities"
export * from "./configuration/entities";
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ import { StarkHttpGetRequestBuilder } from "./http-get-request-builder.intf";
import { StarkHttpGetCollectionRequestBuilder } from "./http-get-collection-request-builder.intf";
import { StarkHttpSearchRequestBuilder } from "./http-search-request-builder.intf";
import { StarkHttpUpdateRequestBuilder } from "./http-update-request-builder.intf";
import {
StarkBackend,
StarkBackendImpl,
StarkHttpRequest,
StarkHttpRequestType,
StarkResource,
StarkSortItemImpl
} from "../entities";
import { StarkBackend, StarkBackendImpl, StarkHttpRequest, StarkHttpRequestType, StarkResource, StarkSortItemImpl } from "../entities";
import { StarkLanguages } from "../../configuration/entities";
import { stringMap } from "../../serialization";
import { StarkHttpEchoType, StarkHttpHeaders, StarkHttpQueryParameters, StarkSortOrder } from "../constants";
Expand Down
46 changes: 27 additions & 19 deletions packages/stark-core/src/http/http.module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { NgModule } from "@angular/core";
import { HttpClient, HttpClientModule } from "@angular/common/http";
import { StarkHttpServiceImpl, starkHttpServiceName } from "./services";
import { StarkLoggingService, starkLoggingServiceName } from "../logging";

// FIXME: remove this factory once LoggingService and SessionService are implemented
export function starkHttpServiceFactory(httpClient: HttpClient, starkLoggingService: StarkLoggingService): StarkHttpServiceImpl<any> {
const sessionService: any = {
fakePreAuthenticationHeaders: new Map<string, string>([["nbb-dummy-header", "some value"], ["nbb-another-header", "whatever"]])
};

return new StarkHttpServiceImpl(starkLoggingService, sessionService, httpClient);
}
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from "@angular/core";
import { HttpClientModule } from "@angular/common/http";
import { STARK_HTTP_SERVICE, StarkHttpServiceImpl } from "./services";

@NgModule({
imports: [HttpClientModule],
providers: [
// FIXME: replace this Factory provider by a simple Class provider once LoggingService and SessionService are implemented
{ provide: starkHttpServiceName, useFactory: starkHttpServiceFactory, deps: [HttpClient, starkLoggingServiceName] }
]
imports: [HttpClientModule]
})
export class StarkHttpModule {}
export class StarkHttpModule {
// instantiate the services only once since they should be singletons
// so the forRoot() should be called only by the AppModule
// see https://angular.io/guide/singleton-services#forroot
public static forRoot(): ModuleWithProviders {
return {
ngModule: StarkHttpModule,
providers: [{ provide: STARK_HTTP_SERVICE, useClass: StarkHttpServiceImpl }]
};
}

// prevent this module from being re-imported
// see https://angular.io/guide/singleton-services#prevent-reimport-of-the-coremodule
public constructor(
@Optional()
@SkipSelf()
parentModule: StarkHttpModule
) {
if (parentModule) {
throw new Error("StarkHttpModule is already loaded. Import it in the AppModule only");
}
}
}
4 changes: 3 additions & 1 deletion packages/stark-core/src/http/services/http.service.intf.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { InjectionToken } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { StarkCollectionResponseWrapper, StarkHttpRequest, StarkResource, StarkSingleItemResponseWrapper } from "../entities";
import { Observable } from "rxjs/Observable";
import { HttpClient } from "@angular/common/http";

export const starkHttpServiceName: string = "StarkHttpService";
export const STARK_HTTP_SERVICE: InjectionToken<StarkHttpService<any>> = new InjectionToken<StarkHttpService<any>>(starkHttpServiceName);

/**
* Stark Http Service
Expand Down
16 changes: 6 additions & 10 deletions packages/stark-core/src/http/services/http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { catchError } from "rxjs/operators/catchError";
import { map } from "rxjs/operators/map";
import { retryWhen } from "rxjs/operators/retryWhen";
import { mergeMap } from "rxjs/operators/mergeMap";
import { Injectable } from "@angular/core";
import { Inject, Injectable } from "@angular/core";
import { HttpClient, HttpHeaders, HttpResponse, HttpErrorResponse } from "@angular/common/http";

import { StarkHttpService, starkHttpServiceName } from "./http.service.intf";
Expand All @@ -28,8 +28,8 @@ import {
StarkSingleItemResponseWrapper,
StarkSingleItemResponseWrapperImpl
} from "../entities";
import { StarkLoggingService } from "../../logging";
import { StarkSessionService } from "../../session";
import { StarkLoggingService, STARK_LOGGING_SERVICE } from "../../logging";
import { StarkSessionService, STARK_SESSION_SERVICE } from "../../session";

/**
* @ngdoc service
Expand All @@ -44,15 +44,11 @@ import { StarkSessionService } from "../../session";
export class StarkHttpServiceImpl<P extends StarkResource> implements StarkHttpService<P> {
protected retryDelay: number = 1000;

private logger: StarkLoggingService;
private sessionService: StarkSessionService;
private httpClient: HttpClient;

// FIXME: uncomment these lines once LoggingService and SessionService are implemented
public constructor(
/*@Inject(starkLoggingServiceName)*/ logger: StarkLoggingService,
/*@Inject(starkSessionServiceName)*/ sessionService: StarkSessionService,
httpClient: HttpClient
@Inject(STARK_LOGGING_SERVICE) private logger: StarkLoggingService,
@Inject(STARK_SESSION_SERVICE) private sessionService: StarkSessionService,
private httpClient: HttpClient
) {
this.logger = logger;
this.sessionService = sessionService;
Expand Down
2 changes: 1 addition & 1 deletion packages/stark-core/src/http/testing.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "./testing/http.mock"
export * from "./testing/http.mock";
32 changes: 26 additions & 6 deletions packages/stark-core/src/logging/logging.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import { NgModule } from "@angular/core";
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from "@angular/core";
import { ActionReducerMap, StoreModule } from "@ngrx/store";

import { loggingReducer, StarkLoggingState } from "./reducers";
import { StarkLoggingActions } from "./actions";
import { StarkLoggingServiceImpl, starkLoggingServiceName } from "./services";
import { StarkLoggingServiceImpl, STARK_LOGGING_SERVICE } from "./services";

const reducers: ActionReducerMap<StarkLoggingState, StarkLoggingActions> = {
logging: loggingReducer
};

@NgModule({
imports: [StoreModule.forFeature("StarkLogging", reducers)],
declarations: [],
providers: [{ provide: starkLoggingServiceName, useClass: StarkLoggingServiceImpl }]
imports: [StoreModule.forFeature("StarkLogging", reducers)]
})
export class StarkLoggingModule {}
export class StarkLoggingModule {
// instantiate the services only once since they should be singletons
// so the forRoot() should be called only by the AppModule
// see https://angular.io/guide/singleton-services#forroot
public static forRoot(): ModuleWithProviders {
return {
ngModule: StarkLoggingModule,
providers: [{ provide: STARK_LOGGING_SERVICE, useClass: StarkLoggingServiceImpl }]
};
}

// prevent this module from being re-imported
// see https://angular.io/guide/singleton-services#prevent-reimport-of-the-coremodule
public constructor(
@Optional()
@SkipSelf()
parentModule: StarkLoggingModule
) {
if (parentModule) {
throw new Error("StarkLoggingModule is already loaded. Import it in the AppModule only");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { InjectionToken } from "@angular/core";
import { StarkError } from "../../common";

export const starkLoggingServiceName: string = "StarkLoggingService";
export const STARK_LOGGING_SERVICE: InjectionToken<StarkLoggingService> = new InjectionToken<StarkLoggingService>(starkLoggingServiceName);

/**
* Stark Logging Service.
Expand Down
33 changes: 27 additions & 6 deletions packages/stark-core/src/routing/routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { NgModule } from "@angular/core";
import { StarkRoutingServiceImpl, starkRoutingServiceName } from "./services";
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from "@angular/core";
import { UIRouterModule } from "@uirouter/angular";
import { StarkRoutingServiceImpl, STARK_ROUTING_SERVICE } from "./services";

@NgModule({
imports: [],
declarations: [],
providers: [{ provide: starkRoutingServiceName, useClass: StarkRoutingServiceImpl }]
imports: [UIRouterModule.forChild()]
})
export class StarkRoutingModule {}
export class StarkRoutingModule {
// instantiate the services only once since they should be singletons
// so the forRoot() should be called only by the AppModule
// see https://angular.io/guide/singleton-services#forroot
public static forRoot(): ModuleWithProviders {
return {
ngModule: StarkRoutingModule,
providers: [{ provide: STARK_ROUTING_SERVICE, useClass: StarkRoutingServiceImpl }]
};
}

// prevent this module from being re-imported
// see https://angular.io/guide/singleton-services#prevent-reimport-of-the-coremodule
public constructor(
@Optional()
@SkipSelf()
parentModule: StarkRoutingModule
) {
if (parentModule) {
throw new Error("StarkRoutingModule is already loaded. Import it in the AppModule only");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Observable } from "rxjs/Observable";
import { HookFn, HookMatchCriteria, HookRegOptions, RawParams, StateDeclaration, StateObject, TransitionOptions } from "@uirouter/core";

import { StarkStateConfigWithParams } from "./state-config-with-params.intf";
import { InjectionToken } from "@angular/core";

export const starkRoutingServiceName: string = "StarkRoutingService";
export const STARK_ROUTING_SERVICE: InjectionToken<StarkRoutingService> = new InjectionToken<StarkRoutingService>(starkRoutingServiceName);

/**
* Stark Routing Service.
Expand Down
8 changes: 4 additions & 4 deletions packages/stark-core/src/routing/services/routing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fromPromise } from "rxjs/observable/fromPromise";
import { empty } from "rxjs/observable/empty";
import { Inject, Injectable } from "@angular/core";

import { StarkLoggingService, starkLoggingServiceName } from "../../logging/services";
import { StarkLoggingService, STARK_LOGGING_SERVICE } from "../../logging/services";
import { StarkRoutingService, starkRoutingServiceName } from "./routing.service.intf";
import {
Navigate,
Expand Down Expand Up @@ -71,11 +71,11 @@ export class StarkRoutingServiceImpl implements StarkRoutingService {
private _starkStateHistory: StarkState[];

public constructor(
@Inject(starkLoggingServiceName) private logger: StarkLoggingService,
@Inject(STARK_LOGGING_SERVICE) private logger: StarkLoggingService,
private store: Store<StarkCoreApplicationState>,
@Inject(STARK_APP_CONFIG) private appConfig: StarkApplicationConfig,
@Inject("$state") private $state: StateService,
@Inject("$transitions") private $transitions: TransitionService
private $state: StateService,
private $transitions: TransitionService
) {
this.appConfig = appConfig;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { InjectionToken } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { StarkUser } from "../../user/entities";

export const starkSessionServiceName: string = "StarkSessionService";
export const STARK_SESSION_SERVICE: InjectionToken<StarkSessionService> = new InjectionToken<StarkSessionService>(starkSessionServiceName);

/**
* Stark Session Service.
Expand Down
8 changes: 4 additions & 4 deletions packages/stark-core/src/session/services/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { map } from "rxjs/operators/map";
import { defer } from "rxjs/observable/defer";
import { validateSync } from "class-validator";

import { StarkLoggingService, starkLoggingServiceName } from "../../logging/services";
import { StarkLoggingService, STARK_LOGGING_SERVICE } from "../../logging/services";
import { StarkSessionService, starkSessionServiceName } from "./session.service.intf";
import { StarkRoutingService, starkRoutingServiceName, StarkRoutingTransitionHook } from "../../routing/services";
import { StarkRoutingService, STARK_ROUTING_SERVICE, StarkRoutingTransitionHook } from "../../routing/services";
import { StarkApplicationConfig, STARK_APP_CONFIG } from "../../configuration/entities";
import { StarkPreAuthentication, StarkSession } from "../entities";
import { StarkUser } from "../../user/entities";
Expand Down Expand Up @@ -76,8 +76,8 @@ export class StarkSessionServiceImpl implements StarkSessionService {

public constructor(
public store: Store<StarkCoreApplicationState>,
@Inject(starkLoggingServiceName) public logger: StarkLoggingService,
@Inject(starkRoutingServiceName) public routingService: StarkRoutingService,
@Inject(STARK_LOGGING_SERVICE) public logger: StarkLoggingService,
@Inject(STARK_ROUTING_SERVICE) public routingService: StarkRoutingService,
@Inject(STARK_APP_CONFIG) private appConfig: StarkApplicationConfig,
// FIXME Uncomment when XSRF Service is implemented
// @Inject(starkXSRFServiceName) public xsrfService: StarkXSRFService,
Expand Down
33 changes: 26 additions & 7 deletions packages/stark-core/src/session/session.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import { NgModule } from "@angular/core";
import { ModuleWithProviders, NgModule, Optional, SkipSelf } from "@angular/core";
import { ActionReducerMap, StoreModule } from "@ngrx/store";

import { sessionReducer, StarkSessionState } from "./reducers";
import { StarkSessionActions } from "./actions";
import { StarkSessionServiceImpl, starkSessionServiceName } from "./services";
import { StarkSessionServiceImpl, STARK_SESSION_SERVICE } from "./services";

const reducers: ActionReducerMap<StarkSessionState, StarkSessionActions> = {
session: sessionReducer
};

@NgModule({
imports: [StoreModule.forFeature("StarkSession", reducers)],
declarations: [],
providers: [{ provide: starkSessionServiceName, useClass: StarkSessionServiceImpl }]
imports: [StoreModule.forFeature("StarkSession", reducers)]
})
export class StarkSessionModule {}
export class StarkSessionModule {
// instantiate the services only once since they should be singletons
// so the forRoot() should be called only by the AppModule
// see https://angular.io/guide/singleton-services#forroot
public static forRoot(): ModuleWithProviders {
return {
ngModule: StarkSessionModule,
providers: [{ provide: STARK_SESSION_SERVICE, useClass: StarkSessionServiceImpl }]
};
}

// prevent this module from being re-imported
// see https://angular.io/guide/singleton-services#prevent-reimport-of-the-coremodule
public constructor(
@Optional()
@SkipSelf()
parentModule: StarkSessionModule
) {
if (parentModule) {
throw new Error("StarkSessionModule is already loaded. Import it in the AppModule only");
}
}
}
8 changes: 4 additions & 4 deletions packages/stark-core/src/user/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { StarkUser } from "../entities";
import { StarkHttpRequest, StarkSingleItemResponseWrapper } from "../../http/entities";
import { StarkApplicationConfig, STARK_APP_CONFIG } from "../../configuration/entities/application";
import { StarkUserRepository } from "./user.repository.intf";
import { StarkLoggingService, starkLoggingServiceName } from "../../logging/services/logging.service.intf";
import { StarkHttpService, starkHttpServiceName } from "../../http/services";
import { StarkLoggingService, STARK_LOGGING_SERVICE } from "../../logging/services/logging.service.intf";
import { StarkHttpService, STARK_HTTP_SERVICE } from "../../http/services";
import { AbstractStarkHttpRepository } from "../../http/repository";

/**
Expand All @@ -21,8 +21,8 @@ import { AbstractStarkHttpRepository } from "../../http/repository";
@Injectable()
export class StarkUserRepositoryImpl extends AbstractStarkHttpRepository<StarkUser> implements StarkUserRepository {
public constructor(
@Inject(starkHttpServiceName) starkHttpService: StarkHttpService<StarkUser>,
@Inject(starkLoggingServiceName) logger: StarkLoggingService,
@Inject(STARK_HTTP_SERVICE) starkHttpService: StarkHttpService<StarkUser>,
@Inject(STARK_LOGGING_SERVICE) logger: StarkLoggingService,
@Inject(STARK_APP_CONFIG) appConfig: StarkApplicationConfig
) {
super(starkHttpService, logger, appConfig.getBackend("userProfile"), "security/userprofile");
Expand Down
2 changes: 2 additions & 0 deletions packages/stark-core/src/user/services/user.service.intf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { InjectionToken } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { StarkUser } from "../entities";

export const starkUserServiceName: string = "StarkUserService";
export const STARK_USER_SERVICE: InjectionToken<StarkUserService> = new InjectionToken<StarkUserService>(starkUserServiceName);

/**
* Stark User Service.
Expand Down
Loading

0 comments on commit 69e1f71

Please sign in to comment.