From 8cffd97b62bc1dd3e133aa1ec03cfff2cccd6988 Mon Sep 17 00:00:00 2001 From: Yuta Kasai Date: Tue, 12 Mar 2024 16:33:18 +0900 Subject: [PATCH] Fix get parameter and delete it only when undefined (#754) This change is almost same as https://github.com/line/line-bot-sdk-nodejs/pull/600. When we call `client.getFollowers(undefined, 100)`, currently it requests `/v2/bot/followers/ids?start=&limit=100`. We should make it to request `/v2/bot/followers/ids?limit=100`. Thus this change delete kay from parameter if value is undefined. --- .../apiBody/normal.pebble | 5 ++ .../api/channelAccessTokenClient.ts | 10 ++++ lib/insight/api/insightClient.ts | 20 +++++++ .../api/manageAudienceClient.ts | 5 ++ lib/messaging-api/api/messagingApiClient.ts | 60 +++++++++++++++++++ lib/module/api/lineModuleClient.ts | 5 ++ test/libs-messagingApi.spec.ts | 37 ++++++++++++ 7 files changed, 142 insertions(+) diff --git a/generator/src/main/resources/line-bot-sdk-nodejs-generator/apiBody/normal.pebble b/generator/src/main/resources/line-bot-sdk-nodejs-generator/apiBody/normal.pebble index 19e4f9b2d..caa26c520 100644 --- a/generator/src/main/resources/line-bot-sdk-nodejs-generator/apiBody/normal.pebble +++ b/generator/src/main/resources/line-bot-sdk-nodejs-generator/apiBody/normal.pebble @@ -24,6 +24,11 @@ "{{param.paramName}}": {{param.paramName}}, {% endfor %} }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); {% endif %} {% if op.hasHeaderParams %}const headerParams = { {% for param in op.headerParams -%} diff --git a/lib/channel-access-token/api/channelAccessTokenClient.ts b/lib/channel-access-token/api/channelAccessTokenClient.ts index 3f90790da..eaf05d43c 100644 --- a/lib/channel-access-token/api/channelAccessTokenClient.ts +++ b/lib/channel-access-token/api/channelAccessTokenClient.ts @@ -94,6 +94,11 @@ export class ChannelAccessTokenClient { clientAssertionType: clientAssertionType, clientAssertion: clientAssertion, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/oauth2/v2.1/tokens/kid", @@ -415,6 +420,11 @@ export class ChannelAccessTokenClient { const queryParams = { accessToken: accessToken, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get("/oauth2/v2.1/verify", queryParams); return { httpResponse: res, body: await res.json() }; diff --git a/lib/insight/api/insightClient.ts b/lib/insight/api/insightClient.ts index e8fb6f0eb..4fa804762 100644 --- a/lib/insight/api/insightClient.ts +++ b/lib/insight/api/insightClient.ts @@ -109,6 +109,11 @@ export class InsightClient { const queryParams = { requestId: requestId, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/insight/message/event", @@ -143,6 +148,11 @@ export class InsightClient { const queryParams = { date: date, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/insight/followers", @@ -177,6 +187,11 @@ export class InsightClient { const queryParams = { date: date, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/insight/message/delivery", @@ -225,6 +240,11 @@ export class InsightClient { from: from, to: to, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/insight/message/event/aggregation", diff --git a/lib/manage-audience/api/manageAudienceClient.ts b/lib/manage-audience/api/manageAudienceClient.ts index 5f83b16c7..ab305b0a5 100644 --- a/lib/manage-audience/api/manageAudienceClient.ts +++ b/lib/manage-audience/api/manageAudienceClient.ts @@ -374,6 +374,11 @@ export class ManageAudienceClient { includesExternalPublicGroups: includesExternalPublicGroups, createRoute: createRoute, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/audienceGroup/list", diff --git a/lib/messaging-api/api/messagingApiClient.ts b/lib/messaging-api/api/messagingApiClient.ts index 725dd6779..aa81d7513 100644 --- a/lib/messaging-api/api/messagingApiClient.ts +++ b/lib/messaging-api/api/messagingApiClient.ts @@ -331,6 +331,11 @@ export class MessagingApiClient { const queryParams = { date: date, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/message/delivery/ad_phone", @@ -369,6 +374,11 @@ export class MessagingApiClient { limit: limit, start: start, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/message/aggregation/list", @@ -469,6 +479,11 @@ export class MessagingApiClient { start: start, limit: limit, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get("/v2/bot/followers/ids", queryParams); return { httpResponse: res, body: await res.json() }; @@ -567,6 +582,11 @@ export class MessagingApiClient { const queryParams = { start: start, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/group/{groupId}/members/ids".replace( @@ -669,6 +689,11 @@ export class MessagingApiClient { const queryParams = { requestId: requestId, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/message/progress/narrowcast", @@ -701,6 +726,11 @@ export class MessagingApiClient { const queryParams = { date: date, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/message/delivery/broadcast", @@ -733,6 +763,11 @@ export class MessagingApiClient { const queryParams = { date: date, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/message/delivery/multicast", @@ -765,6 +800,11 @@ export class MessagingApiClient { const queryParams = { date: date, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/message/delivery/push", @@ -797,6 +837,11 @@ export class MessagingApiClient { const queryParams = { date: date, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/message/delivery/reply", @@ -829,6 +874,11 @@ export class MessagingApiClient { const queryParams = { date: date, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/message/delivery/pnp", @@ -965,6 +1015,11 @@ export class MessagingApiClient { const queryParams = { requestId: requestId, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/richmenu/progress/batch", @@ -1111,6 +1166,11 @@ export class MessagingApiClient { const queryParams = { start: start, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get( "/v2/bot/room/{roomId}/members/ids".replace("{roomId}", String(roomId)), diff --git a/lib/module/api/lineModuleClient.ts b/lib/module/api/lineModuleClient.ts index 999c62d34..cc76e2ddc 100644 --- a/lib/module/api/lineModuleClient.ts +++ b/lib/module/api/lineModuleClient.ts @@ -158,6 +158,11 @@ export class LineModuleClient { start: start, limit: limit, }; + Object.keys(queryParams).forEach((key: keyof typeof queryParams) => { + if (queryParams[key] === undefined) { + delete queryParams[key]; + } + }); const res = await this.httpClient.get("/v2/bot/list", queryParams); return { httpResponse: res, body: await res.json() }; diff --git a/test/libs-messagingApi.spec.ts b/test/libs-messagingApi.spec.ts index 208206e7f..eab38cb37 100644 --- a/test/libs-messagingApi.spec.ts +++ b/test/libs-messagingApi.spec.ts @@ -157,4 +157,41 @@ describe("messagingApi", () => { next: "yANU9IA..", }); }); + + it("get followers without |start| parameter", async () => { + let requestCount = 0; + server.use( + http.get( + "https://api.line.me/v2/bot/followers/ids", + async ({ request, params, cookies }) => { + requestCount++; + + equal( + request.headers.get("Authorization"), + "Bearer test_channel_access_token", + ); + equal( + request.headers.get("User-Agent"), + `${pkg.name}/${pkg.version}`, + ); + + const url = new URL(request.url); + const searchParams = url.searchParams; + equal(searchParams.has("start"), false); + equal(searchParams.get("limit"), "100"); + + return HttpResponse.json({ + userIds: ["UAAAAAAAAAAAAAA"], + next: "yANU9IA..", + }); + }, + ), + ); + + const res = await client.getFollowers(undefined, 100); + deepEqual(res, { + userIds: ["UAAAAAAAAAAAAAA"], + next: "yANU9IA..", + }); + }); });