UNPKG

ngx-cookie-service

Version:

an (aot ready) angular (4.2+) cookie service

144 lines 5.21 kB
// This service is based on the `ng2-cookies` package which sadly is not a service and does // not use `DOCUMENT` injection and therefore doesn't work well with AoT production builds. // Package: https://github.com/BCJTI/ng2-cookies import { Injectable, Inject, PLATFORM_ID, InjectionToken } from '@angular/core'; import { DOCUMENT, isPlatformBrowser } from '@angular/common'; var CookieService = (function () { function CookieService( // The type `Document` may not be used here. Although a fix is on its way, // we will go with `any` for now to support Angular 2.4.x projects. // Issue: https://github.com/angular/angular/issues/12631 // Fix: https://github.com/angular/angular/pull/14894 document, // Get the `PLATFORM_ID` so we can check if we're in a browser. platformId) { this.document = document; this.platformId = platformId; this.documentIsAccessible = isPlatformBrowser(this.platformId); } /** * @param name Cookie name * @returns {boolean} */ CookieService.prototype.check = function (name) { if (!this.documentIsAccessible) { return false; } name = encodeURIComponent(name); var regExp = this.getCookieRegExp(name); var exists = regExp.test(this.document.cookie); return exists; }; /** * @param name Cookie name * @returns {any} */ CookieService.prototype.get = function (name) { if (this.documentIsAccessible && this.check(name)) { name = encodeURIComponent(name); var regExp = this.getCookieRegExp(name); var result = regExp.exec(this.document.cookie); return decodeURIComponent(result[1]); } else { return ''; } }; /** * @returns {} */ CookieService.prototype.getAll = function () { if (!this.documentIsAccessible) { return {}; } var cookies = {}; var document = this.document; if (document.cookie && document.cookie !== '') { var split = document.cookie.split(';'); for (var i = 0; i < split.length; i += 1) { var currentCookie = split[i].split('='); currentCookie[0] = currentCookie[0].replace(/^ /, ''); cookies[decodeURIComponent(currentCookie[0])] = decodeURIComponent(currentCookie[1]); } } return cookies; }; /** * @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 */ CookieService.prototype.set = function (name, value, expires, path, domain, secure) { if (!this.documentIsAccessible) { return; } var cookieString = encodeURIComponent(name) + '=' + encodeURIComponent(value) + ';'; if (expires) { if (typeof expires === 'number') { var dateExpires = new Date(new Date().getTime() + expires * 1000 * 60 * 60 * 24); cookieString += 'expires=' + dateExpires.toUTCString() + ';'; } else { cookieString += 'expires=' + expires.toUTCString() + ';'; } } if (path) { cookieString += 'path=' + path + ';'; } if (domain) { cookieString += 'domain=' + domain + ';'; } if (secure) { cookieString += 'secure;'; } this.document.cookie = cookieString; }; /** * @param name Cookie name * @param path Cookie path * @param domain Cookie domain */ CookieService.prototype.delete = function (name, path, domain) { if (!this.documentIsAccessible) { return; } this.set(name, '', new Date('Thu, 01 Jan 1970 00:00:01 GMT'), path, domain); }; /** * @param path Cookie path * @param domain Cookie domain */ CookieService.prototype.deleteAll = function (path, domain) { if (!this.documentIsAccessible) { return; } var cookies = this.getAll(); for (var cookieName in cookies) { if (cookies.hasOwnProperty(cookieName)) { this.delete(cookieName, path, domain); } } }; /** * @param name Cookie name * @returns {RegExp} */ CookieService.prototype.getCookieRegExp = function (name) { var escapedName = name.replace(/([\[\]\{\}\(\)\|\=\;\+\?\,\.\*\^\$])/ig, '\\$1'); return new RegExp('(?:^' + escapedName + '|;\\s*' + escapedName + ')=(.*?)(?:;|$)', 'g'); }; return CookieService; }()); export { CookieService }; CookieService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ CookieService.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] },] }, { type: InjectionToken, decorators: [{ type: Inject, args: [PLATFORM_ID,] },] }, ]; }; //# sourceMappingURL=cookie.service.js.map