oakserver / oak

A middleware framework for handling HTTP with Deno, Node, Bun and Cloudflare Workers 🐿️ 🦕
https://oakserver.org
MIT License
5.09k stars 231 forks source link

Breaking change in `request.body` handling not documented in v13 #631

Closed vlev1n closed 7 months ago

vlev1n commented 7 months ago

I recently upgraded to oak version 13 and encountered a breaking change in the way request.body is accessed, which isn't documented in the current release notes or documentation. Below are minimal examples showing the usage in versions 12 and 13:

import { Application } from 'https://deno.land/x/oak@v13.0.0/mod.ts'

const app = new Application({ proxy: true })

// oak 12
app.use(async (ctx) => {
  const body = ctx.request.body({ type: 'json' })
  const value = await body.value
  ctx.response.body = value
})

// oak 13
app.use(async (ctx) => {
  const body = await ctx.request.body.json()
  ctx.response.body = body
})

await app.listen({ port: 8000 })

In oak v13, the previous method of accessing the request body results in a Type 'Body' has no call signatures error. I was able to find the correct code by examining the source code, but this change is not reflected in the documentation.

I'm opening this issue for awareness and reference for others who might encounter the same issue. It would be helpful if the documentation could be updated to reflect this and any other breaking changes in v13.

Thank you for your hard work on this project!

kitsonk commented 7 months ago

It is the very first point of the release notes listed as:

  • feat: align request body better to Fetch API (bb996fe)

    BREAKING CHANGE Previous versions of oak had a request body API that was fairly convoluted as oak evolved from the early days of Deno where there wasn't a built in APIs for handling HTTP requests. The Fetch API Request and Response have become the standard almost everywhere and make it easy to get ahold of a request body in many forms.

    Inspired by this the ctx.request.body aligns to this API for the most part, but also still retains some of the advantages that oak provided like providing a ctx.request.body.type to help figure out what sort of body you are looking at as well as support for ctx.request.body.form().

    This also leverages native Fetch API where possible which resolved a lot of issues with decoding multi-part forms. This does mean files that are uploaded to oak become web standard Files that are entries in FormData. This means multipart.ts has been removed from oak and the

Inline API documentation was updated as well.

The README needs to be updated.

kitsonk commented 7 months ago

(Also, the release notes/change log list the other breaking changes in 13)

vlev1n commented 7 months ago

Thank you for pointing out the update in the CHANGELOG.md. I apologize for missing that in my initial review. The only change needed is README to ensure it's immediately visible to new users (like me 😂).

Thanks again for your quick response and for maintaining this project!

BayoKwendo commented 7 months ago

Now we will need to refactor our previous projects because of this update

kitsonk commented 7 months ago

Now we will need to refactor our previous projects because of this update

Yeah, that is why breaking changes are breaking changes, but previous versions are still available. 🤯

kitsonk commented 7 months ago

Fixed in 43de84ab33bcee0d85f0a734a1417ec9c6a6ab91