Skip to content

Commit 7e40057

Browse files
committed
01 Stop reading package.json in code
1 parent 0d7d57c commit 7e40057

File tree

22 files changed

+853
-261
lines changed

22 files changed

+853
-261
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ jobs:
1414
node-version: 18
1515
registry-url: 'https://registry.npmjs.org'
1616
- run: npm install
17-
- name: Update version in package.json, package-lock.json
17+
- name: Update version in package.json, package-lock.json, and lib/version.ts
1818
run: |
1919
VERSION=${{ github.event.release.tag_name }}
2020
VERSION=${VERSION#v}
2121
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" package.json
2222
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" package-lock.json
23+
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" lib/version.ts
2324
- run: npm run release
2425
env:
2526
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

generator/src/main/resources/line-bot-sdk-nodejs-generator/api_test.pebble

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import { {{import.classname}} } from '../{{import.filename}}';
1010
import { createServer } from "node:http";
1111
import { deepEqual, equal, ok } from "node:assert";
1212

13-
const pkg = require("../../../../package.json");
14-
1513
const channel_access_token = "test_channel_access_token";
1614

1715
{% macro paramDummyValue(param) %}
@@ -73,7 +71,7 @@ const channel_access_token = "test_channel_access_token";
7371
{% endif -%}
7472
equal(
7573
req.headers["user-agent"],
76-
`${pkg.name}/${pkg.version}`,
74+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
7775
);
7876
{% if op.isMultipart %}
7977
ok(

lib/channel-access-token/tests/api/ChannelAccessTokenClientTest.spec.ts

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import { VerifyChannelAccessTokenResponse } from "../../model/verifyChannelAcces
1010
import { createServer } from "node:http";
1111
import { deepEqual, equal, ok } from "node:assert";
1212

13-
const pkg = require("../../../../package.json");
14-
1513
const channel_access_token = "test_channel_access_token";
1614

1715
describe("ChannelAccessTokenClient", () => {
@@ -47,7 +45,10 @@ describe("ChannelAccessTokenClient", () => {
4745
),
4846
);
4947

50-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
48+
equal(
49+
req.headers["user-agent"],
50+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
51+
);
5152

5253
res.writeHead(200, { "Content-Type": "application/json" });
5354
res.end(JSON.stringify({}));
@@ -110,7 +111,10 @@ describe("ChannelAccessTokenClient", () => {
110111
),
111112
);
112113

113-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
114+
equal(
115+
req.headers["user-agent"],
116+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
117+
);
114118

115119
res.writeHead(200, { "Content-Type": "application/json" });
116120
res.end(JSON.stringify({}));
@@ -157,7 +161,10 @@ describe("ChannelAccessTokenClient", () => {
157161
.replace("{clientSecret}", "DUMMY"), // string
158162
);
159163

160-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
164+
equal(
165+
req.headers["user-agent"],
166+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
167+
);
161168

162169
res.writeHead(200, { "Content-Type": "application/json" });
163170
res.end(JSON.stringify({}));
@@ -207,7 +214,10 @@ describe("ChannelAccessTokenClient", () => {
207214
.replace("{clientSecret}", "DUMMY"), // string
208215
);
209216

210-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
217+
equal(
218+
req.headers["user-agent"],
219+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
220+
);
211221

212222
res.writeHead(200, { "Content-Type": "application/json" });
213223
res.end(JSON.stringify({}));
@@ -257,7 +267,10 @@ describe("ChannelAccessTokenClient", () => {
257267
.replace("{clientAssertion}", "DUMMY"), // string
258268
);
259269

260-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
270+
equal(
271+
req.headers["user-agent"],
272+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
273+
);
261274

262275
res.writeHead(200, { "Content-Type": "application/json" });
263276
res.end(JSON.stringify({}));
@@ -307,7 +320,10 @@ describe("ChannelAccessTokenClient", () => {
307320
.replace("{clientAssertion}", "DUMMY"), // string
308321
);
309322

310-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
323+
equal(
324+
req.headers["user-agent"],
325+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
326+
);
311327

312328
res.writeHead(200, { "Content-Type": "application/json" });
313329
res.end(JSON.stringify({}));
@@ -359,7 +375,10 @@ describe("ChannelAccessTokenClient", () => {
359375
.replace("{clientSecret}", "DUMMY"), // string
360376
);
361377

362-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
378+
equal(
379+
req.headers["user-agent"],
380+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
381+
);
363382

364383
res.writeHead(200, { "Content-Type": "application/json" });
365384
res.end(JSON.stringify({}));
@@ -417,7 +436,10 @@ describe("ChannelAccessTokenClient", () => {
417436
.replace("{clientSecret}", "DUMMY"), // string
418437
);
419438

420-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
439+
equal(
440+
req.headers["user-agent"],
441+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
442+
);
421443

422444
res.writeHead(200, { "Content-Type": "application/json" });
423445
res.end(JSON.stringify({}));
@@ -470,7 +492,10 @@ describe("ChannelAccessTokenClient", () => {
470492
"/v2/oauth/revoke".replace("{accessToken}", "DUMMY"), // string
471493
);
472494

473-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
495+
equal(
496+
req.headers["user-agent"],
497+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
498+
);
474499

475500
res.writeHead(200, { "Content-Type": "application/json" });
476501
res.end(JSON.stringify({}));
@@ -511,7 +536,10 @@ describe("ChannelAccessTokenClient", () => {
511536
"/v2/oauth/revoke".replace("{accessToken}", "DUMMY"), // string
512537
);
513538

514-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
539+
equal(
540+
req.headers["user-agent"],
541+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
542+
);
515543

516544
res.writeHead(200, { "Content-Type": "application/json" });
517545
res.end(JSON.stringify({}));
@@ -555,7 +583,10 @@ describe("ChannelAccessTokenClient", () => {
555583
.replace("{accessToken}", "DUMMY"), // string
556584
);
557585

558-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
586+
equal(
587+
req.headers["user-agent"],
588+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
589+
);
559590

560591
res.writeHead(200, { "Content-Type": "application/json" });
561592
res.end(JSON.stringify({}));
@@ -605,7 +636,10 @@ describe("ChannelAccessTokenClient", () => {
605636
.replace("{accessToken}", "DUMMY"), // string
606637
);
607638

608-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
639+
equal(
640+
req.headers["user-agent"],
641+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
642+
);
609643

610644
res.writeHead(200, { "Content-Type": "application/json" });
611645
res.end(JSON.stringify({}));
@@ -652,7 +686,10 @@ describe("ChannelAccessTokenClient", () => {
652686
"/v2/oauth/verify".replace("{accessToken}", "DUMMY"), // string
653687
);
654688

655-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
689+
equal(
690+
req.headers["user-agent"],
691+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
692+
);
656693

657694
res.writeHead(200, { "Content-Type": "application/json" });
658695
res.end(JSON.stringify({}));
@@ -693,7 +730,10 @@ describe("ChannelAccessTokenClient", () => {
693730
"/v2/oauth/verify".replace("{accessToken}", "DUMMY"), // string
694731
);
695732

696-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
733+
equal(
734+
req.headers["user-agent"],
735+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
736+
);
697737

698738
res.writeHead(200, { "Content-Type": "application/json" });
699739
res.end(JSON.stringify({}));
@@ -744,7 +784,10 @@ describe("ChannelAccessTokenClient", () => {
744784
),
745785
);
746786

747-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
787+
equal(
788+
req.headers["user-agent"],
789+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
790+
);
748791

749792
res.writeHead(200, { "Content-Type": "application/json" });
750793
res.end(JSON.stringify({}));
@@ -795,7 +838,10 @@ describe("ChannelAccessTokenClient", () => {
795838
),
796839
);
797840

798-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
841+
equal(
842+
req.headers["user-agent"],
843+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
844+
);
799845

800846
res.writeHead(200, { "Content-Type": "application/json" });
801847
res.end(JSON.stringify({}));

lib/http-axios.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import axios, {
66
} from "axios";
77
import { Readable } from "node:stream";
88
import { HTTPError, ReadError, RequestError } from "./exceptions";
9-
10-
const pkg = require("../package.json");
9+
import { USER_AGENT } from "./version";
1110

1211
interface httpClientConfig extends Partial<AxiosRequestConfig> {
1312
baseURL?: string;
@@ -25,7 +24,7 @@ export default class HTTPClient {
2524
this.instance = axios.create({
2625
baseURL,
2726
headers: Object.assign({}, defaultHeaders, {
28-
"User-Agent": `${pkg.name}/${pkg.version}`,
27+
"User-Agent": USER_AGENT,
2928
}),
3029
});
3130

lib/http-fetch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Readable } from "node:stream";
22
import { HTTPFetchError } from "./exceptions";
3+
import { USER_AGENT } from "./version";
34

4-
const pkg = require("../package.json");
55
export interface FetchRequestConfig {
66
headers?: Record<string, string>;
77
}
@@ -32,7 +32,7 @@ export default class HTTPFetchClient {
3232
constructor(config: httpFetchClientConfig) {
3333
this.baseURL = config.baseURL;
3434
this.defaultHeaders = {
35-
"User-Agent": `${pkg.name}/${pkg.version}`,
35+
"User-Agent": USER_AGENT,
3636
...config.defaultHeaders,
3737
};
3838
}

lib/insight/tests/api/InsightClientTest.spec.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import { GetStatisticsPerUnitResponse } from "../../model/getStatisticsPerUnitRe
99
import { createServer } from "node:http";
1010
import { deepEqual, equal, ok } from "node:assert";
1111

12-
const pkg = require("../../../../package.json");
13-
1412
const channel_access_token = "test_channel_access_token";
1513

1614
describe("InsightClient", () => {
@@ -25,7 +23,10 @@ describe("InsightClient", () => {
2523
equal(reqUrl.pathname, "/v2/bot/insight/demographic");
2624

2725
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
28-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
26+
equal(
27+
req.headers["user-agent"],
28+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
29+
);
2930

3031
res.writeHead(200, { "Content-Type": "application/json" });
3132
res.end(JSON.stringify({}));
@@ -62,7 +63,10 @@ describe("InsightClient", () => {
6263
equal(reqUrl.pathname, "/v2/bot/insight/demographic");
6364

6465
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
65-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
66+
equal(
67+
req.headers["user-agent"],
68+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
69+
);
6670

6771
res.writeHead(200, { "Content-Type": "application/json" });
6872
res.end(JSON.stringify({}));
@@ -112,7 +116,10 @@ describe("InsightClient", () => {
112116
);
113117

114118
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
115-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
119+
equal(
120+
req.headers["user-agent"],
121+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
122+
);
116123

117124
res.writeHead(200, { "Content-Type": "application/json" });
118125
res.end(JSON.stringify({}));
@@ -165,7 +172,10 @@ describe("InsightClient", () => {
165172
);
166173

167174
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
168-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
175+
equal(
176+
req.headers["user-agent"],
177+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
178+
);
169179

170180
res.writeHead(200, { "Content-Type": "application/json" });
171181
res.end(JSON.stringify({}));
@@ -218,7 +228,10 @@ describe("InsightClient", () => {
218228
);
219229

220230
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
221-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
231+
equal(
232+
req.headers["user-agent"],
233+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
234+
);
222235

223236
res.writeHead(200, { "Content-Type": "application/json" });
224237
res.end(JSON.stringify({}));
@@ -271,7 +284,10 @@ describe("InsightClient", () => {
271284
);
272285

273286
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
274-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
287+
equal(
288+
req.headers["user-agent"],
289+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
290+
);
275291

276292
res.writeHead(200, { "Content-Type": "application/json" });
277293
res.end(JSON.stringify({}));
@@ -324,7 +340,10 @@ describe("InsightClient", () => {
324340
);
325341

326342
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
327-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
343+
equal(
344+
req.headers["user-agent"],
345+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
346+
);
328347

329348
res.writeHead(200, { "Content-Type": "application/json" });
330349
res.end(JSON.stringify({}));
@@ -377,7 +396,10 @@ describe("InsightClient", () => {
377396
);
378397

379398
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
380-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
399+
equal(
400+
req.headers["user-agent"],
401+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
402+
);
381403

382404
res.writeHead(200, { "Content-Type": "application/json" });
383405
res.end(JSON.stringify({}));
@@ -447,7 +469,10 @@ describe("InsightClient", () => {
447469
);
448470

449471
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
450-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
472+
equal(
473+
req.headers["user-agent"],
474+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
475+
);
451476

452477
res.writeHead(200, { "Content-Type": "application/json" });
453478
res.end(JSON.stringify({}));
@@ -523,7 +548,10 @@ describe("InsightClient", () => {
523548
);
524549

525550
equal(req.headers["authorization"], `Bearer ${channel_access_token}`);
526-
equal(req.headers["user-agent"], `${pkg.name}/${pkg.version}`);
551+
equal(
552+
req.headers["user-agent"],
553+
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
554+
);
527555

528556
res.writeHead(200, { "Content-Type": "application/json" });
529557
res.end(JSON.stringify({}));

0 commit comments

Comments
 (0)