Skip to content

Commit d8a28a3

Browse files
committed
refactor: only stringify objects types
1 parent 48b9b6d commit d8a28a3

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/core/infra/repositories/ApiRepository.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export abstract class ApiRepository {
2020

2121
public async doPost(
2222
apiEndpoint: string,
23-
data: string | object,
23+
data: string | object | boolean,
2424
queryParams: object = {},
2525
contentType: string = ApiConstants.CONTENT_TYPE_APPLICATION_JSON
2626
): Promise<AxiosResponse> {
@@ -29,7 +29,7 @@ export abstract class ApiRepository {
2929

3030
public async doPut(
3131
apiEndpoint: string,
32-
data: string | object,
32+
data: string | object | boolean,
3333
queryParams: object = {},
3434
contentType: string = ApiConstants.CONTENT_TYPE_APPLICATION_JSON
3535
): Promise<AxiosResponse> {
@@ -70,12 +70,20 @@ export abstract class ApiRepository {
7070
private async doRequest(
7171
method: 'post' | 'put',
7272
apiEndpoint: string,
73-
data: string | object,
73+
data: string | object | boolean,
7474
queryParams: object = {},
7575
contentType: string = ApiConstants.CONTENT_TYPE_APPLICATION_JSON
7676
): Promise<AxiosResponse> {
77-
const requestData =
78-
contentType == ApiConstants.CONTENT_TYPE_APPLICATION_JSON ? JSON.stringify(data) : data
77+
let requestData = data
78+
79+
if (contentType === ApiConstants.CONTENT_TYPE_APPLICATION_JSON) {
80+
if (typeof data === 'object') {
81+
requestData = JSON.stringify(data)
82+
} else if (typeof data === 'boolean') {
83+
requestData = data.toString()
84+
}
85+
}
86+
7987
const requestUrl = buildRequestUrl(apiEndpoint)
8088
const requestConfig = buildRequestConfig(true, queryParams, contentType)
8189

test/unit/auth/AuthRepository.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('logout', () => {
3535

3636
expect(axios.post).toHaveBeenCalledWith(
3737
expectedApiEndpoint,
38-
JSON.stringify(''),
38+
'',
3939
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
4040
)
4141

@@ -46,7 +46,7 @@ describe('logout', () => {
4646

4747
expect(axios.post).toHaveBeenCalledWith(
4848
expectedApiEndpoint,
49-
JSON.stringify(''),
49+
'',
5050
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE
5151
)
5252
})
@@ -59,7 +59,7 @@ describe('logout', () => {
5959

6060
expect(axios.post).toHaveBeenCalledWith(
6161
`${TestConstants.TEST_API_URL}/logout`,
62-
JSON.stringify(''),
62+
'',
6363
TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
6464
)
6565
expect(error).toBeInstanceOf(Error)

0 commit comments

Comments
 (0)