Skip to content

Commit

Permalink
Merge pull request #70 from marmelab/servers-parameters
Browse files Browse the repository at this point in the history
Add more parameters to BaseServer
  • Loading branch information
djhi authored Jun 4, 2024
2 parents f09da18 + d9ce1c6 commit 3f97e60
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 16 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var data = {
}
};
// initialize fake REST server
var restServer = new FakeRest.Server();
var restServer = new FakeRest.SinonServer();
restServer.init(data);
// use sinon.js to monkey-patch XmlHttpRequest
Expand Down Expand Up @@ -111,7 +111,7 @@ const data = {
preferred_format: 'hardback',
}
};
const restServer = new FakeRest.FetchServer({ baseUrl: 'http://localhost:3000' });
const restServer = new FakeRest.FetchMockServer({ baseUrl: 'http://localhost:3000' });
restServer.init(data);
fetchMock.mock('begin:http://localhost:3000', restServer.getHandler());
```
Expand Down Expand Up @@ -366,7 +366,7 @@ Operators are specified as suffixes on each filtered field. For instance, applyi

```js
// initialize a rest server with a custom base URL
const restServer = new FakeRest.Server({ baseUrl: 'http://my.custom.domain' }); // only URLs starting with my.custom.domain will be intercepted
const restServer = new FakeRest.SinonServer({ baseUrl: 'http://my.custom.domain' }); // only URLs starting with my.custom.domain will be intercepted
restServer.toggleLogging(); // logging is off by default, enable it to see network calls in the console
// Set all JSON data at once - only if identifier name is 'id'
restServer.init(json);
Expand Down Expand Up @@ -406,7 +406,7 @@ restServer.setDefaultQuery(function(resourceName) {
restServer.setBatchUrl('/batch');

// you can create more than one fake server to listen to several domains
const restServer2 = new FakeRest.Server({ baseUrl: 'http://my.other.domain' });
const restServer2 = new FakeRest.SinonServer({ baseUrl: 'http://my.other.domain' });
// Set data collection by collection - allows to customize the identifier name
const authorsCollection = new FakeRest.Collection({ items: [], identifierName: '_id' });
authorsCollection.addOne({ first_name: 'Leo', last_name: 'Tolstoi' }); // { _id: 0, first_name: 'Leo', last_name: 'Tolstoi' }
Expand All @@ -432,7 +432,7 @@ By default, FakeRest uses an auto incremented sequence for the items identifiers
import FakeRest from 'fakerest';
import uuid from 'uuid';

const restServer = new FakeRest.Server({ baseUrl: 'http://my.custom.domain', getNewId: () => uuid.v5() });
const restServer = new FakeRest.SinonServer({ baseUrl: 'http://my.custom.domain', getNewId: () => uuid.v5() });
```

This can also be specified at the collection level:
Expand All @@ -441,7 +441,7 @@ This can also be specified at the collection level:
import FakeRest from 'fakerest';
import uuid from 'uuid';

const restServer = new FakeRest.Server({ baseUrl: 'http://my.custom.domain' });
const restServer = new FakeRest.SinonServer({ baseUrl: 'http://my.custom.domain' });
const authorsCollection = new FakeRest.Collection({ items: [], identifierName: '_id', getNewId: () => uuid.v5() });
```

Expand Down
23 changes: 21 additions & 2 deletions src/BaseServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { CollectionItem, Query, QueryFunction } from './types.js';

export class BaseServer {
baseUrl: string | null = null;
identifierName = 'id';
loggingEnabled = false;
defaultQuery: QueryFunction = () => ({});
batchUrl: string | null = null;
Expand All @@ -13,17 +14,35 @@ export class BaseServer {

constructor({
baseUrl = '',
batchUrl = null,
data,
defaultQuery = () => ({}),
identifierName = 'id',
getNewId,
loggingEnabled = false,
}: {
baseUrl?: string;
batchUrl?: string | null;
data?: Record<string, CollectionItem[] | CollectionItem>;
defaultQuery?: QueryFunction;
identifierName?: string;
getNewId?: () => number | string;
loggingEnabled?: boolean;
} = {}) {
this.baseUrl = baseUrl;
this.batchUrl = batchUrl;
this.getNewId = getNewId;
this.loggingEnabled = loggingEnabled;
this.identifierName = identifierName;
this.defaultQuery = defaultQuery;

if (data) {
this.init(data);
}
}

/**
* Shortcut for adding several collections if identifierName is always 'id'
* Shortcut for adding several collections if identifierName is always the same
*/
init(data: Record<string, CollectionItem[] | CollectionItem>) {
for (const name in data) {
Expand All @@ -33,7 +52,7 @@ export class BaseServer {
name,
new Collection({
items: value,
identifierName: 'id',
identifierName: this.identifierName,
getNewId: this.getNewId,
}),
);
Expand Down
2 changes: 1 addition & 1 deletion src/Collection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection } from './Collection.js';
import { Server } from './Server.js';
import { Server } from './SinonServer.js';
import type { CollectionItem } from './types.js';

describe('Collection', () => {
Expand Down
14 changes: 11 additions & 3 deletions src/FakeRest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Server } from './Server.js';
import { FetchServer } from './FetchServer.js';
import { Server, SinonServer } from './SinonServer.js';
import { FetchServer, FetchMockServer } from './FetchMockServer.js';
import { Collection } from './Collection.js';
import { Single } from './Single.js';
import { getMswHandlers } from './msw.js';
import { get } from 'lodash';

export { Server, FetchServer, Collection, Single, getMswHandlers };
export {
Server,
SinonServer,
FetchServer,
FetchMockServer,
Collection,
Single,
getMswHandlers,
};
export default { Server, FetchServer, Collection, Single, getMswHandlers };
7 changes: 6 additions & 1 deletion src/FetchServer.ts → src/FetchMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BaseServer } from './BaseServer.js';
import { parseQueryString } from './parseQueryString.js';
import type { MockResponse, MockResponseObject } from 'fetch-mock';

export class FetchServer extends BaseServer {
export class FetchMockServer extends BaseServer {
requestInterceptors: FetchMockRequestInterceptor[] = [];
responseInterceptors: FetchMockResponseInterceptor[] = [];

Expand Down Expand Up @@ -113,6 +113,11 @@ export class FetchServer extends BaseServer {
}
}

/**
* @deprecated Use FetchServer instead
*/
export const FetchServer = FetchMockServer;

export type FetchMockFakeRestRequest = Partial<Request> & {
requestBody?: string;
responseText?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/Single.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Single } from './Single.js';
import { Collection } from './Collection.js';
import { Server } from './Server.js';
import { Server } from './SinonServer.js';

describe('Single', () => {
describe('constructor', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/Server.spec.ts → src/SinonServer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sinon, { type SinonFakeXMLHttpRequest } from 'sinon';

import { type SinonFakeRestRequest, Server } from './Server.js';
import { type SinonFakeRestRequest, Server } from './SinonServer.js';
import { Single } from './Single.js';
import { Collection } from './Collection.js';

Expand Down
7 changes: 6 additions & 1 deletion src/Server.ts → src/SinonServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SinonFakeXMLHttpRequest } from 'sinon';
import { BaseServer } from './BaseServer.js';
import { parseQueryString } from './parseQueryString.js';

export class Server extends BaseServer {
export class SinonServer extends BaseServer {
requestInterceptors: SinonRequestInterceptor[] = [];
responseInterceptors: SinonResponseInterceptor[] = [];

Expand Down Expand Up @@ -208,6 +208,11 @@ export class Server extends BaseServer {
}
}

/**
* @deprecated Use SinonServer instead
*/
export const Server = SinonServer;

export type SinonFakeRestRequest = Partial<SinonFakeXMLHttpRequest> & {
requestBody?: string;
responseText?: string;
Expand Down

0 comments on commit 3f97e60

Please sign in to comment.