Newer
Older

Marlène SIMONDANT
committed
import { Controller, Get, HttpService, HttpException, HttpStatus, Logger, Param } from '@nestjs/common';
import { ConfigurationService } from '../configuration/configuration.service';
import { Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { Page } from './schemas/page.schema';
import { rewriteGhostImgUrl } from '../shared/utils';
@Controller('pages')
export class PagesController {
private readonly logger = new Logger(PagesController.name);
constructor(private readonly httpService: HttpService, private readonly configService: ConfigurationService) {}
@Get(':slug')
public async getPagebySlug(@Param('slug') slug: string): Promise<Observable<{ pages: Page[] }>> {
return this.httpService
.get(`${process.env.GHOST_HOST_AND_PORT}/ghost/api/v3/content/pages/slug/` + slug, {
params: {
key: process.env.GHOST_CONTENT_API_KEY,
},
})
.pipe(
map((response) => {
return { pages: [rewriteGhostImgUrl(this.configService, response.data.pages[0])] };
}),
catchError((err) => {
this.logger.error(err);
throw new HttpException('Page not found:' + err, HttpStatus.NOT_FOUND);
})
);
}
}