All files / src/app/core/utils/http-status-code http-status-code.service.ts

100% Statements 15/15
82.35% Branches 14/17
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35198x 198x 198x   198x     198x 136x                   22x 4x   22x   18x   18x 3x   15x     4x      
import { Inject, Injectable, Optional } from '@angular/core';
import { Router } from '@angular/router';
import { RESPONSE } from '@nguniversal/express-engine/tokens';
 
import { InjectSingle } from 'ish-core/utils/injection';
 
@Injectable({ providedIn: 'root' })
export class HttpStatusCodeService {
  constructor(private router: Router, @Optional() @Inject(RESPONSE) private response: InjectSingle<typeof RESPONSE>) {}
 
  /**
   * set status for SSR response
   *
   * redirecting to error / not-found page must be disabled for guards, where the routing is organized differently
   *
   * @returns the Promise from the Angular Router or a Promise resolving to true if no routing was necessary or it was disabled
   */
  setStatus(status: number, redirect = true) {
    if (SSR) {
      this.response.status(status);
    }
    if (redirect && status >= 400) {
      // 503: server is unavailable
      const route = status === 503 ? '/maintenance' : '/error';
 
      if (SSR) {
        return this.router.navigateByUrl(route);
      } else {
        return this.router.navigateByUrl(route, { skipLocationChange: status < 500 });
      }
    }
    return Promise.resolve(true);
  }
}