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
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,25 @@ Adds the API Copilot configuration in APIMATIC-BUILD.json

```
USAGE
$ apimatic portal:copilot [-i <value>] [-m <value>] [--disable] [-k <value>]
$ apimatic portal:copilot [-i <value>] [--disable] [-k <value>]

FLAGS
-i, --input=<value> [default: ./] path to the parent directory containing the 'src' directory, which
includes API specifications and configuration files.
-k, --auth-key=<value> override current authentication state with an authentication key.
-m, --welcome-message=<value> welcome message for the API copilot
--disable marks the API Copilot as disabled in the configuration
-i, --input=<value> [default: ./] path to the parent directory containing the 'src' directory, which includes API
specifications and configuration files.
-k, --auth-key=<value> override current authentication state with an authentication key.
--disable marks the API Copilot as disabled in the configuration

DESCRIPTION
Adds the API Copilot configuration in APIMATIC-BUILD.json
Configure API Copilot for your API Documentation portal

EXAMPLES
$ apimatic portal:copilot --input="./" --welcome-message="Welcome to our API!"
Displays available API Copilots associated with your account and allows you to select which one to integrate with your
portal. Each APIMatic account includes one Copilot by default. The selected Copilot will be added to your
APIMATIC-BUILD.json file

EXAMPLES
$ apimatic portal:copilot --input="./"

$ apimatic portal:copilot --input="./" --disable
```

