From dc6725377bbbf26b1cc6e762cdce6dd4df05101a Mon Sep 17 00:00:00 2001 From: Keith Date: Tue, 6 Jun 2023 23:55:16 +0800 Subject: [PATCH] fix: return empty array if payload of batch request is an empty array Short circuit batch request with an empty array if request payload is an empty array --- packages/ckb-sdk-rpc/__tests__/ckb-rpc.test.js | 5 +++++ packages/ckb-sdk-rpc/src/index.ts | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/packages/ckb-sdk-rpc/__tests__/ckb-rpc.test.js b/packages/ckb-sdk-rpc/__tests__/ckb-rpc.test.js index f46c056e..26c4c7e0 100644 --- a/packages/ckb-sdk-rpc/__tests__/ckb-rpc.test.js +++ b/packages/ckb-sdk-rpc/__tests__/ckb-rpc.test.js @@ -1728,6 +1728,11 @@ describe('Test with mock', () => { }, ]) }) + + it('should return empty array as response if request is an empty array', async () => { + const emptyBatch = rpc.createBatchRequest([]) + expect(await emptyBatch.exec()).toEqual([]) + }) }) }) diff --git a/packages/ckb-sdk-rpc/src/index.ts b/packages/ckb-sdk-rpc/src/index.ts index 37d5c946..410d712f 100644 --- a/packages/ckb-sdk-rpc/src/index.ts +++ b/packages/ckb-sdk-rpc/src/index.ts @@ -101,6 +101,10 @@ class CKBRPC extends Base { } }) + if (!payload.length) { + return [] + } + const batchRes = await axios({ method: 'POST', headers: { 'content-type': 'application/json' },