From 55be47797917ec9cca0b75a06314bd9cc114f84b Mon Sep 17 00:00:00 2001 From: SkyZeroZx <73321943+SkyZeroZx@users.noreply.github.com> Date: Sat, 1 Nov 2025 17:45:34 -0500 Subject: [PATCH] refactor(http): migrate XSRF classes to use inject() function Remove constructor injection in favor of inject() calls --- packages/common/http/src/xsrf.ts | 11 ++++------- packages/common/http/test/xsrf_spec.ts | 11 ++++++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/common/http/src/xsrf.ts b/packages/common/http/src/xsrf.ts index ffb3b3b8e48..1303d6e7836 100644 --- a/packages/common/http/src/xsrf.ts +++ b/packages/common/http/src/xsrf.ts @@ -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( */ @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, next: HttpHandler): Observable> { return runInInjectionContext(this.injector, () => diff --git a/packages/common/http/test/xsrf_spec.ts b/packages/common/http/test/xsrf_spec.ts index 0db531a862a..df0267cb691 100644 --- a/packages/common/http/test/xsrf_spec.ts +++ b/packages/common/http/test/xsrf_spec.ts @@ -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');