Skip to content

Commit

Permalink
Adds open api options and new tests for base class and client
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Nov 15, 2018
1 parent b10d60f commit 6a78c56
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/__mocks__/axios.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default {
create: jest.fn(() => Promise.resolve({})),
get: jest.fn(() => Promise.resolve({ data: {} })),
};
16 changes: 16 additions & 0 deletions src/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mockAxios from 'axios';

import Client from './client';

it('will be constructed using passed in options', async () => {
Client({ domain: 'admin', version: 'v2' });

expect(mockAxios.create).toHaveBeenCalledWith({
baseURL: `https://admin.coconutcalendar.com/api/v2/open`,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
},
});
});
6 changes: 4 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import axios, { AxiosInstance } from 'axios';

const Client = (domain: string): AxiosInstance => {
import { OpenApiOptions } from './types/options';

const Client = ({ domain, version }: OpenApiOptions): AxiosInstance => {
return axios.create({
baseURL: `https://${domain}.coconutcalendar.com/api/v2/open`,
baseURL: `https://${domain}.coconutcalendar.com/api/${version}/open`,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Expand Down
12 changes: 12 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import OpenApi from './index';

it('will be constructed with appropriate options', async () => {
const instance = new OpenApi({
domain: 'admin',
});

expect(instance).toHaveProperty('options', {
domain: 'admin',
version: 'v2',
});
});
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { AxiosInstance } from 'axios';

import Client from './client';
import User from './resources/user';
import { OpenApiOptions } from './types/options';
import { UserResource } from './types/resources';

export default class OpenApi {
protected client: AxiosInstance;
protected domain: string;
protected options: OpenApiOptions;
protected user: UserResource;

constructor(domain: string) {
this.client = Client(domain);
this.domain = domain;
constructor({ domain, version = 'v2' }: OpenApiOptions) {
this.client = Client({ domain, version });
this.options = { domain, version };
this.user = new User(this.client);
}

Expand Down
4 changes: 4 additions & 0 deletions src/types/options.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface OpenApiOptions {
domain: string;
version?: string;
}

0 comments on commit 6a78c56

Please sign in to comment.