Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
fix(repository): exclude hidden properties in response body
Browse files Browse the repository at this point in the history
closes #3
  • Loading branch information
shubhamp-sf committed Oct 28, 2022
1 parent 9368548 commit 3e254fd
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/sequelize/sequelize.repository.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class SequelizeRepository<
options?: AnyObject,
): Promise<T> {
const data = await this.sequelizeModel.create(entity, options);
return data.toJSON();
return this.excludeHiddenProps(data.toJSON());
}

// `updateById` is not implemented separately because the existing one in
Expand Down Expand Up @@ -105,7 +105,7 @@ export class SequelizeRepository<
...options,
});
return data.map(entity => {
return entity.toJSON();
return this.excludeHiddenProps(entity.toJSON());
});
}

Expand Down Expand Up @@ -134,7 +134,7 @@ export class SequelizeRepository<
throw new EntityNotFoundError(this.entityClass, id);
}
// TODO: include relations in object
return data.toJSON() as T & Relations;
return this.excludeHiddenProps(data.toJSON());
}

replaceById(
Expand Down Expand Up @@ -385,4 +385,22 @@ export class SequelizeRepository<
}
return sequelizeDefinition;
}

/**
* Remove hidden properties specified in model from response body. (See: https://github.com/sourcefuse/loopback4-sequelize/issues/3)
* @param entity normalized entity. You can use `entity.toJSON()`'s value
* @returns normalized entity excluding the hiddenProperties
*/
private excludeHiddenProps(entity: T & Relations): T & Relations {
const hiddenProps = this.entityClass.definition.settings.hiddenProperties;
if (!hiddenProps) {
return entity;
}

for (const propertyName of hiddenProps as Array<keyof typeof entity>) {
delete entity[propertyName];
}

return entity;
}
}

0 comments on commit 3e254fd

Please sign in to comment.