mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-04 03:35:36 +08:00
Type received and forwarded HTTP request methods as open strings (#8766)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Tim Smart <hello@timsmart.co>
This commit is contained in:
committed by
effect-bot
co-authored by
Claude Opus 5.5
Tim Smart
parent
16321ff12e
commit
3e1ee0c3ca
@@ -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`.
|
||||
@@ -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)])])
|
||||
|
||||
@@ -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<string>
|
||||
@@ -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<string>,
|
||||
@@ -1110,16 +1114,15 @@ export function toUrl(self: HttpClientRequest): Option.Option<URL> {
|
||||
* @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
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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<string, string>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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*() {
|
||||
|
||||
@@ -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<string>()
|
||||
strictEqual(request.method, "PROPFIND")
|
||||
})
|
||||
|
||||
describe("bodyFile", () => {
|
||||
const oversized = 9007199254740993n
|
||||
const fileSystem = (size: bigint) =>
|
||||
|
||||
@@ -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<string>()
|
||||
strictEqual(request.method, "PROPFIND")
|
||||
|
||||
const forwarded = HttpServerRequest.toClientRequest(request)
|
||||
expectTypeOf(forwarded.method).toEqualTypeOf<string>()
|
||||
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()
|
||||
|
||||
@@ -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<HttpMethod.HttpMethod.WithBody>()
|
||||
} else {
|
||||
expect(knownMethod).type.toBe<HttpMethod.HttpMethod.NoBody>()
|
||||
}
|
||||
})
|
||||
|
||||
it("accepts received methods without narrowing them to known methods", () => {
|
||||
expect(HttpMethod.hasBody(receivedMethod)).type.toBe<boolean>()
|
||||
if (HttpMethod.hasBody(receivedMethod)) {
|
||||
expect(receivedMethod).type.toBe<string>()
|
||||
} else {
|
||||
expect(receivedMethod).type.toBe<string>()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user