From 6a78c567122be0c2ef2567800799f4669d39e0c0 Mon Sep 17 00:00:00 2001 From: Craig Paul Date: Thu, 15 Nov 2018 11:11:41 -0600 Subject: [PATCH] Adds open api options and new tests for base class and client --- src/__mocks__/axios.ts | 1 + src/client.test.ts | 16 ++++++++++++++++ src/client.ts | 6 ++++-- src/index.test.ts | 12 ++++++++++++ src/index.ts | 9 +++++---- src/types/options.d.ts | 4 ++++ 6 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 src/client.test.ts create mode 100644 src/index.test.ts create mode 100644 src/types/options.d.ts diff --git a/src/__mocks__/axios.ts b/src/__mocks__/axios.ts index ab47636..1cefdc8 100644 --- a/src/__mocks__/axios.ts +++ b/src/__mocks__/axios.ts @@ -1,3 +1,4 @@ export default { + create: jest.fn(() => Promise.resolve({})), get: jest.fn(() => Promise.resolve({ data: {} })), }; diff --git a/src/client.test.ts b/src/client.test.ts new file mode 100644 index 0000000..663317a --- /dev/null +++ b/src/client.test.ts @@ -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', + }, + }); +}); diff --git a/src/client.ts b/src/client.ts index fee0047..942c137 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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', diff --git a/src/index.test.ts b/src/index.test.ts new file mode 100644 index 0000000..52a26de --- /dev/null +++ b/src/index.test.ts @@ -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', + }); +}); diff --git a/src/index.ts b/src/index.ts index 4ce820d..6c7306e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); } diff --git a/src/types/options.d.ts b/src/types/options.d.ts new file mode 100644 index 0000000..636a1fc --- /dev/null +++ b/src/types/options.d.ts @@ -0,0 +1,4 @@ +export interface OpenApiOptions { + domain: string; + version?: string; +}