From 72c03f7e93f08e88fc6ed13be32f1d30b2578ab0 Mon Sep 17 00:00:00 2001 From: rocketraccoon Date: Tue, 23 Jun 2026 01:20:20 +0700 Subject: [PATCH] fix: optional params --- src/config.test.ts | 8 ++++++++ src/config.ts | 25 ++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/config.test.ts b/src/config.test.ts index 309317c..396bc5c 100644 --- a/src/config.test.ts +++ b/src/config.test.ts @@ -40,6 +40,10 @@ describe("config", () => { ); }); + test("should not throw if value is empty and plugin is disabled", () => { + expect(() => parseConfig(config({ enabled: false, token: "" }))).not.toThrow(); + }); + test("should set passed value", () => { expect(parseConfig(config({ token: "123456789" })).token).toBe("123456789"); }); @@ -54,6 +58,10 @@ describe("config", () => { expect(() => parseConfig(config({ help: "" }))).toThrow(/'help' option must be of a non empty string type/); }); + test("should not throw if value is an empty string and plugin is disabled", () => { + expect(() => parseConfig(config({ enabled: false, help: "" }))).not.toThrow(); + }); + test("should set passed value", () => { expect(parseConfig(config({ help: "https://" })).help).toBe("https://"); }); diff --git a/src/config.ts b/src/config.ts index 244d559..a5a69e0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,11 +3,9 @@ import type { Parser } from "gemini-configparser"; import { PluginOptionTypeError, PluginTokenOptionAbsenceError } from "./errors"; -export type PluginConfig = { - enabled: boolean; - token: string; - help: string; -}; +export type PluginConfig = + | { enabled: true; token: string; help: string } + | { enabled: false; token?: string; help?: string }; const isNonEmptyString = (v: unknown): boolean => typeof v === "string" && v !== ""; @@ -27,12 +25,6 @@ const boolean = (name: string): Parser => validate: assertType(name, v => typeof v === "boolean", "boolean"), }); -const nonEmptyString = (name: string): Parser => - option({ - defaultValue: "", - validate: assertType(name, isNonEmptyString, "non empty string"), - }); - export function parseConfig(options: Record): PluginConfig { const { env, argv } = process; @@ -42,12 +34,19 @@ export function parseConfig(options: Record): PluginConfig { token: option({ defaultValue: "", validate: (v, config) => { - if (!isNonEmptyString(v)) { + if (!isNonEmptyString(v) && config.enabled) { throw new PluginTokenOptionAbsenceError(config.help); } }, }), - help: nonEmptyString("help"), + help: option({ + defaultValue: "", + validate: (v, config) => { + if (config.enabled) { + assertType("help", isNonEmptyString, "non empty string")(v); + } + }, + }), }), { envPrefix: "testplane_oauth_", cliPrefix: "--oauth-" }, );