-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ngx-api-utils): refactor the
NgxApiModule
and remove `forR…
…oot` as it breaks The error shown is: ``` ERROR in Error during template compile of AppModule Function calls are not supported in decorators but NgxApiUtilsModule was called. ``` refs angular/angular#23609 ng-packagr/ng-packagr#767
- Loading branch information
Showing
14 changed files
with
111 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
packages/ngx-api-utils/src/lib/api-auth-guard/api-auth-guard-url-for-authenticated.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const API_AUTH_GUARD_URL_FOR_AUTHENTICATED = new InjectionToken<string>('API_AUTH_GUARD_URL_FOR_AUTHENTICATED'); | ||
export const API_AUTH_GUARD_URL_FOR_AUTHENTICATED = new InjectionToken<string>('API_AUTH_GUARD_URL_FOR_AUTHENTICATED', { | ||
providedIn: 'root', | ||
factory: () => '/' | ||
}); |
5 changes: 4 additions & 1 deletion
5
packages/ngx-api-utils/src/lib/api-auth-guard/api-auth-guard-url-for-authentication.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const API_AUTH_GUARD_URL_FOR_AUTHENTICATION = new InjectionToken<string>('API_AUTH_GUARD_URL_FOR_AUTHENTICATION'); | ||
export const API_AUTH_GUARD_URL_FOR_AUTHENTICATION = new InjectionToken<string>('API_AUTH_GUARD_URL_FOR_AUTHENTICATION', { | ||
providedIn: 'root', | ||
factory: () => '/login' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const API_HTTP_BASE_URL = new InjectionToken<string>('API_HTTP_BASE_URL'); | ||
export const API_HTTP_BASE_URL = new InjectionToken<string>('API_HTTP_BASE_URL', { | ||
providedIn: 'root', | ||
factory: () => '/api' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
packages/ngx-api-utils/src/lib/auth-token/auth-token-auto-remove.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const AUTH_TOKEN_AUTO_REMOVE = new InjectionToken<string>('AUTH_TOKEN_AUTO_REMOVE'); | ||
export const AUTH_TOKEN_AUTO_REMOVE = new InjectionToken<boolean>('AUTH_TOKEN_AUTO_REMOVE', { | ||
providedIn: 'root', | ||
factory: () => true | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const AUTH_TOKEN_NAME = new InjectionToken<string>('AUTH_TOKEN_NAME'); | ||
export const AUTH_TOKEN_NAME = new InjectionToken<string>('AUTH_TOKEN_NAME', { | ||
providedIn: 'root', | ||
factory: () => 'id_token' | ||
}); |
119 changes: 15 additions & 104 deletions
119
packages/ngx-api-utils/src/lib/ngx-api-utils.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,28 @@ | ||
import { NgModule, Optional, SkipSelf, ModuleWithProviders, InjectionToken } from '@angular/core'; | ||
import { HttpHeaders, HttpClientModule, HttpInterceptor } from '@angular/common/http'; | ||
import { AUTH_TOKEN_NAME, AUTH_TOKEN_AUTO_REMOVE } from './auth-token/public_api'; | ||
import { NgModule } from '@angular/core'; | ||
import { HttpClientModule } from '@angular/common/http'; | ||
import { | ||
API_HTTP_BASE_URL, | ||
API_HTTP_DEFAULT_HEADERS, | ||
API_HTTP_AUTHORIZATION_HEADER_NAME, | ||
API_HTTP_AUTHORIZATION_HEADER_TOKEN_TYPE_PREFIX, | ||
API_HTTP_INTERCEPTORS_INJECTION_TOKEN, | ||
API_HTTP_INTERCEPTORS, | ||
ApiBaseUrlInterceptor, | ||
ApiDefaultHeadersInterceptor, | ||
ApiAuthorizationHeaderInterceptor | ||
} from './api-http/public_api'; | ||
import { | ||
API_AUTH_GUARD_PUBLIC_ONLY_ROUTES, | ||
API_AUTH_GUARD_URL_FOR_AUTHENTICATED, | ||
API_AUTH_GUARD_URL_FOR_AUTHENTICATION | ||
} from './api-auth-guard/public_api'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
HttpClientModule | ||
], | ||
declarations: [], | ||
exports: [] | ||
}) | ||
export class NgxApiUtilsModule { | ||
static forRoot( | ||
config?: { | ||
baseUrl?: string, | ||
authTokenName?: string, | ||
authTokenAutoRemove?: boolean, | ||
defaultHeaders?: HttpHeaders | string | { [name: string]: string | string[]; }, | ||
authorizationHeaderName?: string, | ||
authorizationHeaderTokenTypePrefix?: string, | ||
interceptorsInjectionToken?: InjectionToken<InjectionToken<HttpInterceptor[]>>, | ||
authGuardPublicOnlyRoutes?: RegExp, | ||
authGuardUrlForAuthenticated?: string, | ||
authGuardUrlForAuthentication?: string | ||
exports: [], | ||
providers: [ | ||
{ | ||
provide: API_HTTP_INTERCEPTORS, useExisting: ApiBaseUrlInterceptor, multi: true | ||
}, | ||
{ | ||
provide: API_HTTP_INTERCEPTORS, useExisting: ApiDefaultHeadersInterceptor, multi: true | ||
}, | ||
{ | ||
provide: API_HTTP_INTERCEPTORS, useExisting: ApiAuthorizationHeaderInterceptor, multi: true | ||
} | ||
): ModuleWithProviders { | ||
config = { | ||
baseUrl: '/api', | ||
defaultHeaders: { | ||
'accept': 'application/json, */*' | ||
}, | ||
authTokenName: 'id_token', | ||
authorizationHeaderName: 'Authorization', | ||
authorizationHeaderTokenTypePrefix: 'Bearer ', | ||
authGuardUrlForAuthenticated: '/', | ||
authGuardUrlForAuthentication: '/login', | ||
...config, | ||
}; | ||
return { | ||
ngModule: NgxApiUtilsModule, | ||
providers: [ | ||
{ | ||
provide: AUTH_TOKEN_NAME, | ||
useValue: config.authTokenName | ||
}, | ||
{ | ||
provide: AUTH_TOKEN_AUTO_REMOVE, | ||
useValue: config.authTokenAutoRemove | ||
}, | ||
{ | ||
provide: API_HTTP_BASE_URL, | ||
useValue: config.baseUrl | ||
}, | ||
{ | ||
provide: API_HTTP_DEFAULT_HEADERS, | ||
useValue: config.defaultHeaders | ||
}, | ||
{ | ||
provide: API_HTTP_AUTHORIZATION_HEADER_NAME, | ||
useValue: config.authorizationHeaderName | ||
}, | ||
{ | ||
provide: API_HTTP_AUTHORIZATION_HEADER_TOKEN_TYPE_PREFIX, | ||
useValue: config.authorizationHeaderTokenTypePrefix | ||
}, | ||
{ | ||
provide: API_HTTP_INTERCEPTORS_INJECTION_TOKEN, | ||
useValue: config.interceptorsInjectionToken || API_HTTP_INTERCEPTORS | ||
}, | ||
{ | ||
provide: API_HTTP_INTERCEPTORS, useClass: ApiBaseUrlInterceptor, multi: true | ||
}, | ||
{ | ||
provide: API_HTTP_INTERCEPTORS, useClass: ApiDefaultHeadersInterceptor, multi: true | ||
}, | ||
{ | ||
provide: API_HTTP_INTERCEPTORS, useClass: ApiAuthorizationHeaderInterceptor, multi: true | ||
}, | ||
{ | ||
provide: API_AUTH_GUARD_PUBLIC_ONLY_ROUTES, useValue: config.authGuardPublicOnlyRoutes | ||
}, | ||
{ | ||
provide: API_AUTH_GUARD_URL_FOR_AUTHENTICATED, useValue: config.authGuardUrlForAuthenticated | ||
}, | ||
{ | ||
provide: API_AUTH_GUARD_URL_FOR_AUTHENTICATION, useValue: config.authGuardUrlForAuthentication | ||
} | ||
] | ||
}; | ||
} | ||
|
||
constructor( | ||
@Optional() @SkipSelf() parentModule?: NgxApiUtilsModule, | ||
) { | ||
// throw in case someone wrongly imports this module twice | ||
if (parentModule) { | ||
throw new Error( | ||
'NgxApiUtilsModule is already loaded. Import it in the AppModule only'); | ||
} | ||
} | ||
} | ||
] | ||
}) | ||
export class NgxApiUtilsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters