effect icon indicating copy to clipboard operation
effect copied to clipboard

feat: add helper types for HttpApi

Open floydspace opened this issue 1 month ago • 1 comments

Type

  • [x] Refactor
  • [ ] Feature
  • [ ] Bug Fix
  • [x] Optimization
  • [ ] Documentation Update

Description

My motivation is having an ability to define endpoint handlers as standalone functions, but this requires defining handler type explicitly, so the ExtractHandlerType can help with that, so it will infer what possible from HttpApi definitions

Examples:

const group1 = HttpApiGroup.make("Group1").add(
  HttpApiEndpoint.get("Endpoint1")`/`
)
const group2 = HttpApiGroup.make("Group2").add(
  HttpApiEndpoint.get("Endpoint2")`/`
)
const api = HttpApi.make("TestApi").add(group1).add(group2)

//      ┌─── | HttpApiGroup<"Group1", HttpApiEndpoint<"Endpoint1", "GET">, never>
//      |    | HttpApiGroup<"Group2", HttpApiEndpoint<"Endpoint2", "GET">, never>
//      ▼
type groups = HttpApi.HttpApi.Groups<typeof api>

//      ┌─── HttpApiEndpoint<"Endpoint1", "GET">
//      ▼
type endpoints = HttpApi.HttpApi.EndpointsWithGroupName<typeof api, "Group1">

//      ┌─── HttpApiEndpoint.Handler<HttpApiEndpoint<"Endpoint1", "GET">, never, never>
//      ▼
type handler = HttpApi.HttpApi.ExtractHandlerType<
  typeof api,
  "Group1",
  "Endpoint1",
  never,
  never
>

the end goal is smt like this

declare const listPersons: ExtractHandlerType<typeof ServiceApi, "persons", "listPersons", never, PersonStore>;

const personsGroup = HttpApiBuilder.group(ServiceApi, "persons", (handlers) =>
  handlers.handle("listPersons", listPersons)
);

Please review carefuly the parts where I narrowed down the string generics to the expected literals

Related

  • Related Issue #
  • Closes #

floydspace avatar Nov 08 '25 21:11 floydspace

🦋 Changeset detected

Latest commit: 7d119f0ac0d9cc1b2f18435a3c5a83cb2740c342

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 31 packages
Name Type
@effect/platform Patch
@effect/cli Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-drizzle Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-node Patch
@effect/sql Patch
@effect/workflow Patch
@effect/ai Patch
@effect/ai-amazon-bedrock Patch
@effect/ai-anthropic Patch
@effect/ai-google Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/sql-kysely Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Nov 08 '25 21:11 changeset-bot[bot]