Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
andersondanilo committed Apr 9, 2020
1 parent fe694ef commit 05c7657
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions dist-esm/Options.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default interface Options {
idKey?: string;
fields: any;
}
10 changes: 8 additions & 2 deletions dist-esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function serialize(ctx) {
};
}
function serializeEntity(entity, transformer, options, includedByType) {
const attributes = Object.assign({}, transformer.transform(entity, options));
let attributes = Object.assign({}, transformer.transform(entity, options));
const idKey = options.idKey || 'id';
const id = attributes[idKey];
const id = attributes[idKey] || entity[idKey];
if (!id) {
throw new JsonApiError('Resource without id');
}
Expand All @@ -44,6 +44,12 @@ function serializeEntity(entity, transformer, options, includedByType) {
: serializeRelation(ctx.input, ctx.transformer, options, ctx.included, includedByType)
};
}
if (options.fields && options.fields[transformer.type]) {
const allowed = typeof (options.fields[transformer.type]) === 'string'
? options.fields[transformer.type].split(',')
: options.fields[transformer.type].split(',');
attributes = whitelist(attributes, allowed);
}
const data = {
id,
type: transformer.type,
Expand Down
1 change: 1 addition & 0 deletions dist/Options.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default interface Options {
idKey?: string;
fields: any;
}
10 changes: 8 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ function serialize(ctx) {
};
}
function serializeEntity(entity, transformer, options, includedByType) {
const attributes = Object.assign({}, transformer.transform(entity, options));
let attributes = Object.assign({}, transformer.transform(entity, options));
const idKey = options.idKey || 'id';
const id = attributes[idKey];
const id = attributes[idKey] || entity[idKey];
if (!id) {
throw new JsonApiError_1.default('Resource without id');
}
Expand All @@ -49,6 +49,12 @@ function serializeEntity(entity, transformer, options, includedByType) {
: serializeRelation(ctx.input, ctx.transformer, options, ctx.included, includedByType)
};
}
if (options.fields && options.fields[transformer.type]) {
const allowed = typeof (options.fields[transformer.type]) === 'string'
? options.fields[transformer.type].split(',')
: options.fields[transformer.type].split(',');
attributes = whitelist(attributes, allowed);
}
const data = {
id,
type: transformer.type,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-fractal",
"version": "1.0.6",
"version": "1.0.7",
"description": "JSON:API Serializer inspired by Fractal (PHP)",
"license": "MIT",
"main": "dist/index",
Expand Down

0 comments on commit 05c7657

Please sign in to comment.