diff --git a/src/PublicAPI.js b/src/PublicAPI.js index adcbe3c2..0d0b90b2 100644 --- a/src/PublicAPI.js +++ b/src/PublicAPI.js @@ -511,7 +511,7 @@ export default class PublicAPI extends Core { * @param {object} entry object representing the entry. * @returns {Promise} the newly created EntryResource */ - createEntry(model, entry) { + createEntry(model, entry, levels) { return Promise.resolve() .then(() => { if (!model) { @@ -521,11 +521,21 @@ export default class PublicAPI extends Core { if (!entry) { throw new Error('Cannot create resource with undefined object.'); } + + if (levels < 1 || levels >= 5) { + throw new Error('levels must be between 1 and 5'); + } + return this.link(`${this[shortIDSymbol]}:${model}`); }) .then(link => validator.validate(entry, `${link.profile}?template=post`)) .then(() => this.follow(`${this[shortIDSymbol]}:${model}`)) - .then(request => post(this[environmentSymbol], request, entry)) + .then(request => { + if (levels) { + request.withTemplateParameters({ _levels: levels }); + } + return post(this[environmentSymbol], request, entry) + }) .then(([res, traversal]) => createEntry(res, this[environmentSymbol], traversal)); } diff --git a/test/publicAPI/PublicAPI.test.js b/test/publicAPI/PublicAPI.test.js index d198056c..89629485 100644 --- a/test/publicAPI/PublicAPI.test.js +++ b/test/publicAPI/PublicAPI.test.js @@ -531,6 +531,43 @@ describe('PublicAPI', () => { throw err; }); }); + it('should create entry, levels', () => { + const getStub = sinon.stub(helper, 'get'); + getStub.returns(resolver('public-dm-root.json')); + const postStub = sinon.stub(helper, 'post'); + postStub.returns(resolver('public-entry.json')); + + return api.createEntry('allFields', { + text: 'hehe', + formattedText: 'hehe', + number: 1, + decimal: 1.1, + boolean: true, + datetime: new Date().toISOString(), + location: { + latitude: 0, + longitude: 0, + }, + email: 'someone@anything.com', + url: 'https://anything.com', + phone: '+49 11 8 33', + json: {}, + entry: '1234567', + entries: [ + '1234567', + ], + }, 1) + .then((entry) => { + entry.should.be.instanceOf(EntryResource); + getStub.restore(); + postStub.restore(); + }) + .catch((err) => { + getStub.restore(); + postStub.restore(); + throw err; + }); + }); it('should throw on undefined model', () => { return api.createEntry() .should.be.rejectedWith('model must be defined'); @@ -543,6 +580,14 @@ describe('PublicAPI', () => { return api.createEntry('allFields', {}) .should.be.rejectedWith('JSON Schema Validation error'); }); + it('should throw on levels 0', () => { + return api.createEntry('allFields', {}, 0) + .should.be.rejectedWith('levels must be between 1 and 5'); + }); + it('should throw on levels 5', () => { + return api.createEntry('allFields', {}, 5) + .should.be.rejectedWith('levels must be between 1 and 5'); + }); it('should load asset list', () => { const stub = sinon.stub(helper, 'get'); diff --git a/typings/PublicAPI.d.ts b/typings/PublicAPI.d.ts index 69f498d9..c2b29557 100644 --- a/typings/PublicAPI.d.ts +++ b/typings/PublicAPI.d.ts @@ -46,7 +46,7 @@ export declare class PublicAPI extends Core { entry(model: string, id: string, options: number | { _levels?: number, _fields?: number }): Promise; - createEntry(model: string, entry: any): Promise; + createEntry(model: string, entry: any, levels?: number): Promise; checkPermission(permission: string, refresh?: boolean): Promise;