Skip to content

Commit 6d7585e

Browse files
lucaslenzchristianengledercadtChristian Engleder
authored
Meta data for Resource objects (#15)
* Add Meta inside the data object * Fix import order * Update serializer-options.ts Fix import orders * Added tests for the dataMeta tag; Co-authored-by: christianengledercadt <44844093+christianengledercadt@users.noreply.github.com> Co-authored-by: Christian Engleder <ce@cadt.at>
1 parent d4fbab9 commit 6d7585e

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/serializer-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface SerializerOptions {
1414
relationshipLinks?: Links,
1515
relationshipMeta?: RelationshipMeta,
1616
dataLinks?: Links,
17+
dataMeta?: any,
1718
included?: boolean,
1819
includedLinks?: Links,
1920
embed?: boolean,

src/serializer-utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ export class SerializerUtils {
145145
data.links = this.getLinks(this.record, this.opts.dataLinks);
146146
}
147147

148+
// Data meta.
149+
if (this.opts.dataMeta) {
150+
data.meta = this.getMeta(this.record, this.opts.dataMeta);
151+
}
152+
148153
_.each(this.opts.attributes, (attribute) => {
149154
var splittedAttributes = attribute.split(':');
150155

test/serialize.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,40 @@ describe('Options', function () {
195195
});
196196
});
197197

198+
describe('dataMeta', function () {
199+
it('should set the dataMeta key (plain)', function (done) {
200+
var dataSet = {
201+
id: '1',
202+
firstName: 'Sandro',
203+
lastName: 'Munda',
204+
};
205+
206+
var json = new JSONAPISerializer('user', {
207+
attributes: ['firstName', 'lastName'],
208+
dataMeta: { count: 1 }
209+
}).serialize(dataSet);
210+
211+
expect(json.data.meta.count).equal(1);
212+
done();
213+
});
214+
215+
it('should set the dataMeta key (function)', function (done) {
216+
var dataSet = {
217+
id: '1',
218+
firstName: 'Sandro',
219+
lastName: 'Munda',
220+
};
221+
222+
var json = new JSONAPISerializer('user', {
223+
attributes: ['firstName', 'lastName'],
224+
dataMeta: { count: function(record) { return 1; } }
225+
}).serialize(dataSet);
226+
227+
expect(json.data.meta.count).equal(1);
228+
done();
229+
});
230+
});
231+
198232
describe('included', function () {
199233
it('should include or not the compound documents', function (done) {
200234
var dataSet = [{

0 commit comments

Comments
 (0)