Repository navigation
fix(io-ts-http): allow non-JSON decoded body types in GenericHttpRequest - #1210
sharmis-tp wants to merge 1 commit into
Conversation
1b70627 to
52b2388
Compare
|
This PR has been marked as stale because it has had no activity for 7 days. Remove the stale label or comment to keep it open. It will be closed automatically in 7 more days if no activity occurs. |
|
Hi @OttoAllmendinger, this implements the approach you OK'd in #1116. It's mergeable and only needs a review. Could you take a look, or remove the stale label so it doesn't auto-close? Happy to change anythin |
|
This PR has been marked as stale because it has had no activity for 7 days. Remove the stale label or comment to keep it open. It will be closed automatically in 7 more days if no activity occurs. |
|
Keeping this open — the change is still needed. Let me know if there's anything else to address. |
|
Still active — CI is green apart from the automated humans-in-the-loop gate, which I can't control from my side. Ready to merge whenever you get a chance. |
Closes #1116.
GenericHttpRequesttyped its body asoptional(Json), which constrains both the decoded and the encoded type toJson. A route that intersects it with a body codec decoding to non-JSON values, such asBigIntFromString, could not be called throughsuperagent-wrapper:RequestType<Route>made the body(Json | undefined) & { amount: bigint }, so.post({ params: {}, query: {}, body: { amount } })failed with TS2322.route.request.encodethrewno codec found to encode value in union type (Json | undefined). The union picks its member withJson.is, which rejects the bigint, so the request never reached the wire.Change
As discussed in the issue, the body is now
optional(JsonBody), whereJsonBodyis at.Type<unknown, Json, unknown>:unknown, which matches the documentedbody?: unknown.isaccepts any value, so the route's own body codec does the encoding.Json, soHttpRequestCodecstill requires a route's body codec to encode to JSON.Json, and the codec keeps the nameJson, so server-side decoding and its error messages are unchanged.Tests
io-ts-http:GenericHttpRequestintersected with aBigIntFromStringbody decodes'100'to a bigint and encodes it back to'100'. The test assigns a bigint body tot.TypeOf<typeof Request>, so the build fails without this change.superagent-wrapper: a route with a bigint body is posted through the client. The server receives the amount as a string, and the response decodes back to a bigint.Without the change, the build fails with TS2322 and both runtime tests fail with the union encode error. With it,
npm run buildpasses andnpm testpasses in every package.