elysiajs / eden

Fully type-safe Elysia client
MIT License
147 stars 37 forks source link

Eden can't handle GET method with params when using Elysia.all #97

Open chillcicada opened 3 months ago

chillcicada commented 3 months ago

Problem

Eden can't handle GET method with params when using Elysia.all()

Reproduce

here is a minimal reproduce way:

run bun create elysia app to create a new project and open it in editor

run bun add @elysiajs/eden

modify the index.ts as:

// index.ts
import { Elysia } from "elysia";

const app = new Elysia()
  .all("/", (req) => {
    console.log(req);

    // other params have the same result.
    return req.query
  })
  .listen(3000);

export type App = typeof app;

create an index.test.ts file with:

// index.test.ts
import { treaty } from '@elysiajs/eden'

import type { App } from ".";

const api = treaty<App>('localhost:3000');

const res = await api.index.get({ query: { name: 'Elysia' }});
// i think the method call should keep in line with the normal get method,
// but in fact the get method here accepts two params as the following figure,
// and i use the normal get method called mode

console.log(res);

run bun dev and then bun .\src\index.test.ts, it'll crash:

{
  data: null,
  error: 1 | var c=(t,e,r)=>{if(t.endsWith("/")||(t+="/"),e==="index"&&(e=""),!r||!Object.keys(r).length)return`${t}${e}`;let s="";for(let[i,u]of Object.entries(r))s+=`${i}=${u}&`;return`${t}${e}?${s.slice(0,-1)}`},o=t=>t.trim().length!==0&&!Number.isNaN(Number(t));var n=class extends Error{constructor(r,s){super(s+"");this.status=r;this.value=s}};export{c as a,o as b,n as c};

                                                                                ^
error: Failed to parse body as found: undefined
      at new n (D:\Desktop\Projects\playground\j\w\node_modules\@elysiajs\eden\dist\chunk-HUZ6GXDC.mjs:1:297) 
      at D:\Desktop\Projects\playground\j\w\node_modules\@elysiajs\eden\dist\chunk-VA7FGDJH.mjs:1:5065        
,
  response: Response (0 KB) {
    ok: false,
    url: "http://localhost:3000/?name=Elysia",
    status: 400,
    statusText: "Bad Request",
    headers: Headers {
      "content-type": "text/plain;charset=utf-8",
      "date": "Sat, 25 May 2024 16:14:13 GMT",
      "content-length": "40",
    },
    redirected: false,
    bodyUsed: true
  },
  status: 400,
  headers: Headers {
    "content-type": "text/plain;charset=utf-8",
    "date": "Sat, 25 May 2024 16:14:13 GMT",
    "content-length": "40",
  },
}

Also, the type of the api.index.get in index.test.ts is:

2024-05-26_00-17-02

note that if use api.index.get({}, { query: { name: 'Elysia' }}) can also result in the similar error.

only when there is no param of api.index.get() that the eden can handle it right

As a contrast when use only Elysia.get()

// index.ts
// ...
const app = new Elysia()
  .get("/", (req) => {
    // ...
  })
// ...

It works normally. And the type of the api.index.get in index.test.ts is:

2024-05-26_00-22-17

Meanwhile, the behavior of Elysia.all() for GET method with params in the browser is normal.

Other info

OS: Microsoft Windows NT 10.0.22631.0 x64

Bun version: 1.1.10


Thank you♥️!