astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)

Home Page:openapi-zod-client.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Throws general error rather than graceful exit when `--output` is in nonexistent directory

andenacitelli opened this issue · comments

commented

Reproduce

Point output towards any directory that does not exist.

openapi-zod-client ./src/api.yaml --output a/b/c/api.client.ts --export-schemas

Gives the following output:

$ openapi-zod-client ./src/api.yaml --output a/b/c/api.client.ts --export-schemas
Retrieving OpenAPI document from ./src/api.yaml
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: ENOENT: no such file or directory, open 'C:\Users\aacit\WebstormProjects\credit-card-bonuses\a\b\c\api.client.ts'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\aacit\\WebstormProjects\\credit-card-bonuses\\a\\b\\c\\api.client.ts'
}

Node.js v18.15.0

First time reading this, I was confused why it said it couldn't find the file, because I thought this was supposed to be creating the file. While this currently errors out as it should, I feel like there could be a much better error message here, or at least some documentation to cover for that case.

My use case here is I have a generated/ directory inside my src/ folder. When this gets run as part of GitHub Actions, that generated directory doesn't yet exist, so this errors out.

Workaround

Workaround is simple. Use mkdirp to create the directory before you try and generate into it.

mkdirp src/generated &&  openapi-zod-client ./src/api.yaml --output src/generated/api.client.ts --export-schemas"

I left that one on purpose since the error seemed quite clear to me and the workaround that you found feels appropriate
I'm open to having that built-in tho if you really want it, it's also fine

commented

Agree that it's a fine error, was more just throwing it out to hear your thoughts and to get it indexed on Google incase anyone else runs into a similar issue. Better to spend your time improving other things.