Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.

feat: Adds constructFilter to config. #3

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export interface TodoEntity extends Entity {
import factory from '@js-entity-repos/express/dist/factory';

const todosFacade = factory<TodoEntity>({
// Optional property that determines the default pagination limit.
// Optional property that modifies a filter for the service.
constructFilter: (filter) => {
return filter;
},
// Optional property.
defaultPaginationLimit: 10,
// Optional property that catches errors from handlers.
errorCatcher: (handler) => (req, res) => {
Expand Down
2 changes: 2 additions & 0 deletions src/FacadeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Facade from '@js-entity-repos/core/dist/Facade';
import Entity from '@js-entity-repos/core/dist/types/Entity';
import Filter from '@js-entity-repos/core/dist/types/Filter';
import ErrorCatcher from './utils/ErrorCatcher';

export default interface FacadeConfig<E extends Entity> {
readonly constructFilter: (filter: Filter<E>) => any;
readonly service: Facade<E>;
readonly errorCatcher: ErrorCatcher;
readonly defaultPaginationLimit: number;
Expand Down
1 change: 1 addition & 0 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import catchErrors from './utils/catchErrors';

export default <E extends Entity>(factoryConfig: FactoryConfig<E>): Router => {
const facadeConfig: FacadeConfig<E> = {
constructFilter: (filter) => filter,
defaultPaginationLimit: 10,
errorCatcher: catchErrors,
...factoryConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/functions/countEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getJsonQueryParam from '../utils/getJsonQueryParam';
export default <E extends Entity>(config: FacadeConfig<E>) => {
return catchErrors(async (req: Request, res: Response) => {
const { count } = await config.service.countEntities({
filter: getJsonQueryParam(req.query, 'filter'),
filter: config.constructFilter(getJsonQueryParam(req.query, 'filter')),
});
res.status(OK).json(count);
});
Expand Down
2 changes: 1 addition & 1 deletion src/functions/getEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default <E extends Entity>(config: FacadeConfig<E>) => {
return catchErrors(async (req: Request, res: Response) => {
const limit = getNumberQueryParam(req.query, 'limit', config.defaultPaginationLimit);
const result = await config.service.getEntities({
filter: getJsonQueryParam(req.query, 'filter'),
filter: config.constructFilter(getJsonQueryParam(req.query, 'filter')),
pagination: {
cursor: req.query.cursor,
forward: req.query.forward === 'true',
Expand Down
2 changes: 1 addition & 1 deletion src/functions/getEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getJsonQueryParam from '../utils/getJsonQueryParam';
export default <E extends Entity>(config: FacadeConfig<E>) => {
return catchErrors(async (req: Request, res: Response) => {
const { entity } = await config.service.getEntity({
filter: getJsonQueryParam(req.query, 'filter'),
filter: config.constructFilter(getJsonQueryParam(req.query, 'filter')),
id: req.params.id,
});
res.status(OK).json(entity);
Expand Down
2 changes: 1 addition & 1 deletion src/functions/patchEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getJsonQueryParam from '../utils/getJsonQueryParam';
export default <E extends Entity>(config: FacadeConfig<E>) => {
return catchErrors(async (req: Request, res: Response) => {
const { entity } = await config.service.patchEntity({
filter: getJsonQueryParam(req.query, 'filter'),
filter: config.constructFilter(getJsonQueryParam(req.query, 'filter')),
id: req.params.id,
patch: req.body,
});
Expand Down
2 changes: 1 addition & 1 deletion src/functions/removeEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getJsonQueryParam from '../utils/getJsonQueryParam';
export default <E extends Entity>(config: FacadeConfig<E>) => {
return catchErrors(async (req: Request, res: Response) => {
await config.service.removeEntities({
filter: getJsonQueryParam(req.query, 'filter'),
filter: config.constructFilter(getJsonQueryParam(req.query, 'filter')),
});
res.status(NO_CONTENT).send();
});
Expand Down
2 changes: 1 addition & 1 deletion src/functions/removeEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import getJsonQueryParam from '../utils/getJsonQueryParam';
export default <E extends Entity>(config: FacadeConfig<E>) => {
return catchErrors(async (req: Request, res: Response) => {
await config.service.removeEntity({
filter: getJsonQueryParam(req.query, 'filter'),
filter: config.constructFilter(getJsonQueryParam(req.query, 'filter')),
id: req.params.id,
});
res.status(NO_CONTENT).send();
Expand Down
2 changes: 1 addition & 1 deletion src/functions/replaceEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default <E extends Entity>(config: FacadeConfig<E>) => {
return catchErrors(async (req: Request, res: Response) => {
const { entity } = await config.service.replaceEntity({
entity: req.body,
filter: getJsonQueryParam(req.query, 'filter'),
filter: config.constructFilter(getJsonQueryParam(req.query, 'filter')),
id: req.params.id,
});
res.status(OK).json(entity);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getJsonQueryParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import Query from './Query';

export default (data: Query, paramName: string) => {
const paramValue = data[paramName];
return paramValue === undefined ? undefined : parseJson(paramValue, [paramName]);
return paramValue === undefined ? {} : parseJson(paramValue, [paramName]);
};