Skip to content

Commit

Permalink
Remove config.js in favor of .env (#92)
Browse files Browse the repository at this point in the history
* Remove config.js in favor of .env

* Put backend version option in package.json

* Add accept json header to requests
  • Loading branch information
phyrog authored Jul 10, 2017
1 parent 4c5fda7 commit e86072d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
PORT=3001

REACT_APP_BACKEND_HOST="http://localhost:3000"
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "ontohub-frontend",
"version": "0.1.0",
"ontohub": {
"backendVersion": "> 0.0.0-90"
},
"private": true,
"license": "AGPL-3.0",
"dependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { Switch, Route } from 'react-router-dom'
import styled from 'styled-components'
import routes from './routes'
import ReactCSSTransitionReplace from 'react-css-transition-replace'
import config from './config.json'

const backendVersion = config.ontohub.backendVersion

const App = (props) =>
<div className={props.className}>
Expand Down Expand Up @@ -42,7 +45,7 @@ const App = (props) =>
/>
)}
</Switch>
<VersionWarning requirement={props.config.version} />
<VersionWarning requirement={backendVersion} />
</div>

const StyledApp = styled(App)`
Expand Down
24 changes: 20 additions & 4 deletions src/apollo/client.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import { ApolloClient, createNetworkInterface } from 'react-apollo'
import config from '../config'

const backendHost = process.env.REACT_APP_BACKEND_HOST

const networkInterface = createNetworkInterface({
uri: `${config.api.endpoint}/graphql`
uri: `${backendHost}/graphql`
})
networkInterface.use([
{
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {} // Create the header object if needed.
req.options.headers = {}
}
next()
}
},
{
applyMiddleware(req, next) {
Object.assign(req.options.headers, {
Accept: 'application/json'
})
next()
}
},
{
applyMiddleware(req, next) {
let authToken = localStorage.getItem('auth-token')
if (authToken) {
req.options.headers['Authorization'] = `Bearer ${authToken}`
Object.assign(req.options.headers, {
Authorization: `Bearer ${authToken}`
})
}
next()
}
Expand Down
25 changes: 0 additions & 25 deletions src/config.js

This file was deleted.

1 change: 1 addition & 0 deletions src/config.json
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'
import ReactDOM from 'react-dom'
import registerServiceWorker from './registerServiceWorker'
import config from './config'
import App from './App'
import { BrowserRouter as Router } from 'react-router-dom'
import { ApolloProvider } from 'react-apollo'
Expand All @@ -14,7 +13,7 @@ ReactDOM.render(
<ApolloProvider client={client}>
<Router>
<ThemeProvider theme={theme}>
<App config={config} />
<App />
</ThemeProvider>
</Router>
</ApolloProvider>,
Expand Down

0 comments on commit e86072d

Please sign in to comment.