File size: 12,800 Bytes
31e8457 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | /**
* HttpApiSchema provides helpers to annotate Effect Schema values with HTTP API metadata
* (status codes and payload/response encodings) used by the HttpApi builder, client,
* and OpenAPI generation.
*
* Mental model:
* - A "Schema" is the base validation/encoding description from `Schema`.
* - An "Encoding" tells HttpApi how to serialize/parse a payload or response body.
* - A "Status" is metadata that chooses the HTTP response status code.
* - "Empty" schemas represent responses with no body (204/201/202 or custom).
* - "NoContent" schemas can still decode into a value via {@link asNoContent}.
* - Multipart is a payload-only encoding for file-like form data.
*
* Common tasks:
* - Set a response status on a schema -> {@link status}
* - Declare an empty response -> {@link Empty}, {@link NoContent}, {@link Created}, {@link Accepted}
* - Decode an empty response into a value -> {@link asNoContent}
* - Force a specific encoding -> {@link asJson}, {@link asFormUrlEncoded}, {@link asText}, {@link asUint8Array}
* - Mark multipart payloads -> {@link asMultipart}, {@link asMultipartStream}
*
* Gotchas:
* - If you don't set an encoding, HttpApi assumes JSON by default.
* - {@link asFormUrlEncoded} expects the schema's encoded type to be a record of strings.
* - {@link asText} expects the encoded type to be `string`, and {@link asUint8Array} expects `Uint8Array`.
* - Multipart encodings are intended for request payloads; response multipart is not supported.
* - These helpers annotate schemas; they don't perform validation or IO by themselves.
*
* @since 4.0.0
*/
import { constVoid, type LazyArg } from "../../Function.ts"
import * as Schema from "../../Schema.ts"
import * as AST from "../../SchemaAST.ts"
import * as Transformation from "../../SchemaTransformation.ts"
import { hasBody, type HttpMethod } from "../http/HttpMethod.ts"
import type * as Multipart_ from "../http/Multipart.ts"
declare module "../../Schema.ts" {
namespace Annotations {
interface Augment {
readonly httpApiStatus?: number | undefined
/**
* The encoding of the payload or response.
* This is kept internal because encodings are only exposed through the `as*` functions.
* @internal
*/
readonly "~httpApiEncoding"?: Encoding | undefined
}
}
}
/**
* @internal
*/
export type Encoding = PayloadEncoding | ResponseEncoding
/**
* Encodings for payloads
* @internal
*/
export type PayloadEncoding =
| {
readonly _tag: "Multipart"
readonly mode: "buffered" | "stream"
readonly contentType: string
readonly limits?: Multipart_.withLimits.Options | undefined
}
| {
readonly _tag: "Json" | "FormUrlEncoded" | "Uint8Array" | "Text"
readonly contentType: string
}
/**
* Encodings for responses
* @internal
*/
export type ResponseEncoding = {
readonly _tag: "Json" | "FormUrlEncoded" | "Uint8Array" | "Text"
readonly contentType: string
}
const statusCodeByLiteral = {
Continue: 100,
SwitchingProtocols: 101,
Processing: 102,
EarlyHints: 103,
OK: 200,
Ok: 200,
Created: 201,
Accepted: 202,
NonAuthoritativeInformation: 203,
NoContent: 204,
ResetContent: 205,
PartialContent: 206,
MultiStatus: 207,
AlreadyReported: 208,
ImUsed: 226,
MultipleChoices: 300,
MovedPermanently: 301,
Found: 302,
SeeOther: 303,
NotModified: 304,
TemporaryRedirect: 307,
PermanentRedirect: 308,
BadRequest: 400,
Unauthorized: 401,
PaymentRequired: 402,
Forbidden: 403,
NotFound: 404,
MethodNotAllowed: 405,
NotAcceptable: 406,
ProxyAuthenticationRequired: 407,
RequestTimeout: 408,
Conflict: 409,
Gone: 410,
LengthRequired: 411,
PreconditionFailed: 412,
PayloadTooLarge: 413,
UriTooLong: 414,
UnsupportedMediaType: 415,
RangeNotSatisfiable: 416,
ExpectationFailed: 417,
ImATeapot: 418,
MisdirectedRequest: 421,
UnprocessableEntity: 422,
Locked: 423,
FailedDependency: 424,
TooEarly: 425,
UpgradeRequired: 426,
PreconditionRequired: 428,
TooManyRequests: 429,
RequestHeaderFieldsTooLarge: 431,
UnavailableForLegalReasons: 451,
InternalServerError: 500,
NotImplemented: 501,
BadGateway: 502,
ServiceUnavailable: 503,
GatewayTimeout: 504,
HttpVersionNotSupported: 505,
VariantAlsoNegotiates: 506,
InsufficientStorage: 507,
LoopDetected: 508,
NotExtended: 510,
NetworkAuthenticationRequired: 511
} as const
/**
* Common HTTP status code literals accepted by {@link status}.
*
* @category status
* @since 4.0.0
*/
export type StatusLiteral = keyof typeof statusCodeByLiteral
/**
* A convenience function to set the HTTP status code of a schema.
*
* This is equivalent to calling `.annotate({ httpApiStatus: code })` on the schema.
*
* You can pass either a numeric status code (for example, `201`) or a common
* literal name (for example, `"Created"`).
*
* @category status
* @since 4.0.0
*/
export function status(code: number): <S extends Schema.Top>(self: S) => S["Rebuild"]
export function status(code: StatusLiteral): <S extends Schema.Top>(self: S) => S["Rebuild"]
export function status(code: number | StatusLiteral) {
const statusCode = typeof code === "string" ? statusCodeByLiteral[code] : code
return <S extends Schema.Top>(self: S): S["Rebuild"] => {
return self.annotate({ httpApiStatus: statusCode })
}
}
/**
* Creates a void schema with the given HTTP status code.
* This is used to represent empty responses with a specific status code.
*
* @see {@link asEmpty} for creating a no content response that can be decoded into a meaningful value on the client side.
*
* @category Empty
* @since 4.0.0
*/
export const Empty = (code: number): Schema.Void => Schema.Void.pipe(status(code))
/**
* @since 4.0.0
*/
export interface NoContent extends Schema.Void {}
/**
* A void schema with the HTTP status code 204.
* This is used to represent empty responses with the status code 204.
*
* @since 4.0.0
* @category Empty
*/
export const NoContent: NoContent = Empty(204)
/**
* @since 4.0.0
*/
export interface Created extends Schema.Void {}
/**
* A void schema with the HTTP status code 201.
* This is used to represent empty responses with the status code 201.
*
* @category Empty
* @since 4.0.0
*/
export const Created: Created = Empty(201)
/**
* @since 4.0.0
*/
export interface Accepted extends Schema.Void {}
/**
* A void schema with the HTTP status code 202.
* This is used to represent empty responses with the status code 202.
*
* @category Empty
* @since 4.0.0
*/
export const Accepted: Accepted = Empty(202)
/**
* @since 4.0.0
*/
export interface asNoContent<S extends Schema.Top> extends Schema.decodeTo<Schema.toType<S>, Schema.Void> {}
/**
* Marks a schema as a no content response.
*
* The `decode` function is used to decode the response body on the client side into a meaningful value.
*
* @see {@link NoContent} for a void schema with the status code 204.
* @see {@link Empty} for creating a void schema with a specific status code.
*
* @category Encoding
* @since 4.0.0
*/
export function asNoContent<S extends Schema.Top>(options: {
readonly decode: LazyArg<S["Type"]>
}) {
return (self: S): asNoContent<S> => {
return Schema.Void.pipe(
Schema.decodeTo(
Schema.toType(self),
Transformation.transform({
decode: options.decode,
encode: constVoid
})
)
)
}
}
/**
* @since 4.0.0
*/
export const MultipartTypeId = "~effect/httpapi/HttpApiSchema/Multipart"
/**
* @since 4.0.0
*/
export type MultipartTypeId = typeof MultipartTypeId
/**
* @since 4.0.0
*/
export interface asMultipart<S extends Schema.Top> extends Schema.brand<S["Rebuild"], MultipartTypeId> {}
/**
* Marks a schema as a multipart payload.
*
* @see {@link asMultipartStream} for a multipart stream payload.
*
* @category Encoding
* @since 4.0.0
*/
export function asMultipart(options?: Multipart_.withLimits.Options) {
return <S extends Schema.Top>(self: S): asMultipart<S> =>
self.pipe(Schema.brand(MultipartTypeId)).annotate({
"~httpApiEncoding": {
_tag: "Multipart",
mode: "buffered",
contentType: defaultContentType("Multipart"),
limits: options
}
})
}
/**
* @since 4.0.0
*/
export const MultipartStreamTypeId = "~effect/httpapi/HttpApiSchema/MultipartStream"
/**
* @since 4.0.0
*/
export type MultipartStreamTypeId = typeof MultipartStreamTypeId
/**
* @since 4.0.0
*/
export interface asMultipartStream<S extends Schema.Top> extends Schema.brand<S["Rebuild"], MultipartStreamTypeId> {}
/**
* Marks a schema as a multipart stream payload.
*
* @see {@link asMultipart} for a buffered multipart payload.
*
* @category Encoding
* @since 4.0.0
*/
export function asMultipartStream(options?: Multipart_.withLimits.Options) {
return <S extends Schema.Top>(self: S): asMultipartStream<S> =>
self.pipe(Schema.brand(MultipartStreamTypeId)).annotate({
"~httpApiEncoding": {
_tag: "Multipart",
mode: "stream",
contentType: defaultContentType("Multipart"),
limits: options
}
})
}
function asNonMultipartEncoding<S extends Schema.Top>(self: S, options: {
readonly _tag: "Json" | "FormUrlEncoded" | "Uint8Array" | "Text"
readonly contentType?: string | undefined
}): S["Rebuild"] {
return self.annotate({
"~httpApiEncoding": {
_tag: options._tag,
contentType: options.contentType ?? defaultContentType(options._tag)
}
})
}
function defaultContentType(_tag: Encoding["_tag"]): string {
switch (_tag) {
case "Multipart":
return "multipart/form-data"
case "Json":
return "application/json"
case "FormUrlEncoded":
return "application/x-www-form-urlencoded"
case "Uint8Array":
return "application/octet-stream"
case "Text":
return "text/plain"
}
}
/**
* Marks a schema as a JSON payload / response.
*
* @category Encoding
* @since 4.0.0
*/
export function asJson(options?: {
readonly contentType?: string
}) {
return <S extends Schema.Top>(self: S) => asNonMultipartEncoding(self, { _tag: "Json", ...options })
}
/**
* Marks a schema as a URL params payload / response.
*
* The schema encoded side must be a record of strings.
*
* @category Encoding
* @since 4.0.0
*/
export function asFormUrlEncoded(options?: {
readonly contentType?: string
}) {
return <S extends Schema.Top>(
self: S
) => asNonMultipartEncoding(self, { _tag: "FormUrlEncoded", ...options })
}
/**
* Marks a schema as a text payload / response.
*
* The schema encoded side must be a string.
*
* @category Encoding
* @since 4.0.0
*/
export function asText(options?: {
readonly contentType?: string
}) {
return <S extends Schema.Top & { readonly Encoded: string }>(self: S) =>
asNonMultipartEncoding(self, { _tag: "Text", ...options })
}
/**
* Marks a schema as a binary payload / response.
*
* The schema encoded side must be a `Uint8Array`.
*
* @category Encoding
* @since 4.0.0
*/
export function asUint8Array(options?: {
readonly contentType?: string
}) {
return <S extends Schema.Top & { readonly Encoded: Uint8Array }>(self: S) =>
asNonMultipartEncoding(self, { _tag: "Uint8Array", ...options })
}
/**
* @since 4.0.0
*/
export const isNoContent = (ast: AST.AST): boolean => {
if (AST.isVoid(ast)) return true
const encoded = AST.toEncoded(ast)
if (AST.isVoid(encoded)) return true
const target = ast.encoding?.[0].to
if (target === undefined) return false
return AST.isVoid(target)
}
const resolveHttpApiEncoding = AST.resolveAt<Encoding>("~httpApiEncoding")
const resolveHttpApiStatus = AST.resolveAt<number>("httpApiStatus")
const defaultJsonEncoding: Encoding = {
_tag: "Json",
contentType: "application/json"
}
const defaultUrlEncodedEncoding: Encoding = {
_tag: "FormUrlEncoded",
contentType: "application/x-www-form-urlencoded"
}
function getEncoding(ast: AST.AST): Encoding {
return resolveHttpApiEncoding(ast) ?? defaultJsonEncoding
}
/** @internal */
export function getPayloadEncoding(ast: AST.AST, method: HttpMethod): PayloadEncoding {
const encoding = resolveHttpApiEncoding(ast)
if (encoding) return encoding
return hasBody(method) ? defaultJsonEncoding : defaultUrlEncodedEncoding
}
/** @internal */
export function getResponseEncoding(ast: AST.AST): ResponseEncoding {
const out = getEncoding(ast)
if (out._tag === "Multipart") {
throw new Error("Multipart is not supported in response")
}
return out
}
/** @internal */
export function getStatusSuccess(self: AST.AST): number {
return resolveHttpApiStatus(self) ?? 200
}
/** @internal */
export function getStatusError(self: AST.AST): number {
return resolveHttpApiStatus(self) ?? 500
}
|