Skip to content

Commit

Permalink
Do not override base url (#836)
Browse files Browse the repository at this point in the history
## Changes
The constructor of line-bot-sdk-nodejs client modifies the given config,
which should ideally behave immutably. Reusing the config as is can lead
to unexpected behavior(see below sample `#2`). This change ensures that
the config is not unintentionally overwritten.

## How to reproduce this

(normal)
```ts
// #1 (it works, but `config`'s baseURL is updated expectedly)
const config = {
  channelAccessToken: "token-token",
};
const blobClient = new messagingApi.MessagingApiBlobClient(config);
// config.baseURL -> https://api-data.line.me (Unexpected, but it works)
```

(this issue)
Executing the following code does not set `api.line-data.me`. A
workaround is to clone the config, but this is not convenient and
debugging can be troublesome.

```ts
// #2 (blob doesn't work, because the first unexpected update blocks blob client uses expected baseURL)
const config = {
  channelAccessToken: "token-token",
};
const client = new messagingApi.MessagingApiClient(config);
// config.baseURL -> https://api.line.me
const blobClient = new messagingApi.MessagingApiBlobClient(config);
// config.baseURL -> https://api.line.me, not https://api-data.line.me (Unexpected, and it won't work)
```
  • Loading branch information
Yang-33 authored May 7, 2024
1 parent f986b45 commit 2b7783d
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ export class {{operations.classname}} {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = '{{endpoint(operations.classname)}}';
}
const baseURL = config.baseURL || '{{endpoint(operations.classname)}}';
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
{% if authMethods != null -%}
Authorization: "Bearer " + config.channelAccessToken,
{% endif -%}
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/channel-access-token/api/channelAccessTokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ export class ChannelAccessTokenClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api.line.me";
}
const baseURL = config.baseURL || "https://api.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/insight/api/insightClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ export class InsightClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api.line.me";
}
const baseURL = config.baseURL || "https://api.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/liff/api/liffClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ export class LiffClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api.line.me";
}
const baseURL = config.baseURL || "https://api.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/manage-audience/api/manageAudienceBlobClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export class ManageAudienceBlobClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api-data.line.me";
}
const baseURL = config.baseURL || "https://api-data.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/manage-audience/api/manageAudienceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ export class ManageAudienceClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api.line.me";
}
const baseURL = config.baseURL || "https://api.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/messaging-api/api/messagingApiBlobClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export class MessagingApiBlobClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api-data.line.me";
}
const baseURL = config.baseURL || "https://api-data.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/messaging-api/api/messagingApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,12 @@ export class MessagingApiClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api.line.me";
}
const baseURL = config.baseURL || "https://api.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/module-attach/api/lineModuleAttachClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export class LineModuleAttachClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://manager.line.biz";
}
const baseURL = config.baseURL || "https://manager.line.biz";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/module/api/lineModuleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ export class LineModuleClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api.line.me";
}
const baseURL = config.baseURL || "https://api.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
6 changes: 2 additions & 4 deletions lib/shop/api/shopClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ export class ShopClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
if (!config.baseURL) {
config.baseURL = "https://api.line.me";
}
const baseURL = config.baseURL || "https://api.line.me";
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
baseURL: config.baseURL,
baseURL: baseURL,
});
}

Expand Down
12 changes: 12 additions & 0 deletions test/libs-messagingApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,16 @@ describe("messagingApi", () => {
next: "yANU9IA..",
});
});

it("config is not overrided", async () => {
const config = {
channelAccessToken: "token-token",
baseURL: undefined,
};
const client = new messagingApi.MessagingApiClient(config);
equal(config.baseURL, undefined);

const blobClient = new messagingApi.MessagingApiBlobClient(config);
equal(config.baseURL, undefined);
});
});

0 comments on commit 2b7783d

Please sign in to comment.