fredx87 / openapi-io-ts Goto Github PK
View Code? Open in Web Editor NEWOpenAPI code generation tool with validation powered by io-ts
License: MIT License
OpenAPI code generation tool with validation powered by io-ts
License: MIT License
First off, I wish I know FP better so I could contribute more here, but this stuff reads like stereo instructions to me sadly. So I figure I'll do my best to provide you with the info and hopefully this is easy stuff for you.
1) Response Name Typeo
First off I think there is a typo here https://github.com/Fredx87/openapi-io-ts/blob/main/packages/cli/src/parser/components.ts#L202
I believe it should be const generatedName = toValidVariableName(name, "pascal");
without the quotes around name. I also think responses shouild eb pascal case, as you will see later. I keep getting a name.ts
in my responses folder unless I fix this.
2) Import type when not used as value
Secondly, another minor one, is could we change operations.ts
here https://github.com/Fredx87/openapi-io-ts/blob/main/packages/cli/src/codegen/operations.ts#L119
to this
import type { TaskEither } from "fp-ts/TaskEither";
Since this import is only used as a type in the operation my TS config keeps yelling at me because I am using importsNotUsedAsValues
.
3) Issues with reusable Responses
Alright now for the big stuff, I am seeing issues when trying to share responses across my spec. This is mainly for error response as I have a common error handler that all my api will use, so every endpoint will get the same API error schema. I would like to just write that one and share it. I see 2 ways this could go down.
a shared response with a custom schema inline.
a shared response with a ref to a schema object
To test this I have added the following errors to addPet
in `petstore.yaml
"400":
description: Invalid input
content:
application/json:
schema:
type: object
properties:
custommessage:
type: string
"401":
description: Invalid input
$ref: "#/components/responses/Error401"
"405":
description: Invalid input
$ref: "#/components/responses/Error405"
The inline 400 error works as expected. I then added the following items to components (line 623)
components:
responses:
Error401:
content:
application/json:
schema:
type: object
properties:
noref:
type: string
Error405:
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
schemas:
Error:
properties:
message:
type: string
As a result, after updating that name issue above. I am seeing the following contents
Error401.ts
export const Error401 = { _tag: "JsonResponse", decoder: Error401Schema};
Here the Error401Schema does not exist anywhere and obviously is not important in anyway
Error405.ts
export const Error405 = { _tag: "JsonResponse", decoder: schemas.Error};
Better but there is no import for schemas in this file so this also would break if used.
Finally operations/allPets.ts
"200": { _tag: "JsonResponse", decoder: schemas.Pet },
"400": { _tag: "JsonResponse", decoder: AddPetResponse400Schema },
"401": { _tag: "JsonResponse", decoder: Error401Schema },
"405": { _tag: "JsonResponse", decoder: schemas.Error }
Again Error401Schema does not exist anywhere and is never imported. Also 405 refers to schemas.Error
. Though I guess this is technicall ok, I am thinking this probably shoudl refer to Error405Schema
which then is simple
export type Error405Schema = schemas.Error
This is minor I guess, but seems like what I would have expected. Maybe i am off though.
I am thinking overall Error401.ts
and Error405.ts
should look more like schema files. I am not seeing much purpose in having the { _tag: "JsonResponse", decoder: Error401Schema }
exported from there. It would be nice if there was an exported Error401
io-ts type and a Error401
TS Type. Then in the addPetOperation
if it could import and reference those schemas.
I hope this is all making since, I have been banging my head against this for a day or so trying to clean it up myself, but the fp mentality is just killing me. If i can provide any other info or testing please let me know.
Goes without saying but thanks for this library, I am hoping to use it as a foundational piece to my architecture.
#20 fixes it, but has been languishing.
We are running into an issue generating for our project. The instructions in the readme were followed, and summarized below as follows.
Added to package.json
as a devDependency
using:
npm i -D @openapi-io-ts/cli
Added a command in our package.json
using:
scripts: {
...
"openapi-io-ts:generate": "openapi-io-ts -i apps/dashboard-api/src/docs/openapi.yaml -o apps/dashboard-api/src/codecs/generated",
...
}
Note that apps/dashboard-api/src/docs/openapi.yaml
is a valid openapi format using:
openapi: 3.0.1
We get the following:
$ npm run openapi-io-ts:generate
> [email protected] openapi-io-ts:generate
> openapi-io-ts -i apps/dashboard-api/src/docs/openapi.yaml -o apps/dashboard-api/src/codecs/generated
/Users/paolo/Dyn/redcoast/node_modules/@openapi-io-ts/cli/dist/bin.js:981
return (0, import_function16.pipe)(RTE12.Do, RTE12.bind("schema", () => JsonReference.is(s) ? getOpenapiSchemaFromRef(s) : RTE12.right(s)), RTE12.map((obj) => obj.schema.default));
^
TypeError: Cannot read properties of undefined (reading 'default')
at /Users/paolo/Dyn/redcoast/node_modules/@openapi-io-ts/cli/dist/bin.js:981:173
at /Users/paolo/Dyn/redcoast/node_modules/fp-ts/lib/Either.js:268:52
at Object.pipe (/Users/paolo/Dyn/redcoast/node_modules/fp-ts/lib/function.js:286:20)
at Object._map [as map] (/Users/paolo/Dyn/redcoast/node_modules/fp-ts/lib/Either.js:56:49)
at /Users/paolo/Dyn/redcoast/node_modules/fp-ts/lib/Functor.js:19:92
My expectation is that the command will generate the the necessary files based on the openapi.yml file passed in as a parameter. Is there something I am missing here? Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.