Skip to content

Commit

Permalink
Move logging logic to mock implementation classes
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Jun 6, 2024
1 parent 3c655d1 commit 7cca4a9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
8 changes: 0 additions & 8 deletions src/AbstractBaseServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Collection<any>> = {};
singles: Record<string, Single<any>> = {};
Expand All @@ -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;

Expand Down Expand Up @@ -57,10 +54,6 @@ export abstract class AbstractBaseServer {
}
}

toggleLogging() {
this.loggingEnabled = !this.loggingEnabled;
}

/**
* @param Function ResourceName => object
*/
Expand Down Expand Up @@ -198,7 +191,6 @@ export type BaseServerOptions = {
defaultQuery?: QueryFunction;
identifierName?: string;
getNewId?: () => number | string;
loggingEnabled?: boolean;
};

export type BaseRequest = {
Expand Down
20 changes: 19 additions & 1 deletion src/adapters/FetchMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import type {
import { parseQueryString } from '../parseQueryString.js';

export class FetchMockServer extends BaseServer<Request, MockResponseObject> {
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;
Expand Down Expand Up @@ -89,7 +103,7 @@ export class FetchMockServer extends BaseServer<Request, MockResponseObject> {
}
}

export const getFetchMockHandler = (options: BaseServerOptions) => {
export const getFetchMockHandler = (options: FetchMockServerOptions) => {
const server = new FetchMockServer(options);
return server.getHandler();
};
Expand All @@ -106,3 +120,7 @@ export type FetchMockFakeRestRequest = Partial<Request> & {
queryString?: string;
params?: { [key: string]: any };
};

export type FetchMockServerOptions = BaseServerOptions & {
loggingEnabled?: boolean;
};
20 changes: 19 additions & 1 deletion src/adapters/SinonServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
};
Expand All @@ -141,3 +155,7 @@ export type SinonFakeRestResponse = {
body: any;
headers: Record<string, string>;
};

export type SinonServerOptions = BaseServerOptions & {
loggingEnabled?: boolean;
};

0 comments on commit 7cca4a9

Please sign in to comment.