Skip to content

Commit

Permalink
Adds on and take as options for a resource
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Nov 15, 2018
1 parent f9b700b commit c8f7ba0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/resources/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ it('will set location filter using a string', async () => {
});
});

it('will set the page we are on', async () => {
const resource = new User(mockAxios);

expect(resource.on(4)).toHaveProperty('page', 4);
});

it('will set service filter using a number', async () => {
const resource = new User(mockAxios);

Expand Down Expand Up @@ -65,6 +71,12 @@ it('will set service filter using an array of strings', async () => {
});
});

it('will set the limit given', async () => {
const resource = new User(mockAxios);

expect(resource.take(5)).toHaveProperty('limit', 5);
});

it('will set the sortable filter', async () => {
const resource = new User(mockAxios);

Expand All @@ -79,7 +91,9 @@ it('can string all filterable options together', async () => {
.assigned()
.at(1)
.performing([1, 2])
.sortBy('created'),
.sortBy('created')
.take(5)
.on(1)
);

expected.toHaveProperty('filters', {
Expand All @@ -88,6 +102,8 @@ it('can string all filterable options together', async () => {
services: [1, 2],
});
expected.toHaveProperty('sortable', 'created');
expected.toHaveProperty('limit', 5);
expected.toHaveProperty('page', 1);
});

it('can get users without additional parameters', async () => {
Expand All @@ -107,6 +123,8 @@ it('can get users with additional parameters', async () => {
.at(1)
.performing([1, 2])
.sortBy('created')
.take(5)
.on(1)
.get();

expect(mockAxios.get).toHaveBeenCalledTimes(1);
Expand All @@ -117,6 +135,8 @@ it('can get users with additional parameters', async () => {
location: 1,
service: [1, 2],
},
limit: 5,
page: 1,
sort: 'created',
},
});
Expand Down
24 changes: 24 additions & 0 deletions src/resources/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import { UserResource } from '../types/resources';
export default class User implements UserResource {
protected client: AxiosInstance;
protected filters: UserFilter;
protected page: number | null;
protected sortable: string | null;
protected limit: number | null;

constructor(client: AxiosInstance) {
this.client = client;
this.filters = {};
this.page = null;
this.sortable = null;
this.limit = null;
}

public assigned(assigned: boolean = true): this {
Expand All @@ -35,13 +39,27 @@ export default class User implements UserResource {
params.filters = parameters;
}

if (this.limit) {
params.limit = this.limit;
}

if (this.page) {
params.page = this.page;
}

if (this.sortable) {
params.sort = this.sortable;
}

return await this.client.get('users', { params });
}

public on(page: number): this {
this.page = page;

return this;
}

public performing(services: number | number[] | string | string[]): this {
this.filters.services = services;

Expand All @@ -54,6 +72,12 @@ export default class User implements UserResource {
return this;
}

public take(limit: number): this {
this.limit = limit;

return this;
}

protected params(): UserParameters {
const params: UserParameters = {};

Expand Down
2 changes: 2 additions & 0 deletions src/types/filters.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export interface Filterable<T> {
filters?: T;
limit?: number;
page?: number;
sort?: string;
}

Expand Down
4 changes: 4 additions & 0 deletions src/types/resources.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export interface Resource {
get(): Promise<any>;

on(page: number): this;

sortBy(sortable: string): this;

take(limit: number): this;
}

export interface UserResource extends Resource {
Expand Down

0 comments on commit c8f7ba0

Please sign in to comment.