|
| 1 | +import { RESPONSE } from '@nguniversal/express-engine/tokens' |
| 2 | +import { Inject, Injectable } from '@angular/core' |
| 3 | +import * as express from 'express' |
| 4 | + |
| 5 | +// tslint:disable:no-this |
| 6 | +// tslint:disable:no-object-mutation |
| 7 | +// tslint:disable-next-line:no-class |
| 8 | +@Injectable() |
| 9 | +export class HeaderService { |
| 10 | + constructor(@Inject(RESPONSE) private response: express.Response) {} |
| 11 | + getHeader(key: string): string | undefined { |
| 12 | + return this.response.getHeader(key) as string | undefined |
| 13 | + } |
| 14 | + |
| 15 | + setHeader(key: string, value: string): void { |
| 16 | + this.response.header(key, value) |
| 17 | + } |
| 18 | + |
| 19 | + setHeaders(dictionary: { readonly [key: string]: string }): void { |
| 20 | + Object.keys(dictionary).forEach(key => this.setHeader(key, dictionary[key])) |
| 21 | + } |
| 22 | + |
| 23 | + removeHeader(key: string): void { |
| 24 | + this.response.removeHeader(key) |
| 25 | + } |
| 26 | + |
| 27 | + appendHeader(key: string, value: string, delimiter = ','): void { |
| 28 | + const current = this.getHeader(key) |
| 29 | + |
| 30 | + // tslint:disable-next-line:no-if-statement |
| 31 | + if (!current) { |
| 32 | + this.setHeader(key, value) |
| 33 | + } else { |
| 34 | + const newValue = [...current.split(delimiter), value] |
| 35 | + .filter((el, i, a) => i === a.indexOf(el)) |
| 36 | + .join(delimiter) |
| 37 | + this.response.header(key, newValue) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
0 commit comments