Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuma0129 committed Jan 31, 2025
1 parent 8828a49 commit ab24432
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/app/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createCommand } from "commander";
import { LiffApiClient } from "../../api/liff.js";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { getLineBaseUrl } from "../../config/stores/common.js";

const createAction = async (options: {
channelId?: string;
Expand All @@ -14,10 +15,10 @@ const createAction = async (options: {
Please provide a valid channel ID or set the current channel first.
`);
}

const lineBaseUrl = getLineBaseUrl();
const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
baseUrl: lineBaseUrl,
});
const { liffId } = await client.addApp({
view: {
Expand Down
4 changes: 3 additions & 1 deletion src/app/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Command } from "commander";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { LiffApiClient } from "../../api/liff.js";
import inquirer from "inquirer";
import { getLineBaseUrl } from "../../config/stores/common.js";

const deleteAction = async (options: {
channelId?: string;
Expand All @@ -24,9 +25,10 @@ const deleteAction = async (options: {
]);
if (!confirmDelete) return;

const lineBaseUrl = getLineBaseUrl();
const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
baseUrl: lineBaseUrl,
});
console.info(`Deleting LIFF app...`);
await client.deleteApp(options.liffId);
Expand Down
4 changes: 3 additions & 1 deletion src/app/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from "commander";
import { LiffApiClient } from "../../api/liff.js";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { getLineBaseUrl } from "../../config/stores/common.js";

const listAction = async (options: { channelId?: string }) => {
const accessToken = (await resolveChannel(options?.channelId))?.accessToken;
Expand All @@ -10,9 +11,10 @@ const listAction = async (options: { channelId?: string }) => {
`);
}

const lineBaseUrl = getLineBaseUrl();
const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
baseUrl: lineBaseUrl,
});
const { apps } = await client.fetchApps();

Expand Down
4 changes: 3 additions & 1 deletion src/app/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createCommand } from "commander";
import { LiffApiClient } from "../../api/liff.js";
import { resolveChannel } from "../../channel/resolveChannel.js";
import { getLineBaseUrl } from "../../config/stores/common.js";

const updateAction = async (options: {
liffId: string;
Expand All @@ -16,9 +17,10 @@ const updateAction = async (options: {
`);
}

const lineBaseUrl = getLineBaseUrl();
const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
baseUrl: lineBaseUrl,
});
await client.updateApp(options.liffId, {
view: {
Expand Down
4 changes: 3 additions & 1 deletion src/channel/renewAccessToken.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { AuthApiClient } from "../api/auth.js";
import { getLineBaseUrl } from "../config/stores/common.js";
import { ChannelInfo, upsertChannel } from "./stores/channels.js";

export const renewAccessToken = async (
channelId: string,
channelSecret: string,
issuedAt: number,
): Promise<ChannelInfo | undefined> => {
const lineBaseUrl = getLineBaseUrl();
const client = new AuthApiClient({
baseUrl: "https://api.line.me",
baseUrl: lineBaseUrl,
});
const res = await client.fetchStatelessChannelAccessToken({
channelId: channelId,
Expand Down
9 changes: 9 additions & 0 deletions src/config/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Command } from "commander";
import { makeSetCommand } from "./set.js";

export const installConfigCommands = (program: Command) => {
const config = program.command("config");
config.description("Configure common settings");

config.addCommand(makeSetCommand());
};
26 changes: 26 additions & 0 deletions src/config/commands/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Command } from "commander";
import { setLiffBaseUrl, setLineBaseUrl } from "../stores/common.js";

const setAction = async (key: string, value: string) => {
switch (key) {
case "baseUrl.line":
setLineBaseUrl(value);
break;
case "baseUrl.liff":
setLiffBaseUrl(value);
break;
default:
throw new Error(`Unknown key: ${key}`);
}
};

export const makeSetCommand = () => {
const set = new Command("set");
set
.description("Set configuration")
.argument("<key>", "The key to set")
.argument("<value>", "The value to set")
.action(setAction);

return set;
};
17 changes: 17 additions & 0 deletions src/config/stores/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { store } from "./index.js";

export const getLineBaseUrl = (): string => {
return store.get("common.baseUrl.line") ?? "https://api.line.me";
};

export const setLineBaseUrl = (url: string) => {
store.set("common.baseUrl.line", url);
};

export const getLiffBaseUrl = (): string => {
return store.get("common.baseUrl.liff") ?? "https://liff.line.me";
};

export const setLiffBaseUrl = (url: string) => {
store.set("common.baseUrl.liff", url);
};
74 changes: 74 additions & 0 deletions src/config/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Conf from "conf";
import { promises as fs } from "fs";

const packageJson = JSON.parse(
await fs.readFile(new URL("../../../package.json", import.meta.url), "utf8"),
);

export interface RootConfig {
currentChannelId?: string;
channels?: {
[channelId: string]: {
secret: string;
accessToken: string;
expiresIn: number;
issuedAt: number;
};
};
common?: {
baseUrl?: {
liff: string;
line: string;
};
};
}

export const store = new Conf<RootConfig>({
projectName: packageJson.name,
projectVersion: packageJson.version,
schema: {
currentChannelId: {
type: "string",
},
channels: {
type: "object",
properties: {
secret: {
type: "string",
description: "The secret for the channel",
},
accessToken: {
type: "string",
description: "The access token for the channel",
},
expiresIn: {
type: "number",
description: "The number of seconds the access token is valid for",
},
issuedAt: {
type: "number",
description:
"The milliseconds timestamp when the access token was issued",
},
},
},
common: {
type: "object",
properties: {
baseUrl: {
type: "object",
properties: {
liff: {
type: "string",
description: "The base URL for LIFF Application",
},
line: {
type: "string",
description: "The base URL for LINE API",
},
},
},
},
},
},
});
7 changes: 5 additions & 2 deletions src/serve/serveAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getCurrentChannelId } from "../channel/stores/channels.js";
import { LocalProxy } from "../proxy/local-proxy.js";
import resolveEndpointUrl from "./resolveEndpointUrl.js";
import pc from "picocolors";
import { getLiffBaseUrl, getLineBaseUrl } from "../config/stores/common.js";

export const serveAction = async (
options: {
Expand Down Expand Up @@ -48,12 +49,14 @@ export const serveAction = async (
}

const httpsUrl = await localProxy.connect(endpointUrl);
const liffUrl = new URL("https://liff.line.me/");
const liffBaseUrl = getLiffBaseUrl();
const liffUrl = new URL(liffBaseUrl);
liffUrl.pathname = options.liffId;

const lineBaseUrl = getLineBaseUrl();
const client = new LiffApiClient({
token: accessToken,
baseUrl: "https://api.line.me",
baseUrl: lineBaseUrl,
});
await client.updateApp(options.liffId, {
view: { url: httpsUrl.toString() },
Expand Down
1 change: 1 addition & 0 deletions src/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Commands:
channel Manage LIFF channels
app Manage LIFF apps
serve [options] Manage HTTPS dev server
config Configure common settings
help [command] display help for command
`);
});
Expand Down
2 changes: 2 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Command } from "commander";
import { installChannelCommands } from "./channel/commands/index.js";
import { installAppCommands } from "./app/commands/index.js";
import { serveCommands } from "./serve/index.js";
import { installConfigCommands } from "./config/commands/index.js";

export const setupCLI = (program: Command) => {
installChannelCommands(program);
installAppCommands(program);
serveCommands(program);
installConfigCommands(program);
// TODO .version?
return {
run: (argv = process.argv) => {
Expand Down

0 comments on commit ab24432

Please sign in to comment.