Gathering detailed insights and metrics for @api-ts/openapi-generator
Gathering detailed insights and metrics for @api-ts/openapi-generator
Gathering detailed insights and metrics for @api-ts/openapi-generator
Gathering detailed insights and metrics for @api-ts/openapi-generator
@hey-api/openapi-ts
🚀 The OpenAPI to TypeScript codegen. Generate clients, SDKs, validators, and more.
@cosmotech/api-ts
OpenAPI client for @cosmotech/api-ts
@infinite-debugger/swagger-to-ts
swagger-to-ts is a powerful library that allows you to generate TypeScript code from Swagger documentation or OpenAPI specifications. It simplifies the process of integrating API definitions into your TypeScript projects, saving you time and effort.
@protocolnebula/ts-openapi-generator
Build API and models from Swagger/OpenAPI to use in any project type
npm install @api-ts/openapi-generator
Typescript
Module System
Node Version
NPM Version
84.5
Supply Chain
97.6
Quality
95.8
Maintenance
100
Vulnerability
96.7
License
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
324,587
Last Day
2,681
Last Week
10,688
Last Month
56,449
Last Year
312,168
Latest Version
5.6.1
Package Id
@api-ts/openapi-generator@5.6.1
Unpacked Size
257.49 kB
Size
48.25 kB
File Count
79
NPM Version
10.5.2
Node Version
22.13.1
Published on
Feb 10, 2025
Cumulative downloads
Total Downloads
Last Day
-7.8%
2,681
Compared to previous day
Last Week
-8.9%
10,688
Compared to previous week
Last Month
31.6%
56,449
Compared to previous month
Last Year
3,506.8%
312,168
Compared to previous year
5
2
The api-ts openapi-generator is a command-line utility for converting an io-ts-http API specification into an OpenAPI specification.
1npm install --save-dev @api-ts/openapi-generator
The openapi-generator assumes the io-ts-http apiSpec
is exported in the top level
of the Typescript file passed as an input parameter. The OpenAPI specification will be
written to stdout.
1ARGUMENTS: 2 <file> - API route definition file 3 4OPTIONS: 5 --name, -n <str> - API name [optional] 6 --version, -v <str> - API version [optional] 7 --codec-file, -c <str> - Custom codec definition file [optional] 8 9FLAGS: 10 --internal, -i - include routes marked private 11 --help, -h - show help
For example:
1npx openapi-generator src/index.ts
In order to use types from external io-ts
types packages, you must ensure two things
are done.
package.json
to include your
source code in the bundle. For example, if the source code is present in the src/
directory, then add src/
to the files array in the package.json
of your project.source
field in the package.json
to be the entry point
of the types in the source code. For example, if the entrypoint is src/index.ts
,
then set "source": "src/index.ts"
in the package.json
When working with openapi-generator
, you may encounter challenges with handling custom
codecs that require JavaScript interpretation or aren't natively supported by the
generator. These issues typically arise with codecs such as new t.Type(...)
and other
primitives that aren't directly supported. However, there are two solutions to address
these challenges effectively. Click here for
the list of supported primitives.
openapi-generator
now offers the ability to define the schema of custom codecs
directly within the types package that defines them, rather than the downstream package
that uses them. This approach is particularly useful for codecs that are used in many
different types packages. Here’s how you can define schemas for your custom codecs in
the upstream repository:
Create a file named openapi-gen.config.js
in the root of your repository.
Add the following line to the package.json
of the types package:
1"customCodecFile": "openapi-gen.config.js"
You must also add "openapi-gen.config.js"
to the files field in the package.json,
so that it is included in the final bundle.
In the openapi-gen.config.js
file, define your custom codecs:
1module.exports = (E) => { 2 return { 3 SampleCodecDefinition: () => 4 E.right({ 5 type: 'string', 6 default: 'defaultString', 7 minLength: 1, 8 }), 9 // ... rest of your custom codec definitions 10 }; 11};
By following these steps, the schemas for your custom codecs will be included in the
generated API docs for any endpoints that use the respective codecs. The input parameter
E
is the namespace import of fp-ts/Either
, and the return type should be a Record
containing AST definitions for external libraries. For more details, see
KNOWN_IMPORTS.
openapi-generator
supports importing codecs from other packages in node_modules
, but
it struggles with io-ts
primitives that need JavaScript interpretation, such as
new t.Type(...)
. To work around this, you can define schemas for these codecs in a
configuration file within your downstream types package (where you generate the API
docs). This allows the generator to understand and use these schemas where necessary.
Follow these steps to create and use a custom codec configuration file:
Create a JavaScript file with the following format:
1module.exports = (E) => { 2 return { 3 'io-ts-bigint': { 4 BigIntFromString: () => E.right({ type: 'string' }), 5 NonZeroBigInt: () => E.right({ type: 'number' }), 6 NonZeroBigIntFromString: () => E.right({ type: 'string' }), 7 NegativeBigIntFromString: () => E.right({ type: 'string' }), 8 NonNegativeBigIntFromString: () => E.right({ type: 'string' }), 9 PositiveBigIntFromString: () => E.right({ type: 'string' }), 10 }, 11 // ... and so on for other packages 12 }; 13};
The input parameter E
is the namespace import of fp-ts/Either
, which avoids
issues with require
. The return type should be a Record
containing AST
definitions for external libraries. For more information on the structure, refer to
KNOWN_IMPORTS.
string
number
bigint
boolean
null
nullType
undefined
unknown
any
array
readonlyArray
object
type
partial
exact
strict
record
union
intersection
literal
keyof
brand
UnknownRecord
void
This section will highlight all the features that this generator supports, with examples to help you add meaningful documentation to your code that will allow clients to use our APIs with ease.
Given an endpoint defined using h.httpRoute
, you can add documentation and metadata to
this endpoint through the use of JSDocs. Here are the following list of attributes that
are supported.
The summary is the first line of the JSDoc. This will be added to the OpenAPI specification as the endpoints' summary
1/** 2 * This is the summary 3 */ 4const route = h.httpRoute({ ... })
The description is the next x
untagged lines of the JSDoc. This will be added to the
OpenAPI specification as the endpoints' description
1/** 2 * This is the summary 3 * This is description line 1 4 * This is description line 2 5 */ 6const route = h.httpRoute({ ... })
All endpoints must have an operationId
to be identifiable. You can add an operation ID
to the specification using the @operationId
tag in JSDocs. This will add it to the
OpenAPI specification for this route.
1/** 2 * This is the summary 3 * This is description line 1 4 * This is description line 2 5 * 6 * @operationId v2.sample.route 7 */ 8const route = h.httpRoute({ ... })
Tags are how we organize endpoints into different groups on dev-portal
. There are many
different tags and tag groups, such as Wallet
, Address
, etc.
Click here
for a full list of tags. You can add a tag to your endpoint using the @tag
JSDoc tag.
1/** 2 * This is the summary 3 * This is description line 1 4 * This is description line 2 5 * 6 * @operationId v2.sample.route 7 * @tag Wallet 8 */ 9const route = h.httpRoute({ ... })
There are many instances where you'd want an endpoint to be private, such as admin
or
internal
routes. You can make an endpoint private in documentation by simply adding a
@private
tag to the JSDoc. In the specification, this will add an x-internal: true
field, which marks the field to be stripped out in a preprocessing
step on
dev-portal.
1/** 2 * This is the summary 3 * This is description line 1 4 * This is description line 2 5 * 6 * @private 7 * @operationId v2.sample.route 8 * @tag Wallet 9 */ 10const route = h.httpRoute({ ... })
If you are working on an endpoint that is unstable, or not completely implemented yet,
you can add the @unstable
tag to ensure that consumers know it is still in development
and may not work as expected.
1/** 2 * This is the summary 3 * This is description line 1 4 * This is description line 2 5 * 6 * @unstable 7 * @operationId v2.sample.route 8 * @tag Wallet 9 */ 10const route = h.httpRoute({ ... })
You can also add example responses to the top level JSDocs of your endpoint, but as you'll see in later sections, there are other ways to do this.
1/** 2 * This is the summary 3 * This is description line 1 4 * This is description line 2 5 * 6 * @unstable 7 * @operationId v2.sample.route 8 * @tag Wallet 9 * @example { example: { object: { key: value }}} 10 */ 11const route = h.httpRoute({ ... })
Any other tags that are added to this top-level will be classified as an uknown tag, and
will be placed inside the x-unknown-tags
field in the OpenAPI specification. You can
use this feature to write custom workflows and filtering logic for you full
specification. For example, you could add a @version
tag and have a workflow that
filters endpoints based on the version field in the x-unknown-tags
field.
1/** 2 * This is the summary 3 * This is description line 1 4 * This is description line 2 5 * 6 * @unstable 7 * @operationId v2.sample.route 8 * @tag Wallet 9 * @example { example: { object: { key: value }}} 10 * @version 3 11 */ 12const route = h.httpRoute({ ... })
This is what the OpenAPI specification will look like for the route we have built.
1{ 2 "openapi": 3.03, 3 "paths": { 4 "/api/v2/sample/route": { 5 "get": { 6 "summary": "This is the summary", 7 "description": "This is description line 1\nThis is description line 2", 8 "operationId": "v2.sample.route", 9 "example": { 10 "object": { 11 "key": "value" 12 } 13 }, 14 "tag": "Wallet", 15 "x-internal": true, // @private 16 "x-unstable": true, // @unstable 17 "x-unknown-tags": { 18 "version": 3 19 } 20 ...parameters, 21 ...requestBody, 22 ...responses 23 } 24 } 25 } 26}
In addition to adding JSDocs for top-level routes, you can also add JSDocs to paremeters, request bodies, and response body schemas.
You can add a description to any schema or field just by adding a JSDoc on top, with a description.
1import * as t from 'io-ts'; 2 3/** 4 * This is a description that will be included in the OpenAPI specification. 5 */ 6const schema = t.type({ 7 field: t.number, 8});
These are the list of OpenAPI tags that you can put in JSDocs, and they will be included in the generated OpenAPI spec.
@default[value: any]
@example [example: any]
@minLength [length: number]
@maxLength [length: number]
@pattern [pattern: regex]
@minimum [min: number]
@maximum [max: number]
@minItems [minItems: number]
@maxItems [maxItems: number]
@minProperties [min: number]
@maxProperties [max: number]
@exclusiveMinimum
@exclusiveMaximum
@multipleOf [num: number]
@uniqueItems
@readOnly
@writeOnly
@format [format: format]
@title [title: string]
Here is an example schema with all these tags in use. Don't worry about the fields, just notice the different JSDocs and JSDocs tags for each field. You can also add as many tags to one field as you want (provided that the tags don't conflict). You may also add descriptions
1include * as t from 'io-ts'; 2 3/** @title Sample Schema Title */ 4const SampleSchema = t.type({ 5 /** @default defaultValueForField1 */ 6 field1: t.string, 7 /** @example exampleForField2 */ 8 field2: t.string, 9 /** @minLength 4 */ 10 field3: t.string, 11 /** @maxLength 6 */ 12 field4: t.string, 13 /** @pattern ^[0-9a-f]{32}$ */ 14 fieldWithId: t.string, 15 /** @minimum 10 */ 16 minField: t.number, 17 /** @maximum 40 */ 18 maxField: t.number, 19 /** @minItems 10 */ 20 minItemsArray: t.array(t.string), 21 /** @maxItems 50 */ 22 maxItemsArray: t.array(t.string), 23 /** @minProperties 3 */, 24 minPropRecord: t.record(t.string, t.string), 25 /** @maxProperties 10 */ 26 maxPropRecord: t.record(t.string, t.string), 27 nestedObject: t.partial({ 28 /** 29 * @minimum 2 30 * @exclusiveMinimum 31 */ 32 exclMin: t.number, 33 /** 34 * @maximum 50 35 * @exclusiveMaximum 36 */ 37 exclMax: t.number, 38 /** @multipleOf 5*/ 39 multOf: t.number, 40 /** @uniqueItems */ 41 arr: t.array(t.string), 42 /** @readOnly */ 43 readOnlyField: t.unknown, 44 /** @writeOnly */ 45 writeOnlyField: t.unknown, 46 /** @format uuid */ 47 uuidField: t.string, 48 /** @title Hello */ 49 titleField: t.string 50 }) 51})
These are some tags that you can use in your schema JSDocs are custom to this generator.
@private
allows you to mark any field as in any schema as private. The final spec
will have x-internal: true
for schemas with the @private
tag.@deprecated
allows to mark any field in any schema as deprecated. The final spec
will include deprecated: true
in the final specificaiton.1import * as t from 'io-ts'; 2 3const Schema = t.type({ 4 /** @private */ 5 privateField: t.string, 6 /** @deprecated */ 7 deprecatedField: t.string, 8 publicNonDeprecatedField: t.string, 9});
No vulnerabilities found.
No security vulnerabilities found.