Skip to content

Commit

Permalink
Adds question resource
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Nov 16, 2018
1 parent 7c310ed commit 25acaa8
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import OpenApi from './index';
import Question from './resources/question';
import Service from './resources/service';
import Setting from './resources/setting';
import User from './resources/user';
Expand Down Expand Up @@ -30,3 +31,10 @@ it('can access the service resource', async () => {
expect(instance).toHaveProperty('services');
expect(instance.services).toBeInstanceOf(Service.prototype.constructor);
});

it('can access the question resource', async () => {
const instance = new OpenApi('admin');

expect(instance).toHaveProperty('questions');
expect(instance.questions).toBeInstanceOf(Question.prototype.constructor);
});
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { AxiosInstance } from 'axios';

import Client from './client';
import Question from './resources/question';
import Service from './resources/service';
import Setting from './resources/setting';
import User from './resources/user';
import { Resource, ServiceResource, UserResource } from './types/resources';
import { QuestionResource, Resource, ServiceResource, UserResource } from './types/resources';

export default class OpenApi {
protected client: AxiosInstance;
protected domain: string;
protected question: QuestionResource;
protected service: ServiceResource;
protected setting: Resource;
protected user: UserResource;

constructor(domain: string) {
this.client = Client(domain);
this.domain = domain;
this.question = new Question(this.client);
this.service = new Service(this.client);
this.setting = new Setting(this.client);
this.user = new User(this.client);
}

get questions(): QuestionResource {
return this.question;
}

get services(): ServiceResource {
return this.service;
}
Expand Down
105 changes: 105 additions & 0 deletions src/resources/question.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import mockAxios from 'axios';

import Question from './question';
import User from './user';

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

expect(resource.performing(1)).toHaveProperty('filters', {
services: 1,
});
});

it('will set service filter using an array of numbers', async () => {
const resource = new User(mockAxios);

expect(resource.performing([1, 2])).toHaveProperty('filters', {
services: [1, 2],
});
});

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

expect(resource.performing('identifier')).toHaveProperty('filters', {
services: 'identifier',
});
});

it('will set service filter using an array of strings', async () => {
const resource = new User(mockAxios);

expect(resource.performing(['identifier', 'other'])).toHaveProperty('filters', {
services: ['identifier', 'other'],
});
});

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

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

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

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

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

expect(resource.sortBy('name,-created')).toHaveProperty('sortable', 'name,-created');
});

it('can string all filterable options together', async () => {
const resource = new Question(mockAxios);

const expected = expect(
resource
.for(3)
.sortBy('created')
.take(5)
.on(1),
);

expected.toHaveProperty('filters', {
services: 3,
});
expected.toHaveProperty('sortable', 'created');
expected.toHaveProperty('limit', 5);
expected.toHaveProperty('page', 1);
});

it('can get questions without additional parameters', async () => {
const resource = new Question(mockAxios);

await resource.get();

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('questions', { params: {} });
});

it('can get questions with additional parameters', async () => {
const resource = new Question(mockAxios);

await resource
.for(3)
.sortBy('created')
.take(5)
.on(1)
.get();

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('questions', {
params: {
filters: {
service: 3,
},
limit: 5,
page: 1,
sort: 'created',
},
});
});
78 changes: 78 additions & 0 deletions src/resources/question.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { AxiosInstance } from 'axios';

import { Filterable, QuestionFilter } from '../types/filters';
import { QuestionParameters } from '../types/parameters';
import { QuestionResource } from '../types/resources';

export default class Question implements QuestionResource {
protected client: AxiosInstance;
protected filters: QuestionFilter;
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 for(services: number | number[] | string | string[]): this {
this.filters.services = services;

return this;
}

public async get(): Promise<any> {
const parameters = this.params();
const params: Filterable<QuestionParameters> = {};

if (Object.keys(parameters).length) {
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('questions', { params });
}

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

return this;
}

public sortBy(sortable: string): this {
this.sortable = sortable;

return this;
}

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

return this;
}

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

if (typeof this.filters.services !== 'undefined') {
params.service = this.filters.services;
}

return params;
}
}
4 changes: 4 additions & 0 deletions src/types/filters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export interface Filterable<T> {
sort?: string;
}

export interface QuestionFilter {
services?: number | number[] | string | string[];
}

export interface ServiceFilter {
assigned?: boolean;
category?: number | string;
Expand Down
4 changes: 4 additions & 0 deletions src/types/parameters.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface QuestionParameters {
service?: number | number[] | string | string[];
}

export interface ServiceParameters {
assigned?: boolean;
category?: number | string;
Expand Down
4 changes: 4 additions & 0 deletions src/types/resources.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export interface Resource {
get(): Promise<any>;
}

export interface QuestionResource extends Pageable {
for(services: number | number[] | string | string[]): this;
}

export interface ServiceResource extends Pageable {
assigned(assigned: boolean): this;

Expand Down

0 comments on commit 25acaa8

Please sign in to comment.