Skip to content

Commit 6aec3d4

Browse files
committed
feat: support header overrides in EndpointClient
1 parent be3c48d commit 6aec3d4

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/endpoint-client.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ export interface ItemsList {
5454
}
5555
}
5656

57+
export interface EndpointClientRequestOptions <T> {
58+
headerOverrides?: HttpClientHeaders
59+
dryRun?: boolean
60+
dryRunReturnValue?: T
61+
}
62+
5763
export class EndpointClient {
5864
private logger: Logger
5965

@@ -90,7 +96,7 @@ export class EndpointClient {
9096

9197
/* eslint-disable @typescript-eslint/no-explicit-any */
9298
public async request<T = any, R = AxiosResponse<T>>(method: HttpClientMethod, path?: string,
93-
data?: any, params?: HttpClientParams): Promise<T> {
99+
data?: any, params?: HttpClientParams, options?: EndpointClientRequestOptions<T>): Promise<T> {
94100
const headers: HttpClientHeaders = this.config.headers ? { ...this.config.headers } : {}
95101

96102
if (this.config.loggingId) {
@@ -111,7 +117,7 @@ export class EndpointClient {
111117
let axiosConfig: AxiosRequestConfig = {
112118
url: this.url(path),
113119
method,
114-
headers,
120+
headers: options?.headerOverrides ? { ...headers, ...options.headerOverrides } : headers,
115121
params,
116122
data,
117123
paramsSerializer: params => qs.stringify(params, { indices: false }),
@@ -122,6 +128,12 @@ export class EndpointClient {
122128
if (this.logger.isDebugEnabled()) {
123129
this.logger.debug(`making axios request: ${JSON.stringify(axiosConfig)}`)
124130
}
131+
if (options?.dryRun) {
132+
if (options.dryRunReturnValue) {
133+
return options.dryRunReturnValue
134+
}
135+
throw new Error('skipping request; dry run mode')
136+
}
125137
let response
126138
try {
127139
response = await axios.request(axiosConfig)

test/unit/endpoint-client.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ describe('Endpoint Client', () => {
6060
axios.request.mockReset()
6161
})
6262

63+
it('request with header overrides', async () => {
64+
axios.request.mockImplementationOnce(() => Promise.resolve({ status: 200, data: { status: 'ok' } }))
65+
const headerOverrides = { 'Content-Type': 'overridden content type'}
66+
const response = await client.request('POST', 'mypath', { name: 'Bob' }, undefined, { headerOverrides })
67+
expect(axios.request).toHaveBeenCalledTimes(1)
68+
expect(axios.request).toHaveBeenCalledWith({
69+
'url': 'https://api.smartthings.com/basepath/mypath',
70+
'method': 'POST',
71+
'headers': {
72+
'Content-Type': 'overridden content type',
73+
'Accept': 'application/json',
74+
'Authorization': 'Bearer asdfghjkl',
75+
'X-ST-CORRELATION': 'AAABBBCCC',
76+
},
77+
'data': { name: 'Bob' },
78+
'params': undefined,
79+
'paramsSerializer': expect.anything(),
80+
})
81+
expect(response.status).toBe('ok')
82+
axios.request.mockReset()
83+
})
84+
6385
it('get', async () => {
6486
axios.request.mockImplementationOnce(() => Promise.resolve({ status: 200, data: { status: 'ok' } }))
6587
const response = await client.get('path2')

0 commit comments

Comments
 (0)