Skip to content

Commit

Permalink
feat: allow createEntry with direct loading of nested elements CMS-2905
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-scherzinger committed Aug 25, 2017
1 parent b361763 commit b7e0cd6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/PublicAPI.js
Expand Up @@ -511,7 +511,7 @@ export default class PublicAPI extends Core {
* @param {object} entry object representing the entry.
* @returns {Promise<EntryResource>} the newly created EntryResource
*/
createEntry(model, entry) {
createEntry(model, entry, levels) {
return Promise.resolve()
.then(() => {
if (!model) {
Expand All @@ -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));
}

Expand Down
45 changes: 45 additions & 0 deletions test/publicAPI/PublicAPI.test.js
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion typings/PublicAPI.d.ts
Expand Up @@ -46,7 +46,7 @@ export declare class PublicAPI extends Core {

entry(model: string, id: string, options: number | { _levels?: number, _fields?: number }): Promise<EntryResource>;

createEntry(model: string, entry: any): Promise<EntryResource>;
createEntry(model: string, entry: any, levels?: number): Promise<EntryResource>;

checkPermission(permission: string, refresh?: boolean): Promise<boolean>;

Expand Down

0 comments on commit b7e0cd6

Please sign in to comment.