API-Token injiziert.
This commit is contained in:
parent
a47741490e
commit
3962b202b9
@ -1,10 +1,28 @@
|
||||
import { ApplicationConfig } from '@angular/core';
|
||||
import { provideRouter } from '@angular/router';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
import { provideClientHydration } from '@angular/platform-browser';
|
||||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||
import { provideHttpClient, withFetch } from '@angular/common/http';
|
||||
import { APP_BASE_HREF } from '@angular/common';
|
||||
import { UrlService } from '../services/url.service';
|
||||
import { API_URL } from './tokens';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [provideRouter(routes), provideClientHydration(), provideAnimationsAsync()]
|
||||
};
|
||||
providers: [
|
||||
provideRouter(routes),
|
||||
provideClientHydration(),
|
||||
provideAnimationsAsync(),
|
||||
provideHttpClient(withFetch()),
|
||||
{
|
||||
provide: APP_BASE_HREF,
|
||||
useFactory: (urlService: UrlService) => urlService.getBaseHref(),
|
||||
deps: [UrlService]
|
||||
},
|
||||
{
|
||||
provide: API_URL,
|
||||
useFactory: (urlService: UrlService) => urlService.getApiUrl(),
|
||||
deps: [UrlService]
|
||||
},
|
||||
]
|
||||
};
|
||||
@ -0,0 +1,3 @@
|
||||
import { InjectionToken } from '@angular/core';
|
||||
|
||||
export const API_URL = new InjectionToken<string>('API_URL');
|
||||
@ -4,6 +4,7 @@
|
||||
<meta charset="utf-8">
|
||||
<title>UserManagerUi</title>
|
||||
<base href="/">
|
||||
<meta name="api-url" content="/api">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { UrlService } from './url.service';
|
||||
|
||||
describe('UrlService', () => {
|
||||
let service: UrlService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(UrlService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,26 @@
|
||||
import { Injectable, Inject, inject } from '@angular/core';
|
||||
import { DOCUMENT } from '@angular/common';
|
||||
import { Meta } from '@angular/platform-browser';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UrlService {
|
||||
document: Document;
|
||||
meta: Meta;
|
||||
|
||||
constructor() {
|
||||
this.document = inject(DOCUMENT)
|
||||
this.meta = inject(Meta)
|
||||
}
|
||||
|
||||
getBaseHref(): string {
|
||||
const baseElement = this.document.querySelector('base');
|
||||
return baseElement?.getAttribute('href') || '/';
|
||||
}
|
||||
|
||||
getApiUrl(): string | null {
|
||||
const apiMetaTag = this.meta.getTag('name="api-url"');
|
||||
return apiMetaTag ? apiMetaTag.content : null;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user