_See code: [src/commands/portal/copilot.ts](https://github.com/apimatic/apimatic-cli/blob/alpha/src/commands/portal/copilot.ts)_
Expand Down Expand Up @@ -404,4 +407,4 @@ EXAMPLES
```

_See code: [src/commands/sdk/generate.ts](https://github.com/apimatic/apimatic-cli/blob/alpha/src/commands/sdk/generate.ts)_
<!-- commandsstop -->
<!-- commandsstop -->
8 changes: 6 additions & 2 deletions src/actions/portal/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CopilotAction {
this.authKey = authKey;
}

public async execute(buildDirectory: DirectoryPath, welcomeMessage: string, enable: boolean): Promise<ActionResult> {
public async execute(buildDirectory: DirectoryPath, enable: boolean): Promise<ActionResult> {
const buildContext = new BuildContext(buildDirectory);

if (!(await buildContext.validate())) {
Expand All @@ -37,6 +37,10 @@ export class CopilotAction {
return ActionResult.error("No copilot key found for the current subscription. Please contact support at [email protected].");
}

const welcomeMessage = await this.prompts.getWelcomeMessage();
if (welcomeMessage === undefined)
return ActionResult.error("Exiting without making any change.");

buildJson.apiCopilotConfig = {
isEnabled: enable,
key: apiCopilotKey,
Expand All @@ -45,7 +49,7 @@ export class CopilotAction {

await buildContext.updateBuildFileContents(buildJson);

this.prompts.copilotConfigured(apiCopilotKey);
this.prompts.copilotConfigured(buildJson.apiCopilotConfig);
return ActionResult.success();
}

Expand Down
18 changes: 8 additions & 10 deletions src/commands/portal/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import { CopilotAction } from "../../actions/portal/copilot.js";
const DEFAULT_WORKING_DIRECTORY = "./";

export default class PortalCopilotEnable extends Command {
static description = "Adds the API Copilot configuration in APIMATIC-BUILD.json";

static summary = "Configure API Copilot for your API Documentation portal";

static description = "Displays available API Copilots associated with your account and allows you to select which one to integrate with your portal. Each APIMatic account includes one Copilot by default. The selected Copilot will be added to your APIMATIC-BUILD.json file";

static flags = {
...FlagsProvider.input,
"welcome-message": Flags.string({
char: "m",
default: "",
description: "welcome message for the API copilot"
}),
disable : Flags.boolean({
default: false,
description: "marks the API Copilot as disabled in the configuration"
Expand All @@ -24,21 +22,21 @@ export default class PortalCopilotEnable extends Command {
};

static examples = [
`apimatic portal:copilot --input="./" --welcome-message="Welcome to our API!"`,
`apimatic portal:copilot --input="./"`
`apimatic portal:copilot --input="./"`,
`apimatic portal:copilot --input="./" --disable`,
];

private readonly prompts = new PortalCopilotPrompts();

async run(): Promise<void> {
const {
flags: { input, "auth-key": authKey, disable, 'welcome-message': welcomeMessage}
flags: { input, "auth-key": authKey, disable}
} = await this.parse(PortalCopilotEnable);

const workingDirectory = new DirectoryPath(input ?? DEFAULT_WORKING_DIRECTORY);
const buildDirectory = input ? new DirectoryPath(input, "src") : workingDirectory.join("src");
const copilotConfigAction = new CopilotAction(new DirectoryPath(this.config.configDir), authKey);
const result = await copilotConfigAction.execute(buildDirectory, welcomeMessage, !disable);
const result = await copilotConfigAction.execute(buildDirectory, !disable);
result.map((message) => this.prompts.logError(message));
}
}
2 changes: 1 addition & 1 deletion src/commands/portal/recipe/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class PortalRecipeNew extends Command {

//TODO: Add a mapper for automatically mapping events to logger and telemetry service.
if (createRecipeResult.isFailed()) {
telemetryService.trackEvent(new RecipeCreationFailedEvent(createRecipeResult.error!, PortalRecipeNew.id, flags));
await telemetryService.trackEvent(new RecipeCreationFailedEvent(createRecipeResult.error!, PortalRecipeNew.id, flags));
portalRecipePrompts.logError(getMessageInRedColor(createRecipeResult.error!));
}
if (createRecipeResult.isCancelled()) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/portal/toc/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ https://docs.apimatic.io/platform-api/#/http/guides/generating-on-prem-api-porta

//TODO: Add a mapper for automatically mapping events to logger and telemetry service.
if (result.isFailed()) {
telemetryService.trackEvent(new TocCreationFailedEvent(result.error!, PortalTocNew.id, flags));
await telemetryService.trackEvent(new TocCreationFailedEvent(result.error!, PortalTocNew.id, flags));
this.error(result.error!);
}
}
Expand Down
34 changes: 28 additions & 6 deletions src/prompts/portal/copilot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { select, cancel, isCancel, outro, confirm, log } from "@clack/prompts";
import { select, cancel, isCancel, outro, confirm, log, text, } from "@clack/prompts";
import { CopilotConfig } from "../../types/build/build.js";
import { getMessageInCyanColor } from "../../utils/utils.js";

export class PortalCopilotPrompts {

public async selectCopilotKey(keys: string[]): Promise<string> {
const selectedKey = await select({
message: 'Select API Copilot key form your subscription:',
message: "Select the ID for the API Copilot you would like to add to this API Portal:",
maxItems: 10,
options: keys.map((key) => ({
value: key,
Expand All @@ -20,16 +21,24 @@ export class PortalCopilotPrompts {
return selectedKey;
}

public copilotConfigured(apiCopilotKey: string) {
public copilotConfigured(apiCopilotConfig: CopilotConfig) {
outro(
`API Copilot is configured successfully with key '${apiCopilotKey}'. You can now generate a portal using the command 'apimatic portal:generate'`
`API Copilot configured successfully!

Copilot ID: ${getMessageInCyanColor(apiCopilotConfig.key)}
Welcome Message: ${getMessageInCyanColor(apiCopilotConfig.welcomeMessage)}
Status: ${getMessageInCyanColor(apiCopilotConfig.isEnabled ? "Enabled" : "Disabled")}

Configuration saved to: APIMATIC-BUILD.json

Run 'apimatic portal:serve' to preview your API Portal and try out the API Copilot.`
);
}

public async confirmOverwrite(): Promise<boolean> {
const shouldOverwrite = await confirm({
message: "API Copilot configuration already exists. Do you want to overwrite?",
initialValue: false,
initialValue: false
});

if (isCancel(shouldOverwrite)) {
Expand All @@ -42,4 +51,17 @@ export class PortalCopilotPrompts {
logError(error: string): void {
log.error(error);
}

async getWelcomeMessage(): Promise<string | undefined> {
const welcomeMessage = await text({
message: "Enter a welcome message for your API Copilot:",
placeholder: "Enter your welcome message here..."
});

if (isCancel(welcomeMessage)) {
return undefined;
}

return welcomeMessage;
}
}