From 3e1ee0c3ca0ef3fecc00a165351b3ffcfc488e42 Mon Sep 17 00:00:00 2001 From: Sebastian Lorenz Date: Mon, 5 Oct 2026 17:49:50 -0700 Subject: [PATCH] Type received and forwarded HTTP request methods as open strings (#8766) Co-authored-by: Claude Opus 5.5 (1M context) Co-authored-by: Tim Smart --- .changeset/open-received-http-methods.md | 5 ++++ packages/effect/src/ai/AiError.ts | 2 +- packages/effect/src/http/HttpClientRequest.ts | 15 ++++++----- packages/effect/src/http/HttpMethod.ts | 12 +++++++-- packages/effect/src/http/HttpServerRequest.ts | 14 ++++++---- .../effect/src/http/internal/decompression.ts | 3 +-- packages/effect/test/ai/AiError.test.ts | 17 ++++++++++++ .../test/http/HttpClientRequest.test.ts | 8 +++++- .../test/http/HttpServerRequest.test.ts | 13 +++++++++- .../effect/typetest/http/HttpMethod.tst.ts | 26 +++++++++++++++++++ 10 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 .changeset/open-received-http-methods.md create mode 100644 packages/effect/typetest/http/HttpMethod.tst.ts diff --git a/.changeset/open-received-http-methods.md b/.changeset/open-received-http-methods.md new file mode 100644 index 0000000000..426374d14f --- /dev/null +++ b/.changeset/open-received-http-methods.md @@ -0,0 +1,5 @@ +--- +"effect": minor +--- + +Type received and forwarded HTTP request methods as `string` so methods such as `PROPFIND` are represented accurately, including in AI HTTP error details. Exhaustive matches on request methods now need a fallback or a guard using `HttpMethod.isHttpMethod`. diff --git a/packages/effect/src/ai/AiError.ts b/packages/effect/src/ai/AiError.ts index 0a84f8a8d7..1920d8b9c3 100644 --- a/packages/effect/src/ai/AiError.ts +++ b/packages/effect/src/ai/AiError.ts @@ -45,7 +45,7 @@ import * as Schema from "../Schema.ts" * @since 4.0.0 */ export const HttpRequestDetails = Schema.Struct({ - method: Schema.Literals(["GET", "POST", "PATCH", "PUT", "DELETE", "HEAD", "OPTIONS", "TRACE", "QUERY"]), + method: Schema.String, url: Schema.String, urlParams: Schema.Array( Schema.Tuple([Schema.String, Schema.Union([Schema.String, Schema.Redacted(Schema.String)])]) diff --git a/packages/effect/src/http/HttpClientRequest.ts b/packages/effect/src/http/HttpClientRequest.ts index 82d918c857..65ee0dca5c 100644 --- a/packages/effect/src/http/HttpClientRequest.ts +++ b/packages/effect/src/http/HttpClientRequest.ts @@ -56,7 +56,11 @@ export const isHttpClientRequest = (u: unknown): u is HttpClientRequest => hasPr */ export interface HttpClientRequest extends Inspectable.Inspectable, Pipeable { readonly [TypeId]: typeof TypeId - readonly method: HttpMethod + /** + * The request method, which may be a method outside the known `HttpMethod` + * literals when the request was converted from a Web or server request. + */ + readonly method: string readonly url: string readonly urlParams: UrlParams.UrlParams readonly hash: Option.Option @@ -126,7 +130,7 @@ const Proto = { * @since 4.0.0 */ export function makeWith( - method: HttpMethod, + method: string, url: string, urlParams: UrlParams.Input, hash: Option.Option, @@ -1110,16 +1114,15 @@ export function toUrl(self: HttpClientRequest): Option.Option { * @since 4.0.0 */ export const fromWeb = (request: globalThis.Request): HttpClientRequest => { - const method = request.method.toUpperCase() as HttpMethod - return modify(empty, { - method, + const method = request.method.toUpperCase() + return modify(makeWith(method, "", UrlParams.empty, Option.none(), Headers.empty, HttpBody.empty), { url: new URL(request.url), headers: request.headers, body: fromWebBody(request, method) }) } -const fromWebBody = (request: globalThis.Request, method: HttpMethod): HttpBody.HttpBody => { +const fromWebBody = (request: globalThis.Request, method: string): HttpBody.HttpBody => { if (!hasBody(method) || request.body === null) { return HttpBody.empty } diff --git a/packages/effect/src/http/HttpMethod.ts b/packages/effect/src/http/HttpMethod.ts index 20c5ae7014..99891ba085 100644 --- a/packages/effect/src/http/HttpMethod.ts +++ b/packages/effect/src/http/HttpMethod.ts @@ -57,12 +57,20 @@ export declare namespace HttpMethod { /** * Returns `true` when a method can carry a request body and narrows it to `HttpMethod.WithBody`. * + * **Details** + * + * Methods outside the known `HttpMethod` literals, such as a received + * `"PROPFIND"`, are treated as capable of carrying a body but are not narrowed. + * * @stability unstable * @category guards * @since 4.0.0 */ -export const hasBody = (method: HttpMethod): method is HttpMethod.WithBody => - method !== "GET" && method !== "HEAD" && method !== "OPTIONS" && method !== "TRACE" +export function hasBody(method: HttpMethod): method is HttpMethod.WithBody +export function hasBody(method: string): boolean +export function hasBody(method: string): boolean { + return method !== "GET" && method !== "HEAD" && method !== "OPTIONS" && method !== "TRACE" +} /** * Provides a readonly set containing every supported `HttpMethod` literal. diff --git a/packages/effect/src/http/HttpServerRequest.ts b/packages/effect/src/http/HttpServerRequest.ts index d26df07233..90d3ffd147 100644 --- a/packages/effect/src/http/HttpServerRequest.ts +++ b/packages/effect/src/http/HttpServerRequest.ts @@ -32,7 +32,7 @@ import * as Headers from "./Headers.ts" import * as HttpBody from "./HttpBody.ts" import * as HttpClientRequest from "./HttpClientRequest.ts" import * as HttpIncomingMessage from "./HttpIncomingMessage.ts" -import { hasBody, type HttpMethod } from "./HttpMethod.ts" +import { hasBody } from "./HttpMethod.ts" import { HttpServerError, type RequestError, RequestParseError } from "./HttpServerError.ts" import * as bodyInternal from "./internal/httpBody.ts" import * as lazyStream from "./internal/lazyStream.ts" @@ -90,7 +90,11 @@ export interface HttpServerRequest extends HttpIncomingMessage.HttpIncomingMessa readonly source: object readonly url: string readonly originalUrl: string - readonly method: HttpMethod + /** + * The uppercase request method as received, which may be a method outside + * the known `HttpMethod` literals (for example `"PROPFIND"`). + */ + readonly method: string readonly cookies: ReadonlyRecord readonly multipart: Effect.Effect< @@ -563,8 +567,8 @@ class ServerRequestImpl extends Inspectable.Class implements HttpServerRequest { options.protocol ?? this.protocol ) } - get method(): HttpMethod { - return this.source.method.toUpperCase() as HttpMethod + get method(): string { + return this.source.method.toUpperCase() } get originalUrl() { return this.source.url @@ -797,7 +801,7 @@ class ClientRequestImpl extends Inspectable.Class implements HttpServerRequest { ) } - get method(): HttpMethod { + get method(): string { return this.source.method } diff --git a/packages/effect/src/http/internal/decompression.ts b/packages/effect/src/http/internal/decompression.ts index 51ec74cf11..5cba978a88 100644 --- a/packages/effect/src/http/internal/decompression.ts +++ b/packages/effect/src/http/internal/decompression.ts @@ -8,7 +8,6 @@ import type * as Socket from "../../socket/Socket.ts" import * as Stream from "../../Stream.ts" import * as Headers from "../Headers.ts" import * as HttpIncomingMessage from "../HttpIncomingMessage.ts" -import type { HttpMethod } from "../HttpMethod.ts" import { HttpServerError, RequestParseError } from "../HttpServerError.ts" import * as HttpServerRequest from "../HttpServerRequest.ts" import * as Multipart from "../Multipart.ts" @@ -100,7 +99,7 @@ export class DecompressedRequest extends Inspectable.Class implements HttpServer return this.inner.originalUrl } - get method(): HttpMethod { + get method(): string { return this.inner.method } diff --git a/packages/effect/test/ai/AiError.test.ts b/packages/effect/test/ai/AiError.test.ts index 540173f749..2cef8686b6 100644 --- a/packages/effect/test/ai/AiError.test.ts +++ b/packages/effect/test/ai/AiError.test.ts @@ -702,6 +702,23 @@ describe("AiError", () => { }) describe("supporting schemas", () => { + describe("HttpRequestDetails", () => { + it.effect("round-trips an extension method", () => + Effect.gen(function*() { + const request = { + method: "PROPFIND", + url: "https://api.example.com/resources", + urlParams: [], + hash: undefined, + headers: {} + } + const encoded = yield* Schema.encodeEffect(AiError.HttpRequestDetails)(request) + assert.strictEqual(encoded.method, "PROPFIND") + const decoded = yield* Schema.decodeEffect(AiError.HttpRequestDetails)(encoded) + assert.deepStrictEqual(decoded, request) + })) + }) + describe("ProviderMetadata", () => { it.effect("should encode and decode roundtrip", () => Effect.gen(function*() { diff --git a/packages/effect/test/http/HttpClientRequest.test.ts b/packages/effect/test/http/HttpClientRequest.test.ts index eaae8da822..5124f960b8 100644 --- a/packages/effect/test/http/HttpClientRequest.test.ts +++ b/packages/effect/test/http/HttpClientRequest.test.ts @@ -1,4 +1,4 @@ -import { describe, it } from "@effect/vitest" +import { describe, expectTypeOf, it } from "@effect/vitest" import { assertFalse, assertNone, assertSome, assertTrue, deepStrictEqual, strictEqual } from "@effect/vitest/utils" import { ByteSize, Effect, FileSystem, Formatter, Redacted, Stream } from "effect" import { Headers, HttpBody, HttpClientRequest } from "effect/http" @@ -104,6 +104,12 @@ describe("HttpClientRequest", () => { }) }) + it("preserves an extension method from a web request without claiming it is a known method", () => { + const request = HttpClientRequest.fromWeb(new Request("http://localhost/", { method: "PROPFIND" })) + expectTypeOf(request.method).toEqualTypeOf() + strictEqual(request.method, "PROPFIND") + }) + describe("bodyFile", () => { const oversized = 9007199254740993n const fileSystem = (size: bigint) => diff --git a/packages/effect/test/http/HttpServerRequest.test.ts b/packages/effect/test/http/HttpServerRequest.test.ts index 33b0b66694..8d611e0336 100644 --- a/packages/effect/test/http/HttpServerRequest.test.ts +++ b/packages/effect/test/http/HttpServerRequest.test.ts @@ -1,10 +1,21 @@ -import { assert, describe, it } from "@effect/vitest" +import { assert, describe, expectTypeOf, it } from "@effect/vitest" import { assertNone, assertSome, deepStrictEqual, strictEqual } from "@effect/vitest/utils" import { Effect, Schema, Stream } from "effect" import { HttpBody, HttpClientRequest, HttpServerRequest } from "effect/http" import * as Option from "effect/Option" describe("HttpServerRequest", () => { + it("preserves an extension method through web-server-client conversion", () => { + const request = HttpServerRequest.fromWeb(new Request("http://localhost/", { method: "PROPFIND" })) + expectTypeOf(request.method).toEqualTypeOf() + strictEqual(request.method, "PROPFIND") + + const forwarded = HttpServerRequest.toClientRequest(request) + expectTypeOf(forwarded.method).toEqualTypeOf() + strictEqual(forwarded.method, "PROPFIND") + strictEqual(HttpServerRequest.fromClientRequest(forwarded).method, "PROPFIND") + }) + it.effect("preserves FormData through client-server-client conversion", () => Effect.gen(function*() { const formData = new FormData() diff --git a/packages/effect/typetest/http/HttpMethod.tst.ts b/packages/effect/typetest/http/HttpMethod.tst.ts new file mode 100644 index 0000000000..d6407a9af7 --- /dev/null +++ b/packages/effect/typetest/http/HttpMethod.tst.ts @@ -0,0 +1,26 @@ +import { HttpMethod } from "effect/http" +import { describe, expect, it } from "tstyche" + +declare const knownMethod: HttpMethod.HttpMethod +declare const receivedMethod: string + +describe("HttpMethod", () => { + describe("hasBody", () => { + it("narrows known methods in both branches", () => { + if (HttpMethod.hasBody(knownMethod)) { + expect(knownMethod).type.toBe() + } else { + expect(knownMethod).type.toBe() + } + }) + + it("accepts received methods without narrowing them to known methods", () => { + expect(HttpMethod.hasBody(receivedMethod)).type.toBe() + if (HttpMethod.hasBody(receivedMethod)) { + expect(receivedMethod).type.toBe() + } else { + expect(receivedMethod).type.toBe() + } + }) + }) +})