Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(provider-function): added provider functions for modules #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions projects/ngx-common/src/lib/modules/config/config.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { NgxConfigService } from './config.service';
import { CONFIG_URL_TOKEN } from './config.token';
import {ModuleWithProviders, NgModule, Provider} from '@angular/core';
import {NgxConfigService} from './config.service';
import {CONFIG_URL_TOKEN} from './config.token';

@NgModule()
export class NgxConfigModule {

public static forRoot(url: string): ModuleWithProviders<NgxConfigModule> {
return {
ngModule: NgxConfigModule,
providers: [
export function provideNgxConfig(url: string): Provider[] {
return [
NgxConfigService,
{
provide: CONFIG_URL_TOKEN,
useValue: url
provide: CONFIG_URL_TOKEN,
useValue: url
}
]
};
}
];
}

@NgModule()
export class NgxConfigModule {

public static forRoot(url: string): ModuleWithProviders<NgxConfigModule> {
return {
ngModule: NgxConfigModule,
providers: provideNgxConfig(url)
};
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
import { InjectionToken, Injector, ModuleWithProviders, NgModule, Type } from '@angular/core';
import { ErrorHandler } from './model/error-handler.model';
import { DefaultErrorHandler } from './handler/default-error.handler';
import { ErrorHandlingService } from './error-handling.service';
import {APP_INITIALIZER, InjectionToken, Injector, ModuleWithProviders, NgModule, Provider, Type} from '@angular/core';
import {ErrorHandler} from './model/error-handler.model';
import {DefaultErrorHandler} from './handler/default-error.handler';
import {ErrorHandlingService} from './error-handling.service';

export const ERROR_HANDLER_TOKEN: InjectionToken<ErrorHandler> = new InjectionToken<ErrorHandler>('ERROR_HANDLER');

const MODULE_PROVIDERS: Provider[] = [
ErrorHandlingService,
DefaultErrorHandler
];

/**
* @ignore
*/
function ngxSerializerInitializer(injector: Injector): () => void {
return () => {
NgxErrorHandlerModule.injector = injector;
}
}

export function provideNgxErrorHandler(handlers: Type<ErrorHandler>[]): Provider[] {
return [
{
provide: APP_INITIALIZER,
useFactory: ngxSerializerInitializer,
multi: true,
deps: [Injector]
},
...MODULE_PROVIDERS,
...handlers.map((handler: Type<ErrorHandler>) => ({
provide: ERROR_HANDLER_TOKEN,
useClass: handler,
multi: true
}))
];
}

// @dynamic
@NgModule()
export class NgxErrorHandlerModule {

private static injector: Injector;

public constructor(injector: Injector) {
NgxErrorHandlerModule.injector = injector;
}
public static injector: Injector;

public static getInjector(): Injector {
return NgxErrorHandlerModule.injector;
}
public static getInjector(): Injector {
return NgxErrorHandlerModule.injector;
}

public static forRoot(handlers: Type<ErrorHandler>[]): ModuleWithProviders<NgxErrorHandlerModule> {
return {
ngModule: NgxErrorHandlerModule,
providers: [
ErrorHandlingService,
DefaultErrorHandler,
...handlers.map((handler: Type<ErrorHandler>) => ({
provide: ERROR_HANDLER_TOKEN,
useClass: handler,
multi: true
}))
]
};
}
/**
* @deprecated use provideNgxErrorHandler() instead
*/
public static forRoot(handlers: Type<ErrorHandler>[]): ModuleWithProviders<NgxErrorHandlerModule> {
return {
ngModule: NgxErrorHandlerModule,
providers: provideNgxErrorHandler(handlers)
};
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { NgxLocalStorageService } from './local-storage.service';
import {ModuleWithProviders, NgModule, Provider} from '@angular/core';
import {NgxLocalStorageService} from './local-storage.service';

export function provideNgxLocalStorage(): Provider[] {
return [
NgxLocalStorageService
];
}

@NgModule()
export class NgxLocalStorageModule {

public static forRoot(): ModuleWithProviders<NgxLocalStorageModule> {
return {
ngModule: NgxLocalStorageModule,
providers: [
NgxLocalStorageService
]
};
}
/**
* @deprecated user provideNgxLocalStorage() instead
*/
public static forRoot(): ModuleWithProviders<NgxLocalStorageModule> {
return {
ngModule: NgxLocalStorageModule,
providers: provideNgxLocalStorage()
};
}
}
Original file line number Diff line number Diff line change
@@ -1,66 +1,126 @@
import { TestBed } from '@angular/core/testing';
import { Component, Directive } from '@angular/core';
import { RouteParam } from './route-param.decorator';
import { ActivatedRoute } from '@angular/router';
import { from, Observable } from 'rxjs';
import { NgxRouteModule } from '../../route.module';
import {TestBed} from '@angular/core/testing';
import {Component, Directive} from '@angular/core';
import {RouteParam} from './route-param.decorator';
import {ActivatedRoute} from '@angular/router';
import {from, Observable} from 'rxjs';
import {NgxRouteModule, provideNgxRoute} from '../../route.module';

describe('RouteParamDecorator', () => {

@Component({
selector: 'lib-component',
template: ''
})
class MyComponent {

@RouteParam('paramKey')
public readonly routeParam$: Observable<string>;

}

@Directive({
selector: '[libDirective]',
})
class MyDirective {

@RouteParam('paramKey')
public readonly routeParam$: Observable<string>;
}

let component: MyComponent;
let directive: MyDirective;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NgxRouteModule
],
declarations: [
MyComponent,
MyDirective
],
providers: [
MyComponent,
MyDirective,
{
provide: ActivatedRoute,
useValue: {
paramMap: from([{get: (key: string) => `${ key }:first`}])
}
}
]
@Component({
selector: 'lib-component',
template: ''
})
class MyComponent {

@RouteParam('paramKey')
public readonly routeParam$: Observable<string>;

}

@Directive({
selector: '[libDirective]',
})
class MyDirective {

@RouteParam('paramKey')
public readonly routeParam$: Observable<string>;
}

let component: MyComponent;
let directive: MyDirective;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
MyComponent,
MyDirective
],
providers: [
MyComponent,
MyDirective,
provideNgxRoute(),
{
provide: ActivatedRoute,
useValue: {
paramMap: from([{get: (key: string) => `${key}:first`}])
}
}
]
});

component = TestBed.inject(MyComponent);
directive = TestBed.inject(MyDirective);
});

component = TestBed.inject(MyComponent);
directive = TestBed.inject(MyDirective);
});
it('should subscribe to param map in components', (done: DoneFn) => {
component.routeParam$.subscribe((value: string) => {
console.log(value);
expect(value).toEqual('paramKey:first');

done();
});
});

});

