elysiajs / eden

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

Type wants me to add `.index` to api function chain before HTTP verb after v1.0 #71

Open brianjor opened 5 months ago

brianjor commented 5 months ago

"@elysiajs/eden": "1.0.5", "elysia": "1.0.7"

index.ts:

import { edenTreaty } from "@elysiajs/eden";
import Elysia from "elysia";

const helloHandlers = new Elysia().get("", () => "hello");

const routes = new Elysia().group("/hello", (_) => _.use(helloHandlers));

const app = new Elysia().use(routes).listen(3000);

const eden = edenTreaty<typeof app>("localhost:3000");

console.log((await eden.hello.get()).data); // <-- has red squiggly, but works!
console.log((await eden.hello.index.get()).data); // <-- no red squiggly, "NOT_FOUND"

app.stop();

output from running:

bun run index.ts
hello
NOT_FOUND

Error message:

Property 'get' does not exist on type '{ index: { get: (params?: { $fetch?: RequestInit | undefined; getRaw?: boolean | undefined; $transform?: Transform<unknown> | undefined; $query?: Record<string, string> | undefined; $headers?: Record<...> | undefined; } | undefined, options?: { ...; } | undefined) => Promise<...>; }; }'
kidqueb commented 5 months ago

I can fix this depending on what the actual expectations are.

Leave .index incase someone decides to use an http verb in their route itself, causing a collision on .get() or whatever the verb was, or do we strip index and assume people wont be using http verbs in their routes?

  1. eden.hello.index.get() should return data.
  2. eden.hello.index.get() should show a type error and eden.hello.get() should return data.
brianjor commented 5 months ago

I am expecting eden.hello.get() to return data, as it was pre 1.0. It still does, as shown in the example, it's just the warning is blocking CI/CD checks preventing me from updating.

I would prefer not to have to add .index to the api calls.

Also this only seems to be an issue with routes behind group. See:

import { edenTreaty } from "@elysiajs/eden";
import Elysia from "elysia";

const helloHandlers = new Elysia().get("", () => "hello");

const routes = new Elysia()
  .get("/goodbye", () => "bye")
  .group("/hello", (_) => _.use(helloHandlers));

const app = new Elysia().use(routes).listen(3000);

const eden = edenTreaty<typeof app>("localhost:3000");

console.log((await eden.hello.get()).data); // <-- has red squiggly, but works!
console.log((await eden.hello.index.get()).data); // <-- no red squiggly, "NOT_FOUND"

console.log((await eden.goodbye.get()).data); // <-- no squiggly, working as expected

app.stop();