@gorniv/ngx-universal
Version:
Packages for Angular Universal
577 lines (563 loc) • 22.1 kB
JavaScript
import * as i0 from '@angular/core';
import { PLATFORM_ID, Injectable, Inject, NgModule, InjectionToken, ViewEncapsulation } from '@angular/core';
import * as i1 from '@angular/platform-browser';
import { makeStateKey } from '@angular/platform-browser';
import { from } from 'rxjs';
import { tap } from 'rxjs/operators';
import { isPlatformBrowser, isPlatformServer, APP_BASE_HREF, DOCUMENT, CommonModule } from '@angular/common';
import * as i2 from '@angular/common/http';
import { __decorate } from 'tslib';
class TransferHttpService {
constructor(transferState, httpClient, platformId) {
this.transferState = transferState;
this.httpClient = httpClient;
this.platformId = platformId;
}
request(method, uri, options) {
return this.getData(method, uri, options, (method, uri, options) => {
return this.httpClient.request(method, typeof uri === 'string' ? uri : uri.url, options);
});
}
/**
* Performs a request with `get` http method.
*/
get(url, options) {
return this.getData('get', url, options, (_method, uri, options) => {
return this.httpClient.get(url, options);
});
}
/**
* Performs a request with `post` http method.
*/
post(url, body, options) {
return this.getPostData('post', url, body, options, (_method, uri, body, options) => {
return this.httpClient.post(url, body, options);
});
}
/**
* Performs a request with `put` http method.
*/
put(url, _body, options) {
return this.getPostData('put', url, _body, options, (_method, uri, _body, options) => {
return this.httpClient.put(url, _body, options);
});
}
/**
* Performs a request with `delete` http method.
*/
delete(url, options) {
return this.getData('delete', url, options, (_method, uri, options) => {
return this.httpClient.delete(url, options);
});
}
/**
* Performs a request with `patch` http method.
*/
patch(url, body, options) {
return this.getPostData('patch', url, body, options, (_method, uri, body, options) => {
return this.httpClient.patch(url, body, options);
});
}
/**
* Performs a request with `head` http method.
*/
head(url, options) {
return this.getData('head', url, options, (_method, uri, options) => {
return this.httpClient.head(url, options);
});
}
/**
* Performs a request with `options` http method.
*/
options(url, options) {
return this.getData('options', url, options, (_method, uri, options) => {
return this.httpClient.options(url, options);
});
}
getData(method, uri, options, callback) {
let url = uri;
if (typeof uri !== 'string') {
url = uri.url;
}
const tempKey = url + (options ? JSON.stringify(options) : '');
const key = makeStateKey(tempKey);
try {
return this.resolveData(key);
}
catch (e) {
return callback(method, uri, options).pipe(tap((data) => {
if (isPlatformBrowser(this.platformId)) {
// Client only code.
// nothing;
}
if (isPlatformServer(this.platformId)) {
this.setCache(key, data);
}
}));
}
}
getPostData(_method, uri, body, options, callback) {
let url = uri;
if (typeof uri !== 'string') {
url = uri.url;
}
const tempKey = url + (body ? JSON.stringify(body) : '') + (options ? JSON.stringify(options) : '');
const key = makeStateKey(tempKey);
try {
return this.resolveData(key);
}
catch (e) {
return callback(_method, uri, body, options).pipe(tap((data) => {
if (isPlatformBrowser(this.platformId)) {
// Client only code.
// nothing;
}
if (isPlatformServer(this.platformId)) {
this.setCache(key, data);
}
}));
}
}
resolveData(key) {
const data = this.getFromCache(key);
if (!data) {
throw new Error();
}
if (isPlatformBrowser(this.platformId)) {
// Client only code.
this.transferState.remove(key);
}
if (isPlatformServer(this.platformId)) {
// Server only code.
}
return from(Promise.resolve(data));
}
setCache(key, data) {
return this.transferState.set(key, data);
}
getFromCache(key) {
return this.transferState.get(key, {});
}
}
TransferHttpService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TransferHttpService, deps: [{ token: i1.TransferState }, { token: i2.HttpClient }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
TransferHttpService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TransferHttpService });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TransferHttpService, decorators: [{
type: Injectable
}], ctorParameters: function () {
return [{ type: i1.TransferState }, { type: i2.HttpClient }, { type: Object, decorators: [{
type: Inject,
args: [PLATFORM_ID]
}] }];
} });
class TransferHttpModule {
}
TransferHttpModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TransferHttpModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
TransferHttpModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.0", ngImport: i0, type: TransferHttpModule });
TransferHttpModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TransferHttpModule, providers: [TransferHttpService] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: TransferHttpModule, decorators: [{
type: NgModule,
args: [{
providers: [TransferHttpService],
}]
}] });
function isBlank(obj) {
return obj === undefined || obj === null;
}
function isPresent(obj) {
return obj !== undefined && obj !== null;
}
function isString(obj) {
return typeof obj === 'string';
}
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,
};
}
function safeDecodeURIComponent(str) {
try {
return decodeURIComponent(str);
}
catch (e) {
return str;
}
}
function safeJsonParse(str) {
try {
return JSON.parse(str);
}
catch (e) {
return str;
}
}
const COOKIE_OPTIONS = new InjectionToken('COOKIE_OPTIONS');
class CookieOptionsProvider {
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);
}
get options() {
return this._options;
}
}
CookieOptionsProvider.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieOptionsProvider, deps: [{ token: COOKIE_OPTIONS }, { token: i0.Injector }], target: i0.ɵɵFactoryTarget.Injectable });
CookieOptionsProvider.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieOptionsProvider });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieOptionsProvider, decorators: [{
type: Injectable
}], ctorParameters: function () {
return [{ type: undefined, decorators: [{
type: Inject,
args: [COOKIE_OPTIONS]
}] }, { type: i0.Injector }];
} });
class CookieService {
constructor(_optionsProvider) {
this._optionsProvider = _optionsProvider;
this.options = this._optionsProvider.options;
}
get cookieString() {
return document.cookie || '';
}
set cookieString(val) {
document.cookie = val;
}
/**
* @name CookieService#get
*
* @description
* Returns the value of given cookie key.
*
* @param key Id to use for lookup.
* @returns Raw cookie value.
*/
get(key) {
return this._cookieReader()[key];
}
/**
* @name CookieService#getObject
*
* @description
* Returns the deserialized value of given cookie key.
*
* @param key Id to use for lookup.
* @returns Deserialized cookie value.
*/
getObject(key) {
const value = this.get(key);
return value ? safeJsonParse(value) : value;
}
/**
* @name CookieService#getAll
*
* @description
* Returns a key value object with all the cookies.
*
* @returns All cookies
*/
getAll() {
return 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.
*/
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.
*/
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.
*/
remove(key, options) {
this._cookieWriter()(key, undefined, options);
}
/**
* @name CookieService#removeAll
*
* @description
* Remove all cookies.
*/
removeAll(options) {
const cookies = this.getAll();
Object.keys(cookies).forEach(key => {
this.remove(key, options);
});
}
_cookieReader() {
let lastCookies = {};
let lastCookieString = '';
let cookieArray, cookie, i, index, name;
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(lastCookies[name])) {
lastCookies[name] = safeDecodeURIComponent(cookie.substring(index + 1));
}
}
}
}
return lastCookies;
}
_cookieWriter() {
const that = this;
return function (name, value, options) {
that.cookieString = that._buildCookieString(name, value, options);
};
}
_buildCookieString(name, value, options) {
const opts = mergeOptions(this.options, options);
let expires = opts.expires;
if (isBlank(value)) {
expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
value = '';
}
if (isString(expires)) {
expires = new Date(expires);
}
const cookieValue = opts.storeUnencoded ? value : encodeURIComponent(value || '');
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
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.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieService, deps: [{ token: CookieOptionsProvider }], target: i0.ɵɵFactoryTarget.Injectable });
CookieService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieService });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieService, decorators: [{
type: Injectable
}], ctorParameters: function () { return [{ type: CookieOptionsProvider }]; } });
class NgxRequest {
}
class NgxResponse {
}
class CookieBackendService extends CookieService {
constructor(request, response, _optionsProvider) {
super(_optionsProvider);
this.request = request;
this.response = response;
}
get cookieString() {
return this.request.cookie || this.request.headers['cookie'] || '';
}
set cookieString(val) {
this.request.cookie = val;
this.request.headers.cookie = val;
}
overrideput(key, value, options = {}) {
let findKey = false;
let newCookie = Object.keys(this.getAll())
// tslint:disable-next-line: no-shadowed-variable
.map((keyItem) => {
if (keyItem === key) {
findKey = true;
return `${key}=${value}`;
}
return `${keyItem}=${this.get(keyItem)}`;
})
.join('; ');
if (!findKey) {
newCookie += `; ${key}=${value}`;
}
this.cookieString = newCookie;
this.response.cookie(key, value);
}
remove(key, options) {
const newCookie = Object.keys(this.getAll())
// tslint:disable-next-line: no-shadowed-variable
.map((keyItem) => {
if (keyItem === key) {
return '';
}
return `${keyItem}=${this.get(keyItem)}`;
})
.join('; ');
this.cookieString = newCookie;
this.response.clearCookie(key);
}
}
CookieBackendService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieBackendService, deps: [{ token: NgxRequest }, { token: NgxResponse }, { token: CookieOptionsProvider }], target: i0.ɵɵFactoryTarget.Injectable });
CookieBackendService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieBackendService });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieBackendService, decorators: [{
type: Injectable
}], ctorParameters: function () {
return [{ type: undefined, decorators: [{
type: Inject,
args: [NgxRequest]
}] }, { type: undefined, decorators: [{
type: Inject,
args: [NgxResponse]
}] }, { type: CookieOptionsProvider }];
} });
function cookieServiceFactory(cookieOptionsProvider) {
return new CookieService(cookieOptionsProvider);
}
class CookieModule {
/**
* Use this method in your root module to provide the CookieService
*/
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
*/
static forChild(options = {}) {
return {
ngModule: CookieModule,
providers: [
{ provide: COOKIE_OPTIONS, useValue: options },
{ provide: CookieService, useFactory: cookieServiceFactory, deps: [CookieOptionsProvider] }
]
};
}
}
CookieModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
CookieModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.0", ngImport: i0, type: CookieModule });
CookieModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieModule, providers: [CookieOptionsProvider] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: CookieModule, decorators: [{
type: NgModule,
args: [{
providers: [CookieOptionsProvider]
}]
}] });
let LinkService = class LinkService {
constructor(rendererFactory, document) {
this.rendererFactory = rendererFactory;
this.document = document;
}
/**
* Inject the State into the bottom of the <head>
*/
addTag(tag, forceCreation) {
try {
const renderer = this.rendererFactory.createRenderer(this.document, {
id: '-1',
encapsulation: ViewEncapsulation.None,
styles: [],
data: {}
});
const link = renderer.createElement('link');
const head = this.document.head;
const selector = this._parseSelector(tag);
if (head === null) {
throw new Error('<head> not found within DOCUMENT.');
}
Object.keys(tag).forEach((prop) => {
return renderer.setAttribute(link, prop, tag[prop]);
});
// [TODO]: get them to update the existing one (if it exists) ?
renderer.appendChild(head, link);
}
catch (e) {
console.error('Error within linkService : ', e);
}
}
_parseSelector(tag) {
// Possibly re-work this
const attr = tag.rel ? 'rel' : 'hreflang';
return `${attr}="${tag[attr]}"`;
}
};
LinkService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: LinkService, deps: [{ token: i0.RendererFactory2 }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Injectable });
LinkService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: LinkService, providedIn: 'root' });
LinkService = __decorate([
Injectable()
], LinkService);
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: LinkService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}, {
type: Injectable
}], ctorParameters: function () {
return [{ type: i0.RendererFactory2 }, { type: Document, decorators: [{
type: Inject,
args: [DOCUMENT]
}] }];
} });
class LinkModule {
}
LinkModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: LinkModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
LinkModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.1.0", ngImport: i0, type: LinkModule, imports: [CommonModule] });
LinkModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: LinkModule, providers: [LinkService], imports: [CommonModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.1.0", ngImport: i0, type: LinkModule, decorators: [{
type: NgModule,
args: [{
imports: [
CommonModule
],
providers: [LinkService]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { COOKIE_OPTIONS, CookieBackendService, CookieModule, CookieOptionsProvider, CookieService, LinkModule, LinkService, NgxRequest, NgxResponse, TransferHttpModule, TransferHttpService, cookieServiceFactory, isBlank, isPresent, isString, mergeOptions, safeDecodeURIComponent, safeJsonParse };
//# sourceMappingURL=gorniv-ngx-universal.mjs.map