nestjs-paginate
Version:
Pagination and filtering helper method for TypeORM repositories or query builders using Nest.js framework.
191 lines • 6.84 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("@nestjs/common/constants");
const decorator_1 = require("./decorator");
// eslint-disable-next-line @typescript-eslint/ban-types
function getParamDecoratorFactory(decorator) {
class Test {
test(_value) {
//
}
}
__decorate([
__param(0, decorator()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], Test.prototype, "test", null);
const args = Reflect.getMetadata(constants_1.ROUTE_ARGS_METADATA, Test, 'test');
return args[Object.keys(args)[0]].factory;
}
const decoratorfactory = getParamDecoratorFactory(decorator_1.Paginate);
function expressContextFactory(query) {
const mockContext = {
getType: () => 'http',
switchToHttp: () => Object({
getRequest: () => Object({
protocol: 'http',
get: () => 'localhost',
originalUrl: '/items?search=2423',
query: query,
}),
}),
getClass: () => {
throw new Error('Function not implemented.');
},
getHandler: () => {
throw new Error('Function not implemented.');
},
getArgs: () => {
throw new Error('Function not implemented.');
},
getArgByIndex: () => {
throw new Error('Function not implemented.');
},
switchToRpc: () => {
throw new Error('Function not implemented.');
},
switchToWs: () => {
throw new Error('Function not implemented.');
},
};
return mockContext;
}
function fastifyContextFactory(query) {
const mockContext = {
getType: () => 'http',
switchToHttp: () => Object({
getRequest: () => Object({
protocol: 'http',
hostname: 'localhost',
url: '/items?search=2423',
originalUrl: '/items?search=2423',
query: query,
}),
}),
getClass: () => {
throw new Error('Function not implemented.');
},
getHandler: () => {
throw new Error('Function not implemented.');
},
getArgs: () => {
throw new Error('Function not implemented.');
},
getArgByIndex: () => {
throw new Error('Function not implemented.');
},
switchToRpc: () => {
throw new Error('Function not implemented.');
},
switchToWs: () => {
throw new Error('Function not implemented.');
},
};
return mockContext;
}
describe('Decorator', () => {
it('should handle express undefined query fields', () => {
const context = expressContextFactory({});
const result = decoratorfactory(null, context);
expect(result).toStrictEqual({
page: undefined,
limit: undefined,
sortBy: undefined,
search: undefined,
searchBy: undefined,
filter: undefined,
select: undefined,
cursor: undefined,
path: 'http://localhost/items',
});
});
it('should handle fastify undefined query fields', () => {
const context = fastifyContextFactory({});
const result = decoratorfactory(null, context);
expect(result).toStrictEqual({
page: undefined,
limit: undefined,
sortBy: undefined,
search: undefined,
searchBy: undefined,
filter: undefined,
select: undefined,
cursor: undefined,
path: 'http://localhost/items',
});
});
it('should handle express defined query fields', () => {
const context = expressContextFactory({
page: '1',
limit: '20',
sortBy: ['id:ASC', 'createdAt:DESC'],
search: 'white',
'filter.name': '$not:$eq:Kitty',
'filter.createdAt': ['$gte:2020-01-01', '$lte:2020-12-31'],
select: ['name', 'createdAt'],
cursor: 'abc123',
});
const result = decoratorfactory(null, context);
expect(result).toStrictEqual({
page: 1,
limit: 20,
sortBy: [
['id', 'ASC'],
['createdAt', 'DESC'],
],
search: 'white',
searchBy: undefined,
select: ['name', 'createdAt'],
path: 'http://localhost/items',
filter: {
name: '$not:$eq:Kitty',
createdAt: ['$gte:2020-01-01', '$lte:2020-12-31'],
},
cursor: 'abc123',
});
});
it('should handle fastify defined query fields', () => {
const context = fastifyContextFactory({
page: '1',
limit: '20',
sortBy: ['id:ASC', 'createdAt:DESC'],
search: 'white',
'filter.name': '$not:$eq:Kitty',
'filter.createdAt': ['$gte:2020-01-01', '$lte:2020-12-31'],
select: ['name', 'createdAt'],
cursor: 'abc123',
});
const result = decoratorfactory(null, context);
expect(result).toStrictEqual({
page: 1,
limit: 20,
sortBy: [
['id', 'ASC'],
['createdAt', 'DESC'],
],
search: 'white',
searchBy: undefined,
path: 'http://localhost/items',
filter: {
name: '$not:$eq:Kitty',
createdAt: ['$gte:2020-01-01', '$lte:2020-12-31'],
},
select: ['name', 'createdAt'],
cursor: 'abc123',
});
});
});
//# sourceMappingURL=decorator.spec.js.map