|
1 | | -/// A protocol that defines the context for an HTTP request. |
| 1 | +/// A context object that carries additional information about an HTTP request. |
2 | 2 | /// |
3 | | -/// Conforming types represent contextual information that can be associated with |
4 | | -/// an HTTP request throughout its lifecycle. |
5 | | -public protocol HTTPRequestContext: Sendable { |
| 3 | +/// `HTTPRequestContext` provides a way to pass metadata and implementation-specific |
| 4 | +/// information through the HTTP request pipeline. This is particularly useful when |
| 5 | +/// you need to attach custom data that should be available throughout the request's |
| 6 | +/// lifecycle. |
| 7 | +/// |
| 8 | +/// ## Implementation-Specific Data |
| 9 | +/// |
| 10 | +/// Different server implementations can store their own specific data by conforming to the |
| 11 | +/// ``ImplementationSpecific`` protocol: |
| 12 | +/// |
| 13 | +/// ```swift |
| 14 | +/// struct MyCustomContext: HTTPRequestContext.ImplementationSpecific { |
| 15 | +/// let requestID: UUID |
| 16 | +/// let startTime: Date |
| 17 | +/// } |
| 18 | +/// |
| 19 | +/// var context = HTTPRequestContext() |
| 20 | +/// context.implementationSpecific = MyCustomContext( |
| 21 | +/// requestID: UUID(), |
| 22 | +/// startTime: Date() |
| 23 | +/// ) |
| 24 | +/// ``` |
| 25 | +public struct HTTPRequestContext: Sendable { |
6 | 26 |
|
7 | | -} |
| 27 | + /// Conform your custom types to this protocol to store implementation-specific |
| 28 | + /// data within an ``HTTPRequestContext``. |
| 29 | + public protocol ImplementationSpecific: Sendable {} |
8 | 30 |
|
9 | | -/// The default implementation of an ``HTTPRequestContext`` for HTTP server requests. |
10 | | -/// |
11 | | -/// This struct provides a concrete type for representing the context of HTTP requests |
12 | | -/// handled by a server. |
13 | | -public struct HTTPServerRequestContext: HTTPRequestContext { |
14 | | - /// Creates a new HTTP server request context. |
15 | | - public init() {} |
| 31 | + /// Optional implementation-specific data associated with this request context. |
| 32 | + /// |
| 33 | + /// Use this property to store custom data that is specific to your HTTP |
| 34 | + /// implementation. The stored value can be any type that conforms to |
| 35 | + /// ``ImplementationSpecific``. |
| 36 | + public var implementationSpecific: (any ImplementationSpecific)? |
16 | 37 | } |
0 commit comments