ngx-cookie-backend
Version:
Implementation of Angular 1.x $cookies service to Angular
122 lines (116 loc) • 5.59 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, Optional, Inject, NgModule } from '@angular/core';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
import { isEmpty, isString, CookieModule, COOKIE_OPTIONS, COOKIE_WRITER, CookieOptionsProvider } from 'ngx-cookie';
const COOKIE_SEPARATOR = ';';
class CookieBackendWriterService {
constructor(request, response) {
this.request = request;
this.response = response;
}
readAllAsString() {
var _a, _b, _c;
const requestHeadersCookies = (_b = (_a = this.request) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b.cookie;
const cookiesFromRequest = (_c = requestHeadersCookies === null || requestHeadersCookies === void 0 ? void 0 : requestHeadersCookies.split(COOKIE_SEPARATOR)) !== null && _c !== void 0 ? _c : [];
const addedCookies = this.getNormalizedResponseCookies();
const allCookies = this.latestUniqueCookieValues(cookiesFromRequest, addedCookies);
return allCookies.join(COOKIE_SEPARATOR);
}
write(name, value, options) {
var _a;
(_a = this.response) === null || _a === void 0 ? void 0 : _a.cookie(name, value, this.getOptions(options));
}
getOptions(options) {
if (isEmpty(options)) {
return {};
}
const opts = Object.assign({}, options);
if (!isEmpty(options.expires)) {
opts.expires = this.getExpires(options.expires);
}
return opts;
}
getExpires(expires) {
return isString(expires) ? new Date(expires) : expires;
}
getNormalizedResponseCookies() {
var _a, _b;
const responseCookies = (_b = (_a = this.response) === null || _a === void 0 ? void 0 : _a.getHeader('Set-Cookie')) !== null && _b !== void 0 ? _b : '';
const addedCookies = Array.isArray(responseCookies) ? responseCookies : [responseCookies];
return addedCookies.flatMap(cookieEntry => cookieEntry.split(COOKIE_SEPARATOR));
}
latestUniqueCookieValues(oldCookies, newerCookies) {
const cookiesMap = new Map();
const oldAndNewCookies = [...oldCookies, ...newerCookies];
oldAndNewCookies
.filter(value => !isEmpty(value))
.map(cookie => cookie.split('='))
.forEach(([key, value]) => cookiesMap.set(key.trim(), value));
const result = [];
cookiesMap.forEach((value, key) => result.push(`${key}=${value}`));
return result;
}
}
CookieBackendWriterService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.4", ngImport: i0, type: CookieBackendWriterService, deps: [{ token: REQUEST, optional: true }, { token: RESPONSE, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
CookieBackendWriterService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.4", ngImport: i0, type: CookieBackendWriterService });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.4", ngImport: i0, type: CookieBackendWriterService, decorators: [{
type: Injectable
}], ctorParameters: function () {
return [{ type: undefined, decorators: [{
type: Optional
}, {
type: Inject,
args: [REQUEST]
}] }, { type: undefined, decorators: [{
type: Optional
}, {
type: Inject,
args: [RESPONSE]
}] }];
} });
class CookieBackendModule {
/**
* Use this method in your root module to provide the CookieService
*/
static withOptions(options = {}) {
return {
ngModule: CookieModule,
providers: [
{ provide: COOKIE_OPTIONS, useValue: options },
{ provide: COOKIE_WRITER, useClass: CookieBackendWriterService }
]
};
}
/**
* @deprecated use `CookieBackendModule.withOptions()` instead
* Use this method in your root module to provide the CookieService
*/
static forRoot(options = {}) {
return this.withOptions(options);
}
/**
* @deprecated use `CookieBackendModule.withOptions()` instead
* Use this method in your other (non root) modules to import the directive/pipe
*/
static forChild(options = {}) {
return this.withOptions(options);
}
}
CookieBackendModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.4", ngImport: i0, type: CookieBackendModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
CookieBackendModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.0.4", ngImport: i0, type: CookieBackendModule, imports: [CookieModule] });
CookieBackendModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.0.4", ngImport: i0, type: CookieBackendModule, providers: [CookieOptionsProvider], imports: [CookieModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.4", ngImport: i0, type: CookieBackendModule, decorators: [{
type: NgModule,
args: [{
imports: [CookieModule],
providers: [CookieOptionsProvider]
}]
}] });
/*
* Public API Surface of ngx-cookie-backend
*/
/**
* Generated bundle index. Do not edit.
*/
export { CookieBackendModule, CookieBackendWriterService };
//# sourceMappingURL=ngx-cookie-backend.mjs.map