jz_ngx-cookie
Version:
Implementation of Angular 1.x $cookies service to Angular
178 lines (177 loc) • 6.65 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable } from '@angular/core';
import { CookieOptionsProvider } from './cookie-options-provider';
import { isBlank, isString, mergeOptions, safeDecodeURIComponent, safeJsonParse } from './utils';
var CookieService = (function () {
function CookieService(_optionsProvider) {
this._optionsProvider = _optionsProvider;
this.options = this._optionsProvider.options;
}
Object.defineProperty(CookieService.prototype, "cookieString", {
get: function () {
return document.cookie || '';
},
set: function (val) {
document.cookie = val;
},
enumerable: true,
configurable: true
});
/**
* @name CookieService#get
*
* @description
* Returns the value of given cookie key.
*
* @param {string} key Id to use for lookup.
* @returns {string} Raw cookie value.
*/
CookieService.prototype.get = function (key) {
return this._cookieReader()[key];
};
/**
* @name CookieService#getObject
*
* @description
* Returns the deserialized value of given cookie key.
*
* @param {string} key Id to use for lookup.
* @returns {Object} Deserialized cookie value.
*/
CookieService.prototype.getObject = function (key) {
var value = this.get(key);
return value ? safeJsonParse(value) : value;
};
/**
* @name CookieService#getAll
*
* @description
* Returns a key value object with all the cookies.
*
* @returns {Object} All cookies
*/
CookieService.prototype.getAll = function () {
return this._cookieReader();
};
/**
* @name CookieService#put
*
* @description
* Sets a value for given cookie key.
*
* @param {string} key Id for the `value`.
* @param {string} value Raw value to be stored.
* @param {CookieOptions} options (Optional) Options object.
*/
CookieService.prototype.put = function (key, value, options) {
this._cookieWriter()(key, value, options);
};
/**
* @name CookieService#putObject
*
* @description
* Serializes and sets a value for given cookie key.
*
* @param {string} key Id for the `value`.
* @param {Object} value Value to be stored.
* @param {CookieOptions} options (Optional) Options object.
*/
CookieService.prototype.putObject = function (key, value, options) {
this.put(key, JSON.stringify(value), options);
};
/**
* @name CookieService#remove
*
* @description
* Remove given cookie.
*
* @param {string} key Id of the key-value pair to delete.
* @param {CookieOptions} options (Optional) Options object.
*/
CookieService.prototype.remove = function (key, options) {
this._cookieWriter()(key, undefined, options);
};
/**
* @name CookieService#removeAll
*
* @description
* Remove all cookies.
*/
CookieService.prototype.removeAll = function () {
var _this = this;
var cookies = this.getAll();
Object.keys(cookies).forEach(function (key) {
_this.remove(key);
});
};
CookieService.prototype._cookieReader = function () {
var lastCookies = {};
var lastCookieString = '';
var cookieArray, cookie, i, index, name;
var currentCookieString = this.cookieString;
if (currentCookieString !== lastCookieString) {
lastCookieString = currentCookieString;
cookieArray = lastCookieString.split('; ');
lastCookies = {};
for (i = 0; i < cookieArray.length; i++) {
cookie = cookieArray[i];
index = cookie.indexOf('=');
if (index > 0) {
name = safeDecodeURIComponent(cookie.substring(0, index));
// the first value that is seen for a cookie is the most
// specific one. values for the same cookie name that
// follow are for less specific paths.
if (isBlank(lastCookies[name])) {
lastCookies[name] = safeDecodeURIComponent(cookie.substring(index + 1));
}
}
}
}
return lastCookies;
};
CookieService.prototype._cookieWriter = function () {
var that = this;
return function (name, value, options) {
that.cookieString = that._buildCookieString(name, value, options);
};
};
CookieService.prototype._buildCookieString = function (name, value, options) {
var opts = mergeOptions(this.options, options);
var expires = opts.expires;
if (isBlank(value)) {
expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
value = '';
}
if (isString(expires)) {
expires = new Date(expires);
}
var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
str += opts.path ? ';path=' + opts.path : '';
str += opts.domain ? ';domain=' + opts.domain : '';
str += expires ? ';expires=' + expires.toUTCString() : '';
str += opts.secure ? ';secure' : '';
// per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
// - 300 cookies
// - 20 cookies per unique domain
// - 4096 bytes per cookie
var cookieLength = str.length + 1;
if (cookieLength > 4096) {
console.log("Cookie '" + name + "' possibly not set or overflowed because it was too \n large (" + cookieLength + " > 4096 bytes)!");
}
return str;
};
return CookieService;
}());
CookieService = __decorate([
Injectable(),
__metadata("design:paramtypes", [CookieOptionsProvider])
], CookieService);
export { CookieService };