ngx-cookie-service

  • Version 22.2.0
  • Published
  • 66.7 kB
  • 1 dependency
  • MIT license

Install

npm i ngx-cookie-service
yarn add ngx-cookie-service
pnpm add ngx-cookie-service

Overview

Angular cookie service

Index

Variables

const COOKIE_ENCODING: InjectionToken<CookieEncoding>;
const COOKIE_OPTIONS: InjectionToken<CookieOptions>;

Functions

function provideCookieEncoding

provideCookieEncoding: (encoding: CookieEncoding) => EnvironmentProviders;
  • Sets how CookieService encodes cookie values when writing them and decodes them when reading.

    export const appConfig: ApplicationConfig = {
    providers: [provideCookieEncoding('minimal')],
    };

    Parameter encoding

    'uri' (default), 'minimal', or a custom CookieCodec

    Returns

    Providers to add to the application config

    @author: Pavankumar Jadda @since: 22.2.0

function provideCookieOptions

provideCookieOptions: (options: CookieOptions) => EnvironmentProviders;
  • Sets default options for every set(), delete() and deleteAll() call of 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.

    export const appConfig: ApplicationConfig = {
    providers: [provideCookieOptions({ path: '/', secure: true, sameSite: 'Strict' })],
    };

    Parameter options

    Default cookie options

    Returns

    Providers to add to the application config

    @author: Pavankumar Jadda @since: 22.2.0

Classes

class CookieService

class CookieService {}

    constructor

    constructor();

      property ɵfac

      static ɵfac: i0.ɵɵFactoryDeclaration<CookieService, never>;

        property ɵprov

        static ɵprov: i0.ɵɵInjectableDeclaration<any>;

          method check

          check: (name: string) => boolean;
          • Return true if Document is accessible, otherwise return false

            Parameter name

            Cookie name

            Returns

            boolean - whether cookie with specified name exists

            @author: Stepan Suvorov @since: 1.0.0

          method delete

          delete: {
          (
          name: string,
          path?: CookieOptions['path'],
          domain?: CookieOptions['domain'],
          secure?: CookieOptions['secure'],
          sameSite?: SameSite
          ): void;
          (name: string, options?: DeleteCookieOptions): 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.

            Parameter name

            Cookie name

            Parameter path

            Cookie path

            Parameter domain

            Cookie domain

            Parameter secure

            Cookie secure flag

            Parameter 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 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.

            Parameter name

            Cookie name

            Parameter options

            Cookie attributes: path, domain, secure, sameSite and partitioned

            @author: Pavankumar Jadda @since: 22.1.0

          method deleteAll

          deleteAll: {
          (
          path?: CookieOptions['path'],
          domain?: CookieOptions['domain'],
          secure?: CookieOptions['secure'],
          sameSite?: SameSite
          ): void;
          (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 delete.

            Parameter path

            Cookie path

            Parameter domain

            Cookie domain

            Parameter secure

            Is the Cookie secure

            Parameter sameSite

            Is the cookie same site

            @author: Stepan Suvorov @since: 1.0.0

          • 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 delete.

            Parameter options

            Cookie attributes: path, domain, secure, sameSite and partitioned

            @author: Pavankumar Jadda @since: 22.1.0

          method get

          get: (name: string) => string;
          • Get cookies by name

            Parameter name

            Cookie name

            Returns

            property value

            @author: Stepan Suvorov @since: 1.0.0

          method getAll

          getAll: () => Record<string, string>;
          • Get all cookies in JSON format

            Returns

            all the cookies in json

            @author: Stepan Suvorov @since: 1.0.0

          method set

          set: {
          (
          name: string,
          value: string,
          expires?: CookieOptions['expires'],
          path?: CookieOptions['path'],
          domain?: CookieOptions['domain'],
          secure?: CookieOptions['secure'],
          sameSite?: SameSite,
          partitioned?: CookieOptions['partitioned']
          ): void;
          (name: string, value: string, options?: CookieOptions): void;
          };
          • Set cookie based on provided information

            Parameter name

            Cookie name

            Parameter value

            Cookie value

            Parameter expires

            Number of days until the cookies expires or an actual Date

            Parameter path

            Cookie path

            Parameter domain

            Cookie domain

            Parameter secure

            Secure flag

            Parameter partitioned

            Partitioned flag

            Parameter sameSite

            OWASP same site token Lax, None, or Strict. Defaults to Lax

            @author: Stepan Suvorov @since: 1.0.0

          • Set cookie based on provided information

            Cookie's parameters: 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

            Parameter name

            Cookie name

            Parameter value

            Cookie value

            Parameter options

            Body with cookie's params

            @author: Stepan Suvorov @since: 1.0.0

          Interfaces

          interface CookieCodec

          interface CookieCodec {}
          • A custom encoding for cookie values, used with 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

          method decode

          decode: (value: string, name: string) => string;

            method encode

            encode: (value: string, name: string) => string;

              interface CookieOptions

              interface CookieOptions {}

                property domain

                domain?: string;

                  property expires

                  expires?: number | Date;

                    property partitioned

                    partitioned?: boolean;

                      property path

                      path?: string;

                        property sameSite

                        sameSite?: SameSite;

                          property secure

                          secure?: boolean;

                            Type Aliases

                            type CookieEncoding

                            type CookieEncoding = 'uri' | 'minimal' | CookieCodec;
                            • How 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 CookieCodec for a custom scheme.

                              @author: Pavankumar Jadda @since: 22.2.0

                            type DeleteCookieOptions

                            type DeleteCookieOptions = Omit<CookieOptions, 'expires'>;
                            • Attributes that identify the cookie to delete. They must match the attributes the cookie was set with.

                            type SameSite

                            type SameSite = 'Lax' | 'None' | 'Strict';

                              Package Files (1)

                              Dependencies (1)

                              Dev Dependencies (0)

                              No dev dependencies.

                              Peer Dependencies (2)

                              Badge

                              To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                              You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/ngx-cookie-service.

                              • Markdown
                                [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/ngx-cookie-service)
                              • HTML
                                <a href="https://www.jsdocs.io/package/ngx-cookie-service"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>