Skip to content

Commit

Permalink
Moves interfaces to correct classes for better declaration of distrib…
Browse files Browse the repository at this point in the history
…uted types
  • Loading branch information
coconutcraig committed Nov 20, 2018
1 parent 0ffe2b2 commit a532c16
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 416 deletions.
47 changes: 34 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
import { AxiosInstance } from 'axios';

import Client from './client';
import Location from './resources/location';
import Question from './resources/question';
import Service from './resources/service';
import Location, { LocationResource } from './resources/location';
import Question, { QuestionResource } from './resources/question';
import Service, { ServiceResource } from './resources/service';
import Setting from './resources/setting';
import TimeSlot from './resources/time-slot';
import User from './resources/user';
import {
LocationResource,
QuestionResource,
Resource,
ServiceResource,
TimeSlotResource,
UserResource,
} from './types/resources';
import TimeSlot, { TimeSlotResource } from './resources/time-slot';
import User, { UserResource } from './resources/user';

export interface Filterable<T> {
filters?: T;
limit?: number;
page?: number;
sort?: string;
}

export interface IncludableParameters {
include?: string;
}

export interface ModelInterface {
getAttributes(): object;
}

export interface Pageable extends Sortable {
on(page: number): this;

take(limit: number): this;
}

export interface Resource {
get(): Promise<any>;
}

export interface Sortable extends Resource {
sortBy(sortable: string): this;
}

