Skip to content

Commit

Permalink
Fix not to decode as JSON the empty response body (#1013)
Browse files Browse the repository at this point in the history
## Changes

This PR fixes the issue where JSON deserialization occurs even when the
response body is empty.

## References
- #998
  • Loading branch information
habara-k authored Oct 24, 2024
1 parent 561d1b4 commit 0e0e5b7
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
{% endfor %},
form,
);
return {httpResponse: res, body: await res.json()};
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@
{% elseif op.hasQueryParams %}queryParams,{% endif %}
{% if op.hasHeaderParams %}{headers: headerParams},{% endif %}
);
return {httpResponse: res, body: await res.json()};
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
{% endif %}
32 changes: 24 additions & 8 deletions lib/channel-access-token/api/channelAccessTokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ export class ChannelAccessTokenClient {
"/oauth2/v2.1/tokens/kid",
queryParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Issue short-lived channel access token
Expand Down Expand Up @@ -157,7 +159,9 @@ export class ChannelAccessTokenClient {
"/v2/oauth/accessToken",
formParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Issues a channel access token that allows you to specify a desired expiration date. This method lets you use JWT assertion for authentication.
Expand Down Expand Up @@ -210,7 +214,9 @@ export class ChannelAccessTokenClient {
"/oauth2/v2.1/token",
formParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Issues a new stateless channel access token, which doesn\'t have max active token limit unlike the other token types. The newly issued token is only valid for 15 minutes but can not be revoked until it naturally expires.
Expand Down Expand Up @@ -272,7 +278,9 @@ export class ChannelAccessTokenClient {
});

const res = await this.httpClient.postForm("/oauth2/v3/token", formParams);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Revoke short-lived or long-lived channel access token
Expand Down Expand Up @@ -306,7 +314,9 @@ export class ChannelAccessTokenClient {
});

const res = await this.httpClient.postForm("/v2/oauth/revoke", formParams);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Revoke channel access token v2.1
Expand Down Expand Up @@ -359,7 +369,9 @@ export class ChannelAccessTokenClient {
"/oauth2/v2.1/revoke",
formParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Verify the validity of short-lived and long-lived channel access tokens
Expand Down Expand Up @@ -393,7 +405,9 @@ export class ChannelAccessTokenClient {
});

const res = await this.httpClient.postForm("/v2/oauth/verify", formParams);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* You can verify whether a Channel access token with a user-specified expiration (Channel Access Token v2.1) is valid.
Expand Down Expand Up @@ -427,6 +441,8 @@ export class ChannelAccessTokenClient {
});

