Skip to content

Commit 3520e97

Browse files
committed
change definition of Server and Collection constructors to use objects
1 parent f5e2506 commit 3520e97

File tree

8 files changed

+466
-380
lines changed

8 files changed

+466
-380
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const data = {
111111
preferred_format: 'hardback',
112112
}
113113
};
114-
const restServer = new FakeRest.FetchServer('http://localhost:3000');
114+
const restServer = new FakeRest.FetchServer({ baseUrl: 'http://localhost:3000' });
115115
restServer.init(data);
116116
fetchMock.mock('begin:http://localhost:3000', restServer.getHandler());
117117
```
@@ -366,7 +366,7 @@ Operators are specified as suffixes on each filtered field. For instance, applyi
366366

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

408408
// you can create more than one fake server to listen to several domains
409-
const restServer2 = new FakeRest.Server('http://my.other.domain');
409+
const restServer2 = new FakeRest.Server({ baseUrl: 'http://my.other.domain' });
410410
// Set data collection by collection - allows to customize the identifier name
411-
const authorsCollection = new FakeRest.Collection([], '_id');
411+
const authorsCollection = new FakeRest.Collection({ items: [], identifierName: '_id' });
412412
authorsCollection.addOne({ first_name: 'Leo', last_name: 'Tolstoi' }); // { _id: 0, first_name: 'Leo', last_name: 'Tolstoi' }
413413
authorsCollection.addOne({ first_name: 'Jane', last_name: 'Austen' }); // { _id: 1, first_name: 'Jane', last_name: 'Austen' }
414414
// collections have auto incremented identifiers by default but accept identifiers already set
@@ -432,7 +432,7 @@ By default, FakeRest uses an auto incremented sequence for the items identifiers
432432
import FakeRest from 'fakerest';
433433
import uuid from 'uuid';
434434

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

438438
This can also be specified at the collection level:
@@ -441,8 +441,8 @@ This can also be specified at the collection level:
441441
import FakeRest from 'fakerest';
442442
import uuid from 'uuid';
443443

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

448448
## Development

example/fetchMock.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import FakeRest from 'fakerest';
33
import { data } from './data';
44

55
export const initializeFetchMock = () => {
6-
const restServer = new FakeRest.FetchServer('http://localhost:3000');
6+
const restServer = new FakeRest.FetchServer({
7+
baseUrl: 'http://localhost:3000',
8+
});
79
if (window) {
810
// @ts-ignore
911
window.restServer = restServer; // give way to update data in the console

src/BaseServer.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ export class BaseServer {
1111
singles: Record<string, Single<any>> = {};
1212
getNewId?: () => number | string;
1313

14-
constructor(baseUrl = '', getNewId?: () => number | string) {
14+
constructor({
15+
baseUrl = '',
16+
getNewId,
17+
}: {
18+
baseUrl?: string;
19+
getNewId?: () => number | string;
20+
} = {}) {
1521
this.baseUrl = baseUrl;
1622
this.getNewId = getNewId;
1723
}
@@ -25,7 +31,11 @@ export class BaseServer {
2531
if (Array.isArray(value)) {
2632
this.addCollection(
2733
name,
28-
new Collection(value, 'id', this.getNewId),
34+
new Collection({
35+
items: value,
36+
identifierName: 'id',
37+
getNewId: this.getNewId,
38+
}),
2939
);
3040
} else {
3141
this.addSingle(name, new Single(value));
@@ -116,7 +126,11 @@ export class BaseServer {
116126
if (!Object.prototype.hasOwnProperty.call(this.collections, name)) {
117127
this.addCollection(
118128
name,
119-
new Collection([] as CollectionItem[], 'id', this.getNewId),
129+
new Collection({
130+
items: [],
131+
identifierName: 'id',
132+
getNewId: this.getNewId,
133+
}),
120134
);
121135
}
122136
return this.collections[name].addOne(item);

0 commit comments

Comments
 (0)