Skip to content

Commit

Permalink
Refactor github issues class
Browse files Browse the repository at this point in the history
  • Loading branch information
saiqulhaq committed Jan 25, 2018
1 parent c20ac93 commit 604396c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"license": "ISC",
"dependencies": {
"axios": "^0.16.1",
"axios-mock-adapter": "^1.12.0",
"vue": "^2.3.3",
"vue-router": "^2.7.0"
},
Expand Down
22 changes: 22 additions & 0 deletions src/github-api-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import axios from 'axios'

export const baseURL = 'https://api.github.com/'

export default function (org, repo, token) {
function getApiConfig (apiPath) {
var config = {
baseURL: baseURL + (apiPath || '')
}
if (token) {
config.headers = { 'Authorization': 'token ' + token }
}
return config
}
// general github ajax client
let github = axios.create(getApiConfig())

// repo-specific ajax client
github.repo = axios.create(getApiConfig(`repos/${org}/${repo}/`))

return github
}
27 changes: 4 additions & 23 deletions src/github-issues.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios' // ajax library
import serialization from './github-serialization.js'
import auth from './github-oauth.js'
import githubApiClient from './github-api-client.js'

/*
* GitHub Issues API. Handles saving and polling of
Expand All @@ -19,7 +19,7 @@ import auth from './github-oauth.js'
export default class GitHubIssueService {
constructor (config) {
this.config = config
this.github = this.createGithubApiClient(config.organization, config.repository)
this.github = githubApiClient(config.organization, config.repository)

// if the user has already logged in (e.g. in a previous session), use the
// authenticated client for all operations. Authenticated calls have a
Expand Down Expand Up @@ -113,7 +113,7 @@ export default class GitHubIssueService {
return this.config.onAuthenticationRequired()
.then(token => {
if (!this.github.isAuthenticated) {
this.github = this.createGithubApiClient(
this.github = githubApiClient(
this.config.organization,
this.config.repository,
token)
Expand All @@ -129,30 +129,11 @@ export default class GitHubIssueService {

deauthenticateClient () {
auth.logOut()
this.github = this.createGithubApiClient(
this.github = githubApiClient(
this.config.organization,
this.config.repository)
}

createGithubApiClient (org, repo, token) {
function getApiConfig (apiPath) {
var config = {
baseURL: 'https://api.github.com/' + (apiPath || '')
}
if (token) {
config.headers = { 'Authorization': 'token ' + token }
}
return config
}
// general github ajax client
var github = axios.create(getApiConfig())

// repo-specific ajax client
github.repo = axios.create(getApiConfig(`repos/${org}/${repo}/`))

return github
}

reportError (err) {
console.log(err)
}
Expand Down
40 changes: 40 additions & 0 deletions test/github-api-client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
const mock = new MockAdapter(axios);

import GithubApiClient, { baseURL } from '../src/github-api-client.js'

describe('baseURL', () => {
it('should equal github api base URL', () => {
expect(baseURL).toBe('https://api.github.com/')
})
})

let token = 'token'
let config = {
organization: 'saiqulhaq',
repository: 'hackmap',
}

describe('GithubApiClient()', () => {
let client
beforeEach(() => {
client = GithubApiClient(config.organization, config.repository, token)

mock.onGet().reply(function(config) {
return [200, {}]
});
})

it('returns axios instance for general repo', () => {
return client.get('foo').then((request) => {
expect(request.config.baseURL).toBe(baseURL)
})
})

it('returns axios instance for specific repo', () => {
return client.repo.get('foo').then((request) => {
expect(request.config.baseURL).toBe(`${baseURL}repos/${config.organization}/${config.repository}/`)
})
})
})

0 comments on commit 604396c

Please sign in to comment.