Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

55 lines
1.5 KiB

/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Sebastian Stehle. All rights reserved
*/
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';
import { SchemaDetailsDto, SchemasService } from './../services/schemas.service';
@Injectable()
export class ResolveSchemaGuard implements Resolve<SchemaDetailsDto> {
constructor(
private readonly schemasService: SchemasService,
private readonly router: Router
) {
}
public resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<SchemaDetailsDto> {
const appName = this.findParameter(route, 'appName');
const schemaName = this.findParameter(route, 'schemaName');
const result =
this.schemasService.getSchema(appName, schemaName).toPromise()
.then(dto => {
if (!dto) {
this.router.navigate(['/404']);
}
return dto;
}).catch(() => {
this.router.navigate(['/404']);
});
return result;
}
private findParameter(route: ActivatedRouteSnapshot, name: string) {
let result: string;
while (route) {
result = route.params[name];
if (result) {
break;
}
route = route.parent;
}
return result;
}
}