export default class OpenApi {
protected client: AxiosInstance;
Expand Down
16 changes: 14 additions & 2 deletions src/models/answer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { AnswerModel } from '../types/models';
import { AnswerParameters } from '../types/parameters';
import { ModelInterface } from '../index';
import Model from './model';

export interface AnswerModel extends ModelInterface {
for(question: number): this;

is(value: string): this;

transform(): object;
}

export interface AnswerParameters {
question: number | null;
value: string | null;
}

export default class Answer extends Model implements AnswerModel {
protected attributes: AnswerParameters;

Expand Down
59 changes: 56 additions & 3 deletions src/models/attendee.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,60 @@
import { AnswerModel, AttendeeModel } from '../types/models';
import { AttendeeParameters, LocationDetailParameters, ReachableDetailParameters } from '../types/parameters';
import { ModelInterface } from '../index';
import { AnswerModel } from './answer';
import Model from './model';

export interface AttendeeModel extends ModelInterface {
answers(answers: AnswerModel | AnswerModel[]): this;

located(details: LocatableDetailParameters): this;

messagable(): this;

named(first: string, last: string): this;

provided(notes: string): this;

reachable(details: ReachableDetailParameters): this;

speaks(language: string): this;

transform(): object;
}

export interface AttendeeParameters {
address?: string;
answers?: AnswerModel[] | [];
cell_phone?: string;
city?: string;
country?: string;
email: string | null;
first_name: string | null;
last_name: string | null;
language?: string;
messagable?: boolean;
notes?: string;
phone?: string;
postcode?: string;
region?: string;
timezone?: string;
work_phone?: string;
}

export interface LocatableDetailParameters {
address?: string;
city?: string;
country?: string;
postcode?: string;
region?: string;
timezone?: string;
}

export interface ReachableDetailParameters {
cell_phone?: string;
email: string;
phone?: string;
work_phone?: string;
}

export default class Attendee extends Model implements AttendeeModel {
protected attributes: AttendeeParameters;

Expand All @@ -21,7 +74,7 @@ export default class Attendee extends Model implements AttendeeModel {
return this;
}

public located(details: LocationDetailParameters): this {
public located(details: LocatableDetailParameters): this {
this.attributes = { ...this.attributes, ...details };

return this;
Expand Down
20 changes: 18 additions & 2 deletions src/models/preference.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { PreferenceModel } from '../types/models';
import { PreferenceParameters } from '../types/parameters';
import { ModelInterface } from '../index';
import Model from './model';

export interface PreferenceModel extends ModelInterface {
between(start: string, end: string): this;

next(): this;

on(day: number): this;

transform(): object;
}

export interface PreferenceParameters {
day?: number | null;
end?: string | null;
start?: string | null;
type?: number | null;
}

const NEXT_AVAILABLE = 1;
const CERTAIN_DAYS = 2;

Expand Down
3 changes: 1 addition & 2 deletions src/resources/appointment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import mockAxios from 'axios';
import Notifications from '../constants/notifications';
import Answer from '../models/answer';
import Attendee from '../models/attendee';
import { AppointmentMatcherParameters, AppointmentNotificationParameters } from '../types/parameters';
import Appointment from './appointment';
import Appointment, { AppointmentMatcherParameters, AppointmentNotificationParameters } from './appointment';

it('can set the location property', async () => {
const resource = new Appointment(mockAxios);
Expand Down
78 changes: 69 additions & 9 deletions src/resources/appointment.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,74 @@
import { AxiosInstance } from 'axios';

import { AppointmentFilter } from '../types/filters';
import { AttendeeModel } from '../types/models';
import {
AppointmentMatcherParameters,
AppointmentNotificationParameters,
AppointmentParameters,
} from '../types/parameters';
import { AppointmentRelationship } from '../types/relationships';
import { AppointmentResource } from '../types/resources';
import { Resource } from '../index';
import { AttendeeModel } from '../models/attendee';

export interface AppointmentFilter {
location?: number;
matchers?: AppointmentMatcherParameters;
notifications?: AppointmentNotificationParameters;
services?: number | number[];
start?: string;
user?: number;
}

export interface AppointmentMatcherParameters {
code: string;
email: string;
id: string | number;
}

export interface AppointmentNotificationParameters {
client?: boolean;
user?: boolean;
}

export interface AppointmentParameters {
data: {
attributes: {
location_id: number | undefined;
service_id: number | number[] | undefined;
staff_id: number | null;
start: string | undefined;
};
relationships: {
attendees: {
data: object[];
};
};
type: string;
};
meta?: {
notify?: {
client?: boolean;
user?: boolean;
};
};
}

export interface AppointmentResource extends Resource {
at(location: number): this;

book(): Promise<any>;

by(user: number): this;

cancel(appointment: number, attendee: number): Promise<any>;

for(services: number | number[]): this;

matching(matchers: AppointmentMatcherParameters): this;

notify(notifications: AppointmentNotificationParameters): this;

starting(start: string): this;

with(attendees: AttendeeModel | AttendeeModel[]): this;
}

export interface AppointmentRelationship {
attendees?: AttendeeModel[] | [];
}

export default class Appointment implements AppointmentResource {
protected client: AxiosInstance;
Expand Down
24 changes: 21 additions & 3 deletions src/resources/location.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import { AxiosInstance } from 'axios';

import { Filterable, LocationFilter } from '../types/filters';
import { LocationParameters } from '../types/parameters';
import { LocationResource } from '../types/resources';
import { Filterable, Pageable } from '../index';

export interface LocationFilter {
assigned?: boolean;
services?: number | number[] | string | string[];
user?: number | string;
}

export interface LocationParameters {
assigned?: boolean;
service?: number | number[] | string | string[];
user?: number | string;
}

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

containing(user: number | string): this;

providing(services: number | number[] | string | string[]): this;
}

export default class Location implements LocationResource {
protected client: AxiosInstance;
Expand Down
16 changes: 13 additions & 3 deletions src/resources/question.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { AxiosInstance } from 'axios';

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

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

export interface QuestionParameters {
service?: number | number[] | string | string[];
}

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

export default class Question implements QuestionResource {
protected client: AxiosInstance;
Expand Down
28 changes: 25 additions & 3 deletions src/resources/service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { AxiosInstance } from 'axios';

import { Filterable, ServiceFilter } from '../types/filters';
import { ServiceParameters } from '../types/parameters';
import { ServiceResource } from '../types/resources';
import { Filterable, Pageable } from '../index';

export interface ServiceFilter {
assigned?: boolean;
category?: number | string;
location?: number | string;
user?: number | string;
}

export interface ServiceParameters {
assigned?: boolean;
category?: number | string;
location?: number | string;
user?: number | string;
}

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

at(location: number | string): this;

by(user: number | string): this;

in(category: number | string): this;
}

export default class Service implements ServiceResource {
protected client: AxiosInstance;
Expand Down
2 changes: 1 addition & 1 deletion src/resources/setting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosInstance } from 'axios';

import { Resource } from '../types/resources';
import { Resource } from '../index';

export default class Setting implements Resource {
protected client: AxiosInstance;
Expand Down
30 changes: 27 additions & 3 deletions src/resources/time-slot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { AxiosInstance } from 'axios';

import { TimeSlotFilter } from '../types/filters';
import { TimeSlotParameters } from '../types/parameters';
import { TimeSlotResource } from '../types/resources';
import { Resource } from '../index';

export interface TimeSlotFilter {
end?: string;
location?: number;
services?: number | number[];
start?: string;
user?: number;
}

export interface TimeSlotParameters {
end?: string;
location_id?: number;
service_id?: number | number[];
staff_id?: number;
start?: string;
}

export interface TimeSlotResource extends Resource {
at(location: number): this;

between(start: string, end: string): this;

by(user: number): this;

for(services: number | number[]): this;
}

export default class TimeSlot implements TimeSlotResource {
protected client: AxiosInstance;
Expand Down
Loading

0 comments on commit a532c16

Please sign in to comment.