describe('RouteParamDecorator injected via Module', () => {

@Component({
selector: 'lib-component',
template: ''
})
class MyComponent {

@RouteParam('paramKey')
public readonly routeParam$: Observable<string>;

}

@Directive({
selector: '[libDirective]',
})
class MyDirective {

@RouteParam('paramKey')
public readonly routeParam$: Observable<string>;
}

let component: MyComponent;
let directive: MyDirective;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NgxRouteModule
],
declarations: [
MyComponent,
MyDirective
],
providers: [
MyComponent,
MyDirective,
{
provide: ActivatedRoute,
useValue: {
paramMap: from([{get: (key: string) => `${key}:first`}])
}
}
]
});

component = TestBed.inject(MyComponent);
directive = TestBed.inject(MyDirective);
});

it('should subscribe to param map in components', (done: DoneFn) => {
component.routeParam$.subscribe((value: string) => {
expect(value).toEqual('paramKey:first');
it('should subscribe to param map in components', (done: DoneFn) => {
component.routeParam$.subscribe((value: string) => {
console.log(value);
expect(value).toEqual('paramKey:first');

done();
done();
});
});
});

});
25 changes: 18 additions & 7 deletions projects/ngx-common/src/lib/modules/route/route.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { Injector, NgModule } from '@angular/core';
import {APP_INITIALIZER, Injector, NgModule, Provider} from '@angular/core';

export function provideNgxRoute(): Provider[] {
return [
{
provide: APP_INITIALIZER,
useFactory: (injector: Injector) => (): void => {
NgxRouteModule.injector = injector;
},
multi: true,
deps: [Injector]
}
]
}

// @dynamic
@NgModule()
@NgModule({
providers: provideNgxRoute()
})
export class NgxRouteModule {

private static injector: Injector;

public constructor(injector: Injector) {
NgxRouteModule.injector = injector;
}
public static injector: Injector;

public static getInjector(): Injector {
return NgxRouteModule.injector;
Expand Down
8 changes: 4 additions & 4 deletions projects/ngx-common/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ export { handleError } from './lib/modules/error-handler/operator/handle-error.o
export { ErrorHandler } from './lib/modules/error-handler/model/error-handler.model';
export { NotImplementedError } from './lib/modules/error-handler/model/not-implemented.error';
export { RuntimeError } from './lib/modules/error-handler/model/runtime.error';
export { NgxErrorHandlerModule, ERROR_HANDLER_TOKEN } from './lib/modules/error-handler/error-handler.module';
export { NgxErrorHandlerModule, provideNgxErrorHandler, ERROR_HANDLER_TOKEN } from './lib/modules/error-handler/error-handler.module';

// local-storage
export { NgxLocalStorageModule } from './lib/modules/local-storage/local-storage.module';
export { NgxLocalStorageModule, provideNgxLocalStorage } from './lib/modules/local-storage/local-storage.module';
export { NgxLocalStorageService } from './lib/modules/local-storage/local-storage.service';

// config
export { NgxConfigModule } from './lib/modules/config/config.module';
export { NgxConfigModule, provideNgxConfig } from './lib/modules/config/config.module';
export { CONFIG_URL_TOKEN } from './lib/modules/config/config.token';
export { NgxConfigService } from './lib/modules/config/config.service';

// route
export { NgxRouteModule } from './lib/modules/route/route.module';
export { NgxRouteModule, provideNgxRoute } from './lib/modules/route/route.module';
export { RouteParam } from './lib/modules/route/decorators/route-param/route-param.decorator';
Loading