Skip to content

Commit 7b77e4e

Browse files
fix(Transaction): Fixed an issue related to query params with special characters (#42)
1 parent db09c78 commit 7b77e4e

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ When we make [non-breaking changes](https://developer.paddle.com/api-reference/a
1212

1313
This means when upgrading minor versions of the SDK, you may notice type errors. You can safely ignore these or fix by adding additional type guards.
1414

15+
## 1.5.1 - 2024-09-10
16+
17+
### Fixed
18+
19+
- Fixed a bug where query parameters with special characters were not passed correctly to the API.
20+
- Dependabot security updates.
21+
22+
---
23+
1524
## 1.5.0 - 2024-08-16
1625

1726
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@paddle/paddle-node-sdk",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"description": "A Node.js SDK that you can use to integrate Paddle Billing with applications written in server-side JavaScript.",
55
"main": "./dist/cjs/index.js",
66
"module": "./dist/esm/index.js",

src/__tests__/resources/transactions.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ describe('TransactionsResource', () => {
5050
const queryParams: ListTransactionQueryParameters = {
5151
after: '2',
5252
id: ['1234'],
53+
'createdAt[GTE]': '2024-09-10T15:38:35.675098Z',
5354
};
5455

5556
const transactionCollection = transactionsResource.list(queryParams);
5657
let transactions = await transactionCollection.next();
5758

58-
expect(paddleInstance.get).toBeCalledWith('/transactions?after=2&id=1234');
59+
expect(paddleInstance.get).toBeCalledWith(
60+
'/transactions?after=2&id=1234&created_at%5BGTE%5D=2024-09-10T15%3A38%3A35.675098Z',
61+
);
5962
expect(transactions.length).toBe(1);
6063
});
6164

src/internal/base/query-parameters.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import snakeCase from 'lodash/snakeCase';
1+
/** We cannot use lodash to convert camelCase to snake_case because it will remove special characters and convert all text to lowercase.
2+
* We have date fields in the format `updatedAt[GTE]` and we need to convert them to `updated_at[GTE]`.
3+
*/
4+
function convertCamelCaseToSnakeCase(input: string) {
5+
return input.replace(/\b(\w+)([A-Z][a-z])/g, (_match: string, p1, p2) => {
6+
return p1.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase() + '_' + p2.toLowerCase();
7+
});
8+
}
29

310
export class QueryParameters<T> {
411
constructor(private readonly queryParameters: T) {}
@@ -7,7 +14,7 @@ export class QueryParameters<T> {
714
for (const key in this.queryParameters) {
815
const value = this.queryParameters[key];
916
if (key && value) {
10-
urlSearchParam.append(snakeCase(key), `${value}`);
17+
urlSearchParam.append(convertCamelCaseToSnakeCase(key), `${value}`);
1118
}
1219
}
1320
return '?' + urlSearchParam.toString();

0 commit comments

Comments
 (0)