Skip to content
New issue

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

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

Already on GitHub? # 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

Conversation

AnderssonPeter
Copy link

@AnderssonPeter AnderssonPeter commented Aug 14, 2024

Resolves: #137

I'm a bit unsure of how to test that it works correctly, do you have any idea´s?

@AnderssonPeter
Copy link
Author

AnderssonPeter commented Aug 14, 2024

After fiddeling around a bit, it does not look like a responseTransformer is generated..

I added

born:
  type: string
  format: date-time

to pet and --useDateType to generate:api the type changes to Date in the definition, but no transformer is generated

@AnderssonPeter
Copy link
Author

Is there some way to attach a debugger when generating code?

@7nohe
Copy link
Owner

7nohe commented Sep 16, 2024

Is there some way to attach a debugger when generating code?

@AnderssonPeter

By outputting the source map during the build with a command like tsc -p tsconfig.json --sourceMap and creating the following launch.json in VSCode, you can attach the debugger.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}/examples/react-app",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run-script",
        "generate:api"
      ]
    }
  ]
}

Copy link
Owner

@7nohe 7nohe left a comment

Choose a reason for hiding this comment

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

Thank you for the PR. It looks like a bit more work is needed.

@@ -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);
}

@7nohe 7nohe added the v1 label Oct 14, 2024
@7nohe 7nohe changed the base branch from main to v1 October 14, 2024 05:57
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When enabeling --useDateType send the correct information to @hey-api/openapi-ts
2 participants