-
Couldn't load subscription status.
- Fork 35
Open
Labels
good first issueGood for newcomersGood for newcomerskind/enhancementImprovements to existing feature.Improvements to existing feature.⚠️ semver/majorBreaks existing public API.Breaks existing public API.
Description
There is an opportunity to DRY by refactoring part of the APIGateway, APIGatewayV2, and APIGateway+WebSocket payload.
As per my assistant:
1. HTTP Headers and Body Handling
- Properties like
headers,multiValueHeaders(or justheadersin V2), andbody, as well asisBase64Encoded, appear in all major APIGateway request/response structs:APIGatewayRequestAPIGatewayV2RequestAPIGatewayWebSocketRequest- Their corresponding response types.
2. Context/Identity Structures
- Each request type defines a nested
Contextstruct (sometimes with further nesting likeIdentityorHTTP), which contains information about the caller, API, stage, request IDs, etc. - Many fields in these context structs overlap, such as
apiId,stage,requestId,domainName, andsourceIp.
3. Codable/Sendable Conformance
- All these types conform to
Codable(and, where supported,Sendable).
4. CodingKeys Mapping
- Several structs use custom
CodingKeysenums to map from JSON input to struct properties, especially for context/requestContext fields.
5. Response Struct Pattern
- The response structs (
APIGatewayResponse,APIGatewayV2Response, and by alias,APIGatewayWebSocketResponse) all follow a similar pattern:statusCode,headers,body, andisBase64Encoded, with slight variations.
Candidates for Code Sharing/Abstraction
- Protocols: Define a protocol for common properties (headers, body, isBase64Encoded) and have each struct conform to it.
protocol APIGatewayRequestCommon: Codable { var headers: HTTPHeaders? { get } var body: String? { get } var isBase64Encoded: Bool? { get } }
- Shared Context/Identity Structs: Refactor common context/identity fields into base structs or protocols, and compose them in each specific context struct as needed.
- Initializers/Decoding Helpers: Shared extensions or helper initializers to reduce repeated decoding logic for commonly structured properties.
Summary Table of Common Properties:
| Property | APIGatewayRequest | APIGatewayV2Request | APIGatewayWebSocketRequest |
|---|---|---|---|
| headers | ✓ | ✓ | ✓ |
| multiValueHeaders | ✓ | (V2 drops this) | ✓ |
| body | ✓ | ✓ | ✓ |
| isBase64Encoded | ✓ | ✓ | ✓ |
| context/requestContext | ✓ | ✓ | ✓ (as context) |
| stage | ✓ (in context) | ✓ (in context) | ✓ (in context) |
| apiId | ✓ (in context) | ✓ (in context) | ✓ (in context) |
| requestId | ✓ (in context) | ✓ (in context) | ✓ (in context) |
| domainName | ✓ (in context) | ✓ (in context) | ✓ (in context) |
| sourceIp | ✓ (in context) | ✓ (in context) | ✓ (in context.identity) |
Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomerskind/enhancementImprovements to existing feature.Improvements to existing feature.⚠️ semver/majorBreaks existing public API.Breaks existing public API.