UNPKG

@ynmstudio/utils

Version:
161 lines (155 loc) 6.88 kB
import { isPlatformBrowser, Location } from '@angular/common'; import * as i0 from '@angular/core'; import { inject, PLATFORM_ID, Injectable } from '@angular/core'; import { HttpResponse, HttpHeaders } from '@angular/common/http'; import { filter } from 'rxjs'; import { IsrService } from '@rx-angular/isr/browser'; /** Inject With Credentials into the request */ class CraftIframeResizerInterceptor { constructor() { } intercept(req, next) { const platform = inject(PLATFORM_ID); if (!isPlatformBrowser(platform)) return next.handle(req); const location = inject(Location); if (req.url.includes('/api')) { const [, paramString] = location.path().split('?'); const urlParams = new URLSearchParams(paramString); if (urlParams.has('x-craft-live-preview') && urlParams.has('token')) { import('iframe-resizer') .then(() => console.log('iFrameResizer loaded')) .catch(error => console.error(error)); } return next.handle(req); } else { return next.handle(req); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftIframeResizerInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftIframeResizerInterceptor }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftIframeResizerInterceptor, decorators: [{ type: Injectable }], ctorParameters: () => [] }); const craftIframeResizerInterceptor = (req, next) => { const platform = inject(PLATFORM_ID); if (!isPlatformBrowser(platform)) return next(req); const location = inject(Location); if (req.url.includes('/api')) { const [, paramString] = location.path().split('?'); const urlParams = new URLSearchParams(paramString); if (urlParams.has('x-craft-live-preview') && urlParams.has('token')) { import('iframe-resizer') .then(() => console.log('iFrameResizer loaded')) .catch(error => console.error(error)); } return next(req); } else { return next(req); } }; /** Inject Preview Token into the request */ class CraftPreviewInterceptor { #location = inject(Location); #isrService = inject(IsrService, { optional: true }) ?? undefined; intercept(req, next) { if (req.url.includes('/api')) { const [, paramString] = this.#location.path().split('?'); const urlParams = new URLSearchParams(paramString); if (urlParams.has('x-craft-live-preview') && urlParams.has('token')) { const duplicate = req.clone({ setParams: { token: urlParams.get("token") || "", fetchPolicy: 'no-cache', }, }); this.#isrService?.addError({ name: 'Craft CMS Preview Mode', message: 'Page should not get cached in preview mode', }); return next.handle(duplicate).pipe(filter((event) => event instanceof HttpResponse)); } else { return next.handle(req); } } else { return next.handle(req); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftPreviewInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftPreviewInterceptor }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftPreviewInterceptor, decorators: [{ type: Injectable }] }); const craftPreviewInterceptor = (req, next) => { const location = inject(Location); const isrService = inject(IsrService, { optional: true }) ?? undefined; if (req.url.includes('/api')) { const [, paramString] = location.path().split('?'); const urlParams = new URLSearchParams(paramString); if ((urlParams.has('x-craft-live-preview') || urlParams.has('x-craft-preview')) && urlParams.has('token')) { const duplicate = req.clone({ setParams: { token: urlParams.get("token") || "", fetchPolicy: 'no-cache', }, }); isrService?.addError({ name: 'Craft CMS Preview Mode', message: 'Page should not get cached in preview mode', }); return next(duplicate).pipe(filter((event) => event instanceof HttpResponse)); } else { return next(req); } } else { return next(req); } }; /** * @deprecated Use `craftPreviewInterceptor` instead */ const CraftPreviewInterceptorFn = craftPreviewInterceptor; /** Inject With Credentials into the request */ class CraftSecurityInterceptor { constructor() { } intercept(req, next) { req = req.clone({ // withCredentials: req.body && req.body.query ? false : true, headers: new HttpHeaders({ Accept: 'application/json', }), }); return next.handle(req); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftSecurityInterceptor, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftSecurityInterceptor }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CraftSecurityInterceptor, decorators: [{ type: Injectable }], ctorParameters: () => [] }); const craftSecurityInterceptor = (req, next) => { req = req.clone({ // withCredentials: req.body && req.body.query ? false : true, setHeaders: { Accept: 'application/json', }, }); return next(req); }; /** * @deprecated Use `craftSecurityInterceptor` instead */ const CraftSecurityInterceptorFn = craftSecurityInterceptor; /** * Generated bundle index. Do not edit. */ export { CraftIframeResizerInterceptor, CraftPreviewInterceptor, CraftPreviewInterceptorFn, CraftSecurityInterceptor, CraftSecurityInterceptorFn, craftIframeResizerInterceptor, craftPreviewInterceptor, craftSecurityInterceptor }; //# sourceMappingURL=ynmstudio-utils-interceptors.mjs.map