BitGo / api-ts

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

feat: support `domain` for `t.record` codec #857

Closed anshchaturvedi closed 1 month ago

anshchaturvedi commented 1 month ago

Problem:

Currently, openapi-generator only supports t.record with unrestricted domains. However, there can be codecs that use an enumerable (usually signified as a t.keyof) to specify that only certain keys are allowed and that all of these keys are required in the t.record.

Our dev-portal doesn't fully support rendering t.record that conforms to the OpenAPI specification, as the rendering code we ~copied~ adopted from @stoplight/json-schema-viewer is outdated and a few versions behind the current release.

Solution:

Although a dev-portal fix to render t.record better is in progress (though it won't be fully complete in its first iteration), we decided to circumvent the inadequate rendering by converting a t.record to a t.type. Both codecs expect all keys to exist in the object and all values of the keys to conform to the specified codec. With this PR, something like:

export const SomeCodec = t.record(t.keyof({ foo: "foo", bar: "bar" }), t.type({ baz: t.string }))

will have its keys "expanded" and inlined by openapi-generator when generated, and will be equivalent to:

export const SomeCodec = t.type({
  foo: t.type({ baz: t.string }),
  bar: t.type({ baz: t.string }),
})

[!IMPORTANT] Any t.record codecs with an unrestricted domain (e.g., t.string) will remain unchanged.

Ticket: DX-637

github-actions[bot] commented 1 month ago

:tada: This PR is included in version @api-ts/openapi-generator@4.25.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: