Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 650af71

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 9db114f commit 650af71

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,
@@ -52,6 +51,9 @@ export const XSRF_HEADER_NAME = new InjectionToken<string>(
5251
*/
5352
@Injectable({providedIn: 'root'})
5453
export class HttpXsrfCookieExtractor implements HttpXsrfTokenExtractor {
54+
private readonly cookieName = inject(XSRF_COOKIE_NAME);
55+
private readonly doc = inject(DOCUMENT);
56+
5557
private lastCookieString: string = '';
5658
private lastToken: string | null = null;
5759

@@ -60,11 +62,6 @@ export class HttpXsrfCookieExtractor implements HttpXsrfTokenExtractor {
6062
*/
6163
parseCount: number = 0;
6264

63-
constructor(
64-
@Inject(DOCUMENT) private doc: any,
65-
@Inject(XSRF_COOKIE_NAME) private cookieName: string,
66-
) {}
67-
6865
getToken(): string | null {
6966
if (typeof ngServerMode !== 'undefined' && ngServerMode) {
7067
return null;
@@ -128,7 +125,7 @@ export function xsrfInterceptorFn(
128125
*/
129126
@Injectable()
130127
export class HttpXsrfInterceptor implements HttpInterceptor {
131-
constructor(private injector: EnvironmentInjector) {}
128+
private readonly injector = inject(EnvironmentInjector);
132129

133130
intercept(initialRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
134131
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)