const res = await this.httpClient.get("/oauth2/v2.1/verify", queryParams);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
}
20 changes: 15 additions & 5 deletions lib/insight/api/insightClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ export class InsightClient {
Types.ApiResponseType<GetFriendsDemographicsResponse>
> {
const res = await this.httpClient.get("/v2/bot/insight/demographic");
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Returns statistics about how users interact with narrowcast messages or broadcast messages sent from your LINE Official Account.
Expand Down Expand Up @@ -119,7 +121,9 @@ export class InsightClient {
"/v2/bot/insight/message/event",
queryParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Returns the number of users who have added the LINE Official Account on or before a specified date.
Expand Down Expand Up @@ -158,7 +162,9 @@ export class InsightClient {
"/v2/bot/insight/followers",
queryParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Returns the number of messages sent from LINE Official Account on a specified day.
Expand Down Expand Up @@ -197,7 +203,9 @@ export class InsightClient {
"/v2/bot/insight/message/delivery",
queryParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* You can check the per-unit statistics of how users interact with push messages and multicast messages sent from your LINE Official Account.
Expand Down Expand Up @@ -250,6 +258,8 @@ export class InsightClient {
"/v2/bot/insight/message/event/aggregation",
queryParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
}
16 changes: 12 additions & 4 deletions lib/liff/api/liffClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export class LiffClient {
const params = addLiffAppRequest;

const res = await this.httpClient.post("/liff/v1/apps", params);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Deletes a LIFF app from a channel.
Expand Down Expand Up @@ -116,7 +118,9 @@ export class LiffClient {
const res = await this.httpClient.delete(
"/liff/v1/apps/{liffId}".replace("{liffId}", String(liffId)),
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Gets information on all the LIFF apps added to the channel.
Expand All @@ -139,7 +143,9 @@ export class LiffClient {
Types.ApiResponseType<GetAllLiffAppsResponse>
> {
const res = await this.httpClient.get("/liff/v1/apps");
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Update LIFF app settings
Expand Down Expand Up @@ -176,6 +182,8 @@ export class LiffClient {
"/liff/v1/apps/{liffId}".replace("{liffId}", String(liffId)),
params,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
}
8 changes: 6 additions & 2 deletions lib/manage-audience/api/manageAudienceBlobClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export class ManageAudienceBlobClient {
"/v2/bot/audienceGroup/upload/byFile",
form,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Create audience for uploading user IDs (by file).
Expand Down Expand Up @@ -155,6 +157,8 @@ export class ManageAudienceBlobClient {
"/v2/bot/audienceGroup/upload/byFile",
form,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
}
44 changes: 33 additions & 11 deletions lib/manage-audience/api/manageAudienceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export class ManageAudienceClient {
String(audienceGroupId),
),
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Add user IDs or Identifiers for Advertisers (IFAs) to an audience for uploading user IDs (by JSON)
Expand Down Expand Up @@ -133,7 +135,9 @@ export class ManageAudienceClient {
"/v2/bot/audienceGroup/upload",
params,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Create audience for uploading user IDs (by JSON)
Expand Down Expand Up @@ -165,7 +169,9 @@ export class ManageAudienceClient {
"/v2/bot/audienceGroup/upload",
params,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Create audience for click-based retargeting
Expand Down Expand Up @@ -199,7 +205,9 @@ export class ManageAudienceClient {
"/v2/bot/audienceGroup/click",
params,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Create audience for impression-based retargeting
Expand Down Expand Up @@ -230,7 +238,9 @@ export class ManageAudienceClient {
const params = createImpBasedAudienceGroupRequest;

const res = await this.httpClient.post("/v2/bot/audienceGroup/imp", params);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Delete audience
Expand Down Expand Up @@ -260,7 +270,9 @@ export class ManageAudienceClient {
String(audienceGroupId),
),
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Gets audience data.
Expand Down Expand Up @@ -290,7 +302,9 @@ export class ManageAudienceClient {
String(audienceGroupId),
),
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Get the authority level of the audience
Expand All @@ -313,7 +327,9 @@ export class ManageAudienceClient {
const res = await this.httpClient.get(
"/v2/bot/audienceGroup/authorityLevel",
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Gets data for more than one audience.
Expand Down Expand Up @@ -384,7 +400,9 @@ export class ManageAudienceClient {
"/v2/bot/audienceGroup/list",
queryParams,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Change the authority level of the audience
Expand Down Expand Up @@ -418,7 +436,9 @@ export class ManageAudienceClient {
"/v2/bot/audienceGroup/authorityLevel",
params,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Renames an existing audience.
Expand Down Expand Up @@ -460,6 +480,8 @@ export class ManageAudienceClient {
),
params,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
}
8 changes: 6 additions & 2 deletions lib/messaging-api/api/messagingApiBlobClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ export class MessagingApiBlobClient {
String(messageId),
),
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
/**
* Download rich menu image.
Expand Down Expand Up @@ -217,6 +219,8 @@ export class MessagingApiBlobClient {
),
params,
);
return { httpResponse: res, body: await res.json() };
const text = await res.text();
const parsedBody = text ? JSON.parse(text) : null;
return { httpResponse: res, body: parsedBody };
}
}
Loading

0 comments on commit 0e0e5b7

Please sign in to comment.