Skip to content

Commit

Permalink
feat(core): switches from express REQUEST to angular Request object (#…
Browse files Browse the repository at this point in the history
…338)

* use official request token that provided by angular

* fix prettier ci

* feat(core): switches from express `REQUEST` to angular `Request` object

---------

Co-authored-by: Emre Hızlı <[email protected]>
  • Loading branch information
pavankjadda and hizliemre authored Jan 29, 2025
1 parent 39339af commit cd89593
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@angular/cli": "^19.0.0",
"@angular/compiler-cli": "^19.0.0",
"@angular/language-service": "^19.0.0",
"@types/express": "^4.17.21",
"@types/jest": "latest",
"@types/node": "latest",
"@typescript-eslint/eslint-plugin": "latest",
Expand Down
31 changes: 9 additions & 22 deletions projects/ngx-cookie-service-ssr/src/lib/ssr-cookie.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { Request } from 'express';
import { Inject, Injectable, InjectionToken, Optional, PLATFORM_ID } from '@angular/core';
import { inject, Injectable, PLATFORM_ID, REQUEST } from '@angular/core';
import { DOCUMENT, isPlatformBrowser } from '@angular/common';

// Define the `Request` token
export const REQUEST = new InjectionToken<Request>('REQUEST');

@Injectable({
providedIn: 'root',
})
export class SsrCookieService {
private readonly documentIsAccessible: boolean;

constructor(
@Inject(DOCUMENT) private document: Document,
// Get the `PLATFORM_ID` so we can check if we're in a browser.
@Inject(PLATFORM_ID) private platformId: any,
@Optional() @Inject(REQUEST) private request: Request
) {
this.documentIsAccessible = isPlatformBrowser(this.platformId);
}
private readonly document = inject(DOCUMENT);
private readonly platformId = inject(PLATFORM_ID);
private readonly request = inject(REQUEST, { optional: true });
private readonly documentIsAccessible: boolean = isPlatformBrowser(this.platformId);

/**
* Get cookie Regular Expression
Expand Down Expand Up @@ -66,7 +56,7 @@ export class SsrCookieService {
check(name: string): boolean {
name = encodeURIComponent(name);
const regExp: RegExp = SsrCookieService.getCookieRegExp(name);
return regExp.test(this.documentIsAccessible ? this.document.cookie : this.request?.headers.cookie);
return regExp.test(this.documentIsAccessible ? this.document.cookie : this.request?.headers.get('cookie'));
}

/**
Expand All @@ -81,14 +71,11 @@ export class SsrCookieService {
get(name: string): string {
if (this.check(name)) {
name = encodeURIComponent(name);

const regExp: RegExp = SsrCookieService.getCookieRegExp(name);
const result: RegExpExecArray = regExp.exec(this.documentIsAccessible ? this.document.cookie : this.request?.headers.cookie);

const result: RegExpExecArray = regExp.exec(this.documentIsAccessible ? this.document.cookie : this.request?.headers.get('cookie'));
return result[1] ? SsrCookieService.safeDecodeURIComponent(result[1]) : '';
} else {
return '';
}
return '';
}

/**
Expand All @@ -101,7 +88,7 @@ export class SsrCookieService {
*/
getAll(): { [key: string]: string } {
const cookies: { [key: string]: string } = {};
const cookieString: any = this.documentIsAccessible ? this.document?.cookie : this.request?.headers.cookie;
const cookieString: any = this.documentIsAccessible ? this.document?.cookie : this.request?.headers.get('cookie');

if (cookieString && cookieString !== '') {
cookieString.split(';').forEach((currentCookie) => {
Expand Down

0 comments on commit cd89593

Please sign in to comment.