Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as Types from "../../types.js";
import {ensureJSON} from "../../utils.js";
import {Readable} from "node:stream";

import HTTPFetchClient, { convertResponseToReadable } from "../../http-fetch.js";
import HTTPFetchClient, { convertResponseToReadable, mergeHeaders } from "../../http-fetch.js";

// ===============================================
// This file is autogenerated - Please do not edit
Expand All @@ -22,7 +22,7 @@ interface httpClientConfig {
{% if authMethods != null -%}
channelAccessToken: string;
{% endif -%}
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}


Expand All @@ -31,12 +31,16 @@ export class {{operations.classname}} {

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || '{{endpoint(operations.classname)}}';
const defaultHeaders = mergeHeaders(
config.defaultHeaders,
{% if authMethods != null -%}
{ Authorization: "Bearer " + config.channelAccessToken }
{% else -%}
{}
{% endif -%}
);
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
{% if authMethods != null -%}
Authorization: "Bearer " + config.channelAccessToken,
{% endif -%}
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
6 changes: 4 additions & 2 deletions lib/channel-access-token/api/channelAccessTokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -32,16 +33,17 @@ import HTTPFetchClient, {

interface httpClientConfig {
baseURL?: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class ChannelAccessTokenClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://api.line.me";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
30 changes: 26 additions & 4 deletions lib/http-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,38 @@ export function convertResponseToReadable(response: Response): Readable {
});
}

export function normalizeHeaders(
headers: Record<string, string> | undefined,
): Record<string, string> {
const normalized: Record<string, string> = {};
if (!headers) {
return normalized;
}
for (const key of Object.keys(headers)) {
normalized[key.toLowerCase()] = headers[key];
}
return normalized;
}

export function mergeHeaders(
base: Record<string, string> | undefined,
override: Record<string, string> | undefined,
): Record<string, string> {
const normalizedBase = normalizeHeaders(base);
const normalizedOverride = normalizeHeaders(override);
return { ...normalizedBase, ...normalizedOverride };
}

export default class HTTPFetchClient {
private readonly baseURL: string;
private readonly defaultHeaders: Record<string, string>;

constructor(config: httpFetchClientConfig) {
this.baseURL = config.baseURL;
this.defaultHeaders = {
"User-Agent": USER_AGENT,
...config.defaultHeaders,
};
this.defaultHeaders = mergeHeaders(
{ "User-Agent": USER_AGENT },
config.defaultHeaders, // allow to override User-Agent
);
}

public async get<T>(url: string, params?: any): Promise<Response> {
Expand Down
10 changes: 6 additions & 4 deletions lib/insight/api/insightClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -32,18 +33,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class InsightClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://api.line.me";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {
Authorization: "Bearer " + config.channelAccessToken,
});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
10 changes: 6 additions & 4 deletions lib/liff/api/liffClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -31,18 +32,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class LiffClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://api.line.me";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {
Authorization: "Bearer " + config.channelAccessToken,
});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
10 changes: 6 additions & 4 deletions lib/manage-audience/api/manageAudienceBlobClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -28,18 +29,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class ManageAudienceBlobClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://api-data.line.me";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {
Authorization: "Bearer " + config.channelAccessToken,
});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
10 changes: 6 additions & 4 deletions lib/manage-audience/api/manageAudienceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -44,18 +45,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class ManageAudienceClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://api.line.me";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {
Authorization: "Bearer " + config.channelAccessToken,
});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
10 changes: 6 additions & 4 deletions lib/messaging-api/api/messagingApiBlobClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -28,18 +29,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class MessagingApiBlobClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://api-data.line.me";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {
Authorization: "Bearer " + config.channelAccessToken,
});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
10 changes: 6 additions & 4 deletions lib/messaging-api/api/messagingApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -74,18 +75,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class MessagingApiClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://api.line.me";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {
Authorization: "Bearer " + config.channelAccessToken,
});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
10 changes: 6 additions & 4 deletions lib/module-attach/api/lineModuleAttachClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -28,18 +29,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class LineModuleAttachClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://manager.line.biz";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {
Authorization: "Bearer " + config.channelAccessToken,
});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
10 changes: 6 additions & 4 deletions lib/module/api/lineModuleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -30,18 +31,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class LineModuleClient {
private httpClient: HTTPFetchClient;

constructor(config: httpClientConfig) {
const baseURL = config.baseURL || "https://api.line.me";
const defaultHeaders = mergeHeaders(config.defaultHeaders, {
Authorization: "Bearer " + config.channelAccessToken,
});
this.httpClient = new HTTPFetchClient({
defaultHeaders: {
Authorization: "Bearer " + config.channelAccessToken,
},
defaultHeaders: defaultHeaders,
baseURL: baseURL,
});
}
Expand Down
10 changes: 6 additions & 4 deletions lib/shop/api/shopClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Readable } from "node:stream";

import HTTPFetchClient, {
convertResponseToReadable,
mergeHeaders,
} from "../../http-fetch.js";

// ===============================================
Expand All @@ -28,18 +29,19 @@ import HTTPFetchClient, {
interface httpClientConfig {
baseURL?: string;
channelAccessToken: string;
// TODO support defaultHeaders?
defaultHeaders?: Record<string, string>;
}

export class ShopClient {
private httpClient: HTTPFetchClient;

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