From 7cca4a995eb212e6753f0ceb5c8a9160c7bed502 Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Thu, 6 Jun 2024 12:00:43 +0200 Subject: [PATCH] Move logging logic to mock implementation classes --- src/AbstractBaseServer.ts | 8 -------- src/adapters/FetchMockServer.ts | 20 +++++++++++++++++++- src/adapters/SinonServer.ts | 20 +++++++++++++++++++- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/AbstractBaseServer.ts b/src/AbstractBaseServer.ts index 9e8e41f..a676b62 100644 --- a/src/AbstractBaseServer.ts +++ b/src/AbstractBaseServer.ts @@ -11,7 +11,6 @@ import type { CollectionItem, Query, QueryFunction } from './types.js'; export abstract class AbstractBaseServer { baseUrl = ''; identifierName = 'id'; - loggingEnabled = false; defaultQuery: QueryFunction = () => ({}); collections: Record> = {}; singles: Record> = {}; @@ -23,11 +22,9 @@ export abstract class AbstractBaseServer { defaultQuery = () => ({}), identifierName = 'id', getNewId, - loggingEnabled = false, }: BaseServerOptions = {}) { this.baseUrl = baseUrl; this.getNewId = getNewId; - this.loggingEnabled = loggingEnabled; this.identifierName = identifierName; this.defaultQuery = defaultQuery; @@ -57,10 +54,6 @@ export abstract class AbstractBaseServer { } } - toggleLogging() { - this.loggingEnabled = !this.loggingEnabled; - } - /** * @param Function ResourceName => object */ @@ -198,7 +191,6 @@ export type BaseServerOptions = { defaultQuery?: QueryFunction; identifierName?: string; getNewId?: () => number | string; - loggingEnabled?: boolean; }; export type BaseRequest = { diff --git a/src/adapters/FetchMockServer.ts b/src/adapters/FetchMockServer.ts index 3af5a9d..792313e 100644 --- a/src/adapters/FetchMockServer.ts +++ b/src/adapters/FetchMockServer.ts @@ -8,6 +8,20 @@ import type { import { parseQueryString } from '../parseQueryString.js'; export class FetchMockServer extends BaseServer { + loggingEnabled = false; + + constructor({ + loggingEnabled = false, + ...options + }: FetchMockServerOptions = {}) { + super(options); + this.loggingEnabled = loggingEnabled; + } + + toggleLogging() { + this.loggingEnabled = !this.loggingEnabled; + } + async extractContext(request: Request) { const req = typeof request === 'string' ? new Request(request) : request; @@ -89,7 +103,7 @@ export class FetchMockServer extends BaseServer { } } -export const getFetchMockHandler = (options: BaseServerOptions) => { +export const getFetchMockHandler = (options: FetchMockServerOptions) => { const server = new FetchMockServer(options); return server.getHandler(); }; @@ -106,3 +120,7 @@ export type FetchMockFakeRestRequest = Partial & { queryString?: string; params?: { [key: string]: any }; }; + +export type FetchMockServerOptions = BaseServerOptions & { + loggingEnabled?: boolean; +}; diff --git a/src/adapters/SinonServer.ts b/src/adapters/SinonServer.ts index d252cbb..ec14fad 100644 --- a/src/adapters/SinonServer.ts +++ b/src/adapters/SinonServer.ts @@ -7,6 +7,20 @@ export class SinonServer extends BaseServer< SinonFakeXMLHttpRequest, SinonFakeRestResponse > { + loggingEnabled = false; + + constructor({ + loggingEnabled = false, + ...options + }: SinonServerOptions = {}) { + super(options); + this.loggingEnabled = loggingEnabled; + } + + toggleLogging() { + this.loggingEnabled = !this.loggingEnabled; + } + extractContextSync(request: SinonFakeXMLHttpRequest) { const req: Request | SinonFakeXMLHttpRequest = typeof request === 'string' ? new Request(request) : request; @@ -126,7 +140,7 @@ export class SinonServer extends BaseServer< } } -export const getSinonHandler = (options: BaseServerOptions) => { +export const getSinonHandler = (options: SinonServerOptions) => { const server = new SinonServer(options); return server.getHandler(); }; @@ -141,3 +155,7 @@ export type SinonFakeRestResponse = { body: any; headers: Record; }; + +export type SinonServerOptions = BaseServerOptions & { + loggingEnabled?: boolean; +};