Skip to content

Commit

Permalink
feat(logging): implement STARK_APP_CONFIG token and implement Stark l…
Browse files Browse the repository at this point in the history
…ogging in Starter
  • Loading branch information
RobbyDeLaet committed Apr 6, 2018
1 parent a649ba0 commit ec09736
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 86 deletions.
9 changes: 5 additions & 4 deletions packages/stark-build/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ module.exports = function(options) {
module: !isProd && METADATA.WATCH ? "commonjs" : "es2015" // TODO is it needed in our case? => Force commonjs module format for TS on dev watch builds.
};

const appNgcOptions = {
...defaultNgcOptions,
...tsConfigApp.raw.angularCompilerOptions
};
const appNgcOptions = Object.assign(
{},
defaultNgcOptions,
tsConfigApp.raw.angularCompilerOptions
);

const environment = buildUtils.getEnvFile(METADATA.envFileSuffix);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use strict";

import { StarkBackend } from "../../../http/entities/backend";
import { InjectionToken } from '@angular/core';

export const STARK_APP_CONFIG: InjectionToken<StarkApplicationConfig> = new InjectionToken<StarkApplicationConfig>('STARK_APP_CONFIG');

export const starkAppConfigConstantName: string = "StarkAppConfig";
/**
* Minimal set of configuration options for Stark applications.
* An implementation should be instantiated and be available under the name defined in the constants.
Expand Down
15 changes: 5 additions & 10 deletions packages/stark-core/src/http/http.module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { NgModule } from "@angular/core";
import { HttpClient, HttpClientModule } from "@angular/common/http";
import { StarkHttpServiceImpl, starkHttpServiceName } from "./services/index";
import { StarkLoggingService, starkLoggingServiceName } from '../logging/index';

// FIXME: remove this factory once LoggingService and SessionService are implemented
export function starkHttpServiceFactory(httpClient: HttpClient): StarkHttpServiceImpl<any> {
const logger: any = {
debug: console.debug,
warn: console.warn,
error: console.error,
correlationId: "dummy-correlation-id"
};
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(logger, sessionService, httpClient)
return new StarkHttpServiceImpl(starkLoggingService, sessionService, httpClient);
}

@NgModule({
Expand All @@ -26,9 +21,9 @@ export function starkHttpServiceFactory(httpClient: HttpClient): StarkHttpServic
],
providers: [
// FIXME: replace this Factory provider by a simple Class provider once LoggingService and SessionService are implemented
{provide: starkHttpServiceName, useFactory: starkHttpServiceFactory, deps: [HttpClient]},
{ provide: starkHttpServiceName, useFactory: starkHttpServiceFactory, deps: [HttpClient, starkLoggingServiceName] }
]
})
export class StarkHttpModule {

export class StarkHttpModule {
}
1 change: 1 addition & 0 deletions packages/stark-core/src/logging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from "./actions/index";
export * from "./entities/index";
export * from "./reducers/index";
export * from "./services/index";
export * from "./logging.module";
6 changes: 2 additions & 4 deletions packages/stark-core/src/logging/logging.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ActionReducerMap, StoreModule } from "@ngrx/store";
import { loggingReducer } from "./reducers/index";
import { StarkLoggingActions } from "./actions/index";
import { StarkLoggingServiceImpl, starkLoggingServiceName } from "./services/index";
import { starkAppConfigConstantName, StarkApplicationConfigImpl } from "../configuration/entities/index";
import { StarkLoggingApplicationState } from "../common/store/starkCoreApplicationState";

const reducers:ActionReducerMap<StarkLoggingApplicationState, StarkLoggingActions> = {
Expand All @@ -15,8 +14,7 @@ starkLogging: loggingReducer
imports: [StoreModule.forRoot(reducers)],
declarations: [],
providers: [
{ provide: starkLoggingServiceName, useClass: StarkLoggingServiceImpl },
{ provide: starkAppConfigConstantName, useClass: StarkApplicationConfigImpl }
{ provide: starkLoggingServiceName, useClass: StarkLoggingServiceImpl }
]
})
export class LoggingModule {}
export class StarkLoggingModule {}
7 changes: 3 additions & 4 deletions packages/stark-core/src/logging/services/logging.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Subject } from "rxjs/Subject";
import { Inject, Injectable } from "@angular/core";

import { StarkLoggingService, starkLoggingServiceName } from "./logging.service.intf";
import { StarkApplicationConfig, starkAppConfigConstantName } from "../../configuration/entities/index";
import { StarkApplicationConfig, STARK_APP_CONFIG } from "../../configuration/entities/index";
import { StarkBackend } from "../../http/entities/backend/index";
import { StarkCoreApplicationState, StarkLoggingApplicationState } from "../../common/store/starkCoreApplicationState";
import { StarkHttpStatusCodes } from "../../http/enumerators/index";
Expand Down Expand Up @@ -52,8 +52,7 @@ export class StarkLoggingServiceImpl implements StarkLoggingService {
// FIXME: uncomment these lines once XSRF Service is implemented
public constructor(
private store: Store<StarkCoreApplicationState>,
@Inject(starkAppConfigConstantName)
private appConfig: StarkApplicationConfig /*,
@Inject(STARK_APP_CONFIG) private appConfig: StarkApplicationConfig /*,
@Inject(starkXSRFServiceName) private xsrfService: StarkXSRFService*/
) {
this.isPersisting = false;
Expand All @@ -68,7 +67,7 @@ export class StarkLoggingServiceImpl implements StarkLoggingService {
// ensuring that the app config is valid before configuring the automatic logging flush
StarkValidationErrorsUtil.throwOnError(
validateSync(this.appConfig),
starkLoggingServiceName + ": " + starkAppConfigConstantName + " constant is not valid."
starkLoggingServiceName + ": " + STARK_APP_CONFIG.toString() + " constant is not valid."
);

this.backend = this.appConfig.getBackend("logging");
Expand Down
6 changes: 3 additions & 3 deletions starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"@angular/platform-server": "5.2.9",
"@angular/router": "5.2.9",
"@nationalbankbelgium/angular-mdi-svg": "1.7.0",
"@nationalbankbelgium/stark-core": "file:../dist/packages-dist/stark-core/nationalbankbelgium-stark-core-10.0.0-alpha.0-f89b1b0.tgz",
"@nationalbankbelgium/stark-core": "file:../dist/packages-dist/stark-core/nationalbankbelgium-stark-core-10.0.0-alpha.0-abad17c.tgz",
"@uirouter/angular": "1.0.1",
"@uirouter/visualizer": "6.0.0",
"core-js": "2.5.3",
Expand All @@ -129,8 +129,8 @@
"zone.js": "0.8.20"
},
"devDependencies": {
"@nationalbankbelgium/stark-build": "file:../dist/packages-dist/stark-build/nationalbankbelgium-stark-build-10.0.0-alpha.0-f89b1b0.tgz",
"@nationalbankbelgium/stark-testing": "file:../dist/packages-dist/stark-testing/nationalbankbelgium-stark-testing-10.0.0-alpha.0-f89b1b0.tgz",
"@nationalbankbelgium/stark-build": "file:../dist/packages-dist/stark-build/nationalbankbelgium-stark-build-10.0.0-alpha.0-abad17c.tgz",
"@nationalbankbelgium/stark-testing": "file:../dist/packages-dist/stark-testing/nationalbankbelgium-stark-testing-10.0.0-alpha.0-abad17c.tgz",
"@types/core-js": "0.9.46",
"@types/hammerjs": "2.0.35",
"@types/node": "6.0.102",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, OnInit } from "@angular/core";
import { Component, Inject, OnInit } from "@angular/core";
import { StarkLoggingService, starkLoggingServiceName } from "@nationalbankbelgium/stark-core";
/**
* We're loading this component asynchronously
* We are using some magic with es6-promise-loader that will wrap the module with a Promise
Expand All @@ -14,7 +15,9 @@ console.log("`ChildBarrel` component loaded asynchronously");
`
})
export class ChildBarrelComponent implements OnInit {
public constructor(@Inject(starkLoggingServiceName) public loggingService: StarkLoggingService) {}

public ngOnInit(): void {
console.log("hello `ChildBarrel` component");
this.loggingService.debug("hello from `ChildBarrel` component");
}
}
7 changes: 5 additions & 2 deletions starter/src/app/+barrel/barrel.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, OnInit } from "@angular/core";
import { Component, Inject, OnInit } from "@angular/core";
import { StarkLoggingService, starkLoggingServiceName } from "@nationalbankbelgium/stark-core";
/**
* We're loading this component asynchronously
* We are using some magic with es6-promise-loader that will wrap the module with a Promise
Expand All @@ -12,7 +13,9 @@ console.log("`Barrel` component loaded asynchronously");
templateUrl: "./barrel.component.html"
})
export class BarrelComponent implements OnInit {
public constructor(@Inject(starkLoggingServiceName) public loggingService: StarkLoggingService) {}

public ngOnInit(): void {
console.log("hello `Barrel` component");
this.loggingService.debug("hello from `Barrel` component");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, OnInit } from "@angular/core";
import { Component, Inject, OnInit } from "@angular/core";
import { StarkLoggingService, starkLoggingServiceName } from "@nationalbankbelgium/stark-core";
/**
* We're loading this component asynchronously
* We are using some magic with es6-promise-loader that will wrap the module with a Promise
Expand All @@ -14,7 +15,9 @@ console.log("`ChildDetail` component loaded asynchronously");
`
})
export class ChildDetailComponent implements OnInit {
public constructor(@Inject(starkLoggingServiceName) public loggingService: StarkLoggingService) {}

public ngOnInit(): void {
console.log("hello `ChildDetail` component");
this.loggingService.debug("hello from `ChildDetail` component");
}
}
7 changes: 5 additions & 2 deletions starter/src/app/+detail/detail.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, OnInit } from "@angular/core";
import { Component, Inject, OnInit } from "@angular/core";
import { StarkLoggingService, starkLoggingServiceName } from "@nationalbankbelgium/stark-core";
/**
* We're loading this component asynchronously
* We are using some magic with es6-promise-loader that will wrap the module with a Promise
Expand All @@ -12,7 +13,9 @@ console.log("`Detail` component loaded asynchronously");
templateUrl: "./detail.component.html"
})
export class DetailComponent implements OnInit {
public constructor(@Inject(starkLoggingServiceName) public loggingService: StarkLoggingService) {}

public ngOnInit(): void {
console.log("hello `Detail` component");
this.loggingService.debug("hello from `Detail` component");
}
}
7 changes: 5 additions & 2 deletions starter/src/app/+dev-module/dev-module.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, OnInit } from "@angular/core";
import { Component, Inject, OnInit } from "@angular/core";
import { StarkLoggingService, starkLoggingServiceName } from "@nationalbankbelgium/stark-core";

@Component({
selector: "dev-module",
Expand All @@ -7,7 +8,9 @@ import { Component, OnInit } from "@angular/core";
`
})
export class DevModuleComponent implements OnInit {
public constructor(@Inject(starkLoggingServiceName) public loggingService: StarkLoggingService) {}

public ngOnInit(): void {
console.log("hello `DevModule` component");
this.loggingService.debug("hello from `DevModule` component");
}
}
35 changes: 31 additions & 4 deletions starter/src/app/about/about.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,37 @@ import { inject, TestBed } from "@angular/core/testing";
* Load the implementations that should be tested.
*/
import { AboutComponent } from "./about.component";
import {
StarkLoggingModule,
STARK_APP_CONFIG,
StarkBackend,
StarkApplicationConfig,
StarkBackendAuthenticationTypes,
StarkLoggingService,
starkLoggingServiceName
} from "@nationalbankbelgium/stark-core";

describe("About", () => {
/**
* Provide our implementations or mocks to the dependency injector
*/
let logger: StarkLoggingService;

const mockBackend: Partial<StarkBackend> = {
authenticationType: StarkBackendAuthenticationTypes.PUBLIC,
name: "logging",
url: "dummy/url"
};

const mockStarkAppConfig: Partial<StarkApplicationConfig> = {
angularDebugInfoEnabled: true,
debugLoggingEnabled: true,
getBackend: jasmine.createSpy("getBackendSpy").and.returnValue(mockBackend)
};

beforeEach(() =>
TestBed.configureTestingModule({
imports: [StarkLoggingModule],
providers: [
/**
* Provide a better mock.
Expand All @@ -27,19 +51,22 @@ describe("About", () => {
}
}
},
AboutComponent
AboutComponent,
{ provide: STARK_APP_CONFIG, useValue: mockStarkAppConfig }
]
})
);

it(
"should log ngOnInit",
inject([AboutComponent], (about: AboutComponent) => {
spyOn(console, "log");
expect(console.log).not.toHaveBeenCalled();
logger = TestBed.get(starkLoggingServiceName);

spyOn(logger, "debug");
expect(logger.debug).not.toHaveBeenCalled();

about.ngOnInit();
expect(console.log).toHaveBeenCalled();
expect(logger.debug).toHaveBeenCalled();
})
);
});
20 changes: 11 additions & 9 deletions starter/src/app/about/about.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, Input, OnInit } from "@angular/core";
import { Component, Inject, Input, OnInit } from "@angular/core";
// import { Transition } from '@uirouter/angular';
import { Observable } from "rxjs/Observable";
import { StarkLoggingService, starkLoggingServiceName } from "@nationalbankbelgium/stark-core";

@Component({
selector: "about",
Expand All @@ -27,10 +28,10 @@ export class AboutComponent implements OnInit {
@Input() public paramData: any;

public localState: any;
public constructor() // public transition: Transition // the last transition could be injected if needed
{
/* empty */
}
public constructor(
// public transition: Transition // the last transition could be injected if needed
@Inject(starkLoggingServiceName) public loggingService: StarkLoggingService
) {}

public ngOnInit(): void {
/**
Expand All @@ -46,7 +47,7 @@ export class AboutComponent implements OnInit {
// if the resolve is an observable, we need to subscribe to it to get the value
if (this.resolvedData) {
this.resolvedData.subscribe((data: any) => {
console.warn("data resolved");
this.loggingService.warn("data resolved");
this.localState = { ...this.localState, ...data };
});
}
Expand All @@ -63,11 +64,12 @@ export class AboutComponent implements OnInit {
// let resolvePromise: Promise<any> = this.transition.injector().get('resolvedData');
// resolvePromise.then((data: any) => this.localState = {...this.localState, ...data});

console.log("hello `About` component");
this.loggingService.debug("hello from `About` component");

/**
* static data that is bundled
* var mockData = require('assets/mock-data/mock-data.json');
* console.log('mockData', mockData);
* this.loggingService.debug('mockData', mockData);
* if you're working with mock data you can also use http.get('assets/mock-data/mock-data.json')
*/
this.asyncDataWithWebpack();
Expand All @@ -80,7 +82,7 @@ export class AboutComponent implements OnInit {
*/
// setTimeout(() => {
// System.import("../../assets/mock-data/mock-data.json").then(json => {
// console.log("async mockData", json);
// this.loggingService.debug("async mockData", json);
// this.localState = { ...this.localState, asyncData: json };
// });
// });
Expand Down
Loading

0 comments on commit ec09736

Please sign in to comment.