sstur / nbit

A zero-dependency, strongly-typed web framework for Bun, Node and Cloudflare workers
65 stars 4 forks source link

`request.json` cannot be deconstructed #2

Closed ImLunaHey closed 1 year ago

ImLunaHey commented 1 year ago

The following doesn't work. I'm forced to use await request.json().

app.put('/org/:id', async ({ json }) => {
    const body = await json();
}),
Application started 
114 |   }
115 |   text() {
116 |     return this.request.text();
117 |   }
118 |   async json() {
119 |     const contentType = getContentType(this.headers);
                                          ^
TypeError: undefined is not an object (evaluating 'this.headers')
      at /Users/xo/code/ImLunaHey/paas/node_modules/@nbit/bun/index.js:119:39
      at json (/Users/xo/code/ImLunaHey/paas/node_modules/@nbit/bun/index.js:118:15)
      at /Users/xo/code/ImLunaHey/paas/src/service-registry.ts:163:35
      at /Users/xo/code/ImLunaHey/paas/src/service-registry.ts:161:37
      at /Users/xo/code/ImLunaHey/paas/node_modules/@nbit/bun/index.js:161:31
ImLunaHey commented 1 year ago

You likely need to use a lib like https://www.npmjs.com/package/auto-bind or bind the methods manually to the class.

sstur commented 1 year ago

I tried this in the browser, to see how standards-compliant Request class handles this, and it appears to also error (see below).

I can see the case for wanting to destructure inline like you're doing there, but I'm hesitant to do it unless I can be sure of the performance implications. I'd need to run an experiment and measure perf, but I don't know when I could get to this. For now closing as standards compliant behavior.

image