Skip to content

Commit

Permalink
fix: support false value in query parameters. (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz authored Feb 3, 2023
1 parent 391c48e commit abb77d7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-spoons-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/api-utils': patch
---

The `OperationRequestBuilder` does not skip the `false` value anymore for URL parameters.
20 changes: 18 additions & 2 deletions packages/apiUtils/src/resolvers2/OperationRequestBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface TestRequest {
userId: number;
traceId: number;
password: string;
flag: boolean;
}

const operation: Operation<TestRequest, unknown, unknown, unknown> = {
Expand All @@ -14,7 +15,7 @@ const operation: Operation<TestRequest, unknown, unknown, unknown> = {
groupName: 'test',
method: 'POST',
urlPathParamNames: ['userId'],
urlSearchParamNames: ['traceId'],
urlSearchParamNames: ['traceId', 'flag'],
urlPathPattern: '/api/{userId}',
bodyParamNames: ['password'],
bodyType: 'properties',
Expand All @@ -23,6 +24,7 @@ const operation: Operation<TestRequest, unknown, unknown, unknown> = {
userId: String(request.userId),
trace_id: String(request.traceId),
extraSearchParam: '100',
flag: request.flag,
};
},
getRequestBody: (request) => {
Expand Down Expand Up @@ -61,12 +63,25 @@ describe('OperationRequestBuilder', () => {
userId: 100,
traceId: 200,
password: 'foo',
flag: true,
});

expect(url).toBe('https://localhost/api/100');
expect(Object.keys(urlSearchParams).length).toBe(2);
expect(Object.keys(urlSearchParams).length).toBe(3);
expect(urlSearchParams['trace_id']).toBe('200');
expect(urlSearchParams['extraSearchParam']).toBe('100');
expect(urlSearchParams['flag']).toBe(true);
});

it('keeps false value', () => {
const { urlSearchParams } = builder.prepareUrl('https://localhost', {
userId: 1,
traceId: 1,
password: '1',
flag: false,
});

expect(urlSearchParams['flag']).toBe(false);
});
});

Expand All @@ -76,6 +91,7 @@ describe('OperationRequestBuilder', () => {
userId: 100,
traceId: 200,
password: 'foo',
flag: true,
}) as OperationRequestPropertiesBody;

expect(Object.keys(body).length).toBe(2);
Expand Down
4 changes: 2 additions & 2 deletions packages/apiUtils/src/resolvers2/OperationRequestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class OperationRequestBuilder<Request> {

for (const paramName of this.operation.urlPathParamNames ?? []) {
const paramValue = urlParams[paramName as string];
if (!paramValue) {
if (paramValue === undefined || paramValue === null) {
throw new Error(`Param ${paramName as string} is required`);
}
urlPath = urlPath.replace(`{${paramName as string}}`, paramValue as string);
Expand All @@ -35,7 +35,7 @@ export class OperationRequestBuilder<Request> {
.filter((paramName) => !this.operation.urlPathParamNames?.includes(paramName as keyof Request))
.forEach((paramName) => {
const paramValue = urlParams[paramName];
if (paramValue) {
if (paramValue !== undefined && paramValue !== null) {
urlSearchParams[paramName] = paramValue;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export const mockGetContractNFTs = MockScenarios.create(
url: `/nft/:address`,
getParams: ({ req }) => ({
address: req.params.address,
disable_total: req.url.searchParams.get('disable_total'),
}),
},
[
{
condition: {
address: '0x75e3e9c92162e62000425c98769965a76c2e387a',
disable_total: 'false',
},
response: {
status: 'SYNCING',
Expand Down Expand Up @@ -63,6 +65,7 @@ export const mockGetContractNFTs = MockScenarios.create(
{
condition: {
address: '0x75e3e9c92162e62000425c98769965a76c2e387',
disable_total: 'true',
},
responseStatus: 400,
response: createErrorResponse('[C0005] Invalid address provided'),
Expand Down
2 changes: 2 additions & 0 deletions packages/evmApi/integration/test/getContractNFTs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('getContractNFTs', () => {
it('should get NFTs for a given contract address', async () => {
const result = await EvmApi.nft.getContractNFTs({
address: '0x75e3e9c92162e62000425c98769965a76c2e387a',
disableTotal: false,
});

const response = result.raw.result?.at(0);
Expand All @@ -33,6 +34,7 @@ describe('getContractNFTs', () => {
const failedResult = await EvmApi.nft
.getContractNFTs({
address: '0x75e3e9c92162e62000425c98769965a76c2e387a',
disableTotal: true,
})
.then()
.catch((err) => {
Expand Down

1 comment on commit abb77d7

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test coverage

Title Lines Statements Branches Functions
api-utils Coverage: 25%
26.2% (49/187) 25.49% (13/51) 22.85% (8/35)
auth Coverage: 88%
91.48% (86/94) 81.81% (18/22) 85.18% (23/27)
evm-api Coverage: 100%
100% (82/82) 66.66% (6/9) 100% (50/50)
common-aptos-utils Coverage: 71%
70.4% (88/125) 65% (13/20) 78.04% (32/41)
common-evm-utils Coverage: 64%
64.67% (983/1520) 19.88% (133/669) 35.39% (212/599)
sol-api Coverage: 96%
96.66% (29/30) 66.66% (6/9) 91.66% (11/12)
common-sol-utils Coverage: 74%
73.77% (135/183) 60% (12/20) 65.67% (44/67)
common-streams-utils Coverage: 91%
91.58% (1176/1284) 78.83% (406/515) 82.45% (423/513)
streams Coverage: 88%
87.85% (557/634) 65.88% (56/85) 87.5% (119/136)

Please sign in to comment.