ngx-cookie-service-ssr
Version:
Angular SSR cookie service
174 lines (171 loc) • 5.47 kB
TypeScript
import * as i0 from '@angular/core';
type SameSite = 'Lax' | 'None' | 'Strict';
interface CookieOptions {
expires?: number | Date;
path?: string;
domain?: string;
secure?: boolean;
sameSite?: SameSite;
partitioned?: boolean;
httpOnly?: boolean;
maxAge?: number;
}
declare class SsrCookieService {
private readonly document;
private readonly platformId;
private readonly request;
private readonly responseInit;
private readonly documentIsAccessible;
/**
* Get cookie Regular Expression
*
* @param name Cookie name
* @returns property RegExp
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
static getCookieRegExp(name: string): RegExp;
/**
* Gets the unencoded version of an encoded component of a Uniform Resource Identifier (URI).
*
* @param encodedURIComponent A value representing an encoded URI component.
*
* @returns The unencoded version of an encoded component of a Uniform Resource Identifier (URI).
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
static safeDecodeURIComponent(encodedURIComponent: string): string;
/**
* Return `true` if {@link Document} is accessible, otherwise return `false`
*
* @param name Cookie name
* @returns boolean - whether cookie with specified name exists
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
check(name: string): boolean;
/**
* Get cookies by name
*
* @param name Cookie name
* @returns property value
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
get(name: string): string;
/**
* Get all cookies in JSON format
*
* @returns all the cookies in json
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
getAll(): Record<string, string>;
/**
* Set cookie based on provided information
*
* Cookie's parameters:
* <pre>
* expires Number of days until the cookies expires or an actual `Date`
* path Cookie path
* domain Cookie domain
* secure Cookie secure flag
* sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`
* </pre>
*
* @param name Cookie name
* @param value Cookie value
* @param options Body with cookie's params
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
set(name: string, value: string, options?: CookieOptions): void;
/**
* Delete cookie by name at given path and domain. If not path is not specified, cookie at '/' path will be deleted.
*
* @param name Cookie name
* @param path Cookie path
* @param domain Cookie domain
* @param secure Cookie secure flag
* @param sameSite Cookie sameSite flag - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
delete(name: string, path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'None' | 'Strict'): void;
/**
* Delete all cookies at given path and domain. If not path is not specified, all cookies at '/' path will be deleted.
*
* @param path Cookie path
* @param domain Cookie domain
* @param secure Is the Cookie secure
* @param sameSite Is the cookie same site
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
deleteAll(path?: string, domain?: string, secure?: boolean, sameSite?: 'Lax' | 'None' | 'Strict'): void;
/**
* Helper method to safely get cookies from request object
* Handles both Angular's REQUEST interface (Web API Request) and Express's req interface
*/
private getRequestCookies;
/**
* Helper method to set cookies on the server response
*
* @param name Cookie name
* @param value Cookie value
* @param options Cookie options
*
* @author: Pavankumar Jadda
* @since: 21.2.0
*/
private setServerCookie;
/**
* Normalizes cookie options from either an options object or individual parameters
*
* @param expiresOrOptions Either a CookieOptions object, expiration (number of days or Date), or undefined
* @param path Cookie path
* @param domain Cookie domain
* @param secure Cookie secure flag
* @param sameSite Cookie sameSite flag
* @param partitioned Cookie partitioned flag
* @returns Normalized CookieOptions object
*
* @author: Pavankumar Jadda
* @since: 21.2.0
*/
private normalizeOptions;
/**
* Builds a cookie string from name, value, and options
*
* @param name Cookie name
* @param value Cookie value
* @param options Cookie options
* @returns Formatted cookie string
*
* @author: Pavankumar Jadda
* @since: 21.2.0
*/
private buildCookieString;
/**
* Appends a Set-Cookie header to the response
* Handles different header formats (Headers object, array, or plain object)
*
* @param cookieString The formatted cookie string to append
*
* @author: Pavankumar Jadda
* @since: 21.2.0
*/
private appendSetCookieHeader;
static ɵfac: i0.ɵɵFactoryDeclaration<SsrCookieService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<SsrCookieService>;
}
export { SsrCookieService };
export type { CookieOptions, SameSite };