UNPKG

@martyganz/ngx-cookie

Version:

Implementation of Angular 1.x $cookies service to Angular

586 lines (575 loc) 16.7 kB
import { InjectionToken, Injectable, Inject, Injector, NgModule } from '@angular/core'; import { APP_BASE_HREF } from '@angular/common'; import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens'; /** * @fileoverview added by tsickle * Generated from: lib/utils.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @param {?} obj * @return {?} */ function isBlank(obj) { return obj === undefined || obj === null; } /** * @param {?} obj * @return {?} */ function isPresent(obj) { return obj !== undefined && obj !== null; } /** * @param {?} obj * @return {?} */ function isString(obj) { return typeof obj === 'string'; } /** * @param {?} oldOptions * @param {?=} newOptions * @return {?} */ function mergeOptions(oldOptions, newOptions) { if (!newOptions) { return oldOptions; } return { path: isPresent(newOptions.path) ? newOptions.path : oldOptions.path, domain: isPresent(newOptions.domain) ? newOptions.domain : oldOptions.domain, expires: isPresent(newOptions.expires) ? newOptions.expires : oldOptions.expires, secure: isPresent(newOptions.secure) ? newOptions.secure : oldOptions.secure, httpOnly: isPresent(newOptions.httpOnly) ? newOptions.httpOnly : oldOptions.httpOnly, storeUnencoded: isPresent(newOptions.storeUnencoded) ? newOptions.storeUnencoded : oldOptions.storeUnencoded, }; } /** * @param {?} str * @return {?} */ function safeDecodeURIComponent(str) { try { return decodeURIComponent(str); } catch (e) { return str; } } /** * @param {?} str * @return {?} */ function safeJsonParse(str) { try { return JSON.parse(str); } catch (e) { return str; } } /** * @fileoverview added by tsickle * Generated from: lib/cookie-options-provider.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ const COOKIE_OPTIONS = new InjectionToken('COOKIE_OPTIONS'); class CookieOptionsProvider { /** * @param {?=} options * @param {?=} _injector */ constructor(options = {}, _injector) { this._injector = _injector; this.defaultOptions = { path: this._injector.get(APP_BASE_HREF, '/'), domain: null, expires: null, secure: false, httpOnly: false }; this._options = mergeOptions(this.defaultOptions, options); } /** * @return {?} */ get options() { return this._options; } } CookieOptionsProvider.decorators = [ { type: Injectable } ]; /** @nocollapse */ CookieOptionsProvider.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [COOKIE_OPTIONS,] }] }, { type: Injector } ]; if (false) { /** * @type {?} * @private */ CookieOptionsProvider.prototype.defaultOptions; /** * @type {?} * @private */ CookieOptionsProvider.prototype._options; /** * @type {?} * @private */ CookieOptionsProvider.prototype._injector; } /** * @fileoverview added by tsickle * Generated from: lib/cookie.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @record */ function ICookieService() { } if (false) { /** * @param {?} key * @return {?} */ ICookieService.prototype.get = function (key) { }; /** * @param {?} key * @return {?} */ ICookieService.prototype.getObject = function (key) { }; /** * @return {?} */ ICookieService.prototype.getAll = function () { }; /** * @param {?} key * @param {?} value * @param {?=} options * @return {?} */ ICookieService.prototype.put = function (key, value, options) { }; /** * @param {?} key * @param {?} value * @param {?=} options * @return {?} */ ICookieService.prototype.putObject = function (key, value, options) { }; /** * @param {?} key * @param {?=} options * @return {?} */ ICookieService.prototype.remove = function (key, options) { }; /** * @param {?=} options * @return {?} */ ICookieService.prototype.removeAll = function (options) { }; } class CookieService { /** * @param {?} _optionsProvider */ constructor(_optionsProvider) { this._optionsProvider = _optionsProvider; this.options = this._optionsProvider.options; } /** * @protected * @return {?} */ get cookieString() { return document.cookie || ''; } /** * @protected * @param {?} val * @return {?} */ set cookieString(val) { document.cookie = val; } /** * \@name CookieService#get * * \@description * Returns the value of given cookie key. * * @param {?} key Id to use for lookup. * @return {?} Raw cookie value. */ get(key) { return ((/** @type {?} */ (this._cookieReader())))[key]; } /** * \@name CookieService#getObject * * \@description * Returns the deserialized value of given cookie key. * * @param {?} key Id to use for lookup. * @return {?} Deserialized cookie value. */ getObject(key) { /** @type {?} */ const value = this.get(key); return value ? safeJsonParse(value) : value; } /** * \@name CookieService#getAll * * \@description * Returns a key value object with all the cookies. * * @return {?} All cookies */ getAll() { return (/** @type {?} */ (this._cookieReader())); } /** * \@name CookieService#put * * \@description * Sets a value for given cookie key. * * @param {?} key Id for the `value`. * @param {?} value Raw value to be stored. * @param {?=} options (Optional) Options object. * @return {?} */ put(key, value, options) { this._cookieWriter()(key, value, options); } /** * \@name CookieService#putObject * * \@description * Serializes and sets a value for given cookie key. * * @param {?} key Id for the `value`. * @param {?} value Value to be stored. * @param {?=} options (Optional) Options object. * @return {?} */ putObject(key, value, options) { this.put(key, JSON.stringify(value), options); } /** * \@name CookieService#remove * * \@description * Remove given cookie. * * @param {?} key Id of the key-value pair to delete. * @param {?=} options (Optional) Options object. * @return {?} */ remove(key, options) { this._cookieWriter()(key, undefined, options); } /** * \@name CookieService#removeAll * * \@description * Remove all cookies. * @param {?=} options * @return {?} */ removeAll(options) { /** @type {?} */ const cookies = this.getAll(); Object.keys(cookies).forEach((/** * @param {?} key * @return {?} */ key => { this.remove(key, options); })); } /** * @private * @return {?} */ _cookieReader() { /** @type {?} */ let lastCookies = {}; /** @type {?} */ let lastCookieString = ''; /** @type {?} */ let cookieArray; /** @type {?} */ let cookie; /** @type {?} */ let i; /** @type {?} */ let index; /** @type {?} */ let name; /** @type {?} */ const 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) { // ignore nameless cookies 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(((/** @type {?} */ (lastCookies)))[name])) { ((/** @type {?} */ (lastCookies)))[name] = safeDecodeURIComponent(cookie.substring(index + 1)); } } } } return lastCookies; } /** * @private * @return {?} */ _cookieWriter() { /** @type {?} */ const that = this; return (/** * @param {?} name * @param {?} value * @param {?=} options * @return {?} */ function (name, value, options) { that.cookieString = that._buildCookieString(name, value, options); }); } /** * @private * @param {?} name * @param {?} value * @param {?=} options * @return {?} */ _buildCookieString(name, value, options) { /** @type {?} */ const opts = mergeOptions(this.options, options); /** @type {?} */ let expires = opts.expires; if (isBlank(value)) { expires = 'Thu, 01 Jan 1970 00:00:00 GMT'; value = ''; } if (isString(expires)) { expires = new Date(expires); } /** @type {?} */ const cookieValue = opts.storeUnencoded ? value : encodeURIComponent(value); /** @type {?} */ let str = encodeURIComponent(name) + '=' + cookieValue; str += opts.path ? ';path=' + opts.path : ''; str += opts.domain ? ';domain=' + opts.domain : ''; str += expires ? ';expires=' + expires.toUTCString() : ''; str += opts.secure ? ';secure' : ''; str += opts.httpOnly ? '; HttpOnly' : ''; // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum: // - 300 cookies // - 20 cookies per unique domain // - 4096 bytes per cookie /** @type {?} */ const cookieLength = str.length + 1; if (cookieLength > 4096) { console.log(`Cookie \'${name}\' possibly not set or overflowed because it was too large (${cookieLength} > 4096 bytes)!`); } return str; } } CookieService.decorators = [ { type: Injectable } ]; /** @nocollapse */ CookieService.ctorParameters = () => [ { type: CookieOptionsProvider } ]; if (false) { /** * @type {?} * @protected */ CookieService.prototype.options; /** * @type {?} * @private */ CookieService.prototype._optionsProvider; } /** * @fileoverview added by tsickle * Generated from: lib/cookie-backend.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class CookieBackendService extends CookieService { /** * @param {?} request * @param {?} response * @param {?} _optionsProvider */ constructor(request, response, _optionsProvider) { super(_optionsProvider); this.request = request; this.response = response; } /** * @protected * @return {?} */ get cookieString() { return this.request.cookie || ''; } /** * @protected * @param {?} val * @return {?} */ set cookieString(val) { this.request.cookie = val; this.response.cookie = val; } } CookieBackendService.decorators = [ { type: Injectable } ]; /** @nocollapse */ CookieBackendService.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [REQUEST,] }] }, { type: undefined, decorators: [{ type: Inject, args: [RESPONSE,] }] }, { type: CookieOptionsProvider } ]; if (false) { /** * @type {?} * @private */ CookieBackendService.prototype.request; /** * @type {?} * @private */ CookieBackendService.prototype.response; } /** * @fileoverview added by tsickle * Generated from: lib/cookie-options.model.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * \@name CookieOptionsArgs * \@description * * Object containing default options to pass when setting cookies. * * The object may have following properties: * * - **path** - {string} - The cookie will be available only for this path and its * sub-paths. By default, this is the URL that appears in your `<base>` tag. * - **domain** - {string} - The cookie will be available only for this domain and * its sub-domains. For security reasons the user agent will not accept the cookie * if the current domain is not a sub-domain of this domain or equal to it. * - **expires** - {string|Date} - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT" * or a Date object indicating the exact date/time this cookie will expire. * - **secure** - {boolean} - If `true`, then the cookie will only be available through a * secured connection. * - **httpOnly** - {boolean} - If `true`, then the cookie will be set with the `HttpOnly` * flag, and will only be accessible from the remote server. Helps to prevent against * XSS attacks. * - **storeUnencoded** - {boolean} - If `true`, then the cookie value will not be encoded and * will be stored as provided. * @record */ function CookieOptions() { } if (false) { /** @type {?|undefined} */ CookieOptions.prototype.path; /** @type {?|undefined} */ CookieOptions.prototype.domain; /** @type {?|undefined} */ CookieOptions.prototype.expires; /** @type {?|undefined} */ CookieOptions.prototype.secure; /** @type {?|undefined} */ CookieOptions.prototype.httpOnly; /** @type {?|undefined} */ CookieOptions.prototype.storeUnencoded; } /** * @fileoverview added by tsickle * Generated from: lib/cookie.factory.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @param {?} cookieOptionsProvider * @return {?} */ function cookieServiceFactory(cookieOptionsProvider) { return new CookieService(cookieOptionsProvider); } /** * @fileoverview added by tsickle * Generated from: lib/cookie.module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class CookieModule { /** * Use this method in your root module to provide the CookieService * @param {?=} options * @return {?} */ static forRoot(options = {}) { return { ngModule: CookieModule, providers: [ { provide: COOKIE_OPTIONS, useValue: options }, { provide: CookieService, useFactory: cookieServiceFactory, deps: [CookieOptionsProvider] } ] }; } /** * Use this method in your other (non root) modules to import the directive/pipe * @param {?=} options * @return {?} */ static forChild(options = {}) { return { ngModule: CookieModule, providers: [ { provide: COOKIE_OPTIONS, useValue: options }, { provide: CookieService, useFactory: cookieServiceFactory, deps: [CookieOptionsProvider] } ] }; } } CookieModule.decorators = [ { type: NgModule, args: [{ providers: [CookieOptionsProvider] },] } ]; /** * @fileoverview added by tsickle * Generated from: public_api.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * Generated from: martyganz-ngx-cookie.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { COOKIE_OPTIONS, CookieBackendService, CookieModule, CookieOptionsProvider, CookieService, cookieServiceFactory, isBlank, isPresent, isString, mergeOptions, safeDecodeURIComponent, safeJsonParse }; //# sourceMappingURL=martyganz-ngx-cookie.js.map