From a4c4943a66caf252f8741f7e4eb01fce15926447 Mon Sep 17 00:00:00 2001 From: dkochergin Date: Wed, 28 Mar 2018 14:11:10 +0300 Subject: [PATCH] Make module AOT-compatible --- package-lock.json | 4 +- package.json | 1 + public_api.ts | 2 +- src/modules/auth/auth.module.ts | 49 ++++++++++--------- .../auth-config/auth-config.provider.ts | 2 + 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index ecd29fc..c831688 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "simple-angular-jwt-auth", - "version": "0.0.7", + "name": "@juztcode/angular-auth", + "version": "0.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index cdd1e91..afd3f62 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "author": "randilfernando ", "version": "0.1.1", "license": "MIT", + "typings": "angular-auth.d.ts", "scripts": { "build": "ng-packagr -p ng-package.json", "pack": "npm run build && cd ./dist && npm pack", diff --git a/public_api.ts b/public_api.ts index cb7b9ce..1b0a828 100644 --- a/public_api.ts +++ b/public_api.ts @@ -1 +1 @@ -export * from './src/modules/auth'; +export * from './src/modules/auth/index'; diff --git a/src/modules/auth/auth.module.ts b/src/modules/auth/auth.module.ts index d8f54e2..0c5c5ba 100644 --- a/src/modules/auth/auth.module.ts +++ b/src/modules/auth/auth.module.ts @@ -1,31 +1,34 @@ -import {ModuleWithProviders, NgModule} from '@angular/core'; +import {InjectionToken, NgModule} from '@angular/core'; import {HTTP_INTERCEPTORS} from '@angular/common/http'; import {TokenInterceptor} from './providers/token/token.interceptor'; -import {DEFAULT_ADDITIONAL_AUTH_CONFIG} from './constants/default.constants'; import {AuthConfigProvider} from './providers/auth-config/auth-config.provider'; -import {AuthConfig} from './types/auth-config.type'; -import {AuthConfigAdditional} from './types/auth-config-additional.type'; import {AuthProvider} from './providers/auth/auth.provider'; import {PermissionProvider} from './providers/permission/permission.provider'; -@NgModule() -export class AuthModule { - static forRoot(mainConfig: AuthConfig, additionalConfig?: AuthConfigAdditional): ModuleWithProviders { - return { - ngModule: AuthModule, - providers: [ - AuthProvider, - PermissionProvider, - { - provide: AuthConfigProvider, - useValue: new AuthConfigProvider(mainConfig, additionalConfig) - }, - { - provide: HTTP_INTERCEPTORS, - useClass: TokenInterceptor, - multi: true - } - ] - }; +export const AUTH_CONFIG = new InjectionToken('auth config'); +export const AUTH_ADDITIONAL_CONFIG = new InjectionToken('auth additional config'); + +export function getConfig(mainConfig, additionalConfig): AuthConfigProvider { + return new AuthConfigProvider(mainConfig, additionalConfig); +} + +@NgModule( + { + providers: [ + AuthProvider, + PermissionProvider, + { + provide: AuthConfigProvider, + useFactory: getConfig, + deps: [AUTH_CONFIG, AUTH_ADDITIONAL_CONFIG] + }, + { + provide: HTTP_INTERCEPTORS, + useClass: TokenInterceptor, + multi: true + } + ] } +) +export class AuthModule { } diff --git a/src/modules/auth/providers/auth-config/auth-config.provider.ts b/src/modules/auth/providers/auth-config/auth-config.provider.ts index 014bc2a..dd96c44 100644 --- a/src/modules/auth/providers/auth-config/auth-config.provider.ts +++ b/src/modules/auth/providers/auth-config/auth-config.provider.ts @@ -1,7 +1,9 @@ import {AuthConfig} from '../../types/auth-config.type'; import {AuthConfigAdditional} from '../../types/auth-config-additional.type'; import {DEFAULT_ADDITIONAL_AUTH_CONFIG} from '../../constants/default.constants'; +import {Injectable} from '@angular/core'; +@Injectable() export class AuthConfigProvider { private config: AuthConfigAdditional & AuthConfig;