Skip to content

Commit

Permalink
Add ability to addOne on a non-initialized collection
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Sep 30, 2020
1 parent 63be85d commit 627cee0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 57 deletions.
7 changes: 4 additions & 3 deletions dist/FakeRest.min.js

Large diffs are not rendered by default.

116 changes: 62 additions & 54 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export default class Server {
}

addOne(name, item) {
if (!this.collections.hasOwnProperty(name)) {
this.addCollection(name, new Collection([], 'id'));
}
return this.collections[name].addOne(item);
}

Expand Down Expand Up @@ -273,65 +276,70 @@ export default class Server {
}

// Handle collections
for (let name of this.getCollectionNames()) {
let matches = request.url.match(new RegExp('^' + this.baseUrl + '\\/(' + name + ')(\\/(\\d+))?(\\?.*)?$' ));
if (!matches) continue;
let params = assign({}, this.defaultQuery(name), request.params);
if (!matches[2]) {
if (request.method == 'GET') {
let count = this.getCount(name, params.filter ? { filter: params.filter } : {});
let items, contentRange, status;
if (count > 0) {
items = this.getAll(name, params);
let first = params.range ? params.range[0] : 0;
let last = params.range ? Math.min(items.length - 1 + first, params.range[1]) : (items.length - 1);
contentRange = `items ${first}-${last}/${count}`;
status = (items.length == count) ? 200 : 206;
} else {
items = [];
contentRange = 'items */0';
status = 200;
}
return this.respond(items, { 'Content-Range': contentRange }, request, status);
let matches = request.url.match(new RegExp('^' + this.baseUrl + '\\/([^\\/?]+)(\\/(\\d+))?(\\?.*)?$' ));
if (!matches) return;
let name = matches[1];
let params = assign({}, this.defaultQuery(name), request.params);
if (!matches[2]) {
if (request.method == 'GET') {
if (!this.getCollection(name)) {
return;
}
if (request.method == 'POST') {
let newResource = this.addOne(name, request.json);
let newResourceURI = this.baseUrl + '/' + name + '/' + newResource[this.getCollection(name).identifierName];
return this.respond(newResource, { Location: newResourceURI }, request, 201);
let count = this.getCount(name, params.filter ? { filter: params.filter } : {});
let items, contentRange, status;
if (count > 0) {
items = this.getAll(name, params);
let first = params.range ? params.range[0] : 0;
let last = params.range ? Math.min(items.length - 1 + first, params.range[1]) : (items.length - 1);
contentRange = `items ${first}-${last}/${count}`;
status = (items.length == count) ? 200 : 206;
} else {
items = [];
contentRange = 'items */0';
status = 200;
}
} else {
let id = matches[3];
if (request.method == 'GET') {
try {
let item = this.getOne(name, id, params);
return this.respond(item, null, request);
} catch (error) {
return request.respond(404);
}
return this.respond(items, { 'Content-Range': contentRange }, request, status);
}
if (request.method == 'POST') {
let newResource = this.addOne(name, request.json);
let newResourceURI = this.baseUrl + '/' + name + '/' + newResource[this.getCollection(name).identifierName];
return this.respond(newResource, { Location: newResourceURI }, request, 201);
}
} else {
if (!this.getCollection(name)) {
return;
}
let id = matches[3];
if (request.method == 'GET') {
try {
let item = this.getOne(name, id, params);
return this.respond(item, null, request);
} catch (error) {
return request.respond(404);
}
if (request.method == 'PUT') {
try {
let item = this.updateOne(name, id, request.json);
return this.respond(item, null, request);
} catch (error) {
return request.respond(404);
}
}
if (request.method == 'PUT') {
try {
let item = this.updateOne(name, id, request.json);
return this.respond(item, null, request);
} catch (error) {
return request.respond(404);
}
if (request.method == 'PATCH') {
try {
let item = this.updateOne(name, id, request.json);
return this.respond(item, null, request);
} catch (error) {
return request.respond(404);
}
}
if (request.method == 'PATCH') {
try {
let item = this.updateOne(name, id, request.json);
return this.respond(item, null, request);
} catch (error) {
return request.respond(404);
}
if (request.method == 'DELETE') {
try {
let item = this.removeOne(name, id);
return this.respond(item, null, request);
} catch (error) {
return request.respond(404);
}
}
if (request.method == 'DELETE') {
try {
let item = this.removeOne(name, id);
return this.respond(item, null, request);
} catch (error) {
return request.respond(404);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/src/Server-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@
expect(server.getAll('foo')).toEqual([{id: 1, name: 'foo'}, {id: 2, name: 'bar'}, {id: 3, name: 'baz'}]);
});

it('should respond to POST /foo by adding an item to collection foo, even if the collection does not exist', function() {
var server = new Server();
var request = getFakeXMLHTTPRequest('POST', '/foo', JSON.stringify({name: 'baz'}));
server.handle(request);
expect(request.status).toEqual(201);
expect(request.responseText).toEqual('{"name":"baz","id":0}');
expect(request.getResponseHeader('Content-Type')).toEqual('application/json');
expect(request.getResponseHeader('Location')).toEqual('/foo/0');
expect(server.getAll('foo')).toEqual([{id: 0, name: 'baz'}]);
});

it('should respond to GET /foo/:id by sending element of identifier id in collection foo', function() {
var server = new Server();
server.addCollection('foo', new Collection([{id: 1, name: 'foo'}, {id: 2, name: 'bar'}]));
Expand Down

0 comments on commit 627cee0

Please sign in to comment.