0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Cleaning up types

This commit is contained in:
Fabien "egg" O'Carroll 2023-12-05 15:53:37 +07:00
parent d65788a274
commit 1eb8a36c67
3 changed files with 26 additions and 8 deletions

View file

@ -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<string, Snippet>;
@ -15,11 +16,17 @@ export class SnippetsRepositoryInMemory implements SnippetsRepository {
this.snippets.set(entity.id.toHexString(), entity);
}
async getAll(order: OrderOf<[]>[], filter?: string | undefined): Promise<Snippet[]> {
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<Snippet[]> {
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<number> {
return 0;
return (await this.getAll([], filter)).length;
}
}

View file

@ -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<Response>();
console.log(error);
console.log(error.id);
response.status(error.statusCode || 500);
response.json({
errors: [

View file

@ -10,7 +10,7 @@ import {Request, Response} from 'express';
@Injectable()
export class LocationHeaderInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
intercept<T>(context: ExecutionContext, next: CallHandler): Observable<T> {
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}/`;