Skip to content

Commit 2ad6b72

Browse files
SkyZeroZxAndrewKushnir
authored andcommitted
refactor(http): migrate XSRF classes to use inject() function
Remove constructor injection in favor of inject() calls (cherry picked from commit 55be477)
1 parent b20bf5d commit 2ad6b72

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

packages/common/http/src/xsrf.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import {DOCUMENT, ɵparseCookieValue as parseCookieValue} from '../../index';
1010
import {
1111
EnvironmentInjector,
12-
Inject,
1312
inject,
1413
Injectable,
1514
InjectionToken,
@@ -55,6 +54,9 @@ export abstract class HttpXsrfTokenExtractor {
5554
*/
5655
@Injectable()
5756
export class HttpXsrfCookieExtractor implements HttpXsrfTokenExtractor {
57+
private readonly cookieName = inject(XSRF_COOKIE_NAME);
58+
private readonly doc = inject(DOCUMENT);
59+
5860
private lastCookieString: string = '';
5961
private lastToken: string | null = null;
6062

@@ -63,11 +65,6 @@ export class HttpXsrfCookieExtractor implements HttpXsrfTokenExtractor {
6365
*/
6466
parseCount: number = 0;
6567

66-
constructor(
67-
@Inject(DOCUMENT) private doc: any,
68-
@Inject(XSRF_COOKIE_NAME) private cookieName: string,
69-
) {}
70-
7168
getToken(): string | null {
7269
if (typeof ngServerMode !== 'undefined' && ngServerMode) {
7370
return null;
@@ -116,7 +113,7 @@ export function xsrfInterceptorFn(
116113
*/
117114
@Injectable()
118115
export class HttpXsrfInterceptor implements HttpInterceptor {
119-
constructor(private injector: EnvironmentInjector) {}
116+
private readonly injector = inject(EnvironmentInjector);
120117

121118
intercept(initialRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
122119
return runInInjectionContext(this.injector, () =>

packages/common/http/test/xsrf_spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import {DOCUMENT} from '../..';
910
import {HttpHeaders} from '../src/headers';
1011
import {HttpRequest} from '../src/request';
1112
import {
@@ -122,7 +123,15 @@ describe('HttpXsrfCookieExtractor', () => {
122123
document = {
123124
cookie: 'XSRF-TOKEN=test',
124125
};
125-
extractor = new HttpXsrfCookieExtractor(document, 'XSRF-TOKEN');
126+
TestBed.configureTestingModule({
127+
providers: [
128+
{
129+
provide: DOCUMENT,
130+
useValue: document,
131+
},
132+
],
133+
});
134+
extractor = TestBed.inject(HttpXsrfCookieExtractor);
126135
});
127136
it('parses the cookie from document.cookie', () => {
128137
expect(extractor.getToken()).toEqual('test');

0 commit comments

Comments
 (0)