@defra/bng-utils-lib 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@defra/bng-utils-lib",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"private": false,
|
|
6
|
+
"description": "A library of utilities for the Biodiversity Net Gain Service",
|
|
7
|
+
"main": "src/utils.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "echo \\\"Error: run tests from root\\\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/DEFRA/biodiversity-net-gain-service.git"
|
|
14
|
+
},
|
|
15
|
+
"author": "Department for Environment, Food and Rural Affairs <https://www.gov.uk/government/organisations/department-for-environment-food-rural-affairs>",
|
|
16
|
+
"license": "OGL-UK-3.0",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"axios": "^0.27.2"
|
|
19
|
+
},
|
|
20
|
+
"gitHead": "c9754436d12d2e124d9a5733dd440cb0bfdd9098"
|
|
21
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import getBearerToken from '../get-bearer-token.js'
|
|
2
|
+
import axios from 'axios'
|
|
3
|
+
|
|
4
|
+
jest.mock('axios')
|
|
5
|
+
|
|
6
|
+
const baseConfig = {
|
|
7
|
+
clientId: 'mockClientId',
|
|
8
|
+
clientSecret: 'mockClientSecret'
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const mockReturnValue = {
|
|
12
|
+
data: {},
|
|
13
|
+
status: 200,
|
|
14
|
+
statusText: 'OK'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('A request for a bearer token', () => {
|
|
18
|
+
it('should result in a REST API call when a scope is included', async () => {
|
|
19
|
+
axios.request.mockReturnValue(mockReturnValue)
|
|
20
|
+
const config = Object.assign({ scope: 'mockScope' }, baseConfig)
|
|
21
|
+
await getBearerToken(config)
|
|
22
|
+
expect(axios.request).toHaveBeenCalled()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('should result in a REST API call when a scope is not included', async () => {
|
|
26
|
+
axios.request.mockReturnValue(mockReturnValue)
|
|
27
|
+
await getBearerToken(baseConfig)
|
|
28
|
+
expect(axios.request).toHaveBeenCalled()
|
|
29
|
+
})
|
|
30
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
|
|
3
|
+
const getBearerToken = async config => {
|
|
4
|
+
const params = new URLSearchParams()
|
|
5
|
+
params.append('grant_type', 'client_credentials')
|
|
6
|
+
|
|
7
|
+
if (config.scope) {
|
|
8
|
+
params.append('scope', config.scope)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const options = {
|
|
12
|
+
method: 'POST',
|
|
13
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
14
|
+
auth: {
|
|
15
|
+
username: config.clientId,
|
|
16
|
+
password: config.clientSecret
|
|
17
|
+
},
|
|
18
|
+
data: params.toString(),
|
|
19
|
+
url: config.url
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const response = await axios.request(options)
|
|
23
|
+
return response.data
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default getBearerToken
|
package/src/utils.js
ADDED