UNPKG

ngx-cookie-service

Version:
213 lines (208 loc) 7.55 kB
import { isPlatformBrowser } from '@angular/common'; import * as i0 from '@angular/core'; import { inject, DOCUMENT, PLATFORM_ID, Injectable } from '@angular/core'; class CookieService { constructor() { this.document = inject(DOCUMENT); this.platformId = inject(PLATFORM_ID); this.documentIsAccessible = isPlatformBrowser(this.platformId); } /** * Get cookie Regular Expression * * @param name Cookie name * @returns property RegExp * * @author: Stepan Suvorov * @since: 1.0.0 */ static getCookieRegExp(name) { const escapedName = name.replace(/([[\]{}()|=;+?,.*^$\\])/gi, '\\$1'); return new RegExp('(?:^' + escapedName + '|;\\s*' + escapedName + ')=(.*?)(?:;|$)'); } /** * 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 */ static safeDecodeURIComponent(encodedURIComponent) { try { return decodeURIComponent(encodedURIComponent); } catch { // probably it is not uri encoded. return as is return encodedURIComponent; } } /** * 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) { if (!this.documentIsAccessible) { return false; } name = encodeURIComponent(name); const regExp = CookieService.getCookieRegExp(name); return regExp.test(this.document.cookie); } /** * Get cookies by name * * @param name Cookie name * @returns property value * * @author: Stepan Suvorov * @since: 1.0.0 */ get(name) { if (this.check(name)) { name = encodeURIComponent(name); const regExp = CookieService.getCookieRegExp(name); const result = regExp.exec(this.document.cookie); return result?.[1] ? CookieService.safeDecodeURIComponent(result[1]) : ''; } else { return ''; } } /** * Get all cookies in JSON format * * @returns all the cookies in json * * @author: Stepan Suvorov * @since: 1.0.0 */ getAll() { if (!this.documentIsAccessible) { return {}; } const cookies = {}; const document = this.document; if (document.cookie && document.cookie !== '') { document.cookie.split(';').forEach((currentCookie) => { const [cookieName, cookieValue] = currentCookie.split('='); cookies[CookieService.safeDecodeURIComponent(cookieName.replace(/^ /, ''))] = CookieService.safeDecodeURIComponent(cookieValue); }); } return cookies; } set(name, value, expiresOrOptions, path, domain, secure, sameSite, partitioned) { if (!this.documentIsAccessible) { return; } if (typeof expiresOrOptions === 'number' || expiresOrOptions instanceof Date || path || domain || secure || sameSite) { const optionsBody = { expires: expiresOrOptions, path, domain, secure, sameSite: sameSite || 'Lax', partitioned, }; this.set(name, value, optionsBody); return; } let cookieString = encodeURIComponent(name) + '=' + encodeURIComponent(value) + ';'; const options = expiresOrOptions ? expiresOrOptions : {}; if (options.expires) { if (typeof options.expires === 'number') { const dateExpires = new Date(new Date().getTime() + options.expires * 1000 * 60 * 60 * 24); cookieString += 'Expires=' + dateExpires.toUTCString() + ';'; } else { cookieString += 'Expires=' + options.expires.toUTCString() + ';'; } } if (options.path) { cookieString += 'Path=' + options.path + ';'; } if (options.domain) { cookieString += 'Domain=' + options.domain + ';'; } if (options.secure === false && options.sameSite === 'None') { options.secure = true; console.warn(`[ngx-cookie-service] Cookie ${name} was forced with secure flag because sameSite=None.` + `More details : https://github.com/stevermeister/ngx-cookie-service/issues/86#issuecomment-597720130`); } if (options.secure) { cookieString += 'Secure;'; } if (!options.sameSite) { options.sameSite = 'Lax'; } cookieString += 'SameSite=' + options.sameSite + ';'; if (options.partitioned) { cookieString += 'Partitioned;'; } this.document.cookie = cookieString; } /** * 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, path, domain, secure, sameSite = 'Lax') { if (!this.documentIsAccessible) { return; } const expiresDate = new Date('Thu, 01 Jan 1970 00:00:01 GMT'); this.set(name, '', { expires: expiresDate, path, domain, secure, sameSite }); } /** * 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, domain, secure, sameSite = 'Lax') { if (!this.documentIsAccessible) { return; } const cookies = this.getAll(); for (const cookieName in cookies) { if (cookies.hasOwnProperty(cookieName)) { this.delete(cookieName, path, domain, secure, sameSite); } } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: CookieService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: CookieService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: CookieService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [] }); /* * Public API Surface of ngx-cookie-service */ /** * Generated bundle index. Do not edit. */ export { CookieService }; //# sourceMappingURL=ngx-cookie-service.mjs.map