Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paginate to koalambda #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/index.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions lib/manipulation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ export declare const putEventToState: (variableName: string) => (ctx: any, next?
export declare const filterProperty: (variableName: string, filterFunction: (record: any) => boolean) => (ctx: any, next?: any) => Promise<void>;
export declare const putEnvVariableToState: (envVariableName: string, variableStatePath?: string) => (ctx: any, next?: any) => Promise<void>;
export declare const executeInOrder: (stopCondition: (ctx: any) => boolean, ...functions: any[]) => (ctx: any, next?: any) => Promise<void>;
/** This function requiers to have two properties specified in the state:
* - 'offset' - number of skipped elemnets,
* - 'count' - number of elements per page
* - 'apiPaginationEndpoint' - the base url (ex. www.a.eu) for pages, result will look like http://www.a.eu/test?offest=1&count=2
*
* It also requires response to have properties:
* - items - The acctual items to return
* - totalCount - total amount of items available
*/
export declare const paginateResponse: (ctx: any, next: any) => Promise<void>;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
},
"dependencies": {
"@types/aws-lambda": "0.0.30",
"api-pagination": "^1.0.6",
"chai-as-promised": "^7.1.1",
"koa-compose": "^4.0.0",
"lodash": "^4.17.4",
Expand Down
54 changes: 54 additions & 0 deletions src/manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import get from 'lodash/get'
import set from 'lodash/set'
import isArray from 'lodash/isArray';
import isFunction from 'lodash/isFunction';
import * as apiPagination from 'api-pagination'
import isNumber from 'lodash/isNumber';
import * as isString from 'lodash/isString';


export const mapPropertyDown = (source: string, target: string, defaultValue?: any) => async (ctx, next?) => {
let value = get(ctx.state, source, defaultValue || null)
Expand Down Expand Up @@ -73,4 +77,54 @@ export const executeInOrder = (stopCondition: (ctx) => boolean, ...functions) =>
}
}
}
}

/** This function requiers to have two properties specified in the state:
* - 'offset' - number of skipped elemnets,
* - 'count' - number of elements per page
* - 'apiPaginationEndpoint' - the base url (ex. www.a.eu) for pages, result will look like http://www.a.eu/test?offest=1&count=2
*
* It also requires response to have properties:
* - items - The acctual items to return
* - totalCount - total amount of items available
*/
export const paginateResponse = async (ctx, next) => {
let offset = ctx.state.offset
let count = ctx.state.count
let apiPaginationEndpoint = ctx.state.apiPaginationEndpoint
let response = ctx.state.response

// Checking input

if(!isNumber(offset) || offset < 0)
throw Error('Missing property "offset" in state object or "offset" <= 0')

if(!isNumber(count) || count <= 0)
throw Error('Missing property "count" in state object or "count" <= 0')

if(!isString(apiPaginationEndpoint))
throw Error('Missing property "apiPaginationEndpoint" in state object or proprty is not string')

if(!response)
throw Error('The response object is null or undefined')

if(!isArray(response.items) || response.items.length === 0)
throw Error('Items should be non empty array')

if(!isNumber(response.totalCount) || response.totalCount < 0)
throw Error('totalCount should be a number > 0')


//Paginate

let pagination: any = {
items: response.items,
requestOffset: offset,
requestCount: count,
totalCount: response.totalCount
}

ctx.state.response = apiPagination.toRest(pagination, apiPaginationEndpoint)

next && await next()
}
Loading