refactor(http): migrate XSRF classes to use inject() function

Remove constructor injection in favor of inject() calls
This commit is contained in:
SkyZeroZx
2025-11-01 17:45:34 -05:00
committed by Andrew Kushnir
parent 7724a9460d
commit 55be477979
2 changed files with 14 additions and 8 deletions
+4 -7
View File
@@ -9,7 +9,6 @@
import {DOCUMENT, ɵparseCookieValue as parseCookieValue} from '../../index';
import {
EnvironmentInjector,
Inject,
inject,
Injectable,
InjectionToken,
@@ -52,6 +51,9 @@ export const XSRF_HEADER_NAME = new InjectionToken<string>(
*/
@Injectable({providedIn: 'root'})
export class HttpXsrfCookieExtractor implements HttpXsrfTokenExtractor {
private readonly cookieName = inject(XSRF_COOKIE_NAME);
private readonly doc = inject(DOCUMENT);
private lastCookieString: string = '';
private lastToken: string | null = null;
@@ -60,11 +62,6 @@ export class HttpXsrfCookieExtractor implements HttpXsrfTokenExtractor {
*/
parseCount: number = 0;
constructor(
@Inject(DOCUMENT) private doc: any,
@Inject(XSRF_COOKIE_NAME) private cookieName: string,
) {}
getToken(): string | null {
if (typeof ngServerMode !== 'undefined' && ngServerMode) {
return null;
@@ -128,7 +125,7 @@ export function xsrfInterceptorFn(
*/
@Injectable()
export class HttpXsrfInterceptor implements HttpInterceptor {
constructor(private injector: EnvironmentInjector) {}
private readonly injector = inject(EnvironmentInjector);
intercept(initialRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return runInInjectionContext(this.injector, () =>
+10 -1
View File
@@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.dev/license
*/
import {DOCUMENT} from '../..';
import {HttpHeaders} from '../src/headers';
import {HttpRequest} from '../src/request';
import {
@@ -122,7 +123,15 @@ describe('HttpXsrfCookieExtractor', () => {
document = {
cookie: 'XSRF-TOKEN=test',
};
extractor = new HttpXsrfCookieExtractor(document, 'XSRF-TOKEN');
TestBed.configureTestingModule({
providers: [
{
provide: DOCUMENT,
useValue: document,
},
],
});
extractor = TestBed.inject(HttpXsrfCookieExtractor);
});
it('parses the cookie from document.cookie', () => {
expect(extractor.getToken()).toEqual('test');