Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send "types+transform" to hey-api-openapi-ts when useDateType is enabled #138

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/generate.mts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function generate(options: LimitedUserConfig, version: string) {
asClass: true,
},
types: {
dates: formattedOptions.useDateType,
dates: formattedOptions.useDateType ? "types+transform" : false,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of the dates option in @hey-api/openapi-ts is boolean | 'types+transform' | 'types'.
Therefore, instead of using a ternary operator here, I believe it would be more appropriate to adjust the type of LimitedUserConfig.

An example of the command when executed via CLI should look as follows:

openapi-rq -i ./petstore.yaml --useDateType=types+transform

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cli.mts will also look like the following.

#!/usr/bin/env node
import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { Command, Option } from "commander";
import { defaultOutputPath } from "./constants.mjs";
import { generate } from "./generate.mjs";

const program = new Command();

export type LimitedUserConfig = {
  input: string;
  output: string;
  client?: "angular" | "axios" | "fetch" | "node" | "xhr";
  request?: string;
  format?: "biome" | "prettier";
  lint?: "biome" | "eslint";
  operationId?: boolean;
  serviceResponse?: "body" | "response";
  base?: string;
  enums?: "javascript" | "typescript" | "typescript+namespace";
  useDateType?: boolean | "types+transform" | "types";
  debug?: boolean;
  noSchemas?: boolean;
  schemaType?: "form" | "json";
  pageParam: string;
  nextPageParam: string;
};

const datesType: Extract<
  LimitedUserConfig["useDateType"],
  "types+transform" | "types"
>[] = ["types", "types+transform"];

async function setupProgram() {
 // ...
  program
    .name("openapi-rq")
    .version(version)
    .description("Generate React Query code based on OpenAPI")
    .requiredOption(
      "-i, --input <value>",
      "OpenAPI specification, can be a path, url or string content (required)"
    )
 // ...
    .option(
      "--useDateType [value]",
      "Use Date type instead of string for date types for models. See https://heyapi.vercel.app/openapi-ts/transformers.html#dates",
      (value) => {
        if (value === undefined) {
          return true;
        }
        if (value === "true" || value === "false") {
          return value === "true";
        }
        if (datesType.includes(value as "types+transform" | "types")) {
          return value;
        }

        throw new Error("Invalid value for useDateType");
      },
      false
    )
    // ...
    .parse();

  const options = program.opts<LimitedUserConfig>();
  await generate(options, version);
}

export: true,
enums: formattedOptions.enums,
},
Expand Down