| 1 | /** |
| 2 | * Defines supported HTTP method names for the unstable HTTP modules. |
| 3 | * |
| 4 | * Values are uppercase string literals such as `"GET"` and `"POST"`, matching |
| 5 | * the method tokens used by HTTP requests and routes. This module also includes |
| 6 | * helpers for checking whether a method can carry a request body and whether an |
| 7 | * unknown value is one of the supported methods. |
| 8 | * |
| 9 | * @stability unstable |
| 10 | * @since 4.0.0 |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Union of supported uppercase HTTP method literals. |
| 15 | * |
| 16 | * @stability unstable |
| 17 | * @category models |
| 18 | * @since 4.0.0 |
| 19 | */ |
| 20 | export type HttpMethod = |
| 21 | | "GET" |
| 22 | | "POST" |
| 23 | | "PUT" |
| 24 | | "DELETE" |
| 25 | | "PATCH" |
| 26 | | "HEAD" |
| 27 | | "OPTIONS" |
| 28 | | "TRACE" |
| 29 | | "QUERY" |
| 30 | |
| 31 | /** |
| 32 | * Namespace containing subtype helpers associated with `HttpMethod`. |
| 33 | * |
| 34 | * @stability unstable |
| 35 | * @since 4.0.0 |
| 36 | */ |
| 37 | export declare namespace HttpMethod { |
| 38 | /** |
| 39 | * HTTP methods that this module treats as not carrying a request body. |
| 40 | * |
| 41 | * @stability unstable |
| 42 | * @category models |
| 43 | * @since 4.0.0 |
| 44 | */ |
| 45 | export type NoBody = "GET" | "HEAD" | "OPTIONS" | "TRACE" |
| 46 | |
| 47 | /** |
| 48 | * HTTP methods that this module treats as capable of carrying a request body. |
| 49 | * |
| 50 | * @stability unstable |
| 51 | * @category models |
| 52 | * @since 4.0.0 |
| 53 | */ |
| 54 | export type WithBody = Exclude<HttpMethod, NoBody> |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns `true` when a method can carry a request body and narrows it to `HttpMethod.WithBody`. |
| 59 | * |
| 60 | * @stability unstable |
| 61 | * @category guards |
| 62 | * @since 4.0.0 |
| 63 | */ |
| 64 | export const hasBody = (method: HttpMethod): method is HttpMethod.WithBody => |
| 65 | method !== "GET" && method !== "HEAD" && method !== "OPTIONS" && method !== "TRACE" |
| 66 | |
| 67 | /** |
| 68 | * Provides a readonly set containing every supported `HttpMethod` literal. |
| 69 | * |
| 70 | * **When to use** |
| 71 | * |
| 72 | * Use when you need to iterate over or test membership against every supported |
| 73 | * HTTP method literal. |
| 74 | * |
| 75 | * @stability unstable |
| 76 | * @category constants |
| 77 | * @since 4.0.0 |
| 78 | */ |
| 79 | export const all: ReadonlySet<HttpMethod> = new Set([ |
| 80 | "GET", |
| 81 | "POST", |
| 82 | "PUT", |
| 83 | "DELETE", |
| 84 | "PATCH", |
| 85 | "HEAD", |
| 86 | "OPTIONS", |
| 87 | "TRACE", |
| 88 | "QUERY" |
| 89 | ]) |
| 90 | |
| 91 | /** |
| 92 | * Provides tuples mapping each supported HTTP method to its short |
| 93 | * request-constructor name. |
| 94 | * |
| 95 | * **When to use** |
| 96 | * |
| 97 | * Use when you need the mapping from supported HTTP method literals to their |
| 98 | * short request-constructor names. |
| 99 | * |
| 100 | * @stability unstable |
| 101 | * @category constants |
| 102 | * @since 4.0.0 |
| 103 | */ |
| 104 | export const allShort = [ |
| 105 | ["GET", "get"], |
| 106 | ["POST", "post"], |
| 107 | ["PUT", "put"], |
| 108 | ["DELETE", "del"], |
| 109 | ["PATCH", "patch"], |
| 110 | ["HEAD", "head"], |
| 111 | ["OPTIONS", "options"], |
| 112 | ["TRACE", "trace"], |
| 113 | ["QUERY", "query"] |
| 114 | ] as const |
| 115 | |
| 116 | /** |
| 117 | * Checks whether a value is a `HttpMethod`. |
| 118 | * |
| 119 | * **Example** (Checking HTTP method values) |
| 120 | * |
| 121 | * ```ts import.meta.vitest |
| 122 | * import { HttpMethod } from "effect/http" |
| 123 | * |
| 124 | * HttpMethod.isHttpMethod("GET") // => true |
| 125 | * HttpMethod.isHttpMethod("get") // => false |
| 126 | * HttpMethod.isHttpMethod(1) // => false |
| 127 | * ``` |
| 128 | * |
| 129 | * @stability unstable |
| 130 | * @category guards |
| 131 | * @since 4.0.0 |
| 132 | */ |
| 133 | export const isHttpMethod = (u: unknown): u is HttpMethod => all.has(u as HttpMethod) |