Skip to content

Commit

Permalink
Stop reading package.json from sdk code (#792)
Browse files Browse the repository at this point in the history
To support dual package, we can't use `require` only if we don't bundle
tools like tsup, esbuild or something. (however, we don't have any plan
to use them.)

Currently code like `require("./package.json")` is only for user-agent
header.
  • Loading branch information
Yang-33 authored Apr 2, 2024
1 parent 1a91338 commit 9e073f3
Show file tree
Hide file tree
Showing 22 changed files with 853 additions and 261 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ jobs:
node-version: 18
registry-url: 'https://registry.npmjs.org'
- run: npm install
- name: Update version in package.json, package-lock.json
- name: Update version in package.json, package-lock.json, and lib/version.ts
run: |
VERSION=${{ github.event.release.tag_name }}
VERSION=${VERSION#v}
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" package.json
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" package-lock.json
sed -i "s/__LINE_BOT_SDK_NODEJS_VERSION__/$VERSION/g" lib/version.ts
- run: npm run release
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { {{import.classname}} } from '../{{import.filename}}';
import { createServer } from "node:http";
import { deepEqual, equal, ok } from "node:assert";

const pkg = require("../../../../package.json");

const channel_access_token = "test_channel_access_token";

{% macro paramDummyValue(param) %}
Expand Down Expand Up @@ -73,7 +71,7 @@ const channel_access_token = "test_channel_access_token";
{% endif -%}
equal(
req.headers["user-agent"],
`${pkg.name}/${pkg.version}`,
"@line/bot-sdk/__LINE_BOT_SDK_NODEJS_VERSION__",
);
{% if op.isMultipart %}
ok(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { VerifyChannelAccessTokenResponse } from "../../model/verifyChannelAcces
import { createServer } from "node:http";
import { deepEqual, equal, ok } from "node:assert";

const pkg = require("../../../../package.json");

const channel_access_token = "test_channel_access_token";

describe("ChannelAccessTokenClient", () => {
Expand Down Expand Up @@ -47,7 +45,10 @@ describe("ChannelAccessTokenClient", () => {
),
);

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

res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({}));
Expand Down Expand Up @@ -110,7 +111,10 @@ describe("ChannelAccessTokenClient", () => {
),
);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({}));
Expand Down Expand Up @@ -744,7 +784,10 @@ describe("ChannelAccessTokenClient", () => {
),
);

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

res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({}));
Expand Down Expand Up @@ -795,7 +838,10 @@ describe("ChannelAccessTokenClient", () => {
),
);

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

res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({}));
Expand Down
5 changes: 2 additions & 3 deletions lib/http-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import axios, {
} from "axios";
import { Readable } from "node:stream";
import { HTTPError, ReadError, RequestError } from "./exceptions";

const pkg = require("../package.json");
import { USER_AGENT } from "./version";

interface httpClientConfig extends Partial<AxiosRequestConfig> {
baseURL?: string;
Expand All @@ -25,7 +24,7 @@ export default class HTTPClient {
this.instance = axios.create({
baseURL,
headers: Object.assign({}, defaultHeaders, {
"User-Agent": `${pkg.name}/${pkg.version}`,
"User-Agent": USER_AGENT,
}),
});

Expand Down
4 changes: 2 additions & 2 deletions lib/http-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Readable } from "node:stream";
import { HTTPFetchError } from "./exceptions";
import { USER_AGENT } from "./version";

const pkg = require("../package.json");
export interface FetchRequestConfig {
headers?: Record<string, string>;
}
Expand Down Expand Up @@ -32,7 +32,7 @@ export default class HTTPFetchClient {
constructor(config: httpFetchClientConfig) {
this.baseURL = config.baseURL;
this.defaultHeaders = {
"User-Agent": `${pkg.name}/${pkg.version}`,
"User-Agent": USER_AGENT,
...config.defaultHeaders,
};
}
Expand Down
52 changes: 40 additions & 12 deletions lib/insight/tests/api/InsightClientTest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { GetStatisticsPerUnitResponse } from "../../model/getStatisticsPerUnitRe
import { createServer } from "node:http";
import { deepEqual, equal, ok } from "node:assert";

const pkg = require("../../../../package.json");

const channel_access_token = "test_channel_access_token";

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 9e073f3

Please sign in to comment.