Skip to content

Commit

Permalink
PP-11120 Improve client interface
Browse files Browse the repository at this point in the history
Export functions for get, post etc. request methods from the Axios base
client. This simplifies how we use it and hides the axios instance.
All these functions take a description for the request as a
parameter, as we always want this to be provided.
  • Loading branch information
stephencdaly committed Oct 12, 2023
1 parent d1e7d7c commit 8788f01
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 72 deletions.
33 changes: 31 additions & 2 deletions app/services/clients/base-client/axios-base-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Client {
/**
* Configure the client. Should only be called once.
*/
_configure (baseURL, options) {
configure (baseURL, options) {
this._axios = axios.create({
baseURL,
timeout: 60 * 1000,
Expand Down Expand Up @@ -110,6 +110,35 @@ class Client {
throw new RESTClientError(errorContext.message, errorContext.service, errorContext.status, errorContext.errorIdentifier, errorContext.reason)
})
}

_getConfigWithDescription (config = {}, description) {
return {
...config,
description
}
}

get (url, description, config) {
return this._axios.get(url, this._getConfigWithDescription(config, description))
}

post (url, payload, description, config) {
return this._axios.post(url, payload, this._getConfigWithDescription(config, description))
}

put (url, payload, description, config) {
return this._axios.put(url, payload, this._getConfigWithDescription(config, description))
}

patch (url, payload, description, config) {
return this._axios.patch(url, payload, this._getConfigWithDescription(config, description))
}

delete (url, description, config) {
return this._axios.delete(url, this._getConfigWithDescription(config, description))
}
}

module.exports = { Client }
module.exports = {
Client
}
12 changes: 6 additions & 6 deletions app/services/clients/base-client/axios-base-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Axios base client', () => {
const requestSuccessSpy = sinon.spy()
const requestFailureSpy = sinon.spy()
const client = new Client(app)
client._configure(baseUrl, {
client.configure(baseUrl, {
onRequestStart: requestStartSpy,
onSuccessResponse: requestSuccessSpy,
onFailureResponse: requestFailureSpy
Expand All @@ -36,7 +36,7 @@ describe('Axios base client', () => {
.get('/')
.reply(200, body)

return expect(client._axios.get('/', { description: 'foo' })).to.be.fulfilled.then((response) => {
return expect(client.get('/', 'foo')).to.be.fulfilled.then((response) => {
expect(response.data).to.deep.equal(body)
})
})
Expand All @@ -50,7 +50,7 @@ describe('Axios base client', () => {
.get('/')
.reply(400, body)

return expect(client._axios.get('/', { description: 'foo' })).to.be.rejected.then((error) => {
return expect(client.get('/', 'foo')).to.be.rejected.then((error) => {
expect(error.message).to.equal('a-message')
expect(error.errorCode).to.equal(400)
expect(error.errorIdentifier).to.equal('AN-ERROR')
Expand All @@ -67,7 +67,7 @@ describe('Axios base client', () => {
.get('/')
.reply(500, body)

return expect(client._axios.get('/', { description: 'foo' })).to.be.rejected.then((error) => {
return expect(client.get('/', 'foo')).to.be.rejected.then((error) => {
expect(error.message).to.equal('a-message')
expect(error.errorCode).to.equal(500)
expect(error.errorIdentifier).to.equal('AN-ERROR')
Expand All @@ -86,7 +86,7 @@ describe('Axios base client', () => {
response: { status: 500 }
})

return expect(client._axios.get('/', { description: 'foo' })).to.be.rejected.then(error => {
return expect(client.get('/', 'foo')).to.be.rejected.then(error => {
expect(error.errorCode).to.equal(500)
sinon.assert.calledThrice(requestStartSpy)
requestStartSpy.getCall(0).calledWithMatch({
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('Axios base client', () => {
response: { status: 500 }
})

return expect(client._axios.get('/')).to.be.rejected.then(error => {
return expect(client.get('/')).to.be.rejected.then(error => {
expect(error.errorCode).to.equal(500)
sinon.assert.calledOnce(requestStartSpy)
sinon.assert.calledOnce(requestFailureSpy)
Expand Down
2 changes: 1 addition & 1 deletion app/services/clients/base-client/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function onFailureResponse (context) {
}

function configureClient (client, baseUrl) {
client._configure(baseUrl, {
client.configure(baseUrl, {
transformRequestAddHeaders,
onRequestStart,
onSuccessResponse,
Expand Down
4 changes: 2 additions & 2 deletions app/services/clients/base-client/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Client config', () => {
nock(baseUrl)
.get('/')
.reply(200)
return expect(client._axios.get('/', { description: 'foo' })).to.be.fulfilled.then((response) => {
return expect(client.get('/', 'foo')).to.be.fulfilled.then((response) => {
expect(response.request.headers).to.have.property('x-request-id', 'abc123')
})
})
Expand All @@ -43,7 +43,7 @@ describe('Client config', () => {
nock(baseUrl)
.get('/')
.reply(200)
return expect(client._axios.get('/', { description: 'foo' })).to.be.fulfilled.then((response) => {
return expect(client.get('/', 'foo')).to.be.fulfilled.then((response) => {
expect(response.request.headers).to.not.have.key('x-request-id')
})
})
Expand Down
Loading

0 comments on commit 8788f01

Please sign in to comment.