From 368937e47bef44dd352d9ab06edba110145a2dd1 Mon Sep 17 00:00:00 2001 From: Yuta Kasai Date: Mon, 20 Nov 2023 16:03:21 +0900 Subject: [PATCH] ChannelAccessToken client doesn't have to require channel access token (#597) Co-authored-by: Tokuhiro Matsuno --- .../api-single.pebble | 5 +++ .../api_test.pebble | 5 +++ .../api/channelAccessTokenClient.ts | 5 +-- .../api/ChannelAccessTokenClientTest.spec.ts | 36 +------------------ test/libs-channelAccessToken.spec.ts | 8 +---- 5 files changed, 13 insertions(+), 46 deletions(-) diff --git a/generator/src/main/resources/line-bot-sdk-nodejs-generator/api-single.pebble b/generator/src/main/resources/line-bot-sdk-nodejs-generator/api-single.pebble index 17ae768db..cf5177c1c 100644 --- a/generator/src/main/resources/line-bot-sdk-nodejs-generator/api-single.pebble +++ b/generator/src/main/resources/line-bot-sdk-nodejs-generator/api-single.pebble @@ -1,5 +1,6 @@ {# @pebvariable name="imports" type="java.util.List>" #} {# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" #} +{# @pebvariable name="authMethods" type="java.util.ArrayList" -#} {% include "./licenseInfo.pebble" %} /* tslint:disable:no-unused-locals */ @@ -19,7 +20,9 @@ import {AxiosResponse} from "axios"; interface httpClientConfig { baseURL?: string; + {% if authMethods != null -%} channelAccessToken: string; + {% endif -%} // TODO support defaultHeaders? } @@ -33,7 +36,9 @@ export class {{operations.classname}} { } this.httpClient = new HTTPClient({ defaultHeaders: { + {% if authMethods != null -%} Authorization: "Bearer " + config.channelAccessToken, + {% endif -%} }, responseParser: this.parseHTTPResponse.bind(this), baseURL: config.baseURL, diff --git a/generator/src/main/resources/line-bot-sdk-nodejs-generator/api_test.pebble b/generator/src/main/resources/line-bot-sdk-nodejs-generator/api_test.pebble index cde85953e..4333bdb06 100644 --- a/generator/src/main/resources/line-bot-sdk-nodejs-generator/api_test.pebble +++ b/generator/src/main/resources/line-bot-sdk-nodejs-generator/api_test.pebble @@ -1,5 +1,6 @@ {# @pebvariable name="imports" type="java.util.List>" #} {# @pebvariable name="operations" type="org.openapitools.codegen.model.OperationMap" #} +{# @pebvariable name="authMethods" type="java.util.ArrayList" -#} import { {{operations.classname}} } from "../../api"; {% for import in imports -%} @@ -21,7 +22,9 @@ describe("{{operations.classname}}", () => { afterEach(() => { server.resetHandlers() }) const client = new {{operations.classname}}({ + {% if authMethods != null -%} channelAccessToken: channel_access_token, + {% endif -%} }); {% for op in operations.operation %} @@ -44,10 +47,12 @@ describe("{{operations.classname}}", () => { ({ request, params, cookies }) => { requestCount++; + {% if authMethods != null -%} equal( request.headers.get("Authorization"), `Bearer ${channel_access_token}`, ); + {% endif -%} equal( request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`, diff --git a/lib/channel-access-token/api/channelAccessTokenClient.ts b/lib/channel-access-token/api/channelAccessTokenClient.ts index 63ebcbf23..54afe88e2 100644 --- a/lib/channel-access-token/api/channelAccessTokenClient.ts +++ b/lib/channel-access-token/api/channelAccessTokenClient.ts @@ -31,7 +31,6 @@ import { AxiosResponse } from "axios"; interface httpClientConfig { baseURL?: string; - channelAccessToken: string; // TODO support defaultHeaders? } @@ -43,9 +42,7 @@ export class ChannelAccessTokenClient { config.baseURL = "https://api.line.me"; } this.httpClient = new HTTPClient({ - defaultHeaders: { - Authorization: "Bearer " + config.channelAccessToken, - }, + defaultHeaders: {}, responseParser: this.parseHTTPResponse.bind(this), baseURL: config.baseURL, }); diff --git a/lib/channel-access-token/tests/api/ChannelAccessTokenClientTest.spec.ts b/lib/channel-access-token/tests/api/ChannelAccessTokenClientTest.spec.ts index 4504d8c8e..798edac2c 100644 --- a/lib/channel-access-token/tests/api/ChannelAccessTokenClientTest.spec.ts +++ b/lib/channel-access-token/tests/api/ChannelAccessTokenClientTest.spec.ts @@ -27,9 +27,7 @@ describe("ChannelAccessTokenClient", () => { server.resetHandlers(); }); - const client = new ChannelAccessTokenClient({ - channelAccessToken: channel_access_token, - }); + const client = new ChannelAccessTokenClient({}); it("getsAllValidChannelAccessTokenKeyIds", async () => { let requestCount = 0; @@ -42,10 +40,6 @@ describe("ChannelAccessTokenClient", () => { http.get(endpoint, ({ request, params, cookies }) => { requestCount++; - equal( - request.headers.get("Authorization"), - `Bearer ${channel_access_token}`, - ); equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`); return HttpResponse.json({}); @@ -74,10 +68,6 @@ describe("ChannelAccessTokenClient", () => { http.post(endpoint, ({ request, params, cookies }) => { requestCount++; - equal( - request.headers.get("Authorization"), - `Bearer ${channel_access_token}`, - ); equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`); return HttpResponse.json({}); @@ -108,10 +98,6 @@ describe("ChannelAccessTokenClient", () => { http.post(endpoint, ({ request, params, cookies }) => { requestCount++; - equal( - request.headers.get("Authorization"), - `Bearer ${channel_access_token}`, - ); equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`); return HttpResponse.json({}); @@ -144,10 +130,6 @@ describe("ChannelAccessTokenClient", () => { http.post(endpoint, ({ request, params, cookies }) => { requestCount++; - equal( - request.headers.get("Authorization"), - `Bearer ${channel_access_token}`, - ); equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`); return HttpResponse.json({}); @@ -182,10 +164,6 @@ describe("ChannelAccessTokenClient", () => { http.post(endpoint, ({ request, params, cookies }) => { requestCount++; - equal( - request.headers.get("Authorization"), - `Bearer ${channel_access_token}`, - ); equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`); return HttpResponse.json({}); @@ -212,10 +190,6 @@ describe("ChannelAccessTokenClient", () => { http.post(endpoint, ({ request, params, cookies }) => { requestCount++; - equal( - request.headers.get("Authorization"), - `Bearer ${channel_access_token}`, - ); equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`); return HttpResponse.json({}); @@ -246,10 +220,6 @@ describe("ChannelAccessTokenClient", () => { http.post(endpoint, ({ request, params, cookies }) => { requestCount++; - equal( - request.headers.get("Authorization"), - `Bearer ${channel_access_token}`, - ); equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`); return HttpResponse.json({}); @@ -276,10 +246,6 @@ describe("ChannelAccessTokenClient", () => { http.get(endpoint, ({ request, params, cookies }) => { requestCount++; - equal( - request.headers.get("Authorization"), - `Bearer ${channel_access_token}`, - ); equal(request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`); return HttpResponse.json({}); diff --git a/test/libs-channelAccessToken.spec.ts b/test/libs-channelAccessToken.spec.ts index 3cad93746..7f9794a90 100644 --- a/test/libs-channelAccessToken.spec.ts +++ b/test/libs-channelAccessToken.spec.ts @@ -5,9 +5,7 @@ import { deepEqual, equal } from "assert"; const pkg = require("../package.json"); -const client = new channelAccessToken.ChannelAccessTokenClient({ - channelAccessToken: "test_channel_access_token", -}); +const client = new channelAccessToken.ChannelAccessTokenClient({}); describe("channelAccessToken", () => { const server = setupServer(); @@ -26,10 +24,6 @@ describe("channelAccessToken", () => { http.post( "https://api.line.me/oauth2/v3/token", async ({ request, params, cookies }) => { - equal( - request.headers.get("Authorization"), - "Bearer test_channel_access_token", - ); equal( request.headers.get("User-Agent"), `${pkg.name}/${pkg.version}`,