Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
feat(applist)!: added pagination
Browse files Browse the repository at this point in the history
The UPS server application list endpoint returns a paginated result (7 items per page).
Without paginating, it was impossible to see any application after the first 7: a new `page` parameter has been added to the list command to allow seeing the following pages.

BREAKING CHANGE: now the list function takes a different set of parameters: instead of just the `filter` it now takes an object with keys `filter` and `page`. The syntax for
filter is the same as before, while the page is just a number representing the page number to be returned
  • Loading branch information
ziccardi committed May 18, 2020
1 parent dff39d8 commit 166d185
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 19 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -77,9 +77,17 @@ optionally a filter parameter can be specified. Applications can be filtered by:
* **pushApplicationID**
* **developer**

The result is returned one page at a time. Each page will contain 7 applications. To specify which page to return, the
`page` parameter must be specified:

```typescript
const apps = upsadm.applications.find({filter: {developer: 'developer'}, page: 2 })
```

**NOTE** You are not limited to use only one key when filtering: if more than one is specified, they are all applied. The only
exception is the `id` filter: if the `id` is specified, all the other filters are ignored.


To create an application we will use the `create` method:
```typescript
const app = upsadm.applications.create('newApp')
Expand Down
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -47,4 +47,4 @@
"bugs": {
"url": "https://github.com/aerogear/unifiedpush-admin-client/issues"
}
}
}
8 changes: 5 additions & 3 deletions src/UnifiedPushAdminClient.ts
Expand Up @@ -69,9 +69,11 @@ export class UnifiedPushAdminClient {
/**
* Finds application
* @param filter a filter to be used to find applications. If not specified, all applications are returned.
* @param page the number of the page to be visualised
*/
find: async (filter?: PushApplicationSearchOptions): Promise<PushApplication[]> =>
this.applicationsAdmin.find(await this.auth(), filter),
find: async ({filter, page}: {filter?: PushApplicationSearchOptions; page?: number} = {}): Promise<
PushApplication[]
> => this.applicationsAdmin.find(await this.auth(), {filter, page}),
/**
* Creates an application in the UPS
* @param name the name of the application to be created
Expand All @@ -84,7 +86,7 @@ export class UnifiedPushAdminClient {
* @param newName
*/
rename: async (pushApplicationID: string, newName: string) => {
const app = (await this.applications.find({pushApplicationID}))[0];
const app = (await this.applications.find({filter: {pushApplicationID}}))[0];
app.name = newName;
// Remove the variants
app.variants = undefined;
Expand Down
8 changes: 6 additions & 2 deletions src/applications/ApplicationsAdmin.ts
Expand Up @@ -9,7 +9,10 @@ export class ApplicationsAdmin {
return [obj];
}

async find(api: AxiosInstance, filter?: PushApplicationSearchOptions): Promise<PushApplication[]> {
async find(
api: AxiosInstance,
{filter, page}: {filter?: PushApplicationSearchOptions; page?: number} = {}
): Promise<PushApplication[]> {
let url = '/applications';
let response: AxiosResponse;

Expand All @@ -27,6 +30,7 @@ export class ApplicationsAdmin {
params: {
includeDeviceCount: filter?.includeDeviceCount === true,
includeActivity: filter?.includeActivity === true,
page,
},
});
}
Expand Down Expand Up @@ -62,7 +66,7 @@ export class ApplicationsAdmin {
//new delete function
async delete(api: AxiosInstance, filter?: PushApplicationSearchOptions) {
return Promise.all(
(await this.find(api, filter)).map(application =>
(await this.find(api, {filter})).map(application =>
api.delete(`/applications/${application.pushApplicationID}`).then(() => application)
)
);
Expand Down
27 changes: 22 additions & 5 deletions test/applications/ApplicationsAdmin.test.ts
Expand Up @@ -41,20 +41,37 @@ describe('Applications Admin', () => {
expect(apps).toMatchObject(ids.slice(0, 10));
});

it('Should return all apps (2nd page)', async () => {
utils.generateApps(upsMock, 45);

let apps = await appAdmin.find(api);
expect(apps).toHaveLength(10);
apps = await appAdmin.find(api, {page: 2});
expect(apps).toHaveLength(10);
apps = await appAdmin.find(api, {page: 3});
expect(apps).toHaveLength(10);
apps = await appAdmin.find(api, {page: 4});
expect(apps).toHaveLength(10);
apps = await appAdmin.find(api, {page: 5});
expect(apps).toHaveLength(5);

//expect(apps).toMatchObject(ids.slice(0, 10));
});

it('Should return a given app', async () => {
utils.generateApps(upsMock, 10);
// get one app
const app = (await appAdmin.find(api))[6];

const filteredApp = await appAdmin.find(api, {
pushApplicationID: app.pushApplicationID,
filter: {pushApplicationID: app.pushApplicationID},
});
expect(filteredApp).toEqual([app]);
});

it('Should return empty result', async () => {
const filteredApp = await appAdmin.find(api, {
developer: APP_DEVELOPER_FILTER_BAD,
filter: {developer: APP_DEVELOPER_FILTER_BAD},
});
expect(filteredApp).toEqual([]);
});
Expand All @@ -65,7 +82,7 @@ describe('Applications Admin', () => {
utils.generateApps(upsMock, 5, new Array(10).fill({developer: 'Dev 2'}));

const filteredApp = await appAdmin.find(api, {
developer: APP_DEVELOPER_FILTER_OK,
filter: {developer: APP_DEVELOPER_FILTER_OK},
});
expect(filteredApp).toHaveLength(8);
expect(filteredApp).toMatchObject(new Array(8).fill({developer: APP_DEVELOPER_FILTER_OK}));
Expand All @@ -78,11 +95,11 @@ describe('Applications Admin', () => {
const idToDelete = ids[5];

expect(await appAdmin.find(api)).toHaveLength(10);
expect(await appAdmin.find(api, idToDelete)).toHaveLength(1);
expect(await appAdmin.find(api, {filter: idToDelete})).toHaveLength(1);

await appAdmin.delete(api, idToDelete);

expect(await appAdmin.find(api)).toHaveLength(9);
expect(await appAdmin.find(api, idToDelete)).toHaveLength(0);
expect(await appAdmin.find(api, {filter: idToDelete})).toHaveLength(0);
});
});
9 changes: 8 additions & 1 deletion test/mocks/rest/applications.ts
Expand Up @@ -2,6 +2,7 @@ import * as nock from 'nock';
import {PushApplication} from '../../../src/applications';
import {UPSEngineMock} from '../engine/UPSEngineMock';
import {ACCESS_TOKEN} from './keycloak';
import {URL} from 'url';

const REST_APPLICATIONS_ENDPOINT = '/rest/applications';

Expand All @@ -27,7 +28,13 @@ export const mockCreateApplication = (scope: nock.Scope, ups: UPSEngineMock, enf

export const mockGetApplications = (scope: nock.Scope, ups: UPSEngineMock, enforceAuth = false) => {
// get all applications
scope = scope.get(/rest\/applications\?/).reply(200, function () {
scope = scope.get(/rest\/applications\?/).reply(200, function (uri: string) {
// uri/?parameterName=value&parameterName2=value&page=...
if (uri.indexOf('page') > 0) {
const parsed = new URL(uri, 'http://localhost:9999');
return ups.getApplications(undefined, (parsed.searchParams.get('page') as unknown) as number);
}

checkAuth(this.req, enforceAuth);
return ups.getApplications();
});
Expand Down

0 comments on commit 166d185

Please sign in to comment.