You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a draft PR to address #153, I want some feedback before proceeding with updating the documentation, README, tests
The idea is that Router now doesn’t work anymore w/ `Serializable` objects and `ResponseFieldsProvider` is not anymore a special `Serializable`.
Router now only accept `ResponseFieldsProvider`s an object that provides:
statusCode (Int)
headerfields (dict)
body (data)
`Serializable` became a `ResponseFieldsProvider` that by default has a status code == 200 and “Content-Type” == “application/json”.
It should have been like that since ever, so we can use Kakapo with images, xml or whatever we want by creating new `ResponseFieldsProvider `
The change is a breaking change but not a big one for users of Kakapo (only objects that were confirming to ResponseFieldsProvider will be broken)
Copy file name to clipboardexpand all lines: Source/Router.swift
+20-18
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ import Foundation
14
14
By default, though, the Router will return a 200 status code and `["Content-Type": "application/json"]` header fields when only returning a Serializable object.
15
15
In order to customize that behavior, check `ResponseFieldsProvider` to provide custom status code and header fields.
Copy file name to clipboardexpand all lines: Source/Serialization/JSONAPIError.swift
+6-13
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@
9
9
import Foundation
10
10
11
11
/// A convenience error object that conform to JSON API
12
-
publicstructJSONAPIError:ResponseFieldsProvider{
12
+
publicstructJSONAPIError:CustomSerializable{
13
13
14
14
/// An object containing references to the source of the error, optionally including any of the following members
15
15
publicstructSource:Serializable{
@@ -78,29 +78,22 @@ public struct JSONAPIError: ResponseFieldsProvider {
78
78
publicvarstatusCode:Int{
79
79
return builder.status
80
80
}
81
-
82
-
/// A `JSONAPIError.Builder` instance contains all the fields.
83
-
publicvarbody:Serializable{
84
-
return builder
85
-
}
86
-
87
-
/// The headerFields that will be returned by the HTTP response.
88
-
publicletheaderFields:[String:String]?
89
81
90
82
/**
91
83
Initialize a `JSONAPIError` and build it with `JSONAPIError.Builder`
92
84
93
85
- parameter statusCode: The status code of the response, will be used also to provide a statusCode for your request
94
-
- parameter headerFields: The headerFields that will be returned by the HTTP response.
95
86
- parameter errorBuilder: A builder that can be used to fill the error objects, it contains all you need to provide an error object confiorming to JSON API (**see `JSONAPIError.Builder`**)
96
87
97
88
- returns: An error that conforms to JSON API specifications and it's ready to be serialized
Copy file name to clipboardexpand all lines: Source/Serialization/Serializer.swift
+23-15
Original file line number
Diff line number
Diff line change
@@ -11,10 +11,32 @@ import Foundation
11
11
/**
12
12
* A protocol to serialize types into JSON representations, the object will be Mirrored to be serialized. Use `CustomReflectable` if you need different behaviors or use `CustomSerializable`if it's not a valid option.
// empty protocol, marks that the object should be Mirrored to be serialized.
16
16
}
17
17
18
+
extensionSerializable{
19
+
publicvarstatusCode:Int{
20
+
return200
21
+
}
22
+
23
+
publicvarheaderFields:[String:String]?{
24
+
return["Content-Type":"application/json"]
25
+
}
26
+
27
+
/**
28
+
Serialize a `Serializable` object and convert the serialized object to `Data`. Unless it is nil the return value is representing a JSON. Usually you don't need to use this method directly since `Router` will automatically serialize objects when needed.
* Conforming to `CustomSerializable` the object won't be Mirrored to be serialized, use it in case `CustomReflectable` is not a viable option. Array for example use this to return an Array with its serialized objects inside.
20
42
*/
@@ -44,20 +66,6 @@ public extension Serializable {
Serialize a `Serializable` object and convert the serialized object to `Data`. Unless it is nil the return value is representing a JSON. Usually you don't need to use this method directly since `Router` will automatically serialize objects when needed.
0 commit comments