ngx-cookie-service
Version:
Angular cookie service
335 lines • 12.2 kB
TypeScript
import * as i0 from "@angular/core";
import { EnvironmentProviders, InjectionToken } from "@angular/core";
export type SameSite = 'Lax' | 'None' | 'Strict';
export interface CookieOptions {
expires?: number | Date;
path?: string;
domain?: string;
secure?: boolean;
sameSite?: SameSite;
partitioned?: boolean;
}
/**
* Attributes that identify the cookie to delete. They must match the attributes the cookie was set with.
*/
export type DeleteCookieOptions = Omit<CookieOptions, 'expires'>;
export declare class CookieService {
private readonly documentIsAccessible;
private readonly document;
private readonly platformId;
private readonly defaultOptions;
private readonly codec;
constructor();
/**
* Get cookie Regular Expression
*
* @param name Cookie name
* @returns property RegExp
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
private static getCookieRegExp;
/**
* Gets the decoded version of an encoded component of a Uniform Resource Identifier (URI).
*
* @param encodedURIComponent A value representing an encoded URI component.
*
* @returns The decoded version of an encoded component of a Uniform Resource Identifier (URI).
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
private static safeDecodeURIComponent;
/**
* 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
*
* @param name Cookie name
* @param value Cookie value
* @param expires Number of days until the cookies expires or an actual `Date`
* @param path Cookie path
* @param domain Cookie domain
* @param secure Secure flag
* @param partitioned Partitioned flag
* @param sameSite OWASP same site token `Lax`, `None`, or `Strict`. Defaults to `Lax`
*
* @author: Stepan Suvorov
* @since: 1.0.0
*/
set(name: string, value: string, expires?: CookieOptions['expires'], path?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite?: SameSite, partitioned?: CookieOptions['partitioned']): void;
/**
* 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 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 `path` is not specified, no `Path` attribute is written and the browser uses its default path: the directory of the URL
* the page was loaded at (not the current router URL). To delete a cookie that was set with `path: '/'`, pass `'/'` here too.
*
* @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?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite?: SameSite): void;
/**
* Delete cookie by name. The options must match the attributes the cookie was set with: `path` and `domain` select the
* cookie, and `partitioned: true` is required to delete a Partitioned cookie.
*
* If `path` is not specified, no `Path` attribute is written and the browser uses its default path: the directory of the URL
* the page was loaded at (not the current router URL). To delete a cookie that was set with `path: '/'`, pass `'/'` here too.
*
* @param name Cookie name
* @param options Cookie attributes: `path`, `domain`, `secure`, `sameSite` and `partitioned`
*
* @author: Pavankumar Jadda
* @since: 22.1.0
*/
delete(name: string, options?: DeleteCookieOptions): void;
/**
* Delete all cookies at given path and domain.
*
* If `path` is not specified, the browser's default path is used for each cookie, as described for {@link delete}.
*
* @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?: CookieOptions['path'], domain?: CookieOptions['domain'], secure?: CookieOptions['secure'], sameSite?: SameSite): void;
/**
* Delete all cookies that can currently be accessed, using the same attributes for each one.
*
* If `path` is not specified, the browser's default path is used for each cookie, as described for {@link delete}.
*
* @param options Cookie attributes: `path`, `domain`, `secure`, `sameSite` and `partitioned`
*
* @author: Pavankumar Jadda
* @since: 22.1.0
*/
deleteAll(options?: DeleteCookieOptions): void;
/**
* Built-in `'uri'` encoding: `encodeURIComponent`, decoded with {@link CookieService.safeDecodeURIComponent}.
*/
private static readonly uriCodec;
/**
* Built-in `'minimal'` encoding: values made only of cookie-octets are written as-is, others use `encodeURIComponent`.
*/
private static readonly minimalCodec;
/**
* Resolves the encoding provided with {@link provideCookieEncoding} to a codec, defaulting to `'uri'`.
*
* @param encoding Provided encoding, or `null` when none was provided
* @returns The codec to use
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
private static toCodec;
/**
* Encodes a cookie value with the configured encoding. Returns `undefined`, with a warning, when the result can't be
* written safely because it isn't a string or contains `;` or a control character.
*
* @param name Cookie name
* @param value Cookie value
* @returns The encoded value, or `undefined` to skip the write
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
private encodeValue;
/**
* Decodes a cookie value with the configured encoding. If a custom `decode` throws, the raw value is returned.
*
* @param value Raw cookie value
* @param name Decoded cookie name
* @returns The decoded value
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
private decodeValue;
/**
* Merges the app-wide defaults from {@link provideCookieOptions} under the options of one call.
*
* Values passed to the call win, and `undefined` or `null` values don't override a default.
*
* @param defaults Default cookie options
* @param options Options passed to the call
* @returns A new, merged CookieOptions object
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
private static withDefaults;
/**
* Returns a validated copy of the options, or `undefined` if the cookie must not be written.
*
* A `path` or `domain` containing `;` or a control character could inject attributes or change the cookie's scope, so the
* whole write is skipped. An invalid `expires` is dropped, which makes a session cookie as browsers already did, and an
* unknown `sameSite` falls back to the `Lax` default. Each case logs a warning. A numeric `expires` (days
* from now) is converted to a `Date`, and `sameSite` is matched case-insensitively.
*
* @param name Cookie name, used in warnings
* @param options Cookie options
* @returns A validated copy of the options, or `undefined` to skip the write
*
* @author: Pavankumar Jadda
* @since: 22.1.0
*/
private static validateOptions;
/**
* Whether a cookie attribute value contains `;` or an ASCII control character, which would end the attribute early or
* corrupt the header.
*
* @param value Attribute value
* @returns `true` if the value is unsafe to write
*
* @author: Pavankumar Jadda
* @since: 22.1.0
*/
private static hasUnsafeCharacter;
/**
* Builds delete options from either an options object or the positional `path`, `domain`, `secure` and `sameSite` arguments.
*
* @param pathOrOptions Either a DeleteCookieOptions object or the cookie path
* @param domain Cookie domain
* @param secure Cookie secure flag
* @param sameSite Cookie sameSite flag
* @returns A new DeleteCookieOptions object
*
* @author: Pavankumar Jadda
* @since: 22.1.0
*/
private static toDeleteOptions;
static ɵfac: i0.ɵɵFactoryDeclaration<CookieService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<any>;
}
/**
* App-wide default options for {@link CookieService}. Provide them with {@link provideCookieOptions}.
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
export declare const COOKIE_OPTIONS: InjectionToken<CookieOptions>;
/**
* Sets default options for every `set()`, `delete()` and `deleteAll()` call of {@link CookieService}.
*
* Options passed to a call win over these defaults, and `undefined` values don't override them. A call that passes its own
* `expires` replaces the default one, so `delete()` always expires the cookie.
*
* ```ts
* export const appConfig: ApplicationConfig = {
* providers: [provideCookieOptions({ path: '/', secure: true, sameSite: 'Strict' })],
* };
* ```
*
* @param options Default cookie options
* @returns Providers to add to the application config
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
export declare function provideCookieOptions(options: CookieOptions): EnvironmentProviders;
/**
* A custom encoding for cookie values, used with {@link provideCookieEncoding}.
*
* `encode` runs on every value written by `set()`, and `decode` on every value read by `get()` and `getAll()`. Cookie names
* keep the default encoding. If `decode` throws, the raw value is returned instead.
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
export interface CookieCodec {
encode(value: string, name: string): string;
decode(value: string, name: string): string;
}
/**
* How {@link CookieService} encodes cookie values:
*
* - `'uri'` (default): `encodeURIComponent` and `decodeURIComponent`.
* - `'minimal'`: values made only of characters allowed in a cookie value (RFC 6265 cookie-octets, except `%`) are written
* as-is, and other values are encoded with `encodeURIComponent`. This is the default of the jshttp `cookie` package used
* by Express, and keeps values such as JWTs and base64 readable by servers that don't URL-decode cookies.
* - A {@link CookieCodec} for a custom scheme.
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
export type CookieEncoding = 'uri' | 'minimal' | CookieCodec;
/**
* The cookie value encoding for {@link CookieService}. Provide it with {@link provideCookieEncoding}.
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
export declare const COOKIE_ENCODING: InjectionToken<CookieEncoding>;
/**
* Sets how {@link CookieService} encodes cookie values when writing them and decodes them when reading.
*
* ```ts
* export const appConfig: ApplicationConfig = {
* providers: [provideCookieEncoding('minimal')],
* };
* ```
*
* @param encoding `'uri'` (default), `'minimal'`, or a custom {@link CookieCodec}
* @returns Providers to add to the application config
*
* @author: Pavankumar Jadda
* @since: 22.2.0
*/
export declare function provideCookieEncoding(encoding: CookieEncoding): EnvironmentProviders;