Skip to content

Commit e2fb7fd

Browse files
authored
fix(ts-client): fix issue with undefined body (#1199)
- fix issue with undefined json body for HEAD requests
1 parent 2898e78 commit e2fb7fd

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

.changeset/modern-feet-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools/ts-client': patch
3+
---
4+
5+
Fix issue with undefined response body

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
22.19.0
1+
24.11.0

packages/checkout-sdk/test/helpers/api-helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const ctpClient = new ClientBuilder()
2828
.withHttpMiddleware(httpMiddleware)
2929
.build()
3030

31+
export type { Transaction } from '../../src'
3132
export const ctpApiBuilder = createApiBuilderFromCtpClient(
3233
ctpClient,
3334
'https://checkout.europe-west1.gcp.commercetools.com'

packages/checkout-sdk/test/integration-tests/transaction.test.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { ctpApiBuilder as ctpApiRoot } from '../helpers/ctp-api-helper'
2-
import { ctpApiBuilder } from '../helpers/api-helpers'
2+
import { ctpApiBuilder, Transaction } from '../helpers/api-helpers'
33

44
describe('::transaction', () => {
5-
let cart: any
5+
let cart: any, transactions: Transaction
66

77
afterAll(async () => {
88
const updatedCart = await ctpApiRoot
@@ -31,7 +31,7 @@ describe('::transaction', () => {
3131
})
3232

3333
it('should create a transaction using created cart', async () => {
34-
const transactions = await ctpApiBuilder
34+
const _transactions = await ctpApiBuilder
3535
.transactions()
3636
.post({
3737
body: {
@@ -59,7 +59,19 @@ describe('::transaction', () => {
5959
})
6060
.execute()
6161

62-
expect(transactions).toBeDefined()
63-
expect(transactions.statusCode).toEqual(201)
62+
expect(_transactions).toBeDefined()
63+
expect(_transactions.statusCode).toEqual(201)
64+
transactions = _transactions.body
65+
})
66+
67+
it('should retrieve a transaction using ID', async () => {
68+
const _transaction = await ctpApiBuilder
69+
.transactions()
70+
.withId({ id: transactions.id })
71+
.get()
72+
.execute()
73+
74+
expect(_transaction).toBeDefined()
75+
expect(_transaction.statusCode).toEqual(200)
6476
})
6577
})

packages/sdk-client-v3/src/utils/executor.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,22 @@ export default async function executor(request: HttpClientConfig) {
146146
}
147147

148148
const response: IResponse = await executeWithRetry()
149+
150+
// check if it's a HEAD request, then return
151+
if (rest.method == 'HEAD') {
152+
return {
153+
data: null,
154+
retryCount,
155+
statusCode: response.status || response.statusCode || data.statusCode,
156+
headers: response.headers,
157+
}
158+
}
159+
149160
try {
150161
// try to parse the `fetch` response as text
151162
if (response.text && typeof response.text == 'function') {
152-
result =
153-
(await response.text()) ||
154-
JSON.stringify(response[Object.getOwnPropertySymbols(response)[1]])
163+
result = await response.text()
164+
155165
data = JSON.parse(result)
156166
} else {
157167
// axios response

0 commit comments

Comments
 (0)