Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpApiBuilder: URL parameters are now automatically converted to arr… #4444

Merged
merged 1 commit into from
Feb 13, 2025

Conversation

gcanti
Copy link
Contributor

@gcanti gcanti commented Feb 12, 2025

…ays when needed, closes #4442

Example

import {
  HttpApi,
  HttpApiBuilder,
  HttpApiEndpoint,
  HttpApiGroup,
  HttpMiddleware,
  HttpServer
} from "@effect/platform"
import { NodeHttpServer, NodeRuntime } from "@effect/platform-node"
import { Effect, Layer, Schema } from "effect"
import { createServer } from "node:http"

const api = HttpApi.make("api").add(
  HttpApiGroup.make("group").add(
    HttpApiEndpoint.get("get", "/")
      .addSuccess(Schema.String)
      .setUrlParams(
        Schema.Struct({
          param: Schema.NonEmptyArray(Schema.String)
        })
      )
  )
)

const usersGroupLive = HttpApiBuilder.group(api, "group", (handlers) =>
  handlers.handle("get", (req) =>
    Effect.succeed(req.urlParams.param.join(", "))
  )
)

const MyApiLive = HttpApiBuilder.api(api).pipe(Layer.provide(usersGroupLive))

const HttpLive = HttpApiBuilder.serve(HttpMiddleware.logger).pipe(
  Layer.provide(MyApiLive),
  HttpServer.withLogAddress,
  Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 }))
)

Layer.launch(HttpLive).pipe(NodeRuntime.runMain)

Previously, if a query parameter was defined as a NonEmptyArray (an array that requires at least one element), providing a single value would cause a parsing error.

For example, this worked fine:

curl "http://localhost:3000/?param=1&param=2"

But this would fail:

curl "http://localhost:3000/?param=1"

Resulting in an error because "1" was treated as a string instead of an array.

With this update, single values are automatically wrapped in an array, so they match the expected schema without requiring manual fixes.

@gcanti gcanti requested a review from tim-smart as a code owner February 12, 2025 17:45
Copy link

changeset-bot bot commented Feb 12, 2025

🦋 Changeset detected

Latest commit: 04c8d0a

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

This PR includes changesets to release 29 packages
Name Type
@effect/platform Patch
@effect/cli Patch
@effect/cluster-node Patch
@effect/cluster-workflow Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 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/ai Patch
@effect/ai-openai Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/cluster-browser Patch
@effect/sql-drizzle 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

@gcanti gcanti merged commit c407726 into main Feb 13, 2025
12 checks passed
@gcanti gcanti deleted the fix-4442 branch February 13, 2025 07:58
@github-actions github-actions bot mentioned this pull request Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

From Discord: Issue with NonEmptyArray Schema for Single Element Arrays in Endpoint URL Parameters
2 participants