From 1eb8a36c67312cbd15e240cfe665e20494e76dfc Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Tue, 5 Dec 2023 15:53:37 +0700 Subject: [PATCH] Cleaning up types --- .../snippets/snippets.repository.inmemory.ts | 13 ++++++++++--- .../src/http/filters/global-exception.filter.ts | 17 +++++++++++++---- .../interceptors/location-header.interceptor.ts | 4 +++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/ghost/ghost/src/core/snippets/snippets.repository.inmemory.ts b/ghost/ghost/src/core/snippets/snippets.repository.inmemory.ts index 916ece9007..40da4c7be3 100644 --- a/ghost/ghost/src/core/snippets/snippets.repository.inmemory.ts +++ b/ghost/ghost/src/core/snippets/snippets.repository.inmemory.ts @@ -2,6 +2,7 @@ import ObjectID from 'bson-objectid'; import {Snippet} from './snippet.entity'; import {SnippetsRepository} from './snippets.repository.interface'; import {OrderOf, Page} from '../../common/repository'; +import nql from '@tryghost/nql'; export class SnippetsRepositoryInMemory implements SnippetsRepository { snippets: Map; @@ -15,11 +16,17 @@ export class SnippetsRepositoryInMemory implements SnippetsRepository { this.snippets.set(entity.id.toHexString(), entity); } async getAll(order: OrderOf<[]>[], filter?: string | undefined): Promise { - return [...this.snippets.values()]; + const filterObj = nql(filter); + return [...this.snippets.values()].filter((value: Snippet) => { + return filterObj.testJSON(value); + }); } async getSome(page: Page, order: OrderOf<[]>[], filter?: string | undefined): Promise { - return [...this.snippets.values()]; + const all = await this.getAll(order, filter); + const start = (page.page - 1) * page.count; + const data = all.slice(start, start + page.count); + return data; } async getOne(id: ObjectID) { @@ -27,6 +34,6 @@ export class SnippetsRepositoryInMemory implements SnippetsRepository { } async getCount(filter?: string | undefined): Promise { - return 0; + return (await this.getAll([], filter)).length; } } diff --git a/ghost/ghost/src/http/filters/global-exception.filter.ts b/ghost/ghost/src/http/filters/global-exception.filter.ts index e385c13e66..cbf36aee64 100644 --- a/ghost/ghost/src/http/filters/global-exception.filter.ts +++ b/ghost/ghost/src/http/filters/global-exception.filter.ts @@ -1,15 +1,24 @@ import {ArgumentsHost, Catch, ExceptionFilter} from '@nestjs/common'; import {Response} from 'express'; +interface GhostError extends Error { + statusCode?: number; + context?: string; + errorType?: string; + errorDetails?: string; + property?: string; + help?: string; + code?: string; + id?: string; + ghostErrorCode?: string; +} + @Catch() export class GlobalExceptionFilter implements ExceptionFilter { - catch(error: any, host: ArgumentsHost) { + catch(error: GhostError, host: ArgumentsHost) { const context = host.switchToHttp(); const response = context.getResponse(); - console.log(error); - console.log(error.id); - response.status(error.statusCode || 500); response.json({ errors: [ diff --git a/ghost/ghost/src/http/interceptors/location-header.interceptor.ts b/ghost/ghost/src/http/interceptors/location-header.interceptor.ts index d7000b89a0..c3e7b87ba8 100644 --- a/ghost/ghost/src/http/interceptors/location-header.interceptor.ts +++ b/ghost/ghost/src/http/interceptors/location-header.interceptor.ts @@ -10,7 +10,7 @@ import {Request, Response} from 'express'; @Injectable() export class LocationHeaderInterceptor implements NestInterceptor { - intercept(context: ExecutionContext, next: CallHandler): Observable { + intercept(context: ExecutionContext, next: CallHandler): Observable { if (context.getType() !== 'http') { return next.handle(); } @@ -50,6 +50,8 @@ export class LocationHeaderInterceptor implements NestInterceptor { const url = new URL('https://ghost.io'); url.protocol = req.secure ? 'https:' : 'http:'; + // We use `any` here because we haven't yet extended the express Request object with the vhost plugin types + // eslint-disable-next-line @typescript-eslint/no-explicit-any url.host = (req as any).vhost ? (req as any).vhost.host : req.get('host'); url.pathname = req.path; url.pathname += `${id}/`;