Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

01 02 use vitest #19

Draft
wants to merge 6 commits into
base: 01-stop-reading-package-json-in-code
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +10,7 @@ import { {{import.classname}} } from '../{{import.filename}}';
import { createServer } from "node:http";
import { deepEqual, equal, ok } from "node:assert";

const pkg = require("../../../../package.json");
import { describe, it } from "vitest";

const channel_access_token = "test_channel_access_token";

Expand Down Expand Up @@ -73,7 +73,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,7 +10,7 @@ import { VerifyChannelAccessTokenResponse } from "../../model/verifyChannelAcces
import { createServer } from "node:http";
import { deepEqual, equal, ok } from "node:assert";

const pkg = require("../../../../package.json");
import { describe, it } from "vitest";

const channel_access_token = "test_channel_access_token";

Expand Down Expand Up @@ -47,7 +47,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 +113,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 +163,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 +216,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 +269,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 +322,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 +377,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 +438,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 +494,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 +538,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 +585,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 +638,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 +688,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 +732,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 +786,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 +840,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: 41 additions & 11 deletions lib/insight/tests/api/InsightClientTest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { GetStatisticsPerUnitResponse } from "../../model/getStatisticsPerUnitRe
import { createServer } from "node:http";
import { deepEqual, equal, ok } from "node:assert";

const pkg = require("../../../../package.json");
import { describe, it } from "vitest";

const channel_access_token = "test_channel_access_token";

Expand All @@ -25,7 +25,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 +65,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 +118,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 +174,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 +230,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 +286,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 +342,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 +398,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 +471,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 +550,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