refactor(common): drop platform checks in HttpXsrfCookieExtractor (#59810)

Replaces `PLATFORM_ID` checks with `ngServerMode` within the `HttpXsrfCookieExtractor`. It is not part of the public API, and thus this change should not affect consumers who may have called the constructor directly.

PR Close #59810
This commit is contained in:
arturovt 2025-01-30 23:50:48 +02:00 committed by Andrew Kushnir
parent c2c8b75d7b
commit 1e2a70c8db
3 changed files with 10 additions and 4 deletions

View file

@ -13,7 +13,6 @@ import {
inject,
Injectable,
InjectionToken,
PLATFORM_ID,
runInInjectionContext,
} from '@angular/core';
import {Observable} from 'rxjs';
@ -66,12 +65,11 @@ export class HttpXsrfCookieExtractor implements HttpXsrfTokenExtractor {
constructor(
@Inject(DOCUMENT) private doc: any,
@Inject(PLATFORM_ID) private platform: string,
@Inject(XSRF_COOKIE_NAME) private cookieName: string,
) {}
getToken(): string | null {
if (this.platform === 'server') {
if (typeof ngServerMode !== 'undefined' && ngServerMode) {
return null;
}
const cookieString = this.doc.cookie || '';

View file

@ -18,6 +18,14 @@ import {
import {HttpTestingController, provideHttpClientTesting} from '../testing';
describe('httpResource', () => {
beforeEach(() => {
globalThis['ngServerMode'] = isNode;
});
afterEach(() => {
globalThis['ngServerMode'] = undefined;
});
beforeEach(() => {
TestBed.configureTestingModule({providers: [provideHttpClient(), provideHttpClientTesting()]});
});

View file

@ -122,7 +122,7 @@ describe('HttpXsrfCookieExtractor', () => {
document = {
cookie: 'XSRF-TOKEN=test',
};
extractor = new HttpXsrfCookieExtractor(document, 'browser', 'XSRF-TOKEN');
extractor = new HttpXsrfCookieExtractor(document, 'XSRF-TOKEN');
});
it('parses the cookie from document.cookie', () => {
expect(extractor.getToken()).toEqual('test');