BitGo / api-ts

Type- and runtime- safe TypeScript APIs
https://bitgo.github.io/api-ts
Other
15 stars 21 forks source link

feat: allow unions to consolidate into single primitive types #842

Closed anshchaturvedi closed 1 month ago

anshchaturvedi commented 1 month ago

This PR aims to introduce a union-consolidation step, where we can simplify union schemas if they meet certain conditions.

Motivation:

We have encountered multiple codecs that type the same A type with different I types. For example, a value intended as a boolean at runtime might be passed into the API as a boolean (true or false), a string ("true" or "false"), or even a number (0 or 1). Currently, dev-portal displays all these options as valid ways to pass a boolean to the API. However, this can be overwhelming and unnecessary for clients. If the API ultimately expects a boolean (typed as t.boolean), we should simplify this by presenting only the primitive boolean type.

Solution: We propose the following criteria for consolidating union schemas into a single primitive type:

  1. The input (I) type is a union.
  2. The runtime (A) type is a primitive type (specifically, string, number, or boolean).
  3. The A type exists literally within the I type.

To support this, we need to address two aspects that are not currently available:

  1. Identifying Primitive Types: Add a primitive property to the schema type to identify primitive io-ts codecs.

  2. Determining Intermediate Types: Add a decodedType property (name can be revised) to the schema type. This will specify the runtime type of codecs like BooleanFromString or NumberFromString, enabling us to determine if a union containing these codecs can be consolidated into a single primitive type.

Ticket: DX-614

github-actions[bot] commented 1 month ago

:tada: This PR is included in version @api-ts/openapi-generator@4.23.0 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] commented 1 month ago

:tada: This PR is included in version @api-ts/express-wrapper@1.0.28 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] commented 1 month ago

:tada: This PR is included in version @api-ts/typed-express-router@1.1.8 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] commented 1 month ago

:tada: This PR is included in version @api-ts/superagent-wrapper@1.2.3 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

github-actions[bot] commented 1 month ago

:tada: This PR is included in version @api-ts/io-ts-http@3.1.0 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket: