ngx-cookie-backend
Version:
Implementation of Angular 1.x $cookies service to Angular
117 lines (111 loc) • 5.13 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() {
const requestHeadersCookies = this.request?.headers?.cookie;
const cookiesFromRequest = requestHeadersCookies?.split(COOKIE_SEPARATOR) ?? [];
const addedCookies = this.getNormalizedResponseCookies();
const allCookies = this.latestUniqueCookieValues(cookiesFromRequest, addedCookies);
return allCookies.join(COOKIE_SEPARATOR);
}
write(name, value, options) {
this.response?.cookie(name, value, this.getOptions(options));
}
getOptions(options) {
if (isEmpty(options)) {
return {};
}
const opts = { ...options };
if (!isEmpty(options.expires)) {
opts.expires = this.getExpires(options.expires);
}
return opts;
}
getExpires(expires) {
return isString(expires) ? new Date(expires) : expires;
}
getNormalizedResponseCookies() {
const responseCookies = this.response?.getHeader('Set-Cookie') ?? '';
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