Skip to content

Commit

Permalink
Cleanup options
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Jun 6, 2024
1 parent d80d993 commit 910c4f9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
6 changes: 5 additions & 1 deletion example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ switch (import.meta.env.VITE_MOCK) {
default:
import('./msw')
.then(({ worker, dataProvider }) => {
return worker.start().then(() => dataProvider);
return worker
.start({
quiet: true,
})
.then(() => dataProvider);
})
.then((dataProvider) => {
ReactDom.render(
Expand Down
24 changes: 0 additions & 24 deletions src/BaseServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import type { CollectionItem, Query, QueryFunction } from './types.js';
export abstract class BaseServer {
baseUrl = '';
identifierName = 'id';
loggingEnabled = false;
defaultQuery: QueryFunction = () => ({});
batchUrl: string | null = null;
collections: Record<string, Collection<any>> = {};
singles: Record<string, Single<any>> = {};
getNewId?: () => number | string;
Expand All @@ -25,12 +23,9 @@ export abstract class BaseServer {
defaultQuery = () => ({}),
identifierName = 'id',
getNewId,
loggingEnabled = false,
}: BaseServerOptions = {}) {
this.baseUrl = baseUrl;
this.batchUrl = batchUrl;
this.getNewId = getNewId;
this.loggingEnabled = loggingEnabled;
this.identifierName = identifierName;
this.defaultQuery = defaultQuery;

Expand Down Expand Up @@ -60,31 +55,13 @@ export abstract class BaseServer {
}
}

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

/**
* @param Function ResourceName => object
*/
setDefaultQuery(query: QueryFunction) {
this.defaultQuery = query;
}

setBatchUrl(batchUrl: string) {
this.batchUrl = batchUrl;
}

/**
* @deprecated use setBatchUrl instead
*/
setBatch(url: string) {
console.warn(
'Server.setBatch() is deprecated, use Server.setBatchUrl() instead',
);
this.batchUrl = url;
}

addCollection<T extends CollectionItem = CollectionItem>(
name: string,
collection: Collection<T>,
Expand Down Expand Up @@ -242,7 +219,6 @@ export type BaseServerOptions = {
defaultQuery?: QueryFunction;
identifierName?: string;
getNewId?: () => number | string;
loggingEnabled?: boolean;
};

export type BaseRequest = {
Expand Down
18 changes: 18 additions & 0 deletions src/FetchMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ export class FetchMockServer extends BaseServerWithMiddlewares<
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 @@ -102,6 +116,10 @@ export const getFetchMockHandler = (options: BaseServerOptions) => {
*/
export const FetchServer = FetchMockServer;

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

export type FetchMockFakeRestRequest = Partial<Request> & {
requestBody?: string;
responseText?: string;
Expand Down
15 changes: 15 additions & 0 deletions src/SinonServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ export class SinonServer extends BaseServerWithMiddlewares<
SinonFakeXMLHttpRequest,
SinonFakeRestResponse
> {
loggingEnabled = false;

constructor({ loggingEnabled = false, ...options }: SinonServerOptions) {

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Collection.spec.ts > Collection > getAll > embed query > should throw an error when trying to embed a non-existing collection

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Collection.spec.ts:630:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Collection.spec.ts > Collection > getAll > embed query > should return the original object for missing embed one

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Collection.spec.ts:644:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Collection.spec.ts > Collection > getAll > embed query > should return the object with the reference object for embed one

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Collection.spec.ts:665:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Collection.spec.ts > Collection > getAll > embed query > should throw an error when trying to embed many a non-existing collection

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Collection.spec.ts:689:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Collection.spec.ts > Collection > getAll > embed query > should return the object with an empty array for missing embed many

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Collection.spec.ts:705:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Collection.spec.ts > Collection > getAll > embed query > should return the object with an array of references for embed many

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Collection.spec.ts:727:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Collection.spec.ts > Collection > getAll > embed query > should return the object with an array of references for embed many using inner array

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Collection.spec.ts:764:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Collection.spec.ts > Collection > getAll > embed query > should allow multiple embeds

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Collection.spec.ts:812:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Single.spec.ts > Single > getOnly > embed query > should throw an error when trying to embed a non-existing collection

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Single.spec.ts:22:32

Check failure on line 12 in src/SinonServer.ts

View workflow job for this annotation

GitHub Actions / unit-test

src/Single.spec.ts > Single > getOnly > embed query > should return the original object for missing embed one

TypeError: Cannot read properties of undefined (reading 'loggingEnabled') ❯ new SinonServer src/SinonServer.ts:12:19 ❯ src/Single.spec.ts:34:32
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 @@ -137,6 +148,10 @@ export const getSinonHandler = (options: BaseServerOptions) => {
*/
export const Server = SinonServer;

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

export type SinonFakeRestResponse = {
status: number;
body: any;
Expand Down

0 comments on commit 910c4f9

Please sign in to comment.