Skip to content

Commit

Permalink
fix(Transaction): Fixed an issue related to query params with special…
Browse files Browse the repository at this point in the history
… characters
  • Loading branch information
vijayasingam-paddle committed Sep 10, 2024
1 parent f7c657a commit 5f530f2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ When we make [non-breaking changes](https://developer.paddle.com/api-reference/a

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.

## 1.5.1 - 2024-09-10

### Fixed

- Fixed a bug where query parameters with special characters were not passed correctly to the API.
- Dependabot security updates.

---

## 1.5.0 - 2024-08-16

### Added
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paddle/paddle-node-sdk",
"version": "1.5.0",
"version": "1.5.1",
"description": "A Node.js SDK that you can use to integrate Paddle Billing with applications written in server-side JavaScript.",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/resources/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ describe('TransactionsResource', () => {
const queryParams: ListTransactionQueryParameters = {
after: '2',
id: ['1234'],
'createdAt[GTE]': '2024-09-10T15:38:35.675098Z',
};

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

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

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

export class QueryParameters<T> {
constructor(private readonly queryParameters: T) {}
Expand All @@ -7,7 +14,7 @@ export class QueryParameters<T> {
for (const key in this.queryParameters) {
const value = this.queryParameters[key];
if (key && value) {
urlSearchParam.append(snakeCase(key), `${value}`);
urlSearchParam.append(convertCamelCaseToSnakeCase(key), `${value}`);
}
}
return '?' + urlSearchParam.toString();
Expand Down

0 comments on commit 5f530f2

Please sign in to comment.