Skip to content

Commit

Permalink
fix: workaround axios bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rossiam committed Apr 23, 2024
1 parent 4e45523 commit 0798d93
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-swans-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smartthings/core-sdk": patch
---

work around bug in axios 0.28.1
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ a work in progress. Changes may still be made that are not backwardly compatible
npm install @smartthings/core-sdk
```

:warning: Note that if you use axios in your project, you must use version 0.28.1 or later.

## Importing

`NodeJS`:
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"dependencies": {
"async-mutex": "^0.4.0",
"axios": "^0.28",
"axios": "^0.28.1",
"http-signature": "^1.3.6",
"lodash.isdate": "^4.0.1",
"lodash.isstring": "^4.0.1",
Expand Down
8 changes: 6 additions & 2 deletions src/endpoint-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export interface EndpointClientRequestOptions <T> {
* Meant to be used before logging the request
*/
function scrubConfig(config: AxiosRequestConfig): string {
const message = JSON.stringify(config)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { paramsSerializer: _unused, ...cleanerConfig } = config
const message = JSON.stringify(cleanerConfig)
const bearerRegex = /"(Bearer [0-9a-f]{8})[0-9a-f-]{28}"/i

if (bearerRegex.test(message)) {
Expand Down Expand Up @@ -180,7 +182,9 @@ export class EndpointClient {
headers: options?.headerOverrides ? { ...headers, ...options.headerOverrides } : headers,
params,
data,
paramsSerializer: params => qs.stringify(params, { indices: false }),
paramsSerializer: {
serialize: params => qs.stringify(params, { indices: false }),
},
}

const authHeaders = await this.config.authenticator.authenticate()
Expand Down
34 changes: 17 additions & 17 deletions test/unit/endpoint-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Mutex } from 'async-mutex'
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse, ParamsSerializerOptions } from 'axios'
import qs from 'qs'

import { AuthData, Authenticator, BearerTokenAuthenticator, NoOpAuthenticator, RefreshData,
Expand Down Expand Up @@ -150,17 +150,17 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')

const stringifyMock = (qs.stringify as jest.Mock<string, [unknown]>)
.mockReturnValue('stringified parameters')
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const paramsSerializer = mockRequest.mock.calls[0][0].paramsSerializer as (params: any) => string
const paramsSerializer = mockRequest.mock.calls[0][0].paramsSerializer as ParamsSerializerOptions

expect(paramsSerializer).toBeDefined()
expect(paramsSerializer({ param: 'value' })).toBe('stringified parameters')
expect(paramsSerializer.serialize?.({ param: 'value' })).toBe('stringified parameters')

expect(stringifyMock).toHaveBeenCalledTimes(1)
expect(stringifyMock).toHaveBeenCalledWith({ param: 'value' }, { indices: false })
Expand All @@ -184,7 +184,7 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -203,7 +203,7 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -226,7 +226,7 @@ describe('EndpointClient', () => {
},
data: { name: 'Bob' },
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -244,7 +244,7 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -267,7 +267,7 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')

Expand Down Expand Up @@ -424,7 +424,7 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -443,7 +443,7 @@ describe('EndpointClient', () => {
params: {
locationId: 'XXX',
},
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -460,7 +460,7 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -477,7 +477,7 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -497,7 +497,7 @@ describe('EndpointClient', () => {
name: 'Bill',
},
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -516,7 +516,7 @@ describe('EndpointClient', () => {
name: 'Bill',
},
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -535,7 +535,7 @@ describe('EndpointClient', () => {
name: 'Joe',
},
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand All @@ -552,7 +552,7 @@ describe('EndpointClient', () => {
},
data: undefined,
params: undefined,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
})
expect(response.status).toBe('ok')
})
Expand Down
4 changes: 2 additions & 2 deletions test/unit/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function expectedRequest(config: any): any {
data: undefined,
params: undefined,
...config,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
}
}

Expand All @@ -19,6 +19,6 @@ export function buildRequest(path?: string, params?: any, data?: any, method = '
},
data: data,
params: params,
paramsSerializer: expect.any(Function),
paramsSerializer: expect.objectContaining({ serialize: expect.any(Function) }),
}
}

0 comments on commit 0798d93

Please sign in to comment.