forked from bkkhack/hackmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}/`) | ||
}) | ||
}) | ||
}) |