Skip to content

Commit

Permalink
Introduce offset-based pagination support
Browse files Browse the repository at this point in the history
  • Loading branch information
manomintis authored and manomintis committed Apr 3, 2024
1 parent 0d0ad7d commit 3fc9891
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ Array of models:
]
```


## Pagination support


Offset-based pagination is supported. Example usage:

```typescript
const users = await User.query().paginate(1, 10)
return new UserResource(users).refine()
```


[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/manomintis/adonis-api-resources/test.yml?style=for-the-badge
[gh-workflow-url]: https://github.com/manomintis/adonis-api-resources/actions/workflows/test.yml "Github action"

Expand Down
13 changes: 10 additions & 3 deletions src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ export abstract class Resource {

// Return refined output
refine(): object {
if (!Array.isArray(this.data)) {
return this.refine_entity(this.data)
} else {
if ('rows' in this.data && 'currentPage' in this.data) {
const meta = (this.data as any).getMeta()
const collection = this.refine_collection((this.data as any).rows)
const result = {
meta: meta,
data: collection,
}
return result
} else if (Array.isArray(this.data)) {
return this.refine_collection(this.data)
}
return this.refine_entity(this.data)
}
}
48 changes: 44 additions & 4 deletions tests/resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,55 @@ class TestResourceResource extends Resource {
}
}

const testObject = {
const testObjectEntity = {
firstName: 'John',
lastName: 'Doe',
}

const result: any = new TestResourceResource(testObject).refine()
const testObjectCollection = [
{
firstName: 'John',
lastName: 'Doe',
},
{
firstName: 'Jane',
lastName: 'Doe',
},
]

class ObjectPaginated {
currentPage = 1
rows = [
{
firstName: 'John',
lastName: 'Doe',
},
{
firstName: 'Jane',
lastName: 'Doe',
},
]
getMeta() {
return { currentPage: 1 }
}
}
const testObjectPaginated = new ObjectPaginated()

const resultEntity: any = new TestResourceResource(testObjectEntity).refine()
const resultCollection: any = new TestResourceResource(testObjectCollection).refine()
const resultPaginated: any = new TestResourceResource(testObjectPaginated).refine()

test.group('Resource', () => {
test('create full name', ({ assert }) => {
assert.equal(result.fullName, 'John Doe')
test('test_entity', ({ assert }) => {
assert.equal(resultEntity.fullName, 'John Doe')
})
test('test_collection', ({ assert }) => {
assert.equal(resultCollection[0].fullName, 'John Doe')
assert.equal(resultCollection[1].fullName, 'Jane Doe')
})
test('test_pagination', ({ assert }) => {
assert.equal(resultPaginated.data[0].fullName, 'John Doe')
assert.equal(resultPaginated.data[1].fullName, 'Jane Doe')
assert.equal(resultPaginated.meta.currentPage, 1)
})
})

0 comments on commit 3fc9891

Please sign in to comment.