@@ -17,63 +17,59 @@ import struct Foundation.Data
17
17
// https://cloud.tencent.com/document/product/583/12513
18
18
19
19
extension APIGateway . Response {
20
- public init < T : Encodable > (
20
+ public init (
21
21
statusCode: HTTPResponseStatus ,
22
22
headers: HTTPHeaders = [ : ] ,
23
- codableBody : T ?
23
+ body : @autoclosure ( ) throws -> Body = . null
24
24
) {
25
- var headers = headers
26
- headers [ " Content-Type " ] = MIME . json. rawValue
27
- self . headers = headers
25
+ let rawBody : Body
28
26
do {
29
- self . body = String (
30
- data: try APIGateway . defaultJSONEncoder. encode ( codableBody) ,
31
- encoding: . utf8
32
- ) ?? " "
33
- self . statusCode = statusCode
34
- } catch let err as EncodingError {
35
- self . body = #"{"error":"EncodingError","message":" \#( " \( err) " . jsonEncoded ( ) ) "}"#
36
- self . statusCode = . internalServerError
27
+ rawBody = try body ( )
37
28
} catch let err {
38
- self . body = #"{"error":"UnexpectedError","message":" \#( " \( err ) " . jsonEncoded ( ) ) "}"#
29
+ self . headers = headers . merging ( [ " Content-Type " : " application/json " ] ) { ( _ , default ) in `default` }
39
30
self . statusCode = . internalServerError
31
+ self . isBase64Encoded = false
32
+ if let err = err as? EncodingError {
33
+ self . body = #"{"error":"EncodingError","message":" \#( " \( err) " . jsonEncoded ( ) ) "}"#
34
+ } else {
35
+ self . body = #"{"error":"UnexpectedError","message":" \#( " \( err) " . jsonEncoded ( ) ) "}"#
36
+ }
37
+ return
40
38
}
41
- self . isBase64Encoded = false
42
- }
43
39
44
- public init (
45
- statusCode: HTTPResponseStatus ,
46
- headers: HTTPHeaders = [ : ] ,
47
- type: MIME ? = nil ,
48
- body: String ? = nil
49
- ) {
50
40
self . statusCode = statusCode
51
- var headers = headers
52
- if let type = type? . rawValue {
53
- headers [ " Content-Type " ] = type
54
- } else {
55
- headers [ " Content-Type " ] = MIME . text. rawValue
41
+ self . headers = headers. merging ( [ " Content-Type " : rawBody. defaultMIME] ) { ( custom, _) in custom }
42
+
43
+ switch rawBody {
44
+ case . data( let dataBody) :
45
+ self . body = dataBody. base64EncodedString ( )
46
+ self . isBase64Encoded = true
47
+ case . json( let stringBody) , . string( let stringBody) :
48
+ self . body = stringBody
49
+ self . isBase64Encoded = false
50
+ case . null:
51
+ self . body = " "
52
+ self . isBase64Encoded = true
56
53
}
57
- self . headers = headers
58
- self . body = body ?? " "
59
- self . isBase64Encoded = false
60
54
}
61
55
62
- public init (
63
- statusCode: HTTPResponseStatus ,
64
- headers: HTTPHeaders = [ : ] ,
65
- type: MIME ? = nil ,
66
- body: Data
67
- ) {
68
- self . statusCode = statusCode
69
- var headers = headers
70
- if let type = type? . rawValue {
71
- headers [ " Content-Type " ] = type
72
- } else {
73
- headers [ " Content-Type " ] = MIME . octet. rawValue
56
+ public enum Body {
57
+ case json( String )
58
+ case data( Data )
59
+ case string( String )
60
+ case null
61
+
62
+ public static func codable< T: Encodable > ( _ body: T ) throws -> Self {
63
+ return . json( String ( data: try APIGateway . defaultJSONEncoder. encode ( body) , encoding: . utf8) ?? " " )
64
+ }
65
+
66
+ var defaultMIME : String {
67
+ switch self {
68
+ case . json: return " application/json "
69
+ case . data: return " application/octet-stream "
70
+ case . string: return " text/plain "
71
+ case . null: return " text/plain "
72
+ }
74
73
}
75
- self . headers = headers
76
- self . body = body. base64EncodedString ( )
77
- self . isBase64Encoded = true
78
74
}
79
75
}
0 commit comments