-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from codecov/dorian/CE-2635/api-abstraction
Dorian/ce 2635/api abstraction
- Loading branch information
Showing
13 changed files
with
493 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PROXY_TO=https://stage-api.codecov.dev |
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 @@ | ||
REACT_APP_API_URL=https://stage-api.codecov.dev |
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 @@ | ||
REACT_APP_API_URL= |
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 |
---|---|---|
|
@@ -21,3 +21,6 @@ site: | |
|
||
ignore: | ||
- "**/*.stories.js" | ||
- ./src/setupTests.js | ||
- ./src/setupProxy.js | ||
- ./src/reportWebVitals.js |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,9 +1,5 @@ | ||
function Home() { | ||
return ( | ||
<div> | ||
Im the home page | ||
</div> | ||
) | ||
return <div>Im the home page</div> | ||
} | ||
|
||
export default Home |
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,13 @@ | ||
const { createProxyMiddleware } = require('http-proxy-middleware') | ||
|
||
module.exports = function (app) { | ||
if (process.env.PROXY_TO) { | ||
app.use( | ||
'/internal', | ||
createProxyMiddleware({ | ||
target: process.env.PROXY_TO, | ||
changeOrigin: true, | ||
}) | ||
) | ||
} | ||
} |
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,23 @@ | ||
import { camelizeKeys, generatePath, getHeaders } from './helpers' | ||
|
||
export async function get({ path, query, provider = 'gh', extraHeaders = {} }) { | ||
const uri = generatePath({ path, query }) | ||
const headers = { | ||
...getHeaders(provider), | ||
...extraHeaders, | ||
} | ||
|
||
const res = await fetch(uri, { headers }) | ||
const data = camelizeKeys(await res.json()) | ||
|
||
return { | ||
data, | ||
res, | ||
} | ||
} | ||
|
||
const Api = { | ||
get, | ||
} | ||
|
||
export default Api |
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,78 @@ | ||
import { rest } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
import * as Cookie from 'js-cookie' | ||
|
||
import Api from './api' | ||
|
||
const rawUserData = { | ||
profile: { | ||
username: 'hello', | ||
nb_orgs: 3, | ||
}, | ||
orgs: [ | ||
{ id: 1, long_name: 'Codecov' }, | ||
{ id: 2, long_name: 'Github' }, | ||
], | ||
} | ||
|
||
// name in camelcase | ||
const userData = { | ||
profile: { | ||
username: 'hello', | ||
nbOrgs: 3, | ||
}, | ||
orgs: [ | ||
{ id: 1, longName: 'Codecov' }, | ||
{ id: 2, longName: 'Github' }, | ||
], | ||
} | ||
|
||
const server = setupServer( | ||
rest.get('/internal/test', (req, res, ctx) => { | ||
const hasToken = Boolean(req.headers.map['authorization']) | ||
return res(ctx.status(hasToken ? 200 : 401), ctx.json(rawUserData)) | ||
}) | ||
) | ||
|
||
beforeAll(() => server.listen()) | ||
afterEach(() => server.resetHandlers()) | ||
afterAll(() => server.close()) | ||
|
||
let result, error | ||
function callApi() { | ||
result = null | ||
error = null | ||
return Api.get({ | ||
path: '/test', | ||
}) | ||
.then((data) => { | ||
result = data | ||
}) | ||
.catch((errorData) => { | ||
error = errorData | ||
}) | ||
} | ||
|
||
describe('when calling an endpoint without a token', () => { | ||
beforeEach(callApi) | ||
|
||
it('has a 401 error', () => { | ||
expect(result.res.status).toBe(401) | ||
}) | ||
}) | ||
|
||
describe('when calling an endpoint with a token', () => { | ||
beforeEach(() => { | ||
Cookie.set('github-token', 'hello') | ||
return callApi() | ||
}) | ||
|
||
afterEach(() => { | ||
Cookie.remove('github-token') | ||
}) | ||
|
||
it('has the data and no error', () => { | ||
expect(error).toBeNull() | ||
expect(result.data).toEqual(userData) | ||
}) | ||
}) |
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,48 @@ | ||
import camelCase from 'lodash/camelCase' | ||
import qs from 'qs' | ||
import * as Cookie from 'js-cookie' | ||
|
||
import config from 'config' | ||
|
||
const ProviderCookieKeyMapping = { | ||
gh: 'github-token', | ||
gl: 'gitlab-token', | ||
bb: 'bitbucket-token', | ||
} | ||
|
||
export function generatePath({ path, query }) { | ||
const baseUrl = `${config.API_URL}/internal` | ||
const queryString = qs.stringify(query, {}) | ||
|
||
return `${baseUrl}${path}?${queryString}` | ||
} | ||
|
||
export function getHeaders(provider) { | ||
const token = Cookie.get(ProviderCookieKeyMapping[provider]) | ||
|
||
const authorizationHeader = token | ||
? { | ||
Authorization: `frontend ${token}`, | ||
} | ||
: {} | ||
|
||
return { | ||
Accept: 'application/json', | ||
...authorizationHeader, | ||
} | ||
} | ||
|
||
export function camelizeKeys(obj) { | ||
if (Array.isArray(obj)) { | ||
return obj.map((v) => camelizeKeys(v)) | ||
} else if (obj !== null && obj.constructor === Object) { | ||
return Object.keys(obj).reduce( | ||
(result, key) => ({ | ||
...result, | ||
[camelCase(key)]: camelizeKeys(obj[key]), | ||
}), | ||
{} | ||
) | ||
} | ||
return obj | ||
} |
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 @@ | ||
export { default } from './api' |