Commit cf1d384c authored by Timothee Gosselin's avatar Timothee Gosselin

add client-config component

parent ab6f9bea
import config from 'config'
const { baseUrl } = config['pubsweet-client']
const { baseUrl } = window.config['pubsweet-client']
const apiUrl = `${baseUrl}/api/wb`
const get = async (endpoint, value) => {
......
......@@ -6,5 +6,6 @@
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="/config.js"></script>
</body>
</html>
......@@ -7,7 +7,8 @@
<body>
<div id="root"></div>
<script type="text/javascript" src="/config.js"></script>
<script type="text/javascript" charset="utf-8" src="/assets/app.js"></script>
</body>
</html>
\ No newline at end of file
</html>
......@@ -10,5 +10,6 @@
"./server/api",
"./server/export",
"./server/models/externalTeam",
"./server/models/externalUser"
"./server/models/externalUser",
"./server/client-config"
]
......@@ -63,6 +63,7 @@ module.exports = {
pubsweet: { components },
'pubsweet-client': {
API_ENDPOINT: '/api',
isPublic: true,
},
'pubsweet-server': {
baseUrl: deferConfig(
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Client config handler returns a response containing public config 1`] = `
"
window.config = {\\"publicConfig\\":{\\"isPublic\\":true,\\"key\\":\\"value\\"}}
"
`;
module.exports = {
server: () => require('./routes'),
}
{
"name": "@elifesciences/xpub-client-config",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {},
"author": "",
"license": "ISC"
}
const config = require('config')
const { pickBy } = require('lodash')
const template = clientConfig => `
window.config = ${JSON.stringify(clientConfig)}
`
module.exports = app => {
app.get('/config.js', (req, res) => {
const clientConfig = pickBy(config, value => value.isPublic)
const response = template(clientConfig)
res.type('js')
res.send(response)
})
}
const express = require('express')
const supertest = require('supertest')
describe('Client config handler', () => {
let routes
const makeApp = () => {
const app = express()
routes(app)
return supertest(app)
}
beforeEach(() => {
jest.mock('config', () => ({
publicConfig: {
isPublic: true,
key: 'value',
},
privateConfig: {
secret: 'password',
},
}))
routes = require('./routes')
})
it('returns javascript', async () => {
const request = makeApp()
const response = await request.get('/config.js').expect(200)
expect(response.headers['content-type']).toEqual(
expect.stringContaining('javascript'),
)
})
it('returns a response containing public config', async () => {
const request = makeApp()
const response = await request.get('/config.js').expect(200)
expect(response.text).toMatchSnapshot()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment