Skip to content

Commit

Permalink
feat: Add support for flags in gen command (#27)
Browse files Browse the repository at this point in the history
* Use upperCase verb in the fetcher

* Add flags override
  • Loading branch information
fabien0102 authored Jan 27, 2022
1 parent 35c06ef commit ec263c2
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
126 changes: 121 additions & 5 deletions cli/src/commands/GenerateCommand.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Command, Option } from "clipanion";
import { Command, Option, UsageError } from "clipanion";
import * as t from "typanion";
import fsExtra from "fs-extra";
import path from "path/posix";
import * as swc from "@swc/core";
import prettier from "prettier";
import { fileURLToPath } from "url";
import slash from "slash";

import { Config, Namespace } from "../types";
import { Config, FromOptions, Namespace } from "../types";
import { getOpenAPISourceFile } from "../core/getOpenAPISourceFile.js";
import { parseOpenAPISourceFile } from "../core/parseOpenAPISourceFile.js";

Expand All @@ -23,6 +24,43 @@ export class GenerateCommand extends Command {

namespace = Option.String();

source = Option.String(`--source`, {
description: "Source of the spec (file, url or github)",
validator: t.isEnum(["file", "url", "github"]),
});

// source=file options
relativePath = Option.String(`--relativePath`, {
description: "[source=file] Relative path of the spec file",
});

// source=url options
url = Option.String("--url", {
description: "[source=url] URL of the spec file",
});
method = Option.String("--method", {
description: "[source=url] HTTP Method",
validator: t.isEnum(["get", "post"]),
});

// source=github options
owner = Option.String("--owner", {
description: "[source=github] Owner of the repository",
});
repository = Option.String("--repository --repo", {
description: "[source=github] Repository name",
});
branch = Option.String("-b --branch", {
description: "[source=github] Branch name",
});
specPath = Option.String("--specPath", {
description: "[source=github] OpenAPI specs file path",
});
pullRequest = Option.String("--pr --pull-request", {
description:
"[source=github] Select a specific pull-request instead of a branch",
});

static paths = [["gen"], ["generate"], Command.Default];
static usage = Command.Usage({
description: "Generate types & components from an OpenAPI file",
Expand Down Expand Up @@ -72,17 +110,95 @@ export class GenerateCommand extends Command {
}
}

/**
* Get `from` options consolidated with cli flags.
*
* @param config config from openapi-codegen.config.ts
* @returns consolidated configuration
*/
getFromOptions(config: Config): FromOptions {
const source = this.source || config.from.source;

switch (source) {
case "file": {
if (config.from.source === "file") {
return {
...config.from,
relativePath: this.relativePath ?? config.from.relativePath,
};
} else {
if (!this.relativePath) {
throw new UsageError("--relativePath argument is missing");
}
return {
source: "file",
relativePath: this.relativePath,
};
}
}

case "url":
if (config.from.source === "url") {
return {
...config.from,
url: this.url ?? config.from.url,
method: this.method ?? config.from.method,
};
} else {
if (!this.url) {
throw new UsageError("--url argument is missing");
}
return {
source: "url",
url: this.url,
method: this.method,
};
}

case "github":
if (config.from.source === "github") {
return {
...config.from,
owner: this.owner ?? config.from.owner,
branch: this.branch ?? config.from.branch,
repository: this.repository ?? config.from.repository,
specPath: this.specPath ?? config.from.specPath,
};
} else {
if (!this.owner) {
throw new UsageError("--owner argument is missing");
}
if (!this.branch) {
throw new UsageError("--branch argument is missing");
}
if (!this.repository) {
throw new UsageError("--repository argument is missing");
}
if (!this.specPath) {
throw new UsageError("--specPath argument is missing");
}

return {
source: "github",
branch: this.branch,
owner: this.owner,
repository: this.repository,
specPath: this.specPath,
};
}
}
}

async execute() {
const configs = await this.loadConfigs();
if (!(this.namespace in configs)) {
this.context.stdout.write(
throw new UsageError(
`"${this.namespace}" is not defined in your configuration`
);
process.exit(1);
}

const config = configs[this.namespace];
const sourceFile = await getOpenAPISourceFile(config.from);
const sourceFile = await getOpenAPISourceFile(this.getFromOptions(config));
const openAPIDocument = await parseOpenAPISourceFile(sourceFile);
const prettierConfig = await prettier.resolveConfig(process.cwd());

Expand Down
2 changes: 1 addition & 1 deletion plugins/typescript/src/templates/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function ${camel(prefix)}Fetch<
const response = await window.fetch(
resolveUrl(url, queryParams, pathParams),
{
method,
method: method.toUpperCase(),
body: body ? JSON.stringify(body) : undefined,
headers: {
"Content-Type": "application/json",
Expand Down

0 comments on commit ec263c2

Please sign in